001: /*
002: * ====================================================================
003: * JAFFA - Java Application Framework For All
004: *
005: * Copyright (C) 2002 JAFFA Development Group
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: * Redistribution and use of this software and associated documentation ("Software"),
022: * with or without modification, are permitted provided that the following conditions are met:
023: * 1. Redistributions of source code must retain copyright statements and notices.
024: * Redistributions must also contain a copy of this document.
025: * 2. Redistributions in binary form must reproduce the above copyright notice,
026: * this list of conditions and the following disclaimer in the documentation
027: * and/or other materials provided with the distribution.
028: * 3. The name "JAFFA" must not be used to endorse or promote products derived from
029: * this Software without prior written permission. For written permission,
030: * please contact mail to: jaffagroup@yahoo.com.
031: * 4. Products derived from this Software may not be called "JAFFA" nor may "JAFFA"
032: * appear in their names without prior written permission.
033: * 5. Due credit should be given to the JAFFA Project (http://jaffa.sourceforge.net).
034: *
035: * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: */
049:
050: package org.jaffa.components.finder;
051:
052: import java.util.*;
053: import org.apache.log4j.Logger;
054: import org.jaffa.presentation.portlet.FormBase;
055: import org.jaffa.presentation.portlet.widgets.model.DropDownModel;
056: import org.jaffa.presentation.portlet.widgets.controller.DropDownController;
057: import org.jaffa.presentation.portlet.widgets.model.RadioButtonModel;
058: import org.jaffa.presentation.portlet.widgets.controller.RadioButtonController;
059: import org.jaffa.presentation.portlet.widgets.model.GridModel;
060: import org.jaffa.presentation.portlet.widgets.controller.UserGridController;
061: import javax.servlet.http.HttpServletRequest;
062:
063: /** This is the base class for all FinderComponent FormBeans.
064: * It has the following properties to support the Criteria Screen -
065: * 1- sortDropDown : The sort criteria to use for the inquiry
066: * 2- exportType : The export option to use for the inquiry (initialized to regular Web Pages)
067: * 3- maxRecords: The maximum number of records to retrieve. This will be initialized to the first value in the option list.
068: *
069: * It has the following properties to support the Results Screen -
070: * 1- rows : This GridModel object will be used for displaying the data in the Results screen
071: * 2- moreRecordsExist : This indicates if more records will be retrieved, if the query is done by using a higher value for the maxRecords dropdown.
072: *
073: * A Finder FormBean will have to provide an implementation for the populateRows() method.
074: * @author GautamJ
075: */
076: public abstract class FinderForm extends FormBase {
077:
078: private static final Logger log = Logger
079: .getLogger(FinderForm.class);
080:
081: /** A global constant for the sortDropDown widget.*/
082: public static final String SORT_DROP_DOWN = "sortDropDown";
083: /** A global constant for the exportType widget.*/
084: public static final String EXPORT_TYPE = "exportType";
085: /** A global constant for the maxRecords widget.*/
086: public static final String MAX_RECORDS = "maxRecords";
087: /** A global constant for the rows widget.*/
088: public static final String ROWS = "rows";
089:
090: // ************************************************************
091: // Methods used by the Criteria Screen
092: // ************************************************************
093:
094: /** Getter for property sortDropDown.
095: * @return Value of property sortDropDown.
096: */
097: public String getSortDropDown() {
098: return ((FinderComponent2) getComponent()).getSortDropDown();
099: }
100:
101: /** Setter for property sortDropDown.
102: * @param sortDropDown New value of property sortDropDown.
103: */
104: public void setSortDropDown(String sortDropDown) {
105: ((FinderComponent2) getComponent())
106: .setSortDropDown(sortDropDown);
107: }
108:
109: /** Getter for DropDown property sortDropDown.
110: * @return Value of DropDown property sortDropDown.
111: */
112: public DropDownModel getSortDropDownWM() {
113: DropDownModel sortDropDownModel = (DropDownModel) getWidgetCache()
114: .getModel(SORT_DROP_DOWN);
115: if (sortDropDownModel == null) {
116: String sortDropDown = getSortDropDown();
117: if (sortDropDown != null)
118: sortDropDownModel = new DropDownModel(sortDropDown);
119: else
120: sortDropDownModel = new DropDownModel("");
121: getWidgetCache()
122: .addModel(SORT_DROP_DOWN, sortDropDownModel);
123: }
124: return sortDropDownModel;
125: }
126:
127: /** Setter for DropDown property sortDropDown. This is invoked by the servlet, when a post is done on the Criteria screen.
128: * @param value New value of DropDown property sortDropDown.
129: */
130: public void setSortDropDownWV(String value) {
131: DropDownController.updateModel(value, getSortDropDownWM());
132: }
133:
134: /** Getter for property exportType.
135: * @return Value of property exportType.
136: */
137: public String getExportType() {
138: return ((FinderComponent2) getComponent()).getExportType();
139: }
140:
141: /** Setter for property exportType.
142: * @param exportType New value of property exportType.
143: */
144: public void setExportType(String exportType) {
145: ((FinderComponent2) getComponent()).setExportType(exportType);
146: }
147:
148: /** Getter for property exportType.
149: * @return Value of property exportType.
150: */
151: public RadioButtonModel getExportTypeWM() {
152: RadioButtonModel exportTypeModel = (RadioButtonModel) getWidgetCache()
153: .getModel(EXPORT_TYPE);
154: if (exportTypeModel == null) {
155: String exportType = getExportType();
156: if (exportType != null)
157: exportTypeModel = new RadioButtonModel(exportType);
158: else
159: exportTypeModel = new RadioButtonModel(
160: FinderComponent2.EXPORT_TYPE_WEB_PAGE);
161: getWidgetCache().addModel(EXPORT_TYPE, exportTypeModel);
162: }
163: return exportTypeModel;
164: }
165:
166: /** Setter for property exportType. This is invoked by the servlet, when a post is done on the Criteria screen.
167: * @param value New value of property exportType.
168: */
169: public void setExportTypeWV(String value) {
170: RadioButtonController.updateModel(value, getExportTypeWM());
171: }
172:
173: /** Getter for property maxRecords.
174: * @return Value of property maxRecords.
175: */
176: public Integer getMaxRecords() {
177: return ((FinderComponent2) getComponent()).getMaxRecords();
178: }
179:
180: /** Setter for property maxRecords.
181: * @param maxRecords New value of property maxRecords.
182: */
183: public void setMaxRecords(Integer maxRecords) {
184: ((FinderComponent2) getComponent()).setMaxRecords(maxRecords);
185: }
186:
187: /** Getter for DropDown property maxRecords.
188: * @return Value of DropDown property maxRecords.
189: */
190: public DropDownModel getMaxRecordsWM() {
191: DropDownModel maxRecordsModel = (DropDownModel) getWidgetCache()
192: .getModel(MAX_RECORDS);
193: if (maxRecordsModel == null) {
194: // create a list of valid values (Long objects) for the maxRecords dropdown
195: List maxRecordsList = new ArrayList();
196: Map maxRecordsDropDownOptions = CriteriaDropDownOptions
197: .getMaxRecordsDropDownOptions();
198: for (Iterator i = maxRecordsDropDownOptions.entrySet()
199: .iterator(); i.hasNext();) {
200: Map.Entry me = (Map.Entry) i.next();
201: maxRecordsList.add(me.getKey());
202: }
203:
204: // Now determine an initial value for the dropdown
205: Integer maxRecords = getMaxRecords();
206: Long initialValue = maxRecords != null ? new Long(
207: maxRecords.intValue()) : null;
208: if (initialValue == null
209: || !maxRecordsList.contains(initialValue))
210: initialValue = CriteriaDropDownOptions
211: .getDefaultMaxRecordsDropDownOption();
212:
213: // Now build the DropDownModel, adding the options
214: maxRecordsModel = new DropDownModel(initialValue.toString());
215: for (Iterator i = maxRecordsDropDownOptions.entrySet()
216: .iterator(); i.hasNext();) {
217: Map.Entry me = (Map.Entry) i.next();
218: maxRecordsModel.addOption((String) me.getValue(), me
219: .getKey().toString());
220: }
221:
222: getWidgetCache().addModel(MAX_RECORDS, maxRecordsModel);
223: }
224: return maxRecordsModel;
225: }
226:
227: /** Setter for DropDown property maxRecords. This is invoked by the servlet, when a post is done on the Criteria screen.
228: * @param value New value of DropDown property maxRecords.
229: */
230: public void setMaxRecordsWV(String value) {
231: DropDownController.updateModel(value, getMaxRecordsWM());
232: }
233:
234: /** This method should be invoked to ensure a valid state of the FormBean. It will validate the data in the models and set the corresponding properties.
235: * Errors will be raised in the FormBean, if any validation fails.
236: * @param request The request stream
237: * @return A true indicates validations went through successfully. */
238: public boolean doValidate(HttpServletRequest request) {
239: String value = null;
240:
241: value = getSortDropDownWM().getValue();
242: if (value != null && value.trim().length() == 0)
243: value = null;
244: setSortDropDown(value);
245:
246: value = getExportTypeWM().getValue();
247: if (value != null && value.trim().length() == 0)
248: value = null;
249: setExportType(value);
250:
251: value = getMaxRecordsWM().getValue();
252: if (value != null && value.trim().length() == 0)
253: setMaxRecords(null);
254: else
255: setMaxRecords(Integer.valueOf(value));
256:
257: return true;
258: }
259:
260: // ************************************************************
261: // Methods used by the Results Screen
262: // ************************************************************
263:
264: /** Getter for property rows. This is invoked by the custom tag, when the jsp is rendered, to get the current value.
265: * This gets the current data from the component.
266: * @return Value of property userRef1.
267: */
268: public GridModel getRowsWM() {
269: GridModel rows = (GridModel) getWidgetCache().getModel(ROWS);
270: if (rows == null) {
271: rows = new GridModel();
272: getWidgetCache().addModel(ROWS, rows);
273: populateRows(rows);
274: }
275: return rows;
276: }
277:
278: /** Setter for property rows. This is invoked by the servlet, when a post is done on the Results screen.
279: * It sets the selected rows on the model.
280: * @param value New value of property userRef1.
281: */
282: public void setRowsWV(String value) {
283: UserGridController.updateModel(value, getRowsWM(), this );
284: }
285:
286: /** Getter for property moreRecordsExist.
287: * @return Value of property moreRecordsExist.
288: */
289: public boolean getMoreRecordsExist() {
290: FinderOutDto finderOutDto = ((FinderComponent2) getComponent())
291: .getFinderOutDto();
292: if (finderOutDto != null
293: && finderOutDto.getMoreRecordsExist() != null)
294: return finderOutDto.getMoreRecordsExist().booleanValue();
295: else
296: return false;
297: }
298:
299: /** Getter for property numberOfRecords.
300: * @return Value of property numberOfRecords.
301: */
302: public Long getNumberOfRecords() {
303: GridModel rows = getRowsWM();
304: return rows != null && rows.getRows() != null ? new Long(rows
305: .getRows().size()) : new Long(0);
306: }
307:
308: /** The FormBean should provide an implementation for this method.
309: * This will populate the input GridModel with the data in the finderOutDto of the Component.
310: * @param rows The GridModel object to populate.
311: */
312: public abstract void populateRows(GridModel rows);
313: }
|