4A Server -  2.0
 All Classes Namespaces Files Functions Variables Enumerator
UserGroupSelectionForUser.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: UserGroupSelectionForUser.java
5  * Description: Backbean for page with selection of user groups
6  */
7 
8 /**
9  * @file UserGroupSelectionForUser.java
10  *
11  * @brief Backbean for page with selection of user groups
12  */
13 
14 package cz.vutbr.fit.knot.annotations.web;
15 
20 import java.io.Serializable;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
25 import javax.faces.bean.ManagedBean;
26 import javax.faces.bean.ViewScoped;
27 import javax.faces.component.html.HtmlDataTable;
28 import javax.persistence.EntityManager;
29 import javax.persistence.EntityTransaction;
30 
31 /**
32  * Backbean for page with administration of user groups
33  *
34  * @brief Backbean for administration of user groups
35  * @author idytrych
36  */
37 @ManagedBean
38 @ViewScoped
39 public class UserGroupSelectionForUser implements Serializable {
40  /**
41  * Table with user groups
42  */
43  private HtmlDataTable listTable;
44  /**
45  * User group selected in the table
46  */
48  /**
49  * Error message to display
50  */
51  private String errorMessage = "";
52  /**
53  * Edited user (for selection variant)
54  */
55  private User editedUser = null;
56  /**
57  * Backup of form data
58  */
59  private Object formBackup = null;
60 
61  /**
62  * Constructor
63  */
65  this.userGroup = new UserGroup();
66  }
67 
68  /**
69  * Gets user group selected in the table
70  *
71  * @return Returns user group selected in the table
72  */
74  return userGroup;
75  }
76 
77  /**
78  * Sets user group selected in the table
79  *
80  * @param userGroup
81  */
83  this.userGroup = userGroup;
84  }
85 
86  /**
87  * Gets table with user groups
88  *
89  * @return Returns table with user groups
90  */
91  public HtmlDataTable getListTable() {
92  return listTable;
93  }
94 
95  /**
96  * Sets table with user groups
97  *
98  * @param listTable Table with user groups
99  */
100  public void setListTable(HtmlDataTable listTable) {
101  this.listTable = listTable;
102  }
103 
104  /**
105  * Gets list of all existing user groups in which aren't given user
106  *
107  * @return Returns list of user groups
108  */
109  public List<UserGroup> getUserGroupsList() {
110  if (editedUser == null) {
111  editedUser = SessionManager.getSession().getEditedUser();
112  formBackup = SessionManager.getSession().getFormBackup();
113  }
114  @SuppressWarnings("unchecked")
115  List<UserGroup> retList = AppBean.getPersistenceManager().getEntitiesByName("UserGroup");
116  // remove user groups in which user already is
117  for (Iterator<UserGroup> it = retList.iterator(); it.hasNext();) {
118  UserGroup ug = it.next();
119  if (editedUser.getGroups().contains(ug)) {
120  it.remove();
121  }
122  }
123  return retList;
124  }
125 
126  /**
127  * Action listener for select links in table
128  *
129  * @returns Returns page outcome (identificator of next page or null to stay here)
130  */
131  public String btnSelectAction(Integer id) {
132  // get row data of selected row
133  UserGroup refUG = new UserGroup(id);
134  int index = getUserGroupsList().indexOf(refUG);
135  userGroup = getUserGroupsList().get(index);
136  try {
137  EntityManager em = AppBean.getPersistenceManager().getEM();
138  EntityTransaction transaction = em.getTransaction();
139  transaction.begin();
140  editedUser = em.merge(editedUser);
141  userGroup = em.merge(userGroup);
142  if (!userGroup.getUsers().contains(editedUser)) {
143  userGroup.getUsers().add(editedUser);
144  }
145  editedUser.addGroup(userGroup);
146  em.flush();
147  transaction.commit();
148  } catch (Exception e) {
149  errorMessage = MessageProvider.getMessage("ucerCantBeAddedDBFailure");
151  String msg = "Adding of user to user group failed.";
152  Logger.getLogger(UserGroupSelectionForUser.class.getName()).log(Level.SEVERE, msg, e);
153  }
154  return null;
155  }
156 
157  AppBean.refreshUsersInSessions(editedUser); // refreshes data in sessions
158 
159  SessionManager.getSession().setEditedUser(editedUser);
160  SessionManager.getSession().setFormBackup(formBackup);
161  return "editUser";
162  }
163 
164  /**
165  * Action listener for cancel button
166  *
167  * @return Returns page outcome (identificator of next page)
168  */
169  public String btnCancelAction() {
170  SessionManager.getSession().setEditedUser(editedUser);
171  SessionManager.getSession().setFormBackup(formBackup);
172  return "editUser";
173  }
174 
175  /**
176  * Gets error message to display
177  *
178  * @return Returns error message to display
179  */
180  public String getErrorMessage() {
181  return errorMessage;
182  }
183 
184  /**
185  * Sets error message to display
186  *
187  * @param errorMessage Error message to display
188  */
189  public void setErrorMessage(String errorMessage) {
190  this.errorMessage = errorMessage;
191  }
192 
193 } // public class UserGroupSelectionForUser
Singleton for storing global variables.
Definition: AppBean.java:47
Class representing user group.
Definition: UserGroup.java:47
Class representing user.
Definition: User.java:51