KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mdarad > framework > util > struts > criteria > FormCriterion


1 /*
2     Mdarad-Toolobox is a collection of tools for Architected RAD
3     (Rapid Application Development) based on an MDA approach.
4     The toolbox contains frameworks and generators for many environments
5     (JAVA, J2EE, Hibernate, .NET, C++, etc.) which allow to generate
6     applications from a design Model
7     Copyright (C) 2004-2005 Elapse Technologies Inc.
8
9     This library is free software; you can redistribute it and/or
10     modify it under the terms of the GNU General Public
11     License as published by the Free Software Foundation; either
12     version 2.1 of the License, or (at your option) any later version.
13
14     This library is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17     General Public License for more details.
18
19     You should have received a copy of the GNU General Public
20     License along with this library; if not, write to the Free Software
21     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */

23 package org.mdarad.framework.util.struts.criteria;
24
25 import java.io.Serializable JavaDoc;
26 import java.util.Locale JavaDoc;
27
28 import org.mdarad.framework.expr.Criterion;
29 import org.mdarad.framework.resources.ResourceMapped;
30 import org.mdarad.framework.resources.ResourcesUtils;
31
32 /**
33  * This abstract class represents a search criterion in a search interface
34  * USAGE: To use a search criterion, derive from <code>FormCriterion</code> and implement
35  * the following methods : {@link #getFormPattern() getFormPattern},
36  * {@link #getHBCriterion() getHBCriterion}, {@link #getObjectType() getObjectType} et
37  * {@link #setPropertyValue(String) setPropertyValue}
38  * @author Philippe Brouillette
39  * @version 1.0
40  */

