001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2008
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.shared.structure;
034:
035: import com.flexive.shared.ObjectWithLabel;
036: import com.flexive.shared.SelectableObjectWithName;
037: import com.flexive.shared.SelectableObjectWithLabel;
038: import com.flexive.shared.exceptions.FxInvalidParameterException;
039: import com.flexive.shared.exceptions.FxNotFoundException;
040: import com.flexive.shared.security.ACL;
041: import com.flexive.shared.value.FxString;
042:
043: import java.io.Serializable;
044: import java.util.ArrayList;
045: import java.util.HashMap;
046: import java.util.List;
047: import java.util.Map;
048:
049: /**
050: * Select list
051: *
052: * @author Markus Plesser (markus.plesser@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
053: */
054: public class FxSelectList implements Serializable, ObjectWithLabel {
055: private static final long serialVersionUID = 7886154346159624577L;
056:
057: /**
058: * A select list containing all countries and their codes.
059: */
060: public static final String COUNTRIES = "COUNTRIES";
061:
062: protected long id;
063: protected FxSelectList parentList;
064: protected long parentListId;
065: protected String name;
066: protected FxString label;
067: protected FxString description;
068: protected boolean allowDynamicItemCreation;
069: protected ACL createItemACL;
070: protected ACL newItemACL;
071: protected Map<Long, FxSelectListItem> items;
072: protected FxSelectListItem defaultItem;
073: protected long defaultItemId;
074:
075: /**
076: * Internal(!) Constructor to be used while loading from storage
077: *
078: * @param id internal id
079: * @param parentListId parent list if >= 0
080: * @param name name (unique)
081: * @param label label
082: * @param description description
083: * @param allowDynamicItemCreation is dynamic item creation allowed?
084: * @param createItemACL ACL whose create flag is checked against for creating new list items
085: * @param newItemACL ACL assigned to new items
086: * @param defaultItemId the id of the default selected item, optional, can be <code>null</code>
087: */
088: public FxSelectList(long id, long parentListId, String name,
089: FxString label, FxString description,
090: boolean allowDynamicItemCreation, ACL createItemACL,
091: ACL newItemACL, long defaultItemId) {
092: this .id = id;
093: this .parentListId = parentListId;
094: this .parentList = null;
095: this .name = name;
096: this .label = label;
097: this .description = description;
098: this .allowDynamicItemCreation = allowDynamicItemCreation;
099: this .createItemACL = createItemACL;
100: this .newItemACL = newItemACL;
101: this .defaultItemId = defaultItemId;
102: this .defaultItem = null;
103: this .items = new HashMap<Long, FxSelectListItem>(10);
104: }
105:
106: /**
107: * Create a new in-memory select list. Not suitable for creating select lists
108: * that will be stored in the database!
109: *
110: * @param name the select list name
111: */
112: public FxSelectList(String name) {
113: this (-1, -1, name, new FxString(name), new FxString(name),
114: true, null, null, 0);
115: }
116:
117: /**
118: * Internal method to synchronize/load parent lists and items
119: *
120: * @param env environment
121: */
122: public void _synchronize(FxEnvironment env) {
123: if (this .defaultItemId > 0) {
124: this .defaultItem = env.getSelectListItem(defaultItemId);
125: }
126: try {
127: if (this .parentListId >= 0) {
128: this .parentList = env.getSelectList(this .parentListId);
129: } else
130: return;
131: } catch (Exception e) {
132: this .parentListId = -1;
133: return;
134: }
135: for (FxSelectListItem item : this .getItems())
136: item._synchronize(env);
137: }
138:
139: /**
140: * Get the internal id
141: *
142: * @return internal id
143: */
144: public long getId() {
145: return id;
146: }
147:
148: /**
149: * Get the parent list
150: *
151: * @return parent list
152: */
153: public FxSelectList getParentList() {
154: return parentList;
155: }
156:
157: /**
158: * Is a parent list set for this list?
159: *
160: * @return if a parent list is set for this list
161: */
162: public boolean hasParentList() {
163: return parentList != null;
164: }
165:
166: /**
167: * Get the (optional!) default item for this list
168: *
169: * @return the (optional!) default item for this list, can be <code>null</code>
170: */
171: public FxSelectListItem getDefaultItem() {
172: return defaultItem;
173: }
174:
175: /**
176: * Is a default item set for this list?
177: *
178: * @return if a default item is set for this list
179: */
180: public boolean hasDefaultItem() {
181: return defaultItem != null;
182: }
183:
184: /**
185: * Get the name
186: *
187: * @return name
188: */
189: public String getName() {
190: return name;
191: }
192:
193: /**
194: * Get the label
195: *
196: * @return label
197: */
198: public FxString getLabel() {
199: return label;
200: }
201:
202: /**
203: * Get the description
204: *
205: * @return description
206: */
207: public FxString getDescription() {
208: return description;
209: }
210:
211: /**
212: * May items be created dynamically? (in UI's other than backends!)
213: *
214: * @return if items may be created dynamically
215: */
216: public boolean isAllowDynamicItemCreation() {
217: return allowDynamicItemCreation;
218: }
219:
220: /**
221: * Get the ACL used to check if a user is allowed to create new items for this list dynamically
222: *
223: * @return ACL used to check if a user is allowed to create new items for this list dynamically
224: */
225: public ACL getCreateItemACL() {
226: return createItemACL;
227: }
228:
229: /**
230: * Get the default ACL assigned to new created items
231: *
232: * @return default ACL assigned to new created items
233: */
234: public ACL getNewItemACL() {
235: return newItemACL;
236: }
237:
238: /**
239: * Get the selectlist item with the given id
240: *
241: * @param id requested select list item id
242: * @return select list item
243: */
244: public FxSelectListItem getItem(long id) {
245: if (this .containsItem(id))
246: return this .items.get(id);
247: throw new FxNotFoundException(
248: "ex.structure.list.item.notFound", id)
249: .asRuntimeException();
250: }
251:
252: /**
253: * Get the (first) selectlist item with the given data
254: *
255: * @param data requested select list item data
256: * @return select list item
257: */
258: public FxSelectListItem getItemByData(String data) {
259: for (FxSelectListItem item : items.values())
260: if (item.getData().equals(data))
261: return item;
262: throw new FxNotFoundException(
263: "ex.structure.list.item.notFound.data", data)
264: .asRuntimeException();
265: }
266:
267: /**
268: * Getter for the map containing items. Used by FxSelectListItem to "add" itself to a list during construction.
269: *
270: * @return map of all items of this list
271: */
272: protected final Map<Long, FxSelectListItem> getItemMap() {
273: return items;
274: }
275:
276: public final List<FxSelectListItem> getItems() {
277: return new ArrayList<FxSelectListItem>(items.values());
278: }
279:
280: /**
281: * Get this FxSelectList as editable
282: *
283: * @return FxSelectListEdit
284: */
285: public FxSelectListEdit asEditable() {
286: return new FxSelectListEdit(this );
287: }
288:
289: /**
290: * Check if this list contains an selectlist item with the requested id
291: *
292: * @param id requested item id
293: * @return if the requested item is contained in this list
294: */
295: public boolean containsItem(long id) {
296: return this .items.containsKey(id);
297: }
298:
299: /**
300: * Factory method to create a select list based on a collection of
301: * {@link SelectableObjectWithLabel} objects. This select list cannot be persisted
302: * to the DB, but may used e.g. for UI input components.
303: *
304: * @param name name of the select list to be created
305: * @param items selectable objects with label
306: * @return a new select list
307: */
308: public static FxSelectList createList(String name,
309: List<? extends SelectableObjectWithLabel> items) {
310: FxSelectListEdit list = new FxSelectList(name).asEditable();
311: list.addAll(items);
312: return list;
313: }
314:
315: /**
316: * Factory method to create a select list based on a collection of
317: * {@link com.flexive.shared.SelectableObjectWithName} objects. This select list cannot be persisted
318: * to the DB, but may used e.g. for UI input components.
319: *
320: * @param name name of the select list to be created
321: * @param items selectable objects
322: * @return a new select list
323: */
324: public static FxSelectList createListWithName(String name,
325: List<? extends SelectableObjectWithName> items) {
326: FxSelectListEdit list = new FxSelectList(name).asEditable();
327: list.addAllWithName(items);
328: return list;
329: }
330:
331: /**
332: * Set a new default item, setting <code>null</code> will clear the default item
333: * To be used internally from FxSelectListEdit only(!)
334: *
335: * @param defaultItem a new default item, setting <code>null</code> will clear the default item
336: */
337: protected void _setDefaultItem(FxSelectListItem defaultItem) {
338: if (defaultItem == null) {
339: this .defaultItem = null;
340: this .defaultItemId = 0;
341: return;
342: }
343: if (defaultItem.getList().getId() != this .getId())
344: throw new FxInvalidParameterException("defaultItem",
345: "ex.structure.list.item.invalidDefaultItem", this
346: .getId(), this.defaultItem.getId())
347: .asRuntimeException();
348: this.defaultItem = defaultItem;
349: }
350: }
|