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.XPathElement;
036: import com.flexive.shared.exceptions.FxApplicationException;
037: import com.flexive.shared.exceptions.FxInvalidParameterException;
038: import com.flexive.shared.exceptions.FxNotFoundException;
039: import com.flexive.shared.value.FxString;
040: import org.apache.commons.lang.StringUtils;
041:
042: import java.util.List;
043:
044: /**
045: * FxGroupAssignment for editing
046: *
047: * @author Markus Plesser (markus.plesser@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
048: */
049: public class FxGroupAssignmentEdit extends FxGroupAssignment {
050:
051: private static final long serialVersionUID = 5823011785222672556L;
052: private boolean isNew;
053:
054: /**
055: * Ctor to make a FxGroupAssignment editable
056: *
057: * @param ga FxGroupAssignment to make editable
058: */
059: public FxGroupAssignmentEdit(FxGroupAssignment ga) {
060: super (ga.getId(), ga.isEnabled(), ga.getAssignedType(), ga
061: .getAlias(), ga.getXPath(), ga.getPosition(),
062: new FxMultiplicity(ga.getMultiplicity()), ga
063: .getDefaultMultiplicity(), ga
064: .getParentGroupAssignment(), ga
065: .getBaseAssignmentId(), ga.getLabel().copy(),
066: ga.getHint().copy(), ga.getGroup().asEditable(), ga
067: .getMode(), FxStructureOption
068: .cloneOptions(ga.options));
069: this .isNew = false;
070: }
071:
072: /**
073: * Constructor to create a new FxGroupAssignmentEdit from an existing FxGroupAssignment as a new one for a given type with a
074: * new alias and a given parentXPath
075: *
076: * @param ga original group assignment
077: * @param type type to assign it
078: * @param alias new alias
079: * @param parentXPath parent XPath within the type to assign
080: * @param parent optional parent assignment if already known (prevents lookup of parentXPath if valid)
081: * @throws com.flexive.shared.exceptions.FxNotFoundException
082: * if parentXPath is invalid
083: * @throws com.flexive.shared.exceptions.FxInvalidParameterException
084: * if parentXPath is invalid
085: */
086: private FxGroupAssignmentEdit(FxGroupAssignment ga, FxType type,
087: String alias, String parentXPath, FxAssignment parent)
088: throws FxNotFoundException, FxInvalidParameterException {
089: super (-1, ga.isEnabled(), type, alias, XPathElement.buildXPath(
090: false, parentXPath, alias), ga.getPosition(),
091: new FxMultiplicity(ga.getMultiplicity()), ga
092: .getDefaultMultiplicity(), ga
093: .getParentGroupAssignment(), ga.getId(), ga
094: .getLabel().copy(), ga.getHint().copy(), ga
095: .getGroup(), ga.getMode(), FxStructureOption
096: .cloneOptions(ga.options));
097: if (parent == null && !"/".equals(parentXPath)) {
098: //check parentXPath unless its top level (root)
099: parent = type.getAssignment(parentXPath);
100: if (parent != null
101: && parent instanceof FxPropertyAssignment)
102: throw new FxInvalidParameterException("parentXPath",
103: "ex.structure.assignment.noGroup", parentXPath);
104: }
105: if (alias == null)
106: setXPath("/"); //got to be root
107: if (parent == null)
108: parentGroupAssignment = null;
109: else
110: parentGroupAssignment = (FxGroupAssignment) parent;
111: isNew = true;
112: }
113:
114: /**
115: * Is this a new instance?
116: *
117: * @return new instance?
118: */
119: public boolean isNew() {
120: return isNew;
121: }
122:
123: /**
124: * Set the enabled flag
125: *
126: * @param enabled enabled flag
127: * @return this
128: */
129: public FxGroupAssignmentEdit setEnabled(boolean enabled) {
130: this .enabled = enabled;
131: return this ;
132: }
133:
134: /**
135: * Set the multiplicity of this assignment. May only be set if the group this assignment belongs to allows
136: * overriding the multiplicity.
137: *
138: * @param multiplicity new multiplicity
139: * @return this
140: * @throws FxInvalidParameterException on errors
141: */
142: public FxGroupAssignmentEdit setMultiplicity(
143: FxMultiplicity multiplicity)
144: throws FxInvalidParameterException {
145: if (!getGroup().mayOverrideBaseMultiplicity())
146: throw new FxInvalidParameterException("MULTIPLICITY",
147: "ex.structure.override.group.forbidden",
148: "Multiplicity", getGroup().getName());
149: this .multiplicity = multiplicity;
150: return this ;
151: }
152:
153: /**
154: * Set the default multiplicity (the number of elements created upon initialization)
155: *
156: * @param defaultMultiplicity the default multiplicity
157: * @return this
158: */
159: public FxGroupAssignmentEdit setDefaultMultiplicity(
160: int defaultMultiplicity) {
161: if (this .getMultiplicity().isValid(defaultMultiplicity)) {
162: this .defaultMultiplicity = defaultMultiplicity;
163: return this ;
164: }
165: if (defaultMultiplicity < this .getMultiplicity().getMin())
166: this .defaultMultiplicity = this .getMultiplicity().getMin();
167: if (defaultMultiplicity > this .getMultiplicity().getMax())
168: this .defaultMultiplicity = this .getMultiplicity().getMax();
169: return this ;
170: }
171:
172: /**
173: * Set the new absolute position
174: *
175: * @param position new absolute position
176: * @return this
177: */
178: public FxGroupAssignmentEdit setPosition(int position) {
179: this .position = position;
180: return this ;
181: }
182:
183: /**
184: * Set a new alias, will affect the xpath as well
185: *
186: * @param alias new alias
187: * @return this
188: * @throws FxInvalidParameterException on errors
189: */
190: public FxGroupAssignmentEdit setAlias(String alias)
191: throws FxInvalidParameterException {
192: if (StringUtils.isEmpty(alias))
193: throw new FxInvalidParameterException("ALIAS",
194: "ex.structure.assignment.noAlias");
195: this .alias = alias.trim().toUpperCase();
196: List<XPathElement> xpe = XPathElement.split(this .XPath);
197: xpe.set(xpe.size() - 1, new XPathElement(this .alias, 1, true));
198: this .XPath = getAssignedType().getName()
199: + XPathElement.toXPathNoMult(xpe);
200: return this ;
201: }
202:
203: /**
204: * Set the XPath, will affect the alias as well
205: *
206: * @param XPath new XPath
207: * @return this
208: * @throws FxInvalidParameterException on errors
209: */
210: public FxGroupAssignmentEdit setXPath(String XPath)
211: throws FxInvalidParameterException {
212: if (StringUtils.isEmpty(XPath)
213: || !XPathElement.isValidXPath(XPath))
214: throw new FxInvalidParameterException("XPATH",
215: "ex.structure.assignment.noXPath");
216: this .XPath = XPath.trim().toUpperCase();
217: //synchronize back the alias unless we're the virtual root group
218: if (!"/".equals(this .XPath))
219: this .alias = XPathElement.lastElement(this .XPath)
220: .getAlias();
221: return this ;
222: }
223:
224: /**
225: * Set the label
226: *
227: * @param label label
228: * @return this
229: */
230: public FxGroupAssignmentEdit setLabel(FxString label) {
231: this .label = label;
232: return this ;
233: }
234:
235: /**
236: * Set the hint message
237: *
238: * @param hint hint message
239: * @return this
240: */
241: public FxGroupAssignmentEdit setHint(FxString hint) {
242: this .hint = hint;
243: return this ;
244: }
245:
246: /**
247: * Set the GroupMode for this group assignment (any-of, one-of)
248: *
249: * @param mode group mode
250: * @return this
251: */
252: public FxGroupAssignmentEdit setMode(GroupMode mode) {
253: this .mode = mode;
254: return this ;
255: }
256:
257: /**
258: * Create a new FxGroupAssignmentEdit from an existing FxGroupAssignment as a new one for a given type with a
259: * new alias and a given parentXPath
260: *
261: * @param ga original group assignment
262: * @param type type to assign it
263: * @param alias new alias
264: * @param parentXPath parent XPath within the type to assign
265: * @param parent optional parent assignment if already known
266: * @return new FxGroupAssignmentEdit
267: * @throws com.flexive.shared.exceptions.FxNotFoundException
268: * if parentXPath is invalid
269: * @throws com.flexive.shared.exceptions.FxInvalidParameterException
270: * if parentXPath is invalid
271: */
272: public static FxGroupAssignmentEdit createNew(FxGroupAssignment ga,
273: FxType type, String alias, String parentXPath,
274: FxAssignment parent) throws FxNotFoundException,
275: FxInvalidParameterException {
276: return new FxGroupAssignmentEdit(ga, type, alias, parentXPath,
277: parent);
278: }
279:
280: /**
281: * Create a new FxGroupAssignmentEdit from an existing FxGroupAssignment as a new one for a given type with a
282: * new alias and a given parentXPath
283: *
284: * @param ga original group assignment
285: * @param type type to assign it
286: * @param alias new alias
287: * @param parentXPath parent XPath within the type to assign
288: * @return new FxGroupAssignmentEdit
289: * @throws com.flexive.shared.exceptions.FxNotFoundException
290: * if parentXPath is invalid
291: * @throws com.flexive.shared.exceptions.FxInvalidParameterException
292: * if parentXPath is invalid
293: */
294: public static FxGroupAssignmentEdit createNew(FxGroupAssignment ga,
295: FxType type, String alias, String parentXPath)
296: throws FxNotFoundException, FxInvalidParameterException {
297: return new FxGroupAssignmentEdit(ga, type, alias, parentXPath,
298: null);
299: }
300:
301: /**
302: * Sets the parent group assignment.
303: *
304: * @param parent the parent group assignment.
305: */
306: public void setParentGroupAssignment(FxGroupAssignment parent) {
307: this .parentGroupAssignment = parent;
308: }
309:
310: /**
311: * Get a (unmodifiable) list of all options set for this group assignment
312: *
313: * @return (unmodifiable) list of all options set for this group assignment
314: */
315: public List<FxStructureOption> getOptions() {
316: return FxStructureOption.getUnmodifieableOptions(options);
317: }
318:
319: /**
320: * Set an option
321: *
322: * @param key option key
323: * @param value value of the option
324: * @return the assignment itself, useful for chained calls
325: * @throws FxInvalidParameterException if the property does not allow overriding
326: */
327: public FxGroupAssignmentEdit setOption(String key, String value)
328: throws FxInvalidParameterException {
329:
330: FxStructureOption gOpt = getGroup().getOption(key);
331: if (gOpt.isSet() && !gOpt.isOverrideable())
332: throw new FxInvalidParameterException(key,
333: "ex.structure.override.forbidden", key, getGroup()
334: .getName());
335:
336: FxStructureOption.setOption(options, key, true, value);
337: return this ;
338: }
339:
340: /**
341: * Set a boolean option
342: *
343: * @param key option key
344: * @param value value of the option
345: * @return the assignment itself, useful for chained calls
346: * @throws FxInvalidParameterException if the property does not allow overriding
347: */
348: public FxGroupAssignmentEdit setOption(String key, boolean value)
349: throws FxInvalidParameterException {
350:
351: FxStructureOption gOpt = getGroup().getOption(key);
352: if (gOpt.isSet() && !gOpt.isOverrideable())
353: throw new FxInvalidParameterException(key,
354: "ex.structure.override.forbidden", key, getGroup()
355: .getName());
356:
357: FxStructureOption.setOption(options, key, true, value);
358: return this ;
359: }
360:
361: /**
362: * Clear an option entry
363: *
364: * @param key option name
365: */
366: public void clearOption(String key) {
367: FxStructureOption.clearOption(options, key);
368: }
369:
370: /**
371: * Returns the group of this assignment as editable.
372: *
373: * @return the editable group object.
374: */
375: public FxGroupEdit getGroupEdit() {
376: if (!(group instanceof FxGroupEdit))
377: throw new FxApplicationException(
378: "ex.structure.noEditableGroup")
379: .asRuntimeException();
380: else
381: return (FxGroupEdit) group;
382: }
383:
384: }
|