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.jaffa.presentation.portlet.FormKey;
055: import org.jaffa.presentation.portlet.widgets.model.IModelMap;
056: import org.jaffa.datatypes.Formatter;
057: import org.apache.log4j.Logger;
058: import org.jaffa.components.finder.FinderComponent;
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: * It has a convenience method, which should be called by the component's Action class, to forward to the generic lookup jsp.
065: * @author GautamJ
066: * @deprecated This class is required by the object_lookup_1_0 pattern. The object_lookup_2_0 pattern has made this class redundant.
067: */
068: public abstract class LookupComponent extends FinderComponent {
069:
070: private static final Logger log = Logger
071: .getLogger(LookupComponent.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: private static final String LOOKUP_JSP_NAME = "jaffa_lookup";
077:
078: /** Holds value of property targetFields. */
079: private String m_targetFields;
080:
081: /** Getter for property targetFields.
082: * @return Value of property targetFields.
083: */
084: public String getTargetFields() {
085: return m_targetFields;
086: }
087:
088: /** Setter for property targetFields.
089: * @param targetFields New value of property targetFields.
090: */
091: public void setTargetFields(String targetFields) {
092: m_targetFields = targetFields;
093: }
094:
095: /** 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).
096: * It will then invoke the quit() method on itself.
097: * Finally it will return a FormKey object for the generic lookup jsp.
098: * @param request The HTTP request we are processing
099: * @param selectedRow The row that gets selected on the Results screen.
100: * @return a FormKey object for the generic lookup jsp.
101: */
102: public FormKey performLookup(HttpServletRequest request,
103: IModelMap selectedRow) {
104: return perform(request, selectedRow);
105: }
106:
107: /** 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).
108: * It will then invoke the quit() method on itself.
109: * Finally it will return a FormKey object for the generic lookup jsp.
110: * @param request The HTTP request we are processing
111: * @param selectedRow The row that gets selected on the Results screen.
112: * @return a FormKey object for the generic lookup jsp.
113: */
114: public FormKey performLookup(HttpServletRequest request,
115: Map selectedRow) {
116: return perform(request, selectedRow);
117: }
118:
119: /** 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).
120: * It will then invoke the quit() method on itself.
121: * Finally it will return a FormKey object for the generic lookup jsp.
122: * @param request The HTTP request we are processing
123: * @param selectedRow The row that gets selected on the Results screen.
124: * @return a FormKey object for the generic lookup jsp.
125: */
126: private FormKey perform(HttpServletRequest request,
127: Object selectedRow) {
128: Map lookupMap = new HashMap();
129: if (selectedRow != null && getTargetFields() != null
130: && getTargetFields().length() > 0) {
131: StringTokenizer tokenizer = new StringTokenizer(
132: getTargetFields(), ";");
133: while (tokenizer.hasMoreTokens()) {
134: String key = tokenizer.nextToken();
135: if (tokenizer.hasMoreTokens()) {
136: String valueField = tokenizer.nextToken();
137: Object value = null;
138: if (selectedRow instanceof Map)
139: value = ((Map) selectedRow).get(valueField);
140: else if (selectedRow instanceof IModelMap)
141: value = ((IModelMap) selectedRow)
142: .get(valueField);
143: value = Formatter.format(value);
144: lookupMap.put(key, (value == null ? "" : value));
145: }
146: }
147: }
148: if (lookupMap.size() > 0)
149: request.setAttribute(LOOKUP_ATTRIBUTE, lookupMap);
150: else
151: request.removeAttribute(LOOKUP_ATTRIBUTE);
152:
153: // Quit this component
154: super .quit();
155:
156: // forward to the jsp
157: return new FormKey(LOOKUP_JSP_NAME, null);
158: }
159:
160: /** This will remove the 'lookup' attribute from the request stream.
161: * It will then invoke the quit() method on itself.
162: * Finally it will return a FormKey object for the generic lookup jsp, which will close the browser.
163: * @param request The HTTP request we are processing
164: * @return a FormKey object for the generic lookup jsp.
165: */
166: public FormKey quitLookup(HttpServletRequest request) {
167: request.removeAttribute(LOOKUP_ATTRIBUTE);
168:
169: // Quit this component
170: super .quit();
171:
172: // forward to the jsp
173: return new FormKey(LOOKUP_JSP_NAME, null);
174: }
175:
176: }
|