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.action;
024:
025: import java.util.Collection;
026: import java.util.List;
027: import java.util.Vector;
028:
029: import javax.servlet.http.HttpServletRequest;
030: import javax.servlet.http.HttpServletResponse;
031:
032: import org.mdarad.framework.util.ClassUtils;
033: import org.mdarad.framework.util.ClassUtilsException;
034: import org.mdarad.framework.util.struts.criteria.CriterionException;
035: import org.mdarad.framework.util.struts.criteria.FormCriterion;
036:
037: /**
038: * This class to display a search form to specify criteria
039: * that will probably be listed in a <code>ListForm</code>
040: *
041: * @author Philippe Brouillette
042: * @version 1.0
043: */
044: public abstract class SearchActionForm extends MdaradActionForm {
045:
046: /**
047: * Abstract method that must be implemented to returns
048: * the list of allowable criteria. In other words, it
049: * returns the types of criteria that can be instanciated.
050: *
051: * @return an array containing the allowable criteria
052: */
053: public abstract FormCriterion[] getAllowableCriteria();
054:
055: /**
056: * Property that keeps the concrete criteria instanciated
057: * by an instance of this form.
058: */
059: private Collection concreteCriteria;
060:
061: /**
062: * Getter for the concrete criteria
063: * @return the concrete criteria as a <code>List</code>
064: * object.
065: */
066: public List getConcreteCriteria() {
067: if (concreteCriteria == null) {
068: concreteCriteria = new Vector();
069: }
070: return (List) concreteCriteria;
071: }
072:
073: /**
074: * Set a list of criteria
075: * @param collection the list of criteria
076: */
077: public void setConcreteCriteria(List collection) {
078: concreteCriteria = collection;
079: }
080:
081: /**
082: * Property that keeps the name of the selected criterion
083: * in the page
084: */
085: private String selectedCriterion;
086:
087: /**
088: * Getter for the selected criterion
089: * @return a string with the name of the selected criterion
090: */
091: public String getSelectedCriterion() {
092: return selectedCriterion;
093: }
094:
095: /**
096: * Setter for the name of the selected criterion
097: * @param criterion the name of the selected criterion
098: */
099: public void setSelectedCriterion(String criterion) {
100: selectedCriterion = criterion;
101: }
102:
103: /**s
104: * Methods that returns the <code>FormCriterion</code> associated
105: * to the name passed as argument
106: * @param name the name of the criterion to be retrieved
107: * @return a <code>FormCriterion</code> object or <code>null</code>
108: * if the criterion doesn't exist.
109: */
110: public FormCriterion getAllowableCriterion(String name) {
111: FormCriterion[] criteria = getAllowableCriteria();
112: for (int i = 0; i < criteria.length; i++) {
113: if (criteria[i].getName().equals(name)) {
114: return criteria[i];
115: }
116: }
117: return null;
118: }
119:
120: /**
121: * Method that adds a criterion from the the list of
122: * allowable criteria to the list of concrete criteria. It
123: * dynamically instanciates a <code>FormCriterion</code> object
124: * depending on the type of the allowable criterion.
125: * @param mapName the name that identifies the allowable criterion
126: * in the map
127: */
128: public FormCriterion addCriterion(String key) {
129: FormCriterion criterion = (FormCriterion) getAllowableCriterion(key);
130: if (criterion == null) {
131: throw new CriterionException("The criterion doesn't exist");
132: }
133: Collection concreteCriteria = getConcreteCriteria();
134: // Instancie un critère concret
135:
136: Object object = null;
137: try {
138: object = ClassUtils.getInstance(criterion.getClass(),
139: new Class[] { criterion.getClass() },
140: new Object[] { criterion });
141: } catch (ClassUtilsException cue) {
142: throw new FormException(cue);
143: }
144: FormCriterion concCriterion = (FormCriterion) object;
145: addCriterion(concCriterion);
146: return concCriterion;
147: }
148:
149: /**
150: * Method that adds a criterion from the the list of
151: * allowable criteria to the list of concrete criteria. It
152: * dynamically instanciates a <code>FormCriterion</code> object
153: * depending on the type of the allowable criterion.
154: * @param mapName the name that identifies the allowable criterion
155: * in the map
156: * @param criterion Instance de critere a ajouter
157: */
158: public FormCriterion addCriterion(FormCriterion criterion) {
159: if (criterion == null) {
160: throw new CriterionException(
161: "The criterion to be added is null");
162: }
163: getConcreteCriteria().add(criterion);
164: return (FormCriterion) criterion;
165: }
166:
167: /**
168: * This method removes the concrete criterion identified by the
169: * index passed as argument.
170: * @param index the index of the criterion to be removed
171: */
172: public void removeCriterion(Integer index) {
173: Collection concreteCriteria = getConcreteCriteria();
174: ((List) concreteCriteria).remove(index.intValue());
175: }
176:
177: /**
178: * Method that initialize the search list form.
179: * @param request
180: * @param response
181: */
182: public void initialize(HttpServletRequest request,
183: HttpServletResponse response) {
184: setConcreteCriteria(new Vector());
185: }
186:
187: }
|