4A Server -  2.0
 All Classes Namespaces Files Functions Variables Enumerator
Documents.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: Documents.java
5  * Description: Backbean for page with administration of uploaded documents
6  */
7 
8 /**
9  * @file Documents.java
10  *
11  * @brief Backbean for page with administration of uploaded documents
12  */
13 
14 package cz.vutbr.fit.knot.annotations.web;
15 
21 import java.io.Serializable;
22 import java.util.ArrayList;
23 import java.util.Iterator;
24 import java.util.List;
25 import javax.faces.bean.ManagedBean;
26 import javax.faces.bean.SessionScoped;
27 import javax.faces.component.html.HtmlDataTable;
28 import javax.faces.model.SelectItem;
29 
30 /**
31  * Backbean for page with administration of uploaded documents
32  *
33  * @brief Backbean for page with administration of uploaded documents
34  * @author idytrych
35  */
36 @ManagedBean
37 @SessionScoped
38 public class Documents implements Serializable {
39  /**
40  * Table with documents
41  */
42  private HtmlDataTable listTable;
43  /**
44  * Document selected in the table
45  */
47  /**
48  * Selected user groups
49  */
50  private List<UserGroup> selGroups = null;
51  /**
52  * Selected users
53  */
54  private List<User> selUsers = null;
55  /**
56  * Error message displayed in page
57  */
58  private String errorMessage = "";
59 
60  /**
61  * Constructor
62  * - Sets selected user groups to user's joined groups
63  * - Sets selected user to logged in user
64  */
65  public Documents() {
66  errorMessage = "";
67  if (selGroups == null) {
68  selGroups = SessionManager.getSession().getLoggedUser().getGroupsAL();
69  }
70  if (selUsers == null) {
71  selUsers = new ArrayList<User>();
72  selUsers.add(SessionManager.getSession().getLoggedUser());
73  }
74  }
75 
76  /**
77  * Gets table with documents
78  *
79  * @return Returns table with documents
80  */
81  public HtmlDataTable getListTable() {
82  return listTable;
83  }
84 
85  /**
86  * Sets table with documents
87  *
88  * @param listTable Table with documents
89  */
90  public void setListTable(HtmlDataTable listTable) {
91  this.listTable = listTable;
92  }
93 
94  /**
95  * Gets list of documents to display in the table
96  *
97  * @return Returns list of documents to display in the table
98  */
99  public List<StoredDocument> getDocumentsList() {
100  List<StoredDocument> rList = new ArrayList<StoredDocument>();
101  // documents must be get for all selected groups
102  for (Iterator<UserGroup> ugIt = selGroups.iterator(); ugIt.hasNext();) {
103  UserGroup ug = ugIt.next();
104  // query database for documents in given group
105  Object[] params = new Object[2];
106  params[0] = "groupId";
107  params[1] = ug.getId();
108  List dList = AppBean.getPersistenceManager().queryDB("StoredDocument.findByGroupId", params);
109  if (dList != null && !dList.isEmpty()) { // if some documents was found
110  for (Iterator dIT = dList.iterator(); dIT.hasNext();) {
111  StoredDocument sDoc = (StoredDocument)dIT.next();
112  if (!rList.contains(sDoc)) {
113  rList.add(sDoc); // add to list
114  }
115  }
116  } // if some documents was found
117  } // for all selected groups
118 
119  // documents must be get for all selected users too
120  for (Iterator<User> uIt = selUsers.iterator(); uIt.hasNext();) {
121  User u = uIt.next();
122  // query database for documents in given group
123  Object[] params = new Object[2];
124  params[0] = "ownerId";
125  params[1] = u.getId();
126  List dList = AppBean.getPersistenceManager().queryDB("StoredDocument.findByOwnerId", params);
127  if (dList != null && !dList.isEmpty()) { // if some documents was found
128  for (Iterator dIT = dList.iterator(); dIT.hasNext();) {
129  StoredDocument sDoc = (StoredDocument)dIT.next();
130  if (!rList.contains(sDoc)) {
131  rList.add(sDoc); // add to list
132  }
133  }
134  } // if some documents was found
135  } // for all selected users
136 
137  return rList;
138  } // getDocumentsList()
139 
140  /**
141  * Action listener for delete links in table
142  *
143  * @return Returns null to stay in this page
144  */
145  public String actionDelete() {
146  doc = (StoredDocument) listTable.getRowData();
147 
148  errorMessage = "";
149 
150  AnnotDocument aDoc = null;
151  // find annotated copy of this document
152  Object[] params = new Object[2];
153  params[0] = "uri";
154  params[1] = doc.getUri();
155  List gList = AppBean.getPersistenceManager().queryDB("AnnotDocument.findByUri", params);
156  if (gList != null && !gList.isEmpty()) { // if annotation was found
157  aDoc = (AnnotDocument) gList.get(0);
158  }
159 
160  // find annotations of this document
161  if (aDoc != null) {
162  params[0] = "sourceDocumentId";
163  params[1] = aDoc.getId();
164  List aList = AppBean.getPersistenceManager().queryDB("Annotation.findBySourceDocumentID", params);
165  if (aList != null && !aList.isEmpty()) { // if annotation was found
166  errorMessage = MessageProvider.getMessage("ifAnnotationsExistsDocumentCantBeDeleted");
167  return null;
168  }
169  }
170 
171  // check permissions
172  if (!doc.getOwner().equals(SessionManager.getSession().getLoggedUser())) {
173  if (SessionManager.getSession().isAdmin()) {
174  errorMessage = MessageProvider.getMessage("delOnlyOwnDoc");
175  return null;
176  }
177  }
178 
179  // delete from DB
180  AppBean.getPersistenceManager().removeEntity(doc);
181  doc = null;
182 
183  return null;
184  } // actionDelete()
185 
186  /**
187  * Gets selected user groups
188  *
189  * @return Returns selected user groups
190  */
191  public List<UserGroup> getSelGroups() {
192  return selGroups;
193  }
194 
195  /**
196  * Gets base URI of stored documents
197  *
198  * @return Returns base URI of stored documents
199  */
200  public String getBaseUri() {
201  return AppBean.getBaseStoredDocumentUri();
202  }
203 
204  /**
205  * Sets selected user groups
206  *
207  * @param selGroups Selected user groups
208  */
209  public void setSelGroups(List<UserGroup> selGroups) {
210  this.selGroups = selGroups;
211  }
212 
213  /**
214  * Gets selected user groups
215  *
216  * @return Returns list of selected user groups
217  */
218  public List<UserGroup> getSelGroupsS() {
219  return selGroups;
220  }
221 
222  /**
223  * Sets selected user groups from strings with groups
224  *
225  * @param selGroupsStrings List of strings with selected user groups in format
226  * "cz.vutbr.fit.knot.annotations.entity.UserGroup[id=N]"
227  * (cz.vutbr.fit.knot.annotations.entity.UserGroup.toString())
228  */
229  public void setSelGroupsS(List<String> selGroupsStrings) {
230  List<UserGroup> nList = new ArrayList<UserGroup>();
231  for (Iterator<String> sIt = selGroupsStrings.iterator(); sIt.hasNext();) {
232  String s = sIt.next();
233  int begin = s.indexOf("[id=") + 4; // find begin of id
234  if (begin < 0 || begin > s.length()) {
235  begin = 0;
236  }
237  int end = s.length() - 1; // find end of id
238  if (end < 0) {
239  end = 0;
240  }
241  String idS = s.substring(begin, end); // id of user group in string
242  Integer id = null;
243  try {
244  id = Integer.parseInt(idS); // parse id of group
245  } catch (NumberFormatException numberFormatException) {
246  continue;
247  }
248 
249  // query DB for user group
250  Object[] params = new Object[2];
251  params[0] = "id";
252  params[1] = id;
253 
254  @SuppressWarnings("unchecked")
255  List<UserGroup> ugList = AppBean.getPersistenceManager().queryDB("UserGroup.findById", params);
256  if (ugList != null && !ugList.isEmpty()) { // if user group was found
257  for (Iterator<UserGroup> ugIt = ugList.iterator(); ugIt.hasNext();) {
258  UserGroup ug = ugIt.next();
259  nList.add(ug);
260  }
261  }
262  } // for ...
263  selGroups = nList;
264  } // setSelGroupsS()
265 
266  /**
267  * Gets all user groups
268  *
269  * @return All user groups in list of SelectItem
270  */
271  public List<SelectItem> getAllGroups() {
272  // query DB for user groups
273  List<SelectItem> rList = new ArrayList<SelectItem>();
274 
275  @SuppressWarnings("unchecked")
276  List<UserGroup> ugList = AppBean.getPersistenceManager().queryDB("UserGroup.findAll");
277  if (ugList != null && !ugList.isEmpty()) { // if types was found
278  for (Iterator<UserGroup> ugIt = ugList.iterator(); ugIt.hasNext();) {
279  UserGroup ug = ugIt.next();
280  rList.add(new SelectItem(ug, ug.getName()));
281  }
282  }
283  return rList;
284  } // getAllGroups()
285 
286  /**
287  * Gets selected users
288  *
289  * @return Returns selected users
290  */
291  public List<User> getSelUsers() {
292  return selUsers;
293  }
294 
295  /**
296  * Sets selected users
297  *
298  * @param selUsers Selected users
299  */
300  public void setSelUsers(List<User> selUsers) {
301  this.selUsers = selUsers;
302  }
303 
304  /**
305  * Gets selected users
306  *
307  * @return Returns list of selected users
308  */
309  public List<User> getSelUsersS() {
310  return selUsers;
311  }
312 
313  /**
314  * Sets selected users from strings with users
315  *
316  * @param selUsersStrings List of strings with selected user groups in format
317  * "cz.vutbr.fit.knot.annotations.entity.User[id=N]"
318  * (cz.vutbr.fit.knot.annotations.entity.User.toString())
319  */
320  public void setSelUsersS(List<String> selUsersStrings) {
321  List<User> nList = new ArrayList<User>();
322  for (Iterator<String> sIt = selUsersStrings.iterator(); sIt.hasNext();) {
323  String s = sIt.next();
324  int begin = s.indexOf("[id=") + 4; // find begin of id
325  if (begin < 0 || begin > s.length()) {
326  begin = 0;
327  }
328  int end = s.length() - 1; // find end of id
329  if (end < 0) {
330  end = 0;
331  }
332  String idS = s.substring(begin, end); // id of user in string
333  Integer id = null;
334  try {
335  id = Integer.parseInt(idS); // parse id of user
336  } catch (NumberFormatException numberFormatException) {
337  continue;
338  }
339 
340  // query DB for user
341  Object[] params = new Object[2];
342  params[0] = "id";
343  params[1] = id;
344 
345  @SuppressWarnings("unchecked")
346  List<User> uList = AppBean.getPersistenceManager().queryDB("User.findById", params);
347  if (uList != null && !uList.isEmpty()) { // if user was found
348  for (Iterator<User> uIt = uList.iterator(); uIt.hasNext();) {
349  User u = uIt.next();
350  nList.add(u);
351  }
352  }
353  } // for ...
354  selUsers = nList;
355  } // setSelUsersS()
356 
357  /**
358  * Gets all users
359  *
360  * @return All users in list of SelectItem
361  */
362  public List<SelectItem> getAllUsers() {
363  // query DB for users
364  List<SelectItem> rList = new ArrayList<SelectItem>();
365 
366  @SuppressWarnings("unchecked")
367  List<User> uList = AppBean.getPersistenceManager().queryDB("User.findAll");
368  if (uList != null && !uList.isEmpty()) { // if types was found
369  for (Iterator<User> uIt = uList.iterator(); uIt.hasNext();) {
370  User u = uIt.next();
371  rList.add(new SelectItem(u, u.getName() + " (" + u.getLogin() + ")"));
372  }
373  }
374  return rList;
375  } // getAllUsers()
376 
377  /**
378  * Gets document selected in the table
379  *
380  * @return Returns document selected in the table
381  */
383  return doc;
384  }
385 
386  /**
387  * Sets document selected in the table
388  *
389  * @param doc Document selected in the table
390  */
391  public void setDoc(StoredDocument doc) {
392  this.doc = doc;
393  }
394 
395  /**
396  * Gets error message displayed in page
397  *
398  * @return Returns error message displayed in page
399  */
400  public String getErrorMessage() {
401  return errorMessage;
402  }
403 
404  /**
405  * Sets error message to be displayed in page
406  *
407  * @param errorMessage Error message to be displayed in page
408  */
409  public void setErrorMessage(String errorMessage) {
410  this.errorMessage = errorMessage;
411  }
412 
413 } // public class Documents
void setSelGroupsS(List< String > selGroupsStrings)
Definition: Documents.java:229
void setSelUsersS(List< String > selUsersStrings)
Definition: Documents.java:320
void setSelUsers(List< User > selUsers)
Definition: Documents.java:300
Class for manipulating with session.
Class representing annotated copy of document.
Singleton for storing global variables.
Definition: AppBean.java:47
Class representing user group.
Definition: UserGroup.java:47
void setSelGroups(List< UserGroup > selGroups)
Definition: Documents.java:209
List< StoredDocument > getDocumentsList()
Definition: Documents.java:99
void setErrorMessage(String errorMessage)
Definition: Documents.java:409
Class representing user.
Definition: User.java:51
Backbean for page with administration of uploaded documents.
Definition: Documents.java:38
void setListTable(HtmlDataTable listTable)
Definition: Documents.java:90