41 public abstract class FormCriterion implements Serializable JavaDoc,
42                                                ResourceMapped {
43
44     /**
45      * Constructor that takes all the properties to initialize a search
46      * criterion. By default, the criterion is dynamic.
47      * @param name name of the criterion. This name must be unique
48      * as it is a key in a map.
49      * @param associatedEntity class type associated to this criterion
50      * @param property property used for the query criterion
51      * @param bundleName bundle name
52      * @param locale locale information
53      */

54     public FormCriterion(String JavaDoc name, Class JavaDoc associatedEntity, CriterionProperty property, String JavaDoc bundleName, Locale JavaDoc locale) {
55         this(name, associatedEntity, property, bundleName, locale, true);
56     }
57
58     /**
59      * Constructor that takes all the properties to initialize a search
60      * criterion.
61      * @param name name of the criterion. This name must be unique
62      * as it is a key in a map.
63      * @param associatedEntity class type associated to this criterion
64      * @param property property used for the query criterion
65      * @param bundleName bundle name
66      * @param locale locale information
67      * @param dynamic boolean that indicates if the criterion is dynamic
68      */

69     public FormCriterion(String JavaDoc name, Class JavaDoc associatedEntity, CriterionProperty property, String JavaDoc bundleName, Locale JavaDoc locale, boolean isDynamic) {
70         if (associatedEntity == null || property == null) {
71             throw new IllegalArgumentException JavaDoc("The associatedEntity class or " +
72                 "the property should never be null");
73         }
74         
75         this.name = name;
76         this.property = property;
77         this.associatedEntity = associatedEntity;
78         setLocale(locale);
79         setBundle(bundleName);
80         setDynamic(new Boolean JavaDoc(isDynamic));
81     }
82
83     /**
84      * Constructor that clones a query criterion. This constructor is used
85      * to instanciate a criterion in a search form when the form
86      * contains dynamic criteria.
87      * @param criterion query criterion that must be of
88      * type <code>FormCriterion</code>
89      * @exception CriterionException lancé si le critère à copier est nul.
90      */

91     public FormCriterion(FormCriterion criterion) {
92         // s'assurer que le critère n'est pas nul
93
if (criterion == null) {
94             throw new CriterionException("The criterion parameter should not be null");
95         }
96         setName(criterion.getName());
97         setProperty(criterion.getProperty());
98         setLocale(criterion.getLocale());
99         setBundle(criterion.getBundle());
100         setAssociatedEntity(criterion.getAssociatedEntity());
101     }
102
103     /**
104      * Property that keeps the property associated to the search
105      * criterion.
106      */

107     private CriterionProperty property;
108     
109     public CriterionProperty getProperty() {
110         return property;
111     }
112
113     public void setProperty(CriterionProperty property) {
114         this.property = property;
115     }
116
117     /**
118      * Value assigns to the search criterion. The type of value must be specified
119      * in the implementation of the method
120      * {@link #getObjectType() getObjectType}
121      */

122     private Object JavaDoc value;
123     
124     protected Object JavaDoc getValue() {
125         return value;
126     }
127
128     protected void setValue(Object JavaDoc object) {
129         setValue(object, getObjectType());
130     }
131
132     /**
133      * Setter of the value of the search criterion
134      * @param object value associated to the criterion
135      * @param type class type of the value object
136      */

137     protected void setValue(Object JavaDoc object, Class JavaDoc type) {
138         if (!object.getClass().isAssignableFrom(type)) {
139             throw new CriterionException("The value of the criterion is of the wrong type, it must be " + type.getName());
140         }
141         value = object;
142     }
143
144     /**
145      * Method used by the form to set the value of the object.
146      * The arg is a string because the forms cannot use <code>Object</code>
147      * @param object value of the property
148      */

149     public abstract void setPropertyValue(String JavaDoc object);
150
151
152     /**
153      * Method that returns the value associated to the search criterion.
154      * The return value is a string to be used with the forms. By default,
155      * the <code>toString()</code> method is called.
156      * @return value used by the criterion.
157      */

158     public String JavaDoc getPropertyValue() {
159         if (getValue() != null) {
160             return getValue().toString();
161         }
162         
163         return "";
164     }
165     /**
166      * Abstract method to be implemented and that is used to
167      * specifies the type of the value object of the criterion.
168      * EX: for numeric property, the typical implementation should be
169      * the following :
170      * public Class getObjectType() {
171      * return Integer.class;
172      * }
173      * @return the class type of the value object
174      */

175     public abstract Class JavaDoc getObjectType();
176
177
178     /**
179      * Property that keeps the name of the criterion. This name must
180      * be unique as it is used in a map class.
181      */

182     private String JavaDoc name;
183
184     public String JavaDoc getName() {
185         return name;
186     }
187
188     public void setName(String JavaDoc name) {
189        this.name = name;
190     }
191
192     /**
193      * Abstract method that should return the display pattern of the search
194      * criterion. The patterns are documented in the class
195      * {@link CriterionFormPatterns CriterionFormPatterns}
196      *
197      * To implement, use the <code>CriterionFormPatterns</code> class to
198      * obtain an object of type :
199      * {@link CriterionFormPattern CriterionFormPattern}
200      * @return return the display pattern
201      */

202     public abstract CriterionFormPattern getFormPattern();
203
204     /**
205      * Abstract method that returns the criterion from the expression
206      * framework. ({@link Criterion})
207      * <p><b>NOTE:</b> The implementation must take care of the type
208      * of operator that is used to determine the criterion to use.
209      * @return the criterion
210      */

211     public abstract Criterion getExprCriterion();
212     
213
214     /**
215      * Property that keeps the locale informations.
216      */

217     private Locale JavaDoc locale;
218
219
220     public Locale JavaDoc getLocale() {
221         return locale;
222     }
223
224     public void setLocale(Locale JavaDoc locale) {
225         this.locale = locale;
226     }
227     
228     /**
229      * Property that keeps the bundle name.
230      */

231     private String JavaDoc bundleName = "";
232     
233     public String JavaDoc getBundle() {
234         return bundleName;
235     }
236
237     public void setBundle(String JavaDoc bundle) {
238         this.bundleName = bundle;
239     }
240     
241     /**
242      * Method that return the label of the property associated to the
243      * search criterion.
244      *
245      * @return property label.
246      */

247     public String JavaDoc getPropertyLabel() {
248         return ResourcesUtils.getMessage(getBundle(), property.getKey(), getLocale());
249     }
250     
251     /**
252      * Property that indicates if the criterion is dynamic. By default, it is
253      * dynamic
254      */

255     private Boolean JavaDoc dynamic = Boolean.TRUE;
256     
257     public Boolean JavaDoc getDynamic() {
258         return dynamic;
259     }
260
261     public void setDynamic(Boolean JavaDoc dynamic) {
262         this.dynamic = dynamic;
263     }
264     
265     /**
266      * Attribute that keeps the object type associated to the search
267      * criterion. Ex : a criterion that search in the name property of the
268      * Product class must pass the Procuct class.
269      */

270     private Class JavaDoc associatedEntity;
271     
272     public Class JavaDoc getAssociatedEntity() {
273         return this.associatedEntity;
274     }
275
276     public void setAssociatedEntity(Class JavaDoc class1) {
277         associatedEntity = class1;
278     }
279 }
Popular Tags