001: /*
002: Mdarad-Toolobox is a collection of tools for Architected RAD
003: (Rapid Application Development) based on an MDA approach.
004: The toolbox contains frameworks and generators for many environments
005: (JAVA, J2EE, Hibernate, .NET, C++, etc.) which allow to generate
006: applications from a design Model
007: Copyright (C) 2004-2005 Elapse Technologies Inc.
008:
009: This library is free software; you can redistribute it and/or
010: modify it under the terms of the GNU General Public
011: License as published by the Free Software Foundation; either
012: version 2.1 of the License, or (at your option) any later version.
013:
014: This library is distributed in the hope that it will be useful,
015: but WITHOUT ANY WARRANTY; without even the implied warranty of
016: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: General Public License for more details.
018:
019: You should have received a copy of the GNU General Public
020: License along with this library; if not, write to the Free Software
021: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
022: */
023: package org.mdarad.framework.util.struts.criteria;
024:
025: import java.util.Locale;
026:
027: import org.mdarad.framework.expr.Criterion;
028: import org.mdarad.framework.expr.OperatorTypes;
029: import org.mdarad.framework.util.ClassUtils;
030: import org.mdarad.framework.util.ClassUtilsException;
031:
032: /**
033: * This class represents a search criterion that presents a list of values to the user.
034: * To use this criterion, you need to set a {@link List List} of {@link CriterionListElement CriterionListElement}
035: * to the criterion. This list will be displayed as the selection of values and can have a multiple
036: * selection.
037: * @author Philippe Brouillette
038: * @version 1.0
039: */
040: public class MultipleListCriterion extends ListCriterion {
041:
042: /**
043: * Constructor that takes all the properties to initialize a search
044: * criterion. By default, the criterion is dynamic.
045: * @param name name of the criterion. This name must be unique
046: * as it is a key in a map.
047: * @param associatedEntity class type associated to this criterion
048: * @param property property used for the query criterion
049: * @param bundleName bundle name
050: * @param locale locale information
051: * @see FormCriterion#FormCriterion(String, CriterionProperty, String, Locale) FormCriterion
052: */
053: public MultipleListCriterion(String name, Class associatedEntity,
054: CriterionProperty property, String bundleName,
055: Locale locale, Class elementType) {
056: super (name, associatedEntity, property, bundleName, locale);
057: this .elementType = elementType;
058: }
059:
060: /**
061: * Constructor that clones a query criterion. This constructor is used
062: * to instanciate a criterion in a search form when the form
063: * contains dynamic criteria.
064: * @param criterion query criterion that must be of
065: * type <code>MultipleListCriterion</code>
066: * @see FormCriterion#FormCriterion(FormCriterion) FormCriterion
067: */
068: public MultipleListCriterion(MultipleListCriterion criterion) {
069: super (criterion);
070: setList(criterion.getList());
071: this .elementType = criterion.getElementType();
072: }
073:
074: /**
075: * Method that assigns the value of the criterion as a String. This method
076: * is needed for the user interface which uses only string values.
077: * @param value value to be assigned
078: * @see FormCriterion#setPropertyValue(java.lang.String)
079: */
080: public void setPropertyValues(String[] values) {
081:
082: if (values != null) {
083: Object[] objValues = new Object[values.length];
084: // construct the object type with the string constructor
085: for (int i = 0; i < objValues.length; i++) {
086: Class[] argTypes = new Class[] { String.class };
087: try {
088: objValues[i] = ClassUtils.getInstance(
089: getElementType(), argTypes,
090: new Object[] { values[i] });
091: } catch (ClassUtilsException cue) {
092: throw new CriterionException(cue);
093: }
094: }
095: setValues(objValues);
096: }
097: }
098:
099: /**
100: * Method that returns the values selected by the user
101: * in string format.
102: * @return array of the string values selected by the
103: * user.
104: */
105: public String[] getPropertyValues() {
106: return (String[]) getValue();
107: }
108:
109: /**
110: * Method used to keep the list of elements selected by the user
111: * @param values selected values.
112: */
113: public void setValues(Object[] values) {
114: // vérifier que les valeurs existents
115: if (values != null) {
116: setValue(values);
117: }
118: }
119:
120: /**
121: * Property that keeps the object type that is kept in
122: * the list of objects.
123: * By default, the type is <code>Object[]</code>
124: */
125: private Class objectType = Object[].class;
126:
127: /**
128: * Method that sets the object type
129: * @param type object class type
130: */
131: private void setObjectType(Class type) {
132: this .objectType = type;
133: }
134:
135: /**
136: * Returns the object class type used by the list of possible values.
137: *
138: * @see FormCriterion#getObjectType()
139: */
140: public Class getObjectType() {
141: return objectType;
142: }
143:
144: /**
145: * This method is not implemented because it should not be used.
146: * @exception UnsupportedOperationException
147: * @see @see FormCriterion#setPropertyValue(java.lang.String)
148: */
149: public void setPropertyValue(String object) {
150: throw new UnsupportedOperationException(
151: "The setPropertyValue with String argument is not supported");
152: }
153:
154: /**
155: * Method that returns the criterion from the expression
156: * framework. ({@link Criterion})
157: * <p><b>NOTE:</b> The implementation must take care of the type
158: * of operator that is used to determine the criterion to use.
159: * @return the criterion
160: */
161: public Criterion getExprCriterion() {
162: Criterion crit = new Criterion(getAssociatedEntity(),
163: getProperty().getName(), OperatorTypes.IN,
164: (Object[]) getValue());
165: return crit;
166: }
167:
168: /**
169: * Method that returns the displaying pattern.
170: *
171: * @see FormCriterion#getFormPattern()
172: */
173: public CriterionFormPattern getFormPattern() {
174: return CriterionFormPatterns.NO_OPERATOR_MULTIPLE_LIST;
175: }
176:
177: /**
178: * Property that keeps the type of each element of the list
179: */
180: private Class elementType = String.class;
181:
182: public Class getElementType() {
183: return elementType;
184: }
185:
186: public void setElementType(Class type) {
187: elementType = type;
188: }
189: }
|