001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2007
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.war.beans.admin.main;
034:
035: import com.flexive.faces.FxJsfUtils;
036: import com.flexive.faces.messages.FxFacesMsgErr;
037: import com.flexive.faces.messages.FxFacesMsgInfo;
038: import com.flexive.shared.CacheAdmin;
039: import com.flexive.shared.EJBLookup;
040: import com.flexive.shared.FxContext;
041: import com.flexive.shared.FxSharedUtils;
042: import com.flexive.shared.exceptions.FxApplicationException;
043: import com.flexive.shared.interfaces.AccountEngine;
044: import com.flexive.shared.interfaces.UserGroupEngine;
045: import com.flexive.shared.security.Mandator;
046: import com.flexive.shared.security.Role;
047: import com.flexive.shared.security.UserGroup;
048: import com.flexive.shared.security.UserTicket;
049:
050: import java.util.ArrayList;
051: import java.util.Hashtable;
052: import java.util.List;
053: import java.util.Map;
054:
055: /**
056: * Bean providing access the the userGroup functionality.
057: *
058: * @author Gregor Schober (gregor.schober@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
059: * @version $Rev: 1 $
060: */
061: public class UserGroupBean {
062:
063: private String name = null;
064: private String color = null;
065: private Mandator mandator = null;
066: private long id = -1;
067: private AccountEngine accountEngine;
068: private UserGroupEngine groupEngine;
069: private List<Role> roles;
070: private Hashtable<Long, List<UserGroup>> groupLists;
071: private long createdGroupId = -1;
072: private static final String ID_CACHE_KEY = UserGroupBean.class
073: + "_id";
074:
075: public List<Role> getRoles() {
076: return (this .roles == null) ? new ArrayList<Role>(0)
077: : this .roles;
078: }
079:
080: public void setRoles(List<Role> roles) {
081: this .roles = roles;
082: }
083:
084: public String getName() {
085: return name;
086: }
087:
088: public void setName(String sName) {
089: this .name = sName;
090: }
091:
092: public String getColor() {
093: return color;
094: }
095:
096: public void setColor(String sColor) {
097: this .color = sColor;
098: }
099:
100: public Mandator getMandator() {
101: return mandator;
102: }
103:
104: public void setMandator(Mandator mandator) {
105: this .mandator = mandator;
106: }
107:
108: public long getId() {
109: return id;
110: }
111:
112: public void setId(long id) {
113: this .id = id;
114: FxJsfUtils.setSessionAttribute(ID_CACHE_KEY, id);
115: }
116:
117: public long getCreatedGroupId() {
118: return createdGroupId;
119: }
120:
121: public UserGroupBean() {
122: try {
123: this .groupEngine = EJBLookup.getUserGroupEngine();
124: this .accountEngine = EJBLookup.getAccountEngine();
125: this .groupLists = new Hashtable<Long, List<UserGroup>>(5);
126: } catch (Exception exc) {
127: new FxFacesMsgErr(exc).addToContext();
128: }
129: }
130:
131: /**
132: * Returns the list of all usergroups.
133: *
134: * @return all usergroups
135: */
136: public List<UserGroup> getList() {
137: try {
138: final UserTicket ticket = FxContext.get().getTicket();
139: long mandatorId;
140: if (ticket.isGlobalSupervisor()) {
141: // Drop down list enabled -> handle it
142: mandatorId = mandator == null ? -1 : mandator.getId();
143: } else {
144: // Only show the groups of the mandator the user belongs to
145: mandatorId = ticket.getMandatorId();
146: }
147:
148: // Load an cache within the result
149: List<UserGroup> result = groupLists.get(mandatorId);
150: if (result == null) {
151: result = groupEngine.loadAll(mandatorId).getList();
152: groupLists.put(mandatorId, result);
153: }
154: return result;
155:
156: } catch (Exception exc) {
157: new FxFacesMsgErr(exc).addToContext();
158: return new ArrayList<UserGroup>(0);
159: }
160: }
161:
162: /**
163: * Get the number of users for a group, call using userGroupBean.userCount['groupId']
164: *
165: * @return user count
166: */
167: public Map<Long, Long> getUserCount() {
168: return FxSharedUtils
169: .getMappedFunction(new FxSharedUtils.ParameterMapper<Long, Long>() {
170: public Long get(Object key) {
171: try {
172: return accountEngine.getAssignedUsersCount(
173: (Long) key, false);
174: } catch (FxApplicationException e) {
175: return 0L;
176: }
177: }
178: });
179: }
180:
181: /**
182: * Creates a new user group.
183: *
184: * @return the next page to render
185: */
186: public String create() {
187: try {
188: final UserTicket ticket = FxContext.get().getTicket();
189: long mandatorId = mandator == null ? -1 : mandator.getId();
190: if (!ticket.isGlobalSupervisor()) {
191: mandatorId = ticket.getMandatorId();
192: }
193: this .setId(groupEngine.create(name, color, mandatorId));
194: this .createdGroupId = this .id;
195: new FxFacesMsgInfo("UserGroup.nfo.created", name)
196: .addToContext();
197:
198: // Assign the given roles to the group
199: try {
200: groupEngine.setRoles(this .id, getRoles());
201: } catch (Exception exc) {
202: new FxFacesMsgErr(exc).addToContext();
203: }
204:
205: // Deselect the group and return to the overview
206: this .setId(-1);
207: return "userGroupOverview";
208: } catch (Exception exc) {
209: new FxFacesMsgErr(exc).addToContext();
210: return "userGroupNew";
211: }
212: }
213:
214: /**
215: * Deletes an existing user group.
216: *
217: * @return the next page to render.
218: */
219: public String delete() {
220: try {
221: ensureIdSet();
222: groupEngine.remove(id);
223: this .groupLists.clear();
224: } catch (Exception exc) {
225: new FxFacesMsgErr(exc).addToContext();
226: }
227: return "userGroupOverview";
228: }
229:
230: /**
231: * Edits a existing user group.
232: *
233: * @return the next page to render.
234: */
235: public String edit() {
236: try {
237: ensureIdSet();
238: UserGroup aGroup = groupEngine.load(id);
239: this .color = aGroup.getColor();
240: this .name = aGroup.getName();
241: this .mandator = CacheAdmin.getEnvironment().getMandator(
242: aGroup.getMandatorId());
243: this .roles = groupEngine.getRoles(aGroup.getId()).getList();
244: } catch (Exception exc) {
245: new FxFacesMsgErr(exc).addToContext();
246: }
247: return "userGroupEdit";
248: }
249:
250: private void ensureIdSet() {
251: if (this .id <= 0) {
252: this .id = (Long) FxJsfUtils
253: .getSessionAttribute(ID_CACHE_KEY);
254: }
255: }
256:
257: /**
258: * Updates a existing group.
259: *
260: * @return the next page to render
261: */
262: public String update() {
263: try {
264: // Update group data
265: groupEngine.update(id, name, color);
266: // Update the role assignments
267: try {
268: groupEngine.setRoles(this .id, getRoles());
269: } catch (Exception exc) {
270: new FxFacesMsgErr(exc).addToContext();
271: }
272: // Reload data
273: edit();
274: } catch (Exception exc) {
275: new FxFacesMsgErr(exc).addToContext();
276: }
277: return "userGroupEdit";
278: }
279:
280: }
|