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.exceptions.FxApplicationException;
041: import com.flexive.shared.interfaces.MandatorEngine;
042: import com.flexive.shared.security.Mandator;
043:
044: import java.util.*;
045:
046: /**
047: * This Bean provides access to the mandator functionality.
048: *
049: * @author Gregor Schober (gregor.schober@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
050: * @version $Rev: 144 $
051: */
052: public class MandatorBean {
053:
054: private Hashtable<Long, Mandator> mandatorsById = null;
055:
056: private long id = -1;
057: private boolean active = false;
058: private String name;
059: private Mandator mandator;
060: private MandatorEngine mandatorInterface;
061:
062: private static final String ID_CACHE_KEY = MandatorBean.class
063: + "_id";
064:
065: // constructor
066: public MandatorBean() {
067: this .mandatorsById = new Hashtable<Long, Mandator>();
068: this .mandatorInterface = EJBLookup.getMandatorEngine();
069: this .mandator = new Mandator();
070: }
071:
072: /**
073: * Returns a hashtable holding all mandators with their ID (String) as key.
074: *
075: * @return a hastable holding all mandators with their ID (String) as key
076: */
077: public Map getMandatorsById() {
078: if (mandatorsById == null || mandatorsById.get(id) == null) {
079: // get active and inactive mandators
080: List<Mandator> mandators = CacheAdmin.getEnvironment()
081: .getMandators(true, true);
082: mandatorsById = new Hashtable<Long, Mandator>(mandators
083: .size());
084: for (Mandator mand : mandators) {
085: mandatorsById.put(mand.getId(), mand);
086: }
087: }
088: return Collections.unmodifiableMap(mandatorsById);
089: }
090:
091: /**
092: * Returns all mandators (active and inactive) in a list
093: *
094: * @return all mandators
095: */
096: public List<Mandator> getMandators() {
097: if (mandatorsById == null || mandatorsById.get(id) == null) {
098: // get active and inactive mandators
099: List<Mandator> mandators = CacheAdmin.getEnvironment()
100: .getMandators(true, true);
101: mandatorsById = new Hashtable<Long, Mandator>(mandators
102: .size());
103: for (Mandator mand : mandators) {
104: mandatorsById.put(mand.getId(), mand);
105: }
106: return mandators;
107: } else {
108: List<Mandator> mandators = new ArrayList<Mandator>(
109: mandatorsById.size());
110: Set set = mandatorsById.entrySet();
111: Iterator it = set.iterator();
112: while (it.hasNext()) {
113: Map.Entry entry = (Map.Entry) it.next();
114: mandators.add((Mandator) entry.getValue());
115: }
116: return mandators;
117: }
118: }
119:
120: public long getId() {
121: return id;
122: }
123:
124: public void setId(long id) {
125: this .id = id;
126: FxJsfUtils.setSessionAttribute(ID_CACHE_KEY, this .id);
127: }
128:
129: public boolean isActive() {
130: return active;
131: }
132:
133: public void setActive(boolean active) {
134: this .active = active;
135: }
136:
137: public Mandator getMandator() {
138: return mandator;
139: }
140:
141: public void setMandator(Mandator mandator) {
142: this .mandator = mandator;
143: }
144:
145: public String getName() {
146: return name;
147: }
148:
149: public void setName(String name) {
150: this .name = name;
151: }
152:
153: private void ensureMandatorIdSet() {
154: if (this .id <= 0) {
155: this .id = (Long) FxJsfUtils
156: .getSessionAttribute(ID_CACHE_KEY);
157: }
158: }
159:
160: /**
161: * Loads the mandator specified by the parameter id.
162: *
163: * @return the next page to render
164: */
165: public String editMandator() {
166: ensureMandatorIdSet();
167: setMandator(CacheAdmin.getEnvironment().getMandator(id));
168: setName(mandator.getName());
169: setActive(mandator.isActive());
170: return "mandatorEdit";
171:
172: }
173:
174: /**
175: * Creates a new mandator from the beans data.
176: *
177: * @return the next jsf page to render
178: */
179: public String createMandator() {
180:
181: // do not accept an empty string as name...
182: if (mandator.getName().length() < 1) {
183: new FxFacesMsgErr("Mandator.err.nameEmpty").addToContext();
184: return "mandatorCreate";
185: }
186: try {
187: setId(mandatorInterface.create(mandator.getName(), mandator
188: .isActive()));
189: setMandator(CacheAdmin.getEnvironment().getMandator(id));
190: // make sure the variable holding the mandator data will be updated by setting it to null
191: updateMandatorList();
192: new FxFacesMsgInfo("Mandator.nfo.created", mandator
193: .getName()).addToContext();
194: } catch (FxApplicationException e) {
195: new FxFacesMsgErr(e).addToContext();
196: return "mandatorCreate";
197: }
198:
199: return "mandatorOverview";
200: }
201:
202: /**
203: * Deletes a mandator, with the id specified by id.
204: *
205: * @return the next pageto render
206: */
207: public String deleteMandator() {
208:
209: ensureMandatorIdSet();
210:
211: try {
212: mandatorInterface.remove(id);
213: // make sure the variable holding the mandator data will be updated by setting it to null
214: updateMandatorList();
215: new FxFacesMsgInfo("Mandator.nfo.deleted").addToContext();
216: } catch (FxApplicationException e) {
217: new FxFacesMsgErr(e).addToContext();
218: }
219: return "mandatorOverview";
220: }
221:
222: /**
223: * Saves the edited mandator
224: *
225: * @return the next page to render
226: */
227: public String saveMandator() {
228:
229: ensureMandatorIdSet();
230:
231: try {
232: // do not accept an empty string as name...
233: if (mandator.getName().length() < 1) {
234: new FxFacesMsgErr("Mandator.err.nameEmpty")
235: .addToContext();
236: return "mandatorEdit";
237: }
238: // check if the value changed - if yes, call the corresponding method to activate or deactivate the mandator
239: if (active != mandator.isActive()) {
240: if (mandator.isActive()) {
241: mandatorInterface.activate(id);
242: } else {
243: mandatorInterface.deactivate(id);
244: }
245: }
246: if (!name.equals(mandator.getName())) {
247: mandatorInterface.changeName(id, mandator.getName());
248: }
249: // make sure the variable holding the mandator data will be updated by setting it to null
250: updateMandatorList();
251: new FxFacesMsgInfo("Mandator.nfo.updated").addToContext();
252: return "mandatorOverview";
253: } catch (FxApplicationException e) {
254: new FxFacesMsgErr(e).addToContext();
255: return "mandatorOverview";
256: }
257: }
258:
259: /**
260: * setting the mandatorsById variable to null ensures that it will be filled with the updated mandators data
261: * upon the next access
262: */
263: private void updateMandatorList() {
264: mandatorsById = null;
265: }
266: }
|