KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mdarad > framework > util > struts > action > SearchActionForm


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.action;
24
25 import java.util.Collection JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Vector JavaDoc;
28
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30 import javax.servlet.http.HttpServletResponse JavaDoc;
31
32 import org.mdarad.framework.util.ClassUtils;
33 import org.mdarad.framework.util.ClassUtilsException;
34 import org.mdarad.framework.util.struts.criteria.CriterionException;
35 import org.mdarad.framework.util.struts.criteria.FormCriterion;
36
37 /**
38  * This class to display a search form to specify criteria
39  * that will probably be listed in a <code>ListForm</code>
40  *
41  * @author Philippe Brouillette
42  * @version 1.0
43  */

44 public abstract class SearchActionForm extends MdaradActionForm {
45     
46     /**
47      * Abstract method that must be implemented to returns
48      * the list of allowable criteria. In other words, it
49      * returns the types of criteria that can be instanciated.
50      *
51      * @return an array containing the allowable criteria
52      */

53     public abstract FormCriterion[] getAllowableCriteria();
54
55
56     /**
57      * Property that keeps the concrete criteria instanciated
58      * by an instance of this form.
59      */

60     private Collection JavaDoc concreteCriteria;
61
62     /**
63      * Getter for the concrete criteria
64      * @return the concrete criteria as a <code>List</code>
65      * object.
66      */

67     public List JavaDoc getConcreteCriteria() {
68         if (concreteCriteria == null) {
69             concreteCriteria = new Vector JavaDoc();
70         }
71         return (List JavaDoc) concreteCriteria;
72     }
73
74     /**
75      * Set a list of criteria
76      * @param collection the list of criteria
77      */

78     public void setConcreteCriteria(List JavaDoc collection) {
79         concreteCriteria = collection;
80     }
81
82     /**
83      * Property that keeps the name of the selected criterion
84      * in the page
85      */

86     private String JavaDoc selectedCriterion;
87
88     /**
89      * Getter for the selected criterion
90      * @return a string with the name of the selected criterion
91      */

92     public String JavaDoc getSelectedCriterion() {
93         return selectedCriterion;
94     }
95
96     /**
97      * Setter for the name of the selected criterion
98      * @param criterion the name of the selected criterion
99      */

100     public void setSelectedCriterion(String JavaDoc criterion) {
101         selectedCriterion = criterion;
102     }
103
104     /**s
105      * Methods that returns the <code>FormCriterion</code> associated
106      * to the name passed as argument
107      * @param name the name of the criterion to be retrieved
108      * @return a <code>FormCriterion</code> object or <code>null</code>
109      * if the criterion doesn't exist.
110      */

111     public FormCriterion getAllowableCriterion(String JavaDoc name) {
112         FormCriterion[] criteria = getAllowableCriteria();
113         for (int i = 0; i < criteria.length; i++) {
114             if (criteria[i].getName().equals(name)) {
115                 return criteria[i];
116             }
117         }
118         return null;
119     }
120
121     /**
122      * Method that adds a criterion from the the list of
123      * allowable criteria to the list of concrete criteria. It
124      * dynamically instanciates a <code>FormCriterion</code> object
125      * depending on the type of the allowable criterion.
126      * @param mapName the name that identifies the allowable criterion
127      * in the map
128      */

129     public FormCriterion addCriterion(String JavaDoc key) {
130         FormCriterion criterion = (FormCriterion) getAllowableCriterion(key);
131         if (criterion == null) {
132             throw new CriterionException("The criterion doesn't exist");
133         }
134         Collection JavaDoc concreteCriteria = getConcreteCriteria();
135         // Instancie un critère concret
136

137         Object JavaDoc object = null;
138         try {
139             object = ClassUtils.getInstance(
140                                     criterion.getClass(),
141                                     new Class JavaDoc[] { criterion.getClass() },
142                                     new Object JavaDoc[] { criterion });
143         } catch (ClassUtilsException cue) {
144             throw new FormException(cue);
145         }
146         FormCriterion concCriterion = (FormCriterion) object;
147         addCriterion(concCriterion);
148         return concCriterion;
149     }
150
151     /**
152      * Method that adds a criterion from the the list of
153      * allowable criteria to the list of concrete criteria. It
154      * dynamically instanciates a <code>FormCriterion</code> object
155      * depending on the type of the allowable criterion.
156      * @param mapName the name that identifies the allowable criterion
157      * in the map
158      * @param criterion Instance de critere a ajouter
159      */

160     public FormCriterion addCriterion(FormCriterion criterion) {
161         if (criterion == null) {
162             throw new CriterionException("The criterion to be added is null");
163         }
164         getConcreteCriteria().add(criterion);
165         return (FormCriterion) criterion;
166     }
167
168
169     /**
170      * This method removes the concrete criterion identified by the
171      * index passed as argument.
172      * @param index the index of the criterion to be removed
173      */

174     public void removeCriterion(Integer JavaDoc index) {
175         Collection JavaDoc concreteCriteria = getConcreteCriteria();
176         ((List JavaDoc)concreteCriteria).remove(index.intValue());
177     }
178
179
180     /**
181      * Method that initialize the search list form.
182      * @param request
183      * @param response
184      */

185     public void initialize(HttpServletRequest JavaDoc request,
186                                  HttpServletResponse JavaDoc response) {
187          setConcreteCriteria(new Vector JavaDoc());
188     }
189
190 }
191
Popular Tags