4A Server -  2.0
 All Classes Namespaces Files Functions Variables Enumerator
TypeSelect.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: Subscriptions.java
5  * Description: Backbean for page with administration of subscriptions
6  */
7 
8 /**
9  * @file TypeSelect.java
10  *
11  * @brief Backbean for page with selection of annotation type
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.persistence.EntityManager;
29 import javax.persistence.Query;
30 import org.openfaces.util.Faces;
31 
32 /**
33  * Backbean for page with selection of annotation type
34  *
35  * @brief Backbean for page with selection of annotation type
36  * @author xpetrm05
37  */
38 @ManagedBean
39 @SessionScoped
40 public class TypeSelect implements Serializable{
41 
42  /**
43  * Selected group ID
44  */
45  private Integer selectedId = null;
46 
47  /**
48  * Selected type of annotation
49  */
50  private AnnotType selType = null;
51 
52  /**
53  * Constructor
54  */
55  public TypeSelect() {
56  }
57 
58  /**
59  * Gets id of selected group
60  *
61  * @return Returns selected type of annotation
62  */
63  public Integer getSelectedId() {
64  return selectedId;
65  }
66 
67  /**
68  * Sets id of selected group
69  *
70  * @param selectedId Id of selected group
71  */
72  public void setSelectedId(Integer selectedId) {
73  this.selectedId = selectedId;
74  }
75 
76  /**
77  * Gets selected anotation type
78  *
79  * @return Returns selected type of annotation
80  */
81  public AnnotType getSelType() {
82  return selType;
83  }
84 
85  /**
86  * Sets selected anotation type
87  *
88  * @param selType Selected type of annotation
89  */
90  public void setSelType(AnnotType selType) {
91  this.selType = selType;
92  }
93 
94  /**
95  * Gets list of all users groups
96  *
97  * @return Returns list of all users group
98  */
99  public List<UserGroup> getGroups() {
100  @SuppressWarnings("unchecked")
101  List<UserGroup> retList = AppBean.getPersistenceManager().queryDB("UserGroup.findAll");
102 
103  return retList;
104  }
105 
106  public String btnSelectGroupAction(){
107  return null;
108  }
109 
110  public List<AnnotType> getNodeChildren() {
111  List<AnnotType> rList = new ArrayList<AnnotType>();
112  AnnotType annotT = Faces.var("annotT", AnnotType.class); // get actual node
113 
114  if (annotT != null) { // if node is set
115 
116  // query DB for types, in which ancestors is actual node
117  EntityManager em = AppBean.getPersistenceManager().getEM();
118  List tList = null;
119  try {
120  Query query = em.createQuery("SELECT a FROM AnnotType a JOIN a.ancestorTypes at WHERE at.id='" + annotT.getId() + "'");
121  tList = query.getResultList();
122  } catch (Exception e) {
124  String msg = "DB query in getNodeChildren failed.";
125  Logger.getLogger(ATypes.class.getName()).log(Level.SEVERE, msg, e);
126  }
127  tList = null;
128  } finally {
129  em.close();
130  }
131  if (tList != null && !tList.isEmpty()) {
132  for (Iterator tIt = tList.iterator(); tIt.hasNext();) {
133  AnnotType at = (AnnotType) tIt.next();
134  rList.add(at); // add to list to return
135  }
136  }
137 
138  // query DB for types which primary ancestor is actual node
139  tList = null;
140  Object[] params = new Object[2];
141  params[0] = "ancestor";
142  params[1] = annotT.getId();
143  tList = AppBean.getPersistenceManager().queryDB("AnnotType.findByAncestor", params);
144  if (tList != null && !tList.isEmpty()) {
145  for (Iterator tIt = tList.iterator(); tIt.hasNext();) {
146  AnnotType at = (AnnotType) tIt.next();
147  if (!rList.contains(at)) {
148  rList.add(at); // add to list to return
149  }
150  }
151  }
152 
153  return rList;
154  } // if node is set
155 
156  // if node is not set, actual node is root node of tree
157  // (children are all top level types of annotations)
158 
159  // types must be get for all selected groups
160  if(selectedId != null){
161  // query database for root types in given group
162  List tList = null;
163  EntityManager em = AppBean.getPersistenceManager().getEM();
164  try {
165  Query query = em.createQuery("SELECT a FROM AnnotType a WHERE a.groupId = '" + selectedId + "' AND a.ancestor IS NULL");
166  tList = query.getResultList();
167  } catch (Exception e) {
169  String msg = "DB query in getNodeChildren failed.";
170  Logger.getLogger(ATypes.class.getName()).log(Level.SEVERE, msg, e);
171  }
172  tList = null;
173  } finally {
174  em.close();
175  }
176 
177  if (tList != null && !tList.isEmpty()) { // if types was found
178  for (Iterator tIt = tList.iterator(); tIt.hasNext();) {
179  AnnotType type = (AnnotType) tIt.next();
180  rList.add(type); // add to list to return
181  }
182  }
183  }
184  return rList;
185  } // getNodeChildren()
186 
187 
188  /**
189  * Returns, whether actuall node in tree has children
190  *
191  * @return If actual node has children, returns true, false otherwise
192  */
193  public boolean getNodeHasChildren() {
194  AnnotType annotT = Faces.var("annotT", AnnotType.class);
195 
196  if (annotT != null) { // if node is set
197  // query DB for types, in which ancestors is actual node
198  EntityManager em = AppBean.getPersistenceManager().getEM();
199  List tList = null;
200  try {
201  Query query = em.createQuery("SELECT a FROM AnnotType a JOIN a.ancestorTypes at WHERE at.id='" + annotT.getId() + "'");
202  tList = query.getResultList();
203  } catch (Exception e) {
205  String msg = "DB query in getNodeHasChildren failed.";
206  Logger.getLogger(ATypes.class.getName()).log(Level.SEVERE, msg, e);
207  }
208  tList = null;
209  } finally {
210  em.close();
211  }
212  if (tList != null && !tList.isEmpty()) {
213  return true;
214  }
215 
216  // query DB for types which primary ancestor is actual node
217  Object[] params = new Object[2];
218  params[0] = "ancestor";
219  params[1] = annotT.getId();
220  tList = AppBean.getPersistenceManager().queryDB("AnnotType.findByAncestor", params);
221  if (tList != null && !tList.isEmpty()) {
222  return true;
223  }
224  } // if (annotT != null ...
225  return false;
226  } // getNodeHasChildren()
227 
228  /**
229  * Action listener for select button
230  *
231  * @return Returns page outcome
232  */
233  public String btnSelectAction() {
234  if(selType != null) SessionManager.getSession().setAnnotationTypeAddress(selType.getUri());
235  else SessionManager.getSession().setAnnotationTypeAddress(null);
236  if(SessionManager.getSession().getComeFrom() != null){
237  return SessionManager.getSession().getComeFrom();
238  }else
239  {
240  return "addSubscription";
241  }
242  }
243 
244  /**
245  * Action listener for cancel button
246  *
247  * @return Returns page outcome
248  */
249  public String btnCancelAction() {
250  SessionManager.getSession().setAnnotationTypeAddress(null); // clean up variable in session
251  if(SessionManager.getSession().getComeFrom() != null){
252  return SessionManager.getSession().getComeFrom();
253  }else
254  {
255  SessionManager.getSession().setEditedSubscription(null);
256  return "addSubscription";
257  }
258  }
259 }
Class for manipulating with session.
Backbean for page with selection of annotation type.
Definition: TypeSelect.java:40
Singleton for storing global variables.
Definition: AppBean.java:47
Class representing user group.
Definition: UserGroup.java:47
Class representing type of annotation.
Definition: AnnotType.java:58