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: InputMaskAction.java,v $
31   * Revision 1.12.2.1  2005/10/08 10:53:22  colinmacleod
32   * Added debug logging when value object is modified or added.
33   *
34   * Revision 1.12  2005/04/11 14:52:05  colinmacleod
35   * Added execute override.
36   *
37   * Revision 1.11  2005/04/09 18:04:18  colinmacleod
38   * Changed copyright text to GPL v2 explicitly.
39   *
40   * Revision 1.10  2005/03/10 10:37:56  colinmacleod
41   * Added handling of client-program session to
42   * persistence session.
43   *
44   * Revision 1.9  2005/01/19 12:56:36  colinmacleod
45   * Added OperationNotSupportedException to clear.
46   *
47   * Revision 1.8  2005/01/10 19:06:02  colinmacleod
48   * Applied persistence manager methods to remove.
49   *
50   * Revision 1.7  2005/01/07 08:08:24  colinmacleod
51   * Moved up a version number.
52   * Changed copyright notices to 2005.
53   * Updated the documentation:
54   *   - started working on multiproject:site docu.
55   *   - changed the logo.
56   * Added checkstyle and fixed LOADS of style issues.
57   * Added separate thirdparty subproject.
58   * Added struts (in web), util and webgui (in webtheme) from ivata op.
59   *
60   * Revision 1.6  2004/12/29 15:32:19  colinmacleod
61   * List/input mask actions are no longer hard-coded.
62   * Overrides added via request parameters
63   * - defaults via new methods on the factory.
64   *
65   * Revision 1.5  2004/12/23 21:28:32  colinmacleod
66   * Modifications to add ivata op compatibility.
67   *
68   * Revision 1.4  2004/11/12 15:10:42  colinmacleod
69   * Moved persistence classes from ivata op as a replacement for
70   * ValueObjectLocator.
71   *
72   * Revision 1.3  2004/11/11 13:49:46  colinmacleod
73   * Added log4j logging.
74   *
75   * Revision 1.2  2004/05/19 20:34:17  colinmacleod
76   * Added methods to add, amend and remove value objects.
77   *
78   * Revision 1.1.1.1  2004/05/16 20:40:33  colinmacleod
79   * Ready for 0.1 release
80   * -----------------------------------------------------------------------------
81   */
82  package com.ivata.mask.web.struts;
83  import java.util.List;
84  
85  import javax.servlet.http.HttpServletRequest;
86  import javax.servlet.http.HttpServletResponse;
87  import javax.servlet.http.HttpSession;
88  
89  import org.apache.log4j.Logger;
90  import org.apache.struts.action.ActionErrors;
91  import org.apache.struts.action.ActionForm;
92  import org.apache.struts.action.ActionMapping;
93  
94  import com.ivata.mask.MaskFactory;
95  import com.ivata.mask.persistence.PersistenceManager;
96  import com.ivata.mask.persistence.PersistenceSession;
97  import com.ivata.mask.util.StringHandling;
98  import com.ivata.mask.util.SystemException;
99  import com.ivata.mask.valueobject.ValueObject;
100 /***
101  * <p>
102  * Add, amend or remove an existing value object in the list of value objects.
103  * </p>
104  *
105  * @since ivata masks 0.4 (2004-05-10)
106  * @author Colin MacLeod
107  * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
108  * @version $Revision: 1.12.2.1 $
109  */
110 public class InputMaskAction extends MaskAction {
111     /***
112      * <p>
113      * This log provides tracing and debugging information.
114      * </p>
115      */
116     private Logger log = Logger.getLogger(InputMaskAction.class);
117     /***
118      * <p>
119      * This factory is needed to access the masks and groups of masks.
120      * </p>
121      */
122     private MaskFactory maskFactory;
123     /***
124      * <p>
125      * Used to locate the value objects by their shared base class.
126      * </p>
127      */
128     private PersistenceManager persistenceManager;
129     /***
130      * <p>
131      * Create a new AddAction with the given value object locator.
132      * </p>
133      *
134      * @param persistenceManagerParam
135      *            used to locate the value objects by their shared base class.
136      * @param maskFactoryParam
137      *            Refer to {@link MaskAction#MaskAction}.
138      * @param authenticatorParam
139      *            Refer to {@link MaskAction#MaskAction}.
140      */
141     public InputMaskAction(final MaskFactory maskFactoryParam,
142             final PersistenceManager persistenceManagerParam,
143             final MaskAuthenticator authenticatorParam) {
144         super(maskFactoryParam, authenticatorParam);
145         this.maskFactory = maskFactoryParam;
146         this.persistenceManager = persistenceManagerParam;
147     }
148 
149     /***
150      * Overridden to trap the Clear/New button. If this is pressed, the forward
151      * <code>new</code> is returned which you can then route to
152      * <code>NewAction</code> via the <strong>Struts</strong> framework.
153      *
154      * @param mappingParam
155      * Refer to {@link MaskAction#execute}.
156      * @param errorsParam
157      * Refer to {@link MaskAction#execute}.
158      * @param formParam
159      * Refer to {@link MaskAction#execute}.
160      * @param requestParam
161      * Refer to {@link MaskAction#execute}.
162      * @param responseParam
163      * Refer to {@link MaskAction#execute}.
164      * @param sessionParam
165      * Refer to {@link MaskAction#execute}.
166      * @return <code>new</code> if the new/clear button was pressed, otherwise
167      * the same as the super constructor.
168      * @throws SystemException
169      * Refer to {@link MaskAction#execute}.
170      */
171     public String execute(ActionMapping mappingParam, ActionErrors errorsParam,
172             ActionForm formParam, HttpServletRequest requestParam,
173             HttpServletResponse responseParam, HttpSession sessionParam)
174             throws SystemException {
175         String clear = getFromRequestOrForm("clear", requestParam, formParam);
176         if (!StringHandling.isNullOrEmpty(clear)) {
177             return "new";
178         }
179         return super.execute(mappingParam, errorsParam, formParam,
180                 requestParam, responseParam, sessionParam);
181     }
182     /***
183      * <p>
184      * Adds the value object to the list, or amends an existing value object.
185      * </p>
186      *
187      * @param mapping
188      *            Refer to
189      *            {@link com.ivata.mask.web.struts.MaskAction#onConfirm}.
190      * @param errors
191      *            Refer to
192      *            {@link com.ivata.mask.web.struts.MaskAction#onConfirm}.
193      * @param form
194      *            Refer to
195      *            {@link com.ivata.mask.web.struts.MaskAction#onConfirm}.
196      * @param request
197      *            Refer to
198      *            {@link com.ivata.mask.web.struts.MaskAction#onConfirm}.
199      * @param response
200      *            Refer to
201      *            {@link com.ivata.mask.web.struts.MaskAction#onConfirm}.
202      * @param session
203      *            Refer to
204      *            {@link com.ivata.mask.web.struts.MaskAction#onConfirm}.
205      * @param defaultForward Refer to
206      *            {@link com.ivata.mask.web.struts.MaskAction#onConfirm}.
207      * @return Refer to {@link com.ivata.mask.web.struts.MaskAction#onConfirm}.
208      * @throws SystemException
209      *             Refer to
210      *             {@link com.ivata.mask.web.struts.MaskAction#onConfirm}.
211      */
212     public String onConfirm(final ActionMapping mapping,
213             final ActionErrors errors, final ActionForm form,
214             final HttpServletRequest request,
215             final HttpServletResponse response,
216             final HttpSession session,
217             final String defaultForward) throws SystemException {
218         assert (form != null);
219         InputMaskForm maskForm = (InputMaskForm) form;
220         Class baseClass = maskForm.getBaseClass();
221         ValueObject valueObject = maskForm.getValueObject();
222         String thisIdString = valueObject.getIdString();
223         // if the id string is null or empty, we should add a new item to the
224         // list
225         PersistenceSession persistenceSession =
226             persistenceManager.openSession(session.getAttribute(
227                     "securitySession"));
228         try {
229             if (StringHandling.isNullOrEmpty(thisIdString)) {
230                 if (log.isDebugEnabled()) {
231                     log.debug("Adding new value object with "
232                         + valueObject.getClass()
233                         + ": "
234                         + valueObject);
235                 }
236                 valueObject = persistenceManager.add(persistenceSession,
237                         valueObject);
238             } else {
239                 if (log.isDebugEnabled()) {
240                     log.debug("Modifiing value object with "
241                         + valueObject.getClass()
242                         + ", id "
243                         + valueObject.getIdString()
244                         + ": "
245                         + valueObject);
246                 }
247                 // if it is not a new item, find the existing one and amend it
248                 persistenceManager.amend(persistenceSession, valueObject);
249             }
250         } finally {
251             persistenceSession.close();
252         }
253         if (maskForm.isRefreshOpener()) {
254             request.setAttribute("refreshOpener", "true");
255         }
256         return defaultForward;
257     }
258     /***
259      * <p>
260      * Removes the value object from the list.
261      * </p>
262      * @param mapping
263      * Refer to {@link com.ivata.mask.web.struts.MaskAction#onDelete}.
264      * @param errors
265      * Refer to {@link com.ivata.mask.web.struts.MaskAction#onDelete}.
266      * @param form
267      * Refer to {@link com.ivata.mask.web.struts.MaskAction#onDelete}.
268      * @param request
269      * Refer to {@link com.ivata.mask.web.struts.MaskAction#onDelete}.
270      * @param response
271      * Refer to {@link com.ivata.mask.web.struts.MaskAction#onDelete}.
272      * @param session
273      * Refer to {@link com.ivata.mask.web.struts.MaskAction#onDelete}.
274      *
275      * @return Refer to {@link com.ivata.mask.web.struts.MaskAction#onDelete}.
276      * @throws SystemException
277      * Refer to {@link com.ivata.mask.web.struts.MaskAction#onDelete}.
278      */
279     public String onDelete(final ActionMapping mapping,
280             final ActionErrors errors, final ActionForm form,
281             final HttpServletRequest request,
282             final HttpServletResponse response,
283             final HttpSession session,
284             final String defaultForward)
285             throws SystemException {
286         InputMaskForm maskForm = (InputMaskForm) form;
287         Class baseClass = maskForm.getBaseClass();
288         PersistenceSession persistenceSession = persistenceManager
289                 .openSession();
290         List valueObjects;
291         boolean found = false;
292         ValueObject thisValueObject = maskForm.getValueObject();
293         String thisIdString = thisValueObject.getIdString();
294         try {
295             persistenceManager.remove(
296                     persistenceSession,
297                     baseClass,
298                     thisIdString);
299         } finally {
300             persistenceSession.close();
301         }
302         clear(mapping, errors, form, request, response, session);
303         if (maskForm.isRefreshOpener()) {
304             request.setAttribute("refreshOpener", "true");
305         }
306         return defaultForward;
307     }
308 }
309