4A Server -  2.0
 All Classes Namespaces Files Functions Variables Enumerator
SelectAT.java
Go to the documentation of this file.
1 /*
2  * Project: Server for annotations sharing
3  * Author: Ing. Jaroslav Dytrych idytrych@fit.vutbr.cz
4  * File: SelectAT.java
5  * Description: Backbean for page with selection of type of annotation
6  */
7 
8 /**
9  * @file SelectAT.java
10  *
11  * @brief Backbean for page with selection of type of annotation
12  */
13 
14 package cz.vutbr.fit.knot.annotations.web;
15 
20 import java.io.Serializable;
21 import java.util.ArrayList;
22 import java.util.Iterator;
23 import java.util.List;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
26 import javax.faces.bean.ManagedBean;
27 import javax.faces.bean.ViewScoped;
28 import javax.persistence.EntityManager;
29 import javax.persistence.Query;
30 import org.openfaces.util.Faces;
31 
32 /**
33  * Backbean for page with selection of type of annotation
34  *
35  * @brief Backbean for page with selection of type of annotation
36  * @author idytrych
37  */
38 @ManagedBean
39 @ViewScoped
40 public class SelectAT implements Serializable {
41 
42  /**
43  * Selected user group
44  */
45  private UserGroup selGroup = null;
46 
47  /**
48  * Selected type of annotation
49  */
50  private AnnotType selType = null;
51 
52  /**
53  * Identificator of page from which user come (to which return after selection)
54  */
55  String retToPage = null;
56 
57  /**
58  * Constructor
59  */
60  public SelectAT() {
61  }
62 
63  /**
64  * Action listener for select links in table
65  *
66  * @param id Id of selected type of annotation
67  * @returns Returns page outcome (identificator of next page or null to stay here)
68  */
69  public String actionSelect(Integer id) {
70  // query DB for selected type
71  List tList = null;
72  AnnotType selected = null;
73  Object[] params = new Object[2];
74  params[0] = "id";
75  params[1] = id;
76  tList = AppBean.getPersistenceManager().queryDB("AnnotType.findById", params);
77  if (tList != null && !tList.isEmpty()) {
78  selected = (AnnotType) tList.get(0);
79  }
80 
81  if (retToPage == null) {
82  retToPage = SessionManager.getSession().getCameFrom();
83  }
84 
85  // pass selected type of annotation through the session
86  SessionManager.getSession().setSelectedAT(selected);
87  return retToPage;
88  }
89 
90  /**
91  * Action listener for cancel button
92  *
93  * @return Returns page outcome (identificator of previous page)
94  */
95  public String btnCancelAction() {
96  SessionManager.getSession().setSelectedAT(null);
97  return retToPage;
98  }
99 
100  /**
101  * Gets selected type of annotation
102  *
103  * @return Returns selected type of annotation
104  */
106  return selType;
107  }
108 
109  /**
110  * Sets selected type of annotation
111  *
112  * @param selType Selected type of annotation
113  */
114  public void setSelType(AnnotType selType) {
115  this.selType = selType;
116  }
117 
118  /**
119  * Gets content of auxiliary variable with actual node for TreeTable
120  *
121  * @return Returns actual node of tree
122  */
124  AnnotType annotTS = Faces.var("annotTS", AnnotType.class);
125  return annotTS;
126  }
127 
128  /**
129  * Gets children of actual node in TreeTable
130  *
131  * @return Returns children of actual node
132  */
133  public List<AnnotType> getNodeChildren() {
134  if (retToPage == null) {
135  retToPage = SessionManager.getSession().getCameFrom();
136  selGroup = SessionManager.getSession().getSelectedUG();
137  }
138  List<AnnotType> rList = new ArrayList<AnnotType>();
139  AnnotType annotTS = Faces.var("annotTS", AnnotType.class); // get actual node
140 
141  if (annotTS != null) { // if node is set
142 
143  // query DB for types, in which ancestors is actual node
144  EntityManager em = AppBean.getPersistenceManager().getEM();
145  List tList = null;
146  try {
147  Query query = em.createQuery("SELECT a FROM AnnotType a JOIN a.ancestorTypes at WHERE at.id='" + annotTS.getId() + "'");
148  tList = query.getResultList();
149  } catch (Exception e) {
151  String msg = "DB query in getNodeChildren failed.";
152  Logger.getLogger(SelectAT.class.getName()).log(Level.SEVERE, msg, e);
153  }
154  tList = null;
155  } finally {
156  em.close();
157  }
158  if (tList != null && !tList.isEmpty()) {
159  for (Iterator tIt = tList.iterator(); tIt.hasNext();) {
160  AnnotType at = (AnnotType) tIt.next();
161  rList.add(at); // add to list to return
162  }
163  }
164 
165  // query DB for types which primary ancestor is actual node
166  tList = null;
167  Object[] params = new Object[2];
168  params[0] = "ancestor";
169  params[1] = annotTS.getId();
170  tList = AppBean.getPersistenceManager().queryDB("AnnotType.findByAncestor", params);
171  if (tList != null && !tList.isEmpty()) {
172  for (Iterator tIt = tList.iterator(); tIt.hasNext();) {
173  AnnotType at = (AnnotType) tIt.next();
174  if (!rList.contains(at)) {
175  rList.add(at); // add to list to return
176  }
177  }
178  }
179 
180  return rList;
181  } // if node is set
182 
183  // if node is not set, actual node is root node of tree
184  // (children are all top level types of annotations)
185 
186  // query database for root types in given group
187  List tList = null;
188  EntityManager em = AppBean.getPersistenceManager().getEM();
189  try {
190  Query query = em.createQuery("SELECT a FROM AnnotType a WHERE a.groupId = '" + selGroup.getId() + "' AND a.ancestor IS NULL");
191  tList = query.getResultList();
192  } catch (Exception e) {
194  String msg = "DB query in getNodeChildren failed.";
195  Logger.getLogger(SelectAT.class.getName()).log(Level.SEVERE, msg, e);
196  }
197  tList = null;
198  } finally {
199  em.close();
200  }
201 
202  if (tList != null && !tList.isEmpty()) { // if types was found
203  for (Iterator tIt = tList.iterator(); tIt.hasNext();) {
204  AnnotType type = (AnnotType) tIt.next();
205  rList.add(type); // add to list to return
206  }
207  }
208 
209 
210  return rList;
211  } // getNodeChildren()
212 
213 
214  /**
215  * Returns, whether actuall node in tree has children
216  *
217  * @return If actual node has children, returns true, false otherwise
218  */
219  public boolean getNodeHasChildren() {
220  AnnotType annotTS = Faces.var("annotTS", AnnotType.class);
221  if (annotTS != null) { // if node is set
222  // query DB for types, in which ancestors is actual node
223  EntityManager em = AppBean.getPersistenceManager().getEM();
224  List tList = null;
225  try {
226  Query query = em.createQuery("SELECT a FROM AnnotType a JOIN a.ancestorTypes at WHERE at.id='" + annotTS.getId() + "'");
227  tList = query.getResultList();
228  } catch (Exception e) {
230  String msg = "DB query in getNodeHasChildren failed.";
231  Logger.getLogger(SelectAT.class.getName()).log(Level.SEVERE, msg, e);
232  }
233  tList = null;
234  } finally {
235  em.close();
236  }
237  if (tList != null && !tList.isEmpty()) {
238  return true;
239  }
240 
241  // query DB for types which primary ancestor is actual node
242  Object[] params = new Object[2];
243  params[0] = "ancestor";
244  params[1] = annotTS.getId();
245  tList = AppBean.getPersistenceManager().queryDB("AnnotType.findByAncestor", params);
246  if (tList != null && !tList.isEmpty()) {
247  return true;
248  }
249  } // if (annotT != null ...
250  return false;
251  } // getNodeHasChildren()
252 
253 } // public class SelectAT
Singleton for storing global variables.
Definition: AppBean.java:47
Backbean for page with selection of type of annotation.
Definition: SelectAT.java:40
Class representing user group.
Definition: UserGroup.java:47
Class representing type of annotation.
Definition: AnnotType.java:58