View Javadoc

1   /*
2    * Copyright (c) 2001 - 2005 ivata limited.
3    * All rights reserved.
4    * -----------------------------------------------------------------------------
5    * ivata masks may be redistributed under the GNU General Public
6    * License as published by the Free Software Foundation;
7    * version 2 of the License.
8    *
9    * These programs are free software; you can redistribute them and/or
10   * modify them under the terms of the GNU General Public License
11   * as published by the Free Software Foundation; version 2 of the License.
12   *
13   * These programs are distributed in the hope that they will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16   *
17   * See the GNU General Public License in the file LICENSE.txt for more
18   * details.
19   *
20   * If you would like a copy of the GNU General Public License write to
21   *
22   * Free Software Foundation, Inc.
23   * 59 Temple Place - Suite 330
24   * Boston, MA 02111-1307, USA.
25   *
26   *
27   * To arrange commercial support and licensing, contact ivata at
28   *                  http://www.ivata.com/contact.jsp
29   * -----------------------------------------------------------------------------
30   * $Log: NewAction.java,v $
31   * Revision 1.7.2.1  2005/10/08 10:54:23  colinmacleod
32   * Added temporary workarounds for bugs in ivata groupware v0.11.x
33   *
34   * Revision 1.7  2005/04/11 14:53:28  colinmacleod
35   * Added defaulting of class name to base class
36   * name.
37   *
38   * Revision 1.6  2005/04/09 18:04:19  colinmacleod
39   * Changed copyright text to GPL v2 explicitly.
40   *
41   * Revision 1.5  2005/01/19 13:14:02  colinmacleod
42   * Renamed CausedByException to SystemException.
43   *
44   * Revision 1.4  2005/01/07 08:08:25  colinmacleod
45   * Moved up a version number.
46   * Changed copyright notices to 2005.
47   * Updated the documentation:
48   *   - started working on multiproject:site docu.
49   *   - changed the logo.
50   * Added checkstyle and fixed LOADS of style issues.
51   * Added separate thirdparty subproject.
52   * Added struts (in web), util and webgui (in webtheme) from ivata op.
53   *
54   * Revision 1.3  2004/12/29 15:32:20  colinmacleod
55   * List/input mask actions are no longer hard-coded.
56   * Overrides added via request parameters - defaults via new methods on the
57   factory.
58   *
59   * Revision 1.2  2004/05/19 20:34:17  colinmacleod
60   * Added methods to add, amend and remove value objects.
61   *
62   * Revision 1.1.1.1  2004/05/16 20:40:32  colinmacleod
63   * Ready for 0.1 release
64   * -----------------------------------------------------------------------------
65   */
66  package com.ivata.mask.web.struts;
67  import javax.servlet.http.HttpServletRequest;
68  import javax.servlet.http.HttpServletResponse;
69  import javax.servlet.http.HttpSession;
70  import org.apache.struts.action.ActionErrors;
71  import org.apache.struts.action.ActionForm;
72  import org.apache.struts.action.ActionMapping;
73  import com.ivata.mask.Mask;
74  import com.ivata.mask.MaskFactory;
75  import com.ivata.mask.util.SystemException;
76  import com.ivata.mask.valueobject.ValueObject;
77  /***
78   * <p>
79   * Create a new value object and create a form for it in the request scope.
80   * </p>
81   *
82   * @since ivata masks 0.1 (2004-05-10)
83   * @author Colin MacLeod
84   * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
85   * @version $Revision: 1.7.2.1 $
86   */
87  public class NewAction extends MaskAction {
88      /***
89       * <p>
90       * This factory is needed to access the masks and groups of masks.
91       * </p>
92       */
93      private MaskFactory maskFactory;
94      /***
95       * <p>
96       * Construct an action to create new value objects.
97       * </p>
98       *
99       * @param maskFactoryParam Refer to {@link MaskAction#MaskAction}.
100      * @param authenticatorParam  Refer to {@link MaskAction#MaskAction}.
101      */
102     public NewAction(final MaskFactory maskFactoryParam,
103             final MaskAuthenticator authenticatorParam) {
104         super(maskFactoryParam, authenticatorParam);
105         this.maskFactory = maskFactoryParam;
106     }
107     /***
108      * <p>
109      * Override this method to create a different class of input mask form.
110      * </p>
111      *
112      * @param requestParam Refer to {@link InputMaskForm#InputMaskForm}.
113      * @param valueObjectParam Refer to {@link InputMaskForm#InputMaskForm}.
114      * @param maskParam  Refer to {@link InputMaskForm#InputMaskForm}.
115      * @param baseClassParam Refer to {@link InputMaskForm#InputMaskForm}.
116      * @return new input mask form instance.
117      */
118     protected InputMaskForm createInputMaskForm(
119             final HttpServletRequest requestParam,
120             final ValueObject valueObjectParam,
121             final Mask maskParam,
122             final Class baseClassParam) {
123         return new InputMaskForm(valueObjectParam, maskParam, baseClassParam);
124     }
125     /***
126      * <p>
127      * Generic method called by the <strong>Struts </strong> interface. Creates
128      * the value object and a form. Always returns success.
129      * </p>
130      *
131      * @param mapping
132      *            The ActionMapping used to select this instance.
133      * @param errors
134      *            valid errors object to append errors to. If there are any
135      *            errors, the action will return to the input.
136      * @param form
137      *            optional ActionForm bean for this request (if any)
138      * @param request
139      *            non-HTTP request we are processing
140      * @param response
141      *            The non-HTTP response we are creating
142      * @param session
143      *            returned from the <code>request</code> parameter.
144      * @exception SystemException
145      *                if there is any problem which prevents processing. It will
146      *                result in the webapp being forwarded to the standard error
147      *                page.
148      * @return this method returns the string used to identify the correct
149      *         <strong>Struts </strong> <code>ActionForward</code> which
150      *         should follow this page, or <code>null</code> if it should
151      *         return to the input.
152      */
153     public String execute(final ActionMapping mapping,
154             final ActionErrors errors,
155             final ActionForm form,
156             final HttpServletRequest request,
157             final HttpServletResponse response,
158             final HttpSession session)
159             throws SystemException {
160         String className = request.getParameter("className");
161         String baseClassName = request.getParameter("baseClass");
162         if (className == null) {
163             className = baseClassName;
164         }
165         if (className == null) {
166             throw new NullPointerException("ERROR in NewAction: you must "
167                     + "specify a request parameter called 'className'.");
168         }
169         Class valueObjectClass;
170         try {
171             valueObjectClass = Class.forName(className);
172         } catch (ClassNotFoundException e) {
173             throw new ValueObjectException(e);
174         }
175         ValueObject valueObject;
176         try {
177             valueObject = (ValueObject) valueObjectClass.newInstance();
178         } catch (InstantiationException e) {
179             throw new ValueObjectException(e, valueObjectClass);
180         } catch (IllegalAccessException e) {
181             throw new ValueObjectException(e, valueObjectClass);
182         }
183         // default the base class name to the class name: if you want, they
184         // can be different, but the class must be a subclass of the base
185         // class
186         if (baseClassName == null) {
187             baseClassName = className;
188         }
189         Class baseClass;
190         try {
191             baseClass = Class.forName(baseClassName);
192         } catch (ClassNotFoundException e) {
193             throw new ValueObjectException(e);
194         }
195         Mask mask = maskFactory.getMask(baseClass, getInputMask(request, form));
196         InputMaskForm inputMaskForm;
197         if (form instanceof InputMaskForm) {
198             inputMaskForm = (InputMaskForm) form;
199         } else {
200             inputMaskForm = createInputMaskForm(request, valueObject, mask,
201                     baseClass);
202         }
203         request.setAttribute(InputMaskForm.REQUEST_ATTRIBUTE, inputMaskForm);
204         // TODO: workaround for ivata groupware
205         session.setAttribute(InputMaskForm.REQUEST_ATTRIBUTE, inputMaskForm);
206         return "success";
207     }
208 }
209