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.lookup;
051:
052: import java.util.*;
053: import javax.servlet.http.HttpServletRequest;
054: import org.apache.log4j.Logger;
055: import org.jaffa.datatypes.Formatter;
056: import org.jaffa.components.finder.FinderComponent2;
057: import org.jaffa.presentation.portlet.FormKey;
058: import org.jaffa.presentation.portlet.widgets.model.IModelMap;
059:
060: /** This is the base class for all Lookup components.
061: * It inherits the properties from FinderComponent.
062: * Additionally it has the following properties -
063: * 1- targetFields : This will have the target-fields to be set based on the selected values from the Lookup.
064: * 2- multiSelectLookupListeners : This will maintain the listeners to be notified when row(s) are selected in the Lookup.
065: * It has a convenience method, which should be called by the component's Action class to forward to the generic lookup jsp.
066: * @author GautamJ
067: */
068: public abstract class LookupComponent2 extends FinderComponent2 {
069:
070: private static final Logger log = Logger
071: .getLogger(LookupComponent2.class);
072:
073: /** A constant used to pass the selected values to the Http Request stream.*/
074: public static final String LOOKUP_ATTRIBUTE = "org.jaffa.components.lookup.LookupComponent.Lookup";
075:
076: /** A constant used to store the CheckBoxModel for each row in the GridModel in the FormBean.*/
077: public static final String MULTI_SELECT_CHECKBOX = "LookupComponent.MultiSelectCheckBox";
078:
079: private static final String LOOKUP_JSP_NAME = "jaffa_lookup";
080: private static final FormKey LOOKUP_FORM_KEY = new FormKey(
081: LOOKUP_JSP_NAME, null);
082:
083: /** Holds value of property targetFields. */
084: private String m_targetFields;
085:
086: private Collection m_multiSelectLookupListeners;
087:
088: // **************************************************************
089: // Component Properties
090: // **************************************************************
091:
092: /** Getter for property targetFields.
093: * @return Value of property targetFields.
094: */
095: public String getTargetFields() {
096: return m_targetFields;
097: }
098:
099: /** Setter for property targetFields.
100: * @param targetFields New value of property targetFields.
101: */
102: public void setTargetFields(String targetFields) {
103: m_targetFields = targetFields;
104: }
105:
106: /** Adds a listener.
107: * @param listener the listener.
108: */
109: public void addMultiSelectLookupListener(
110: IMultiSelectLookupListener listener) {
111: if (m_multiSelectLookupListeners == null)
112: m_multiSelectLookupListeners = new HashSet();
113: m_multiSelectLookupListeners.add(listener);
114: }
115:
116: /** Removes a listener.
117: * @param listener the listener.
118: * @return true if the listener was removed.
119: */
120: public boolean removeMultiSelectLookupListener(
121: IMultiSelectLookupListener listener) {
122: if (m_multiSelectLookupListeners != null)
123: return m_multiSelectLookupListeners.remove(listener);
124: else
125: return false;
126: }
127:
128: /** Returns a true if a MultiSelectLookupListener has been registered with this Lookup.
129: * The associated JSP will use this method to determine if multi-select capability is to be presented to the user.
130: * @return true if a MultiSelectLookupListener has been registered with this Lookup.
131: */
132: public boolean isInMultiSelectLookupMode() {
133: return m_multiSelectLookupListeners != null
134: && m_multiSelectLookupListeners.size() > 0;
135: }
136:
137: // **************************************************************
138: // Additional methods
139: // **************************************************************
140:
141: /** This returns a null, since the Lookup components are not expected to serve up Excel content.
142: * @return null.
143: */
144: protected String getExcelFormName() {
145: return null;
146: }
147:
148: /** This returns a null, since the Lookup components are not expected to serve up XML content.
149: * @return null.
150: */
151: protected String getXmlFormName() {
152: return null;
153: }
154:
155: /** This clears the internal collection of listeners.
156: * It then invokes the quit() method of the base class.
157: */
158: public void quit() {
159: if (m_multiSelectLookupListeners != null) {
160: m_multiSelectLookupListeners.clear();
161: m_multiSelectLookupListeners = null;
162: }
163: super .quit();
164: }
165:
166: /** This will remove the 'lookup' attribute from the request stream.
167: * It will then invoke the quitAndReturnToCallingScreen() method, if MultiSelectLookupListener was registered.
168: * Otherwise it will quit() and return a FormKey object for the generic lookup jsp, which will close the browser.
169: * @param request The HTTP request we are processing
170: * @return a FormKey object.
171: */
172: public FormKey quitLookup(HttpServletRequest request) {
173: request.removeAttribute(LOOKUP_ATTRIBUTE);
174:
175: if (isInMultiSelectLookupMode()) {
176: return quitAndReturnToCallingScreen();
177: } else {
178: // Quit this component
179: quit();
180:
181: // forward to the jsp
182: return LOOKUP_FORM_KEY;
183: }
184: }
185:
186: /** Invokes the rowsSelected() method of the registered IMultiSelectLookupListener objects in the same thread.
187: * It will then invoke the quitLookup() method.
188: * @param request The HTTP request we are processing
189: * @param event The event object containing the row(s) selected.
190: * @return a FormKey object.
191: */
192: public FormKey performMultiSelectLookup(HttpServletRequest request,
193: MultiSelectLookupEvent event) {
194: invokeMultiSelectLookupListeners(event);
195: return quitLookup(request);
196: }
197:
198: /** This will add the 'lookup' attribute on the request stream, with a Map containing the fieldnames (from the targetFields property) and values (from the input selectedRow).
199: * It will then invoke the quit() method on itself.
200: * Finally it will return a FormKey object for the generic lookup jsp.
201: * @param request The HTTP request we are processing
202: * @param selectedRow The row that gets selected on the Results screen.
203: * @return a FormKey object for the generic lookup jsp.
204: */
205: public FormKey performLookup(HttpServletRequest request,
206: IModelMap selectedRow) {
207: return perform(request, selectedRow);
208: }
209:
210: /** This will add the 'lookup' attribute on the request stream, with a Map containing the fieldnames (from the targetFields property) and values (from the input selectedRow).
211: * It will then invoke the quit() method on itself.
212: * Finally it will return a FormKey object for the generic lookup jsp.
213: * @param request The HTTP request we are processing
214: * @param selectedRow The row that gets selected on the Results screen.
215: * @return a FormKey object for the generic lookup jsp.
216: */
217: public FormKey performLookup(HttpServletRequest request,
218: Map selectedRow) {
219: return perform(request, selectedRow);
220: }
221:
222: /** This will add the 'lookup' attribute on the request stream, with a Map containing the fieldnames (from the targetFields property) and values (from the input selectedRow).
223: * It will then invoke the quit() method on itself.
224: * Finally it will return a FormKey object for the generic lookup jsp.
225: * @param request The HTTP request we are processing
226: * @param selectedRow The row that gets selected on the Results screen.
227: * @return a FormKey object for the generic lookup jsp.
228: */
229: private FormKey perform(HttpServletRequest request,
230: Object selectedRow) {
231: Map lookupMap = new HashMap();
232: if (selectedRow != null && getTargetFields() != null
233: && getTargetFields().length() > 0) {
234: StringTokenizer tokenizer = new StringTokenizer(
235: getTargetFields(), ";");
236: while (tokenizer.hasMoreTokens()) {
237: String key = tokenizer.nextToken();
238: if (tokenizer.hasMoreTokens()) {
239: String valueField = tokenizer.nextToken();
240: Object value = null;
241: if (selectedRow instanceof Map)
242: value = ((Map) selectedRow).get(valueField);
243: else if (selectedRow instanceof IModelMap)
244: value = ((IModelMap) selectedRow)
245: .get(valueField);
246: value = Formatter.format(value);
247: lookupMap.put(key, (value == null ? "" : value));
248: }
249: }
250: }
251: if (lookupMap.size() > 0)
252: request.setAttribute(LOOKUP_ATTRIBUTE, lookupMap);
253: else
254: request.removeAttribute(LOOKUP_ATTRIBUTE);
255:
256: // Quit this component
257: quit();
258:
259: // forward to the jsp
260: return LOOKUP_FORM_KEY;
261: }
262:
263: /** Returns a Collection of IMultiSelectLookupListener objects.
264: * @return a Collection of IMultiSelectLookupListener objects.
265: */
266: protected Collection getMultiSelectLookupListeners() {
267: return m_multiSelectLookupListeners;
268: }
269:
270: /** Invokes the rowsSelected() method of the registered IMultiSelectLookupListener objects in the same thread.
271: * @param event The event object containing the row(s) selected.
272: */
273: protected void invokeMultiSelectLookupListeners(
274: MultiSelectLookupEvent event) {
275: if (m_multiSelectLookupListeners != null) {
276: for (Iterator i = m_multiSelectLookupListeners.iterator(); i
277: .hasNext();)
278: ((IMultiSelectLookupListener) i.next())
279: .rowsSelected(event);
280: }
281: }
282:
283: }
|