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.EJBLookup;
039: import com.flexive.shared.FxContext;
040: import com.flexive.shared.CacheAdmin;
041: import com.flexive.shared.interfaces.ACLEngine;
042: import com.flexive.shared.interfaces.UserGroupEngine;
043: import com.flexive.shared.security.*;
044:
045: import javax.faces.event.ActionEvent;
046: import javax.faces.model.SelectItem;
047: import java.io.Serializable;
048: import java.util.ArrayList;
049: import java.util.List;
050:
051: /**
052: * Management of ACLs.
053: *
054: * @author Gregor Schober (gregor.schober@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
055: * @version $Rev: 170 $
056: */
057: public class AclBean {
058:
059: private long id;
060: private Mandator mandator;
061: private ACL acl = null;
062: private ACLEngine aclEngine;
063: private UserGroupEngine groupEngine;
064: private List<ACLAssignmentEdit> assignments;
065: private long assignmentId;
066: private static final String ID_CACHE_KEY = AclBean.class + "_id";
067: private String selectedIds;
068:
069: public String getSelectedIds() {
070: return selectedIds;
071: }
072:
073: public void setSelectedIds(String selectedIds) {
074: this .selectedIds = selectedIds;
075: }
076:
077: public long[] getSelectedIdsAsLong() {
078: if (selectedIds == null || selectedIds.trim().length() == 0) {
079: return new long[0];
080: } else {
081: String ids[] = selectedIds.split(",");
082: long result[] = new long[ids.length];
083: int pos = 0;
084: for (String id : ids) {
085: result[pos++] = Long.valueOf(id);
086: }
087: return result;
088: }
089: }
090:
091: public long getAssignmentId() {
092: return assignmentId;
093: }
094:
095: public void setAssignmentId(long assignmentId) {
096: this .assignmentId = assignmentId;
097: }
098:
099: public AclBean() {
100: this .aclEngine = EJBLookup.getACLEngine();
101: this .groupEngine = EJBLookup.getUserGroupEngine();
102: this .acl = new ACL();
103: }
104:
105: public ACL getAcl() {
106: return acl;
107: }
108:
109: public void setAcl(ACL acl) {
110: this .acl = acl;
111: }
112:
113: public Mandator getMandator() {
114: if (mandator == null) {
115: return CacheAdmin.getEnvironment().getMandator(
116: FxContext.get().getTicket().getMandatorId());
117: }
118: return mandator;
119: }
120:
121: public void setMandator(Mandator mandator) {
122: this .mandator = mandator;
123: }
124:
125: public long getId() {
126: return id;
127: }
128:
129: public void setId(long id) {
130: this .id = id;
131: FxJsfUtils.setSessionAttribute(ID_CACHE_KEY, id);
132: }
133:
134: public List<ACLAssignmentEdit> getAssignments() {
135: if (assignments == null) {
136: assignments = new ArrayList<ACLAssignmentEdit>(5);
137: }
138: return assignments;
139: }
140:
141: public void setAssignments(List<ACLAssignmentEdit> assignments) {
142: this .assignments = assignments;
143: }
144:
145: public void addAssignment(ActionEvent event) {
146: getAssignments().add(new ACLAssignmentEdit(acl));
147: }
148:
149: public void removeAssignment(ActionEvent event) {
150: for (ACLAssignmentEdit ass : assignments) {
151: if (ass.getId() == assignmentId) {
152: assignments.remove(ass);
153: return;
154: }
155: }
156: }
157:
158: /**
159: * Returns a list of all ACLs.
160: *
161: * @return a list of all ACL.
162: */
163: public List<ACL> getList() {
164: try {
165: final UserTicket ticket = FxContext.get().getTicket();
166: long mandatorId = (ticket.isGlobalSupervisor()) ? getMandator()
167: .getId()
168: : ticket.getMandatorId();
169: return CacheAdmin.getFilteredEnvironment().getACLs(
170: mandatorId, false);
171: } catch (Exception exc) {
172: new FxFacesMsgErr(exc).addToContext();
173: return new ArrayList<ACL>(0);
174: }
175: }
176:
177: /**
178: * Deletes the acl specified by the id field.
179: *
180: * @return the next page to render
181: */
182: public String delete() {
183: try {
184: ensureIdSet();
185: aclEngine.remove(id);
186: } catch (Exception exc) {
187: new FxFacesMsgErr(exc).addToContext();
188: }
189: return "aclOverview";
190: }
191:
192: /**
193: * Deletes all selected acls.
194: *
195: * @return the next page to render
196: */
197: public String deleteSelected() {
198: int delCount = 0;
199: long _ids[] = getSelectedIdsAsLong();
200: if (_ids != null && _ids.length > 0) {
201: for (long _id : _ids) {
202: try {
203: aclEngine.remove(_id);
204: delCount++;
205: } catch (Exception exc) {
206: new FxFacesMsgErr(exc).addToContext();
207: }
208: }
209: if (delCount > 0) {
210: new FxFacesMsgInfo("ACL.nfo.deletedWithCount", delCount)
211: .addToContext();
212: } else {
213: new FxFacesMsgErr("ACL.err.noAclDeleted")
214: .addToContext();
215: }
216: } else {
217: new FxFacesMsgInfo("ACL.nfo.noSelected").addToContext();
218: }
219: return "aclOverview";
220: }
221:
222: /**
223: * Creates a new acl.
224: *
225: * @return the next page to render
226: */
227: public String create() {
228: try {
229: // create the acl
230: final UserTicket ticket = FxContext.get().getTicket();
231: long mandatorId = (ticket.isGlobalSupervisor()) ? (mandator == null ? -1
232: : mandator.getId())
233: : ticket.getMandatorId();
234: setId(aclEngine.create(acl.getName(), acl.getLabel(),
235: mandatorId, acl.getColor(), acl.getDescription(),
236: acl.getCategory()));
237: new FxFacesMsgInfo("ACL.nfo.created", acl.getName())
238: .addToContext();
239: // load and display the acl
240: return edit();
241: } catch (Exception exc) {
242: new FxFacesMsgErr(exc).addToContext();
243: }
244: return "aclCreate";
245: }
246:
247: /**
248: * Edits a acl.
249: *
250: * @return the next page to render
251: */
252: public String edit() {
253: try {
254: ensureIdSet();
255: acl = aclEngine.load(id);
256: final List<ACLAssignment> _assignments = aclEngine
257: .loadAssignments(id);
258: assignments = new ArrayList<ACLAssignmentEdit>(_assignments
259: .size() + 5);
260: for (ACLAssignment ass : _assignments)
261: assignments.add(new ACLAssignmentEdit(ass));
262: id = acl.getId();
263: return "aclEdit";
264: } catch (Exception exc) {
265: new FxFacesMsgErr(exc).addToContext();
266: return "aclOverview";
267: }
268: }
269:
270: private void ensureIdSet() {
271: if (this .id <= 0) {
272: this .id = (Long) FxJsfUtils
273: .getSessionAttribute(ID_CACHE_KEY);
274: }
275: }
276:
277: /**
278: * Saves the changes to the ACL.
279: *
280: * @return the next page to render
281: */
282: public String save() {
283: try {
284: ArrayList<ACLAssignment> list = new ArrayList<ACLAssignment>(
285: getAssignments().size());
286: for (ACLAssignmentEdit ass : getAssignments()) {
287: list.add(new ACLAssignment(ass.getAclId(), ass
288: .getGroupId(), ass.getMayRead(), ass
289: .getMayEdit(), ass.getMayRelate(), ass
290: .getMayDelete(), ass.getMayExport(), ass
291: .getMayCreate(), ass.getACLCategory(), ass
292: .getLifeCycleInfo()));
293: }
294: aclEngine.update(id, acl.getName(), acl.getLabel(), acl
295: .getColor(), acl.getDescription(), list);
296: this .acl = aclEngine.load(id);
297: new FxFacesMsgInfo("ACL.nfo.saved", acl.getName())
298: .addToContext();
299: } catch (Exception exc) {
300: new FxFacesMsgErr(exc).addToContext();
301: }
302: return "aclEdit";
303: }
304:
305: /**
306: * Getter for all available categories.
307: *
308: * @return all available categories
309: */
310: public List<SelectItem> getCategories() {
311: List<SelectItem> result = new ArrayList<SelectItem>(
312: ACL.Category.values().length);
313: for (ACL.Category cat : ACL.Category.values()) {
314: result.add(new SelectItem(String.valueOf(cat.getId()),
315: String.valueOf(cat)));
316: }
317: return result;
318: }
319:
320: /**
321: * ACLAssignmentEdit extends ACLAssignment with the ability to set/get the Group as Object
322: * for the jsf frontend.
323: */
324: public static class ACLAssignmentEdit extends ACLAssignment
325: implements Serializable {
326: private static long ID_GEN;
327: private long id;
328: private UserGroup group;
329:
330: public ACLAssignmentEdit(ACLAssignment ass) {
331: super (ass.getAclId(), ass.getGroupId(), ass.getMayRead(),
332: ass.getMayEdit(), ass.getMayRelate(), ass
333: .getMayDelete(), ass.getMayExport(), ass
334: .getMayCreate(), ass.getACLCategory(), ass
335: .getLifeCycleInfo());
336: id = generateId();
337: }
338:
339: public ACLAssignmentEdit(ACL acl) {
340: super (acl.getId(), -1, acl.getCategory(), acl
341: .getLifeCycleInfo());
342: id = generateId();
343: }
344:
345: private synchronized long generateId() {
346: if (ID_GEN == Long.MAX_VALUE) {
347: ID_GEN = 0;
348: }
349: return ID_GEN++;
350: }
351:
352: public long getId() {
353: return id;
354: }
355:
356: public UserGroup getGroup() {
357: if (this .getGroupId() < 0)
358: return null;
359: if (group == null) {
360: try {
361: group = getGroupEngine().load(this .getGroupId());
362: } catch (Throwable t) {
363: new FxFacesMsgErr(t).addToContext();
364: }
365: }
366: return group;
367: }
368:
369: public void setGroup(UserGroup group) {
370: this .group = group;
371: this .setGroupId(group.getId());
372: }
373:
374: /**
375: * Helper funcion, return the group interface of the ACL beans itself.
376: *
377: * @return the group interface
378: */
379: private UserGroupEngine getGroupEngine() {
380: return ((AclBean) FxJsfUtils.getManagedBean("aclBean")).groupEngine;
381: }
382: }
383: }
|