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.presentation.portlet;
051:
052: import org.apache.struts.action.ActionForm;
053: import org.apache.struts.action.ActionMapping;
054: import org.apache.struts.Globals;
055: import javax.servlet.http.HttpServletRequest;
056: import org.jaffa.presentation.portlet.component.Component;
057: import org.jaffa.presentation.portlet.session.WidgetCache;
058: import org.jaffa.presentation.portlet.session.UserSession;
059: import org.apache.struts.action.ActionErrors;
060: import org.apache.struts.action.ActionError;
061: import org.jaffa.presentation.portlet.widgets.taglib.FormTag;
062: import org.jaffa.exceptions.ApplicationException;
063: import org.jaffa.exceptions.ApplicationExceptions;
064: import java.util.Iterator;
065:
066: /** This is the base class for all the 'Form' classes
067: */
068: public class FormBase extends ActionForm {
069:
070: /** Component to which this form belongs */
071: private Component m_component = null;
072:
073: /** Widget-cache for the form. Maintained here for convenience */
074: private WidgetCache m_widgetCache = null;
075:
076: /** Returns the Component to which this Form belongs
077: * @return The Component for this Form
078: */
079: public Component getComponent() {
080: return m_component;
081: }
082:
083: /** Sets the Component for the Form
084: * @param component The Component object
085: */
086: public void setComponent(Component component) {
087: m_component = component;
088: setWidgetCache(null);
089: }
090:
091: /** Returns the WidgetCache of all the models for the Component
092: * @return The WidgetCache object
093: */
094: public WidgetCache getWidgetCache() {
095: if (m_component != null && m_widgetCache == null)
096: m_widgetCache = getComponent().getUserSession()
097: .getWidgetCache(getComponent().getComponentId());
098: return m_widgetCache;
099: }
100:
101: /** Sets the WidgetCache object, which will have the models for the Component
102: * @param widgetCache The WidgetCache object
103: */
104: private void setWidgetCache(WidgetCache widgetCache) {
105: m_widgetCache = widgetCache;
106: }
107:
108: /** The form should override this method for defaulting the data in the form-bean
109: */
110: public void initForm() {
111: }
112:
113: /** The form should override this method for destroying any references that it holds
114: */
115: public void cleanup() {
116: }
117:
118: /** This sets the Component & the WidgetCache fields
119: * @param mapping The mapping used to select this instance
120: * @param request The servlet request we are processing
121: */
122: public void reset(ActionMapping mapping, HttpServletRequest request) {
123: /* NOTE: Prior to Struts1.1, only the ActionServlet was invoking the reset() method.
124: * But in Struts1.1, the reset() method is called from 2 places -
125: * 1- RequestProcessor (which in turn is invoked from ActionServlet)
126: * 2- FormTag
127: * While processing an action, we need to set the component on the formbean, before the other properties are set. Hence the need for setting the component here.
128: * However, it is quite possible, that an action on one screen, will result in returning a FormKey for a different component. In that case, when the other component's screen is being rendered, the FormTag will invoke this method, but pass the componentId of the original component in the request stream. This would mean an erroneous component being set on the formbean. However, the Jaffa-FormTag will ultimately set the correct component. We just shudn't be setting the widget-cache in this method.
129: */
130: // this will set the Component & WidgetCache
131: UserSession us = UserSession.getUserSession(request);
132: String componentId = request
133: .getParameter(FormTag.PARAMETER_COMPONENT_ID);
134: if (componentId != null)
135: setComponent(us.getComponent(componentId));
136: else
137: setComponent(null);
138: setWidgetCache(null);
139: }
140:
141: /** This clears all the errors generated while processing this Form
142: * @param request The servlet request we are processing
143: */
144: public void clearErrors(HttpServletRequest request) {
145: request.removeAttribute(Globals.ERROR_KEY);
146: }
147:
148: /** Returns the errors generated while processing this Form
149: * @param request The servlet request we are processing
150: * @return If no errors are found, return null or an ActionErrors object with recorded error messages
151: */
152: public ActionErrors getErrors(HttpServletRequest request) {
153: return (ActionErrors) request.getAttribute(Globals.ERROR_KEY);
154: }
155:
156: /** Checks if any errors have been generated
157: * @param request The servlet request we are processing
158: * @return true if errors exist
159: */
160: public boolean hasErrors(HttpServletRequest request) {
161: ActionErrors errors = getErrors(request);
162: if (errors != null && !errors.isEmpty())
163: return true;
164: else
165: return false;
166: }
167:
168: /** Record an error
169: * @param request The servlet request we are processing
170: * @param property Property name (or ActionErrors.GLOBAL_ERROR)
171: * @param error The error message to be added
172: */
173: public void raiseError(HttpServletRequest request, String property,
174: ActionError error) {
175: ActionErrors errors = getErrors(request);
176: if (errors == null) {
177: errors = new ActionErrors();
178: request.setAttribute(Globals.ERROR_KEY, errors);
179: }
180: errors.add(property, error);
181: }
182:
183: /** Record an error. This implicitly creates an ActionError for the input key
184: * @param request The servlet request we are processing
185: * @param property Property name (or ActionErrors.GLOBAL_ERROR)
186: * @param key Message key for this error message
187: */
188: public void raiseError(HttpServletRequest request, String property,
189: String key) {
190: raiseError(request, property, new ActionError(key));
191: }
192:
193: /** Record an error. This implicitly creates an ActionError for the input key & replacement values
194: * @param request The servlet request we are processing
195: * @param property Property name (or ActionErrors.GLOBAL_ERROR)
196: * @param key Message key for this error message
197: * @param value0 First replacement value
198: */
199: public void raiseError(HttpServletRequest request, String property,
200: String key, Object value0) {
201: raiseError(request, property, new ActionError(key, value0));
202: }
203:
204: /** Record an error. This implicitly creates an ActionError for the input key & replacement values
205: * @param request The servlet request we are processing
206: * @param property Property name (or ActionErrors.GLOBAL_ERROR)
207: * @param key Message key for this error message
208: * @param value0 First replacement value
209: * @param value1 Second replacement value
210: */
211: public void raiseError(HttpServletRequest request, String property,
212: String key, Object value0, Object value1) {
213: raiseError(request, property, new ActionError(key, value0,
214: value1));
215: }
216:
217: /** Record an error. This implicitly creates an ActionError for the input key & replacement values
218: * @param request The servlet request we are processing
219: * @param property Property name (or ActionErrors.GLOBAL_ERROR)
220: * @param key Message key for this error message
221: * @param value0 First replacement value
222: * @param value1 Second replacement value
223: * @param value2 Third replacement value
224: */
225: public void raiseError(HttpServletRequest request, String property,
226: String key, Object value0, Object value1, Object value2) {
227: raiseError(request, property, new ActionError(key, value0,
228: value1, value2));
229: }
230:
231: /** Record an error. This implicitly creates an ActionError for the input key & replacement values
232: * @param request The servlet request we are processing
233: * @param property Property name (or ActionErrors.GLOBAL_ERROR)
234: * @param key Message key for this error message
235: * @param value0 First replacement value
236: * @param value1 Second replacement value
237: * @param value2 Third replacement value
238: * @param value3 Fourth replacement value
239: */
240: public void raiseError(HttpServletRequest request, String property,
241: String key, Object value0, Object value1, Object value2,
242: Object value3) {
243: raiseError(request, property, new ActionError(key, value0,
244: value1, value2, value3));
245: }
246:
247: /** Record an error. This implicitly creates an ActionError for the input key & replacement values
248: * @param request The servlet request we are processing
249: * @param property Property name (or ActionErrors.GLOBAL_ERROR)
250: * @param key Message key for this error message
251: * @param values Array of replacement values
252: */
253: public void raiseError(HttpServletRequest request, String property,
254: String key, Object[] values) {
255: raiseError(request, property, new ActionError(key, values));
256: }
257:
258: /** Record an error. This implicitly creates an ActionError for the input key & replacement values.
259: * @param request The servlet request we are processing
260: * @param property Property name (or ActionErrors.GLOBAL_ERROR)
261: * @param appExp the exception against which the error is being raised. This exception will have the errorToken and arguments, if any.
262: */
263: public void raiseError(HttpServletRequest request, String property,
264: ApplicationException appExp) {
265: String key = appExp.getMessage();
266: Object[] args = appExp.getArguments();
267: if (args == null || args.length == 0)
268: raiseError(request, property, key);
269: else
270: raiseError(request, property, key, args);
271: }
272:
273: /** Record an error. This implicitly creates an ActionError for the input key & replacement values.
274: * @param request The servlet request we are processing
275: * @param property Property name (or ActionErrors.GLOBAL_ERROR)
276: * @param appExps Error will raised against each ApplicationException passed in this parameter.
277: */
278: public void raiseError(HttpServletRequest request, String property,
279: ApplicationExceptions appExps) {
280: for (Iterator i = appExps.iterator(); i.hasNext();)
281: raiseError(request, property, (ApplicationException) i
282: .next());
283: }
284: }
|