4A Server -  2.0
 All Classes Namespaces Files Functions Variables Enumerator
ATypes.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: ATypes.java
5  * Description: Backbean for page with types of annotations
6  */
7 
8 /**
9  * @file ATypes.java
10  *
11  * @brief Backbean for page with types of annotations
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.SessionScoped;
28 import javax.faces.model.SelectItem;
29 import javax.persistence.EntityManager;
30 import javax.persistence.Query;
31 import org.openfaces.util.Faces;
32 
33 /**
34  * Backbean for page with types of annotations
35  *
36  * @brief Backbean for page with types of annotations
37  * @author idytrych
38  */
39 @ManagedBean
40 @SessionScoped
41 public class ATypes implements Serializable {
42  /**
43  * Selected user groups
44  */
45  private List<UserGroup> selGroups = null;
46  /**
47  * Selected type of annotation
48  */
49  private AnnotType selType = null;
50 
51  /**
52  * Constructor
53  * - Sets selected user groups to user's joined groups
54  */
55  public ATypes() {
56  if (selGroups == null) {
57  selGroups = SessionManager.getSession().getLoggedUser().getGroupsAL();
58  }
59  }
60 
61  /**
62  * Action listener for edit links in table
63  *
64  * @return Returns page outcome to go to editing page
65  */
66  public String actionEdit(Integer id) {
67  // query DB for selected type
68  List tList = null;
69  AnnotType selected = null;
70  Object[] params = new Object[2];
71  params[0] = "id";
72  params[1] = id;
73  tList = AppBean.getPersistenceManager().queryDB("AnnotType.findById", params);
74  if (tList != null && !tList.isEmpty()) {
75  selected = (AnnotType) tList.get(0);
76  }
77 
78  // pass selected type of annotation through the session
79  SessionManager.getSession().setEditedAT(selected);
80  SessionManager.getSession().setFormBackup(null);
81  return "editAT";
82  } // actionEdit()
83 
84  /**
85  * Action listener for delete links in table
86  *
87  * @return Returns page outcome to go to deleting page
88  */
89  public String actionDelete(Integer id) {
90  // query DB for selected type
91  List tList = null;
92  AnnotType selected = null;
93  Object[] params = new Object[2];
94  params[0] = "id";
95  params[1] = id;
96  tList = AppBean.getPersistenceManager().queryDB("AnnotType.findById", params);
97  if (tList != null && !tList.isEmpty()) {
98  selected = (AnnotType) tList.get(0);
99  }
100 
101  // pass selected type of annotation through the session
102  SessionManager.getSession().setEditedAT(selected);
103  SessionManager.getSession().setFormBackup(null);
104  return "deleteAT";
105  } // actionDelete()
106 
107  /**
108  * Gets selected type of annotation
109  *
110  * @return Returns selected type of annotation
111  */
113  return selType;
114  }
115 
116  /**
117  * Sets selected type of annotation
118  *
119  * @param selType Selected type of annotation
120  */
121  public void setSelType(AnnotType selType) {
122  this.selType = selType;
123  }
124 
125  /**
126  * Gets content of auxiliary variable with actual node for TreeTable
127  *
128  * @return Returns actual node of tree
129  */
130  public AnnotType getAnnotT() {
131  AnnotType annotT = Faces.var("annotT", AnnotType.class);
132  return annotT;
133  }
134 
135  /**
136  * Gets children of actual node in TreeTable
137  *
138  * @return Returns children of actual node
139  */
140  public List<AnnotType> getNodeChildren() {
141  List<AnnotType> rList = new ArrayList<AnnotType>();
142  AnnotType annotT = Faces.var("annotT", AnnotType.class); // get actual node
143 
144  if (annotT != null) { // if node is set
145 
146  // query DB for types, in which ancestors is actual node
147  EntityManager em = AppBean.getPersistenceManager().getEM();
148  List tList = null;
149  try {
150  Query query = em.createQuery("SELECT a FROM AnnotType a JOIN a.ancestorTypes at WHERE at.id='" + annotT.getId() + "'");
151  tList = query.getResultList();
152  } catch (Exception e) {
154  String msg = "DB query in getNodeChildren failed.";
155  Logger.getLogger(ATypes.class.getName()).log(Level.SEVERE, msg, e);
156  }
157  tList = null;
158  } finally {
159  em.close();
160  }
161  if (tList != null && !tList.isEmpty()) {
162  for (Iterator tIt = tList.iterator(); tIt.hasNext();) {
163  AnnotType at = (AnnotType) tIt.next();
164  rList.add(at); // add to list to return
165  }
166  }
167 
168  // query DB for types which primary ancestor is actual node
169  tList = null;
170  Object[] params = new Object[2];
171  params[0] = "ancestor";
172  params[1] = annotT.getId();
173  tList = AppBean.getPersistenceManager().queryDB("AnnotType.findByAncestor", params);
174  if (tList != null && !tList.isEmpty()) {
175  for (Iterator tIt = tList.iterator(); tIt.hasNext();) {
176  AnnotType at = (AnnotType) tIt.next();
177  if (!rList.contains(at)) {
178  rList.add(at); // add to list to return
179  }
180  }
181  }
182 
183  return rList;
184  } // if node is set
185 
186  // if node is not set, actual node is root node of tree
187  // (children are all top level types of annotations)
188 
189  // types must be get for all selected groups
190  for (Iterator<UserGroup> ugIt = selGroups.iterator(); ugIt.hasNext();) {
191  UserGroup ug = ugIt.next();
192  // query database for root types in given group
193  List tList = null;
194  EntityManager em = AppBean.getPersistenceManager().getEM();
195  try {
196  Query query = em.createQuery("SELECT a FROM AnnotType a WHERE a.groupId = '" + ug.getId() + "' AND a.ancestor IS NULL");
197  tList = query.getResultList();
198  } catch (Exception e) {
200  String msg = "DB query in getNodeChildren failed.";
201  Logger.getLogger(ATypes.class.getName()).log(Level.SEVERE, msg, e);
202  }
203  tList = null;
204  } finally {
205  em.close();
206  }
207 
208  if (tList != null && !tList.isEmpty()) { // if types was found
209  for (Iterator tIt = tList.iterator(); tIt.hasNext();) {
210  AnnotType type = (AnnotType) tIt.next();
211  rList.add(type); // add to list to return
212  }
213  }
214  } // for all selected groups
215 
216  return rList;
217  } // getNodeChildren()
218 
219 
220  /**
221  * Returns, whether actuall node in tree has children
222  *
223  * @return If actual node has children, returns true, false otherwise
224  */
225  public boolean getNodeHasChildren() {
226  AnnotType annotT = Faces.var("annotT", AnnotType.class);
227  if (annotT != null) { // if node is set
228  // query DB for types, in which ancestors is actual node
229  EntityManager em = AppBean.getPersistenceManager().getEM();
230  List tList = null;
231  try {
232  Query query = em.createQuery("SELECT a FROM AnnotType a JOIN a.ancestorTypes at WHERE at.id='" + annotT.getId() + "'");
233  tList = query.getResultList();
234  } catch (Exception e) {
236  String msg = "DB query in getNodeHasChildren failed.";
237  Logger.getLogger(ATypes.class.getName()).log(Level.SEVERE, msg, e);
238  }
239  tList = null;
240  } finally {
241  em.close();
242  }
243  if (tList != null && !tList.isEmpty()) {
244  return true;
245  }
246 
247  // query DB for types which primary ancestor is actual node
248  Object[] params = new Object[2];
249  params[0] = "ancestor";
250  params[1] = annotT.getId();
251  tList = AppBean.getPersistenceManager().queryDB("AnnotType.findByAncestor", params);
252  if (tList != null && !tList.isEmpty()) {
253  return true;
254  }
255  } // if (annotT != null ...
256  return false;
257  } // getNodeHasChildren()
258 
259  /**
260  * Gets selected user groups
261  *
262  * @return Returns selected user groups
263  */
264  public List<UserGroup> getSelGroups() {
265  return selGroups;
266  }
267 
268  /**
269  * Sets selected user groups
270  *
271  * @param selGroups Selected user groups
272  */
273  public void setSelGroups(List<UserGroup> selGroups) {
274  this.selGroups = selGroups;
275  }
276 
277  /**
278  * Gets selected user groups
279  *
280  * @return Returns list of selected user groups
281  */
282  public List<UserGroup> getSelGroupsS() {
283  return selGroups;
284  }
285 
286  /**
287  * Sets selected user groups from strings with groups
288  *
289  * @param selGroupsStrings List of strings with selected user groups in format
290  * "cz.vutbr.fit.knot.annotations.entity.UserGroup[id=N]"
291  * (cz.vutbr.fit.knot.annotations.entity.UserGroup.toString())
292  */
293  public void setSelGroupsS(List<String> selGroupsStrings) {
294  List<UserGroup> nList = new ArrayList<UserGroup>();
295  for (Iterator<String> sIt = selGroupsStrings.iterator(); sIt.hasNext();) {
296  String s = sIt.next();
297  int begin = s.indexOf("[id=") + 4; // find begin of id
298  if (begin < 0 || begin > s.length()) {
299  begin = 0;
300  }
301  int end = s.length() - 1; // find end of id
302  if (end < 0) {
303  end = 0;
304  }
305  String idS = s.substring(begin, end); // id of user group in string
306  Integer id = null;
307  try {
308  id = Integer.parseInt(idS); // parse id of group
309  } catch (NumberFormatException numberFormatException) {
311  String msg = "Exception in setSelGroupS";
312  Logger.getLogger(ATypes.class.getName()).log(Level.SEVERE, msg, numberFormatException);
313  }
314  continue;
315  }
316 
317  // query DB for user group
318  Object[] params = new Object[2];
319  params[0] = "id";
320  params[1] = id;
321 
322  @SuppressWarnings("unchecked")
323  List<UserGroup> ugList = AppBean.getPersistenceManager().queryDB("UserGroup.findById", params);
324  if (ugList != null && !ugList.isEmpty()) { // if user group was found
325  for (Iterator<UserGroup> ugIt = ugList.iterator(); ugIt.hasNext();) {
326  UserGroup ug = ugIt.next();
327  nList.add(ug);
328  }
329  }
330  } // for ...
331  selGroups = nList;
332  } // setSelGroupsS()
333 
334  /**
335  * Gets all user groups
336  *
337  * @return All user groups in list of SelectItem
338  */
339  public List<SelectItem> getAllGroups() {
340  // query DB for user groups
341  List<SelectItem> rList = new ArrayList<SelectItem>();
342 
343  @SuppressWarnings("unchecked")
344  List<UserGroup> ugList = AppBean.getPersistenceManager().queryDB("UserGroup.findAll");
345  if (ugList != null && !ugList.isEmpty()) { // if types was found
346  for (Iterator<UserGroup> ugIt = ugList.iterator(); ugIt.hasNext();) {
347  UserGroup ug = ugIt.next();
348  rList.add(new SelectItem(ug, ug.getName()));
349  }
350  }
351  return rList;
352  } // getAllGroups()
353 
354 
355 } // public class ATypes
void setSelGroups(List< UserGroup > selGroups)
Definition: ATypes.java:273
Singleton for storing global variables.
Definition: AppBean.java:47
Class representing user group.
Definition: UserGroup.java:47
void setSelType(AnnotType selType)
Definition: ATypes.java:121
void setSelGroupsS(List< String > selGroupsStrings)
Definition: ATypes.java:293
Class representing type of annotation.
Definition: AnnotType.java:58
Backbean for page with types of annotations.
Definition: ATypes.java:41