4A Server -  2.0
 All Classes Namespaces Files Functions Variables Enumerator
AttrO.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: AttrO.java
5  * Description: Backbean for page with attributes from ontology
6  */
7 
8 /**
9  * @file AttrO.java
10  *
11  * @brief Backbean for page with attributes from ontology
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.component.html.HtmlDataTable;
29 import javax.faces.model.SelectItem;
30 import javax.persistence.EntityManager;
31 import javax.persistence.EntityTransaction;
32 
33 /**
34  * Backbean for page with attributes from ontology
35  *
36  * @brief Backbean for page with attributes from ontology
37  * @author idytrych
38  */
39 @ManagedBean
40 @SessionScoped
41 public class AttrO implements Serializable {
42  /**
43  * Table with attributes of given type of annotation
44  */
45  private HtmlDataTable listTable;
46  /**
47  * Selected user groups
48  */
49  private List<UserGroup> selGroups = null;
50  /**
51  * Attribute selected in the table
52  */
53  private TypeAttrOnto attr;
54 
55  /**
56  * Error message displayed in form
57  */
58  private String errorMessage = "";
59 
60 
61  /**
62  * Constructor
63  * - Sets selected user groups to user's joined groups
64  */
65  public AttrO() {
66  if (selGroups == null) {
67  selGroups = SessionManager.getSession().getLoggedUser().getGroupsAL();
68  }
69  }
70 
71  /**
72  * Action listener for link for remove attribute
73  *
74  * @param id Id of removed attribute
75  * @return Returns null to stay in this page
76  */
77  public String actionRemoveAttr(Integer id) {
78  TypeAttrOnto refAt = new TypeAttrOnto(id);
79  int index = getAttrList().indexOf(refAt);
80  attr = getAttrList().get(index);
81  try {
82  EntityManager em = AppBean.getPersistenceManager().getEM();
83  EntityTransaction transaction = em.getTransaction();
84  transaction.begin();
85  attr = em.merge(attr);
86  em.remove(attr);
87  em.flush();
88  transaction.commit();
89  } catch (Exception e) {
91  String msg = "Removing of attribute from ontology failed..";
92  Logger.getLogger(AttrO.class.getName()).log(Level.SEVERE, msg, e);
93  }
94  errorMessage = MessageProvider.getMessage("attrCantBeRemovedDF");
95  }
96  return null;
97  } // actionRemoveAttr()
98 
99  /**
100  * Action listener for link for edit attribute
101  *
102  * @param id Id of edited attribute
103  * @return Returns page outcome (identificator of transition)
104  */
105  public String actionEditAttr(Integer id) {
106  TypeAttrOnto refAt = new TypeAttrOnto(id);
107  int index = getAttrList().indexOf(refAt);
108  attr = getAttrList().get(index);
109 
110  SessionManager.getSession().setEditedTAO(attr);
111  return "editTAO";
112  } // actionEditAttr()
113 
114  /**
115  * Action listener for link for add attribute
116  *
117  * @return Returns page outcome (identificator of transition)
118  */
119  public String actionAddAttr() {
120  SessionManager.getSession().setEditedTAO(null);
121  return "addTAO";
122  } // actionAddAttr()
123 
124  /**
125  * Gets list of attributes from ontology
126  *
127  * @return Returns list of attributes from ontology
128  */
129  public List<TypeAttrOnto> getAttrList() {
130  List<TypeAttrOnto> rList = new ArrayList<TypeAttrOnto>();
131  // query database for attributes
132  Object[] params = new Object[2];
133  params[0] = "groupId";
134  for (Iterator<UserGroup> sgIt = selGroups.iterator(); sgIt.hasNext();) { // for all selected groups
135  UserGroup ug = sgIt.next();
136  params[1] = ug.getId();
137  List aList = AppBean.getPersistenceManager().queryDB("TypeAttrOnto.findByUserGroup", params);
138  if (aList != null && !aList.isEmpty()) { // if some types was found
139  for (Iterator aIt = aList.iterator(); aIt.hasNext();) {
140  TypeAttrOnto tao = (TypeAttrOnto)aIt.next();
141  rList.add(tao);
142  }
143  }
144  } // for all selected groups
145 
146  return rList;
147  } // getAttrList()
148 
149  /**
150  * Gets selected attribute in table
151  *
152  * @return Returns selected attribute in table
153  */
155  return attr;
156  }
157 
158  /**
159  * Sets selected attribute in table
160  *
161  * @param attr Selected attribute in table
162  */
163  public void setAttr(TypeAttrOnto attr) {
164  this.attr = attr;
165  }
166 
167  /**
168  * Gets table with attributes of given type of annotation
169  *
170  * @return Returns table with attributes of given type of annotation
171  */
172  public HtmlDataTable getListTable() {
173  return listTable;
174  }
175 
176  /**
177  * Sets table with attributes of given type of annotation
178  *
179  * @param listTable Table with attributes of given type of annotation
180  */
181  public void setListTable(HtmlDataTable listTable) {
182  this.listTable = listTable;
183  }
184 
185  /**
186  * Gets error message displayed in form
187  *
188  * @return Returns error message displayed in form
189  */
190  public String getErrorMessage() {
191  return errorMessage;
192  }
193 
194  /**
195  * Sets error message to be displayed in form
196  *
197  * @param errorMessage Error message to be displayed in form
198  */
199  public void setErrorMessage(String errorMessage) {
200  this.errorMessage = errorMessage;
201  }
202 
203  /**
204  * Gets selected user groups
205  *
206  * @return Returns selected user groups
207  */
208  public List<UserGroup> getSelGroups() {
209  return selGroups;
210  }
211 
212  /**
213  * Sets selected user groups
214  *
215  * @param selGroups Selected user groups
216  */
217  public void setSelGroups(List<UserGroup> selGroups) {
218  this.selGroups = selGroups;
219  }
220 
221  /**
222  * Gets selected user groups
223  *
224  * @return Returns list of selected user groups
225  */
226  public List<UserGroup> getSelGroupsS() {
227  return selGroups;
228  }
229 
230  /**
231  * Sets selected user groups from strings with groups
232  *
233  * @param selGroupsStrings List of strings with selected user groups in format
234  * "cz.vutbr.fit.knot.annotations.entity.UserGroup[id=N]"
235  * (cz.vutbr.fit.knot.annotations.entity.UserGroup.toString())
236  */
237  public void setSelGroupsS(List<String> selGroupsStrings) {
238  List<UserGroup> nList = new ArrayList<UserGroup>();
239  for (Iterator<String> sIt = selGroupsStrings.iterator(); sIt.hasNext();) {
240  String s = sIt.next();
241  int begin = s.indexOf("[id=") + 4; // find begin of id
242  if (begin < 0 || begin > s.length()) {
243  begin = 0;
244  }
245  int end = s.length() - 1; // find end of id
246  if (end < 0) {
247  end = 0;
248  }
249  String idS = s.substring(begin, end); // id of user group in string
250  Integer id = null;
251  try {
252  id = Integer.parseInt(idS); // parse id of group
253  } catch (NumberFormatException numberFormatException) {
255  String msg = "Exception in setSelGroupS.";
256  Logger.getLogger(ATypes.class.getName()).log(Level.SEVERE, msg, numberFormatException);
257  }
258  continue;
259  }
260 
261  // query DB for user group
262  Object[] params = new Object[2];
263  params[0] = "id";
264  params[1] = id;
265 
266  @SuppressWarnings("unchecked")
267  List<UserGroup> ugList = AppBean.getPersistenceManager().queryDB("UserGroup.findById", params);
268  if (ugList != null && !ugList.isEmpty()) { // if user group was found
269  for (Iterator<UserGroup> ugIt = ugList.iterator(); ugIt.hasNext();) {
270  UserGroup ug = ugIt.next();
271  nList.add(ug);
272  }
273  }
274  } // for ...
275  selGroups = nList;
276  } // setSelGroupsS()
277 
278  /**
279  * Gets all user groups
280  *
281  * @return All user groups in list of SelectItem
282  */
283  public List<SelectItem> getAllGroups() {
284  // query DB for user groups
285  List<SelectItem> rList = new ArrayList<SelectItem>();
286 
287  @SuppressWarnings("unchecked")
288  List<UserGroup> ugList = AppBean.getPersistenceManager().queryDB("UserGroup.findAll");
289  if (ugList != null && !ugList.isEmpty()) { // if types was found
290  for (Iterator<UserGroup> ugIt = ugList.iterator(); ugIt.hasNext();) {
291  UserGroup ug = ugIt.next();
292  rList.add(new SelectItem(ug, ug.getName()));
293  }
294  }
295  return rList;
296  } // getAllGroups()
297 
298 } // public class AttrO
void setListTable(HtmlDataTable listTable)
Definition: AttrO.java:181
Class representing attribute of unknown type of annotation.
void setAttr(TypeAttrOnto attr)
Definition: AttrO.java:163
void setSelGroups(List< UserGroup > selGroups)
Definition: AttrO.java:217
String actionRemoveAttr(Integer id)
Definition: AttrO.java:77
Backbean for page with attributes from ontology.
Definition: AttrO.java:41
List< UserGroup > getSelGroups()
Definition: AttrO.java:208
Singleton for storing global variables.
Definition: AppBean.java:47
void setErrorMessage(String errorMessage)
Definition: AttrO.java:199
Class representing user group.
Definition: UserGroup.java:47
List< UserGroup > getSelGroupsS()
Definition: AttrO.java:226
List< SelectItem > getAllGroups()
Definition: AttrO.java:283
void setSelGroupsS(List< String > selGroupsStrings)
Definition: AttrO.java:237
List< TypeAttrOnto > getAttrList()
Definition: AttrO.java:129