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.faces.beans;
034:
035: import com.flexive.shared.*;
036: import com.flexive.shared.exceptions.FxApplicationException;
037: import com.flexive.shared.security.ACL;
038: import com.flexive.shared.security.Account;
039: import com.flexive.shared.security.Mandator;
040: import com.flexive.shared.security.UserGroup;
041: import com.flexive.shared.structure.FxAssignment;
042: import com.flexive.shared.structure.FxEnvironment;
043: import com.flexive.shared.structure.FxProperty;
044: import com.flexive.shared.structure.FxType;
045: import com.flexive.shared.workflow.StepDefinition;
046:
047: import java.io.Serializable;
048: import java.util.Date;
049: import java.util.HashMap;
050: import java.util.List;
051: import java.util.Map;
052:
053: /**
054: * Wrapper beans containing miscellaneous maps for
055: * associative lookups via JSF EL.
056: *
057: * @author Daniel Lichtenberger (daniel.lichtenberger@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
058: * @version $Rev: 197 $
059: */
060: public class MapBean implements Serializable {
061: private static final long serialVersionUID = -4851802085773447097L;
062: private Map<Long, Mandator> mandatorMap = null;
063: private Map<Long, StepDefinition> stepDefinitionMap = null;
064: private Map<Long, ACL> aclMap = null;
065: private Map<Long, UserGroup> userGroupsMap = null;
066: private Map<Long, FxType> typesMap = null;
067: private Map<String, FxType> typesByNameMap = null;
068: private Map<Long, FxProperty> propertiesMap = null;
069: private Map<String, FxProperty> propertiesByNameMap = null;
070: private Map<Long, FxAssignment> assignmentsMap = null;
071: private Map<Long, Account> accountMap = null;
072: private Map<Long, String> dateTimeMap = null;
073: private Map<Long, FxLanguage> languagesMap = null;
074: private FxEnvironment environment;
075:
076: /**
077: * Constructs a new map beans.
078: */
079: public MapBean() {
080: environment = CacheAdmin.getFilteredEnvironment();
081: }
082:
083: /**
084: * Return all active mandators.
085: *
086: * @return all active mandators.
087: */
088: public Map<Long, Mandator> getMandators() {
089: if (mandatorMap != null) {
090: return mandatorMap;
091: }
092: mandatorMap = new HashMap<Long, Mandator>();
093: return populateMap(mandatorMap, environment.getMandators(true,
094: false));
095: }
096:
097: /**
098: * Return all available step definitions.
099: *
100: * @return all available step definitions.
101: */
102: public Map<Long, StepDefinition> getStepDefinitions() {
103: if (stepDefinitionMap != null) {
104: return stepDefinitionMap;
105: }
106: stepDefinitionMap = new HashMap<Long, StepDefinition>();
107: return populateMapWithLabel(stepDefinitionMap, environment
108: .getStepDefinitions());
109: }
110:
111: /**
112: * Return all available ACLs
113: *
114: * @return all available ACLs
115: */
116: public Map<Long, ACL> getAcls() {
117: if (aclMap != null) {
118: return aclMap;
119: }
120: aclMap = new HashMap<Long, ACL>();
121: return populateMap(aclMap, environment.getACLs());
122: }
123:
124: /**
125: * Return all user groups for the current mandator.
126: *
127: * @return all user groups for the current mandator.
128: * @throws FxApplicationException if an error occured
129: */
130: public Map<Long, UserGroup> getUserGroups()
131: throws FxApplicationException {
132: if (userGroupsMap != null) {
133: return userGroupsMap;
134: }
135: userGroupsMap = new HashMap<Long, UserGroup>();
136: return populateMap(userGroupsMap, EJBLookup
137: .getUserGroupEngine().loadAll(-1).getList());
138: }
139:
140: /**
141: * Return all defined types.
142: *
143: * @return all defined types.
144: */
145: public Map<Long, FxType> getTypes() {
146: if (typesMap != null) {
147: return typesMap;
148: }
149: typesMap = new HashMap<Long, FxType>();
150: return populateMapWithLabel(typesMap, environment.getTypes(
151: true, true, true, true));
152: }
153:
154: /**
155: * Return all defined types, lookup by name (case-insensitive).
156: *
157: * @return all defined types, lookup by name (case-insensitive).
158: */
159: public Map<String, FxType> getTypesByName() {
160: if (typesByNameMap == null) {
161: typesByNameMap = FxSharedUtils
162: .getMappedFunction(new FxSharedUtils.ParameterMapper<String, FxType>() {
163: public FxType get(Object key) {
164: if (key == null) {
165: return null;
166: }
167: return environment.getType(key.toString());
168: }
169: });
170: }
171: return typesByNameMap;
172: }
173:
174: /**
175: * Return all defined properties.
176: *
177: * @return all defined properties.
178: */
179: public Map<Long, FxProperty> getProperties() {
180: if (propertiesMap == null) {
181: propertiesMap = FxSharedUtils
182: .getMappedFunction(new FxSharedUtils.ParameterMapper<Long, FxProperty>() {
183: public FxProperty get(Object key) {
184: if (key == null) {
185: return null;
186: }
187: return environment.getProperty((Long) key);
188: }
189: });
190: }
191: return propertiesMap;
192: }
193:
194: /**
195: * Return all defined properties, lookup by name (case-insensitive).
196: *
197: * @return all defined properties, lookup by name (case-insensitive).
198: */
199: public Map<String, FxProperty> getPropertiesByName() {
200: if (propertiesByNameMap == null) {
201: propertiesByNameMap = FxSharedUtils
202: .getMappedFunction(new FxSharedUtils.ParameterMapper<String, FxProperty>() {
203: public FxProperty get(Object key) {
204: if (key == null) {
205: return null;
206: }
207: return environment.getProperty(key
208: .toString());
209: }
210: });
211: }
212: return propertiesByNameMap;
213: }
214:
215: /**
216: * Return all defined assignments.
217: *
218: * @return all defined assignments.
219: */
220: public Map<Long, FxAssignment> getAssignments() {
221: if (assignmentsMap == null) {
222: assignmentsMap = FxSharedUtils
223: .getMappedFunction(new FxSharedUtils.ParameterMapper<Long, FxAssignment>() {
224: public FxAssignment get(Object key) {
225: if (key == null) {
226: return null;
227: }
228: return environment
229: .getAssignment((Long) key);
230: }
231: });
232: }
233: return assignmentsMap;
234: }
235:
236: /**
237: * Get an account by id
238: *
239: * @return map containing accounts by id
240: */
241: public Map<Long, Account> getAccount() {
242: if (accountMap == null) {
243: accountMap = FxSharedUtils
244: .getMappedFunction(new FxSharedUtils.ParameterMapper<Long, Account>() {
245: private Map<Long, Account> cache = new HashMap<Long, Account>(
246: 10);
247:
248: @SuppressWarnings({"SuspiciousMethodCalls"})
249: public Account get(Object key) {
250: if (key == null || !(key instanceof Long)) {
251: return null;
252: }
253: if (cache.containsKey(key)) {
254: return cache.get(key);
255: }
256: try {
257: cache.put((Long) key, EJBLookup
258: .getAccountEngine().load(
259: (Long) key));
260: return cache.get(key);
261: } catch (FxApplicationException e) {
262: throw e.asRuntimeException();
263: }
264: }
265: });
266: }
267: return accountMap;
268: }
269:
270: /**
271: * A date/time formatter
272: *
273: * @return date/time formatter
274: */
275: public Map<Long, String> getDateTime() {
276: if (dateTimeMap == null) {
277: dateTimeMap = FxSharedUtils
278: .getMappedFunction(new FxSharedUtils.ParameterMapper<Long, String>() {
279:
280: public String get(Object key) {
281: if (key == null || !(key instanceof Long)) {
282: return null;
283: }
284: return FxFormatUtils.getDateTimeFormat()
285: .format(new Date((Long) key));
286: }
287: });
288: }
289: return dateTimeMap;
290: }
291:
292: /**
293: * Provides access to the system languages by ID.
294: *
295: * @return the languages by ID
296: */
297: public Map<Long, FxLanguage> getLanguage() {
298: if (languagesMap == null) {
299: languagesMap = FxSharedUtils
300: .getMappedFunction(new FxSharedUtils.ParameterMapper<Long, FxLanguage>() {
301: public FxLanguage get(Object key) {
302: if (key == null) {
303: return null;
304: }
305: final long id = key instanceof Long ? (Long) key
306: : Long.valueOf(key.toString());
307: try {
308: return EJBLookup.getLanguageEngine()
309: .load(id);
310: } catch (FxApplicationException e) {
311: throw e.asRuntimeException();
312: }
313: }
314: });
315: }
316: return languagesMap;
317: }
318:
319: /**
320: * Put all objects in the given hash map.
321: *
322: * @param <T> item type parameter
323: * @param map the map to be populated
324: * @param items all items to be added to the map.
325: * @return the populated map
326: */
327: private <T extends SelectableObjectWithName> Map<Long, T> populateMap(
328: Map<Long, T> map, List<T> items) {
329: for (T item : items) {
330: map.put(item.getId(), item);
331: }
332: return map;
333: }
334:
335: /**
336: * Put all objects in the given hash map.
337: *
338: * @param <T> item type parameter
339: * @param map the map to be populated
340: * @param items all items to be added to the map.
341: * @return the populated map
342: */
343: private <T extends SelectableObjectWithLabel> Map<Long, T> populateMapWithLabel(
344: Map<Long, T> map, List<T> items) {
345: for (T item : items) {
346: map.put(item.getId(), item);
347: }
348: return map;
349: }
350:
351: }
|