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.value;
034:
035: import com.flexive.shared.CacheAdmin;
036: import com.flexive.shared.FxSharedUtils;
037: import com.flexive.shared.exceptions.FxInvalidParameterException;
038: import com.flexive.shared.structure.FxSelectList;
039: import com.flexive.shared.structure.FxSelectListItem;
040: import org.apache.commons.lang.StringUtils;
041:
042: import java.io.Serializable;
043: import java.util.ArrayList;
044: import java.util.List;
045: import java.util.Map;
046: import java.util.Scanner;
047:
048: /**
049: * FxValue implementation for FxSelectList items with many selectable items
050: *
051: * @author Markus Plesser (markus.plesser@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
052: */
053: public class FxSelectMany extends FxValue<SelectMany, FxSelectMany>
054: implements Serializable {
055:
056: private static final long serialVersionUID = -5974340863508061455L;
057: private FxSelectList list = null;
058:
059: /**
060: * Constructor
061: *
062: * @param multiLanguage multilanguage value?
063: * @param defaultLanguage the default language
064: * @param translations HashMap containing language->translation mapping
065: */
066: public FxSelectMany(boolean multiLanguage, long defaultLanguage,
067: Map<Long, SelectMany> translations) {
068: super (multiLanguage, defaultLanguage, translations);
069: checkForEmptyTranslations(translations);
070: }
071:
072: /**
073: * Constructor
074: *
075: * @param defaultLanguage the default language
076: * @param translations HashMap containing language->translation mapping
077: */
078: public FxSelectMany(long defaultLanguage,
079: Map<Long, SelectMany> translations) {
080: super (defaultLanguage, translations);
081: checkForEmptyTranslations(translations);
082: }
083:
084: /**
085: * Constructor
086: *
087: * @param multiLanguage multilanguage value?
088: * @param translations HashMap containing language->translation mapping
089: */
090: public FxSelectMany(boolean multiLanguage,
091: Map<Long, SelectMany> translations) {
092: super (multiLanguage, translations);
093: checkForEmptyTranslations(translations);
094: }
095:
096: /**
097: * Constructor
098: *
099: * @param translations HashMap containing language->translation mapping
100: */
101: public FxSelectMany(Map<Long, SelectMany> translations) {
102: super (translations);
103: checkForEmptyTranslations(translations);
104: }
105:
106: /**
107: * Constructor - create value from an array of translations
108: *
109: * @param translations HashMap containing language->translation mapping
110: * @param pos position (index) in the array to use
111: */
112: public FxSelectMany(Map<Long, SelectMany[]> translations, int pos) {
113: super (translations, pos);
114: FxSharedUtils.checkParameterEmpty(translations, "translations");
115: for (SelectMany[] translation : translations.values()) {
116: FxSharedUtils.checkParameterEmpty(translation[pos],
117: "translation");
118: }
119: }
120:
121: /**
122: * Constructor
123: *
124: * @param multiLanguage multilanguage value?
125: * @param defaultLanguage the default language
126: * @param value single initializing value
127: */
128: public FxSelectMany(boolean multiLanguage, long defaultLanguage,
129: SelectMany value) {
130: super (multiLanguage, defaultLanguage, value);
131: FxSharedUtils.checkParameterEmpty(value, "value");
132: this .list = value.getSelectList();
133: }
134:
135: /**
136: * Constructor
137: *
138: * @param defaultLanguage the default language
139: * @param value single initializing value
140: */
141: public FxSelectMany(long defaultLanguage, SelectMany value) {
142: super (defaultLanguage, value);
143: FxSharedUtils.checkParameterEmpty(value, "value");
144: }
145:
146: /**
147: * Constructor
148: *
149: * @param multiLanguage multilanguage value?
150: * @param value single initializing value
151: */
152: public FxSelectMany(boolean multiLanguage, SelectMany value) {
153: super (multiLanguage, value);
154: FxSharedUtils.checkParameterEmpty(value, "value");
155: }
156:
157: /**
158: * Constructor
159: *
160: * @param value single initializing value
161: */
162: public FxSelectMany(SelectMany value) {
163: super (value);
164: FxSharedUtils.checkParameterEmpty(value, "value");
165: }
166:
167: /**
168: * Constructor
169: *
170: * @param clone original FxValue to be cloned
171: */
172: public FxSelectMany(FxValue<SelectMany, FxSelectMany> clone) {
173: super (clone);
174: }
175:
176: /**
177: * {@inheritDoc}
178: */
179: @Override
180: public Class<SelectMany> getValueClass() {
181: return SelectMany.class;
182: }
183:
184: /** {@inheritDoc} */
185: @Override
186: public String getStringValue(SelectMany value) {
187: StringBuilder out = new StringBuilder();
188: for (FxSelectListItem item : value.getSelected()) {
189: out.append(out.length() > 0 ? "," : "")
190: .append(item.getId());
191: }
192: return out.toString();
193: }
194:
195: /**
196: * Evaluates the given string value to an object of type SelectMany.
197: *
198: * @param value comma seperated list of selected entries
199: * @return the value interpreted as SelectMany
200: */
201: @Override
202: public SelectMany fromString(String value) {
203: if (StringUtils.isEmpty(value))
204: throw new FxInvalidParameterException("value",
205: "ex.content.value.invalid.list")
206: .asRuntimeException();
207: List<FxSelectListItem> items = new ArrayList<FxSelectListItem>(
208: 10);
209: Scanner sc = new Scanner(value).useDelimiter(",");
210: while (sc.hasNextLong()) {
211: items.add(CacheAdmin.getEnvironment().getSelectListItem(
212: sc.nextLong()));
213: }
214: if (items.size() == 0)
215: throw new FxInvalidParameterException("value",
216: "ex.content.value.invalid.list")
217: .asRuntimeException();
218: SelectMany sel = new SelectMany(items.get(0).getList());
219: for (FxSelectListItem item : items)
220: sel.selectItem(item);
221: return sel;
222: }
223:
224: /**
225: * {@inheritDoc}
226: */
227: @Override
228: public FxSelectMany copy() {
229: return new FxSelectMany(this );
230: }
231:
232: /**
233: * Return true if T is immutable (e.g. java.lang.String). This prevents cloning
234: * of the translations in copy constructors.
235: *
236: * @return true if T is immutable (e.g. java.lang.String)
237: */
238: @Override
239: public boolean isImmutableValueType() {
240: return true;
241: }
242:
243: /**
244: * Get the SelectList for this SelectMany
245: *
246: * @return SelectList
247: */
248: public FxSelectList getSelectList() {
249: if (list == null) {
250: if (this .isMultiLanguage()) {
251: list = this .getBestTranslation().getSelectList();
252: } else
253: list = this .singleValue.getSelectList();
254: }
255: return list;
256: }
257:
258: /** {@inheritDoc} */
259: @Override
260: protected boolean isAcceptsEmptyDefaultTranslations() {
261: return false;
262: }
263:
264: private void checkForEmptyTranslations(
265: Map<Long, SelectMany> translations) {
266: FxSharedUtils.checkParameterEmpty(translations, "translations");
267: for (SelectMany translation : translations.values()) {
268: FxSharedUtils.checkParameterEmpty(translation,
269: "translation");
270: }
271: }
272:
273: }
|