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: ListAction.java,v $
31   * Revision 1.7  2005/04/11 14:52:45  colinmacleod
32   * Added checking for the new button.
33   *
34   * Revision 1.6  2005/04/09 18:04:18  colinmacleod
35   * Changed copyright text to GPL v2 explicitly.
36   *
37   * Revision 1.5  2005/01/19 13:14:02  colinmacleod
38   * Renamed CausedByException to SystemException.
39   *
40   * Revision 1.4  2005/01/07 08:08:24  colinmacleod
41   * Moved up a version number.
42   * Changed copyright notices to 2005.
43   * Updated the documentation:
44   *   - started working on multiproject:site docu.
45   *   - changed the logo.
46   * Added checkstyle and fixed LOADS of style issues.
47   * Added separate thirdparty subproject.
48   * Added struts (in web), util and webgui (in webtheme) from ivata op.
49   *
50   * Revision 1.3  2004/12/29 15:32:19  colinmacleod
51   * List/input mask actions are no longer hard-coded.
52   * Overrides added via request parameters
53   * - defaults via new methods on the factory.
54   *
55   * Revision 1.2  2004/11/12 15:10:42  colinmacleod
56   * Moved persistence classes from ivata op as a replacement for
57   * ValueObjectLocator.
58   *
59   * Revision 1.1.1.1  2004/05/16 20:40:32  colinmacleod
60   * Ready for 0.1 release
61   * -----------------------------------------------------------------------------
62   */
63  package com.ivata.mask.web.struts;
64  import java.util.List;
65  import javax.servlet.http.HttpServletRequest;
66  import javax.servlet.http.HttpServletResponse;
67  import javax.servlet.http.HttpSession;
68  import org.apache.struts.action.ActionErrors;
69  import org.apache.struts.action.ActionForm;
70  import org.apache.struts.action.ActionMapping;
71  import com.ivata.mask.Mask;
72  import com.ivata.mask.MaskFactory;
73  import com.ivata.mask.persistence.PersistenceManager;
74  import com.ivata.mask.persistence.PersistenceSession;
75  import com.ivata.mask.util.StringHandling;
76  import com.ivata.mask.util.SystemException;
77  /***
78   * <p>
79   * Load the list for a given base class. The base class is specified in a
80   * request parameter, or may be specified in the request as a
81   * <code>ListForm</code>.
82   * </p>
83   *
84   * @since ivata masks 0.1 (2004-04-30)
85   * @author Colin MacLeod
86   * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
87   * @version $Revision: 1.7 $
88   */
89  public class ListAction extends MaskAction {
90      /***
91       * <p>
92       * This factory is needed to access the masks and groups of masks.
93       * </p>
94       */
95      private MaskFactory maskFactory;
96      /***
97       * <p>
98       * Used to locate the value objects by their shared base class.
99       * </p>
100      */
101     private PersistenceManager persistenceManager;
102     /***
103      * <p>
104      * Create a new list action with the given value object locator.
105      * </p>
106      *
107      * @param persistenceManagerParam
108      *            used to locate the value objects by their shared base class.
109      * @param maskFactoryParam
110      *            Refer to {@link MaskAction#MaskAction}.
111      * @param authenticatorParam
112      *            Refer to {@link MaskAction#MaskAction}.
113      */
114     public ListAction(final PersistenceManager persistenceManagerParam,
115             final MaskFactory maskFactoryParam,
116             final MaskAuthenticator authenticatorParam) {
117         super(maskFactoryParam, authenticatorParam);
118         this.maskFactory = maskFactoryParam;
119         this.persistenceManager = persistenceManagerParam;
120     }
121     /***
122      * This method does all the hard work, preparing objects for display in the
123      * list.
124      *
125      * @param mapping
126      * Refer to {@link com.ivata.mask.web.struts.MaskAction#execute}.
127      * @param errors
128      * Refer to {@link com.ivata.mask.web.struts.MaskAction#execute}.
129      * @param form
130      * Refer to {@link com.ivata.mask.web.struts.MaskAction#execute}.
131      * @param request
132      * Refer to {@link com.ivata.mask.web.struts.MaskAction#execute}.
133      * @param response
134      * Refer to {@link com.ivata.mask.web.struts.MaskAction#execute}.
135      * @param session
136      * Refer to {@link com.ivata.mask.web.struts.MaskAction#execute}.
137      * @return Refer to {@link com.ivata.mask.web.struts.MaskAction#execute}.
138      * @throws SystemException
139      * Refer to {@link com.ivata.mask.web.struts.MaskAction#execute}.
140      */
141     public String execute(final ActionMapping mapping,
142             final ActionErrors errors, final ActionForm form,
143             final HttpServletRequest request,
144             final HttpServletResponse response, final HttpSession session)
145             throws SystemException {
146         // if the new button was pressed, just return new to go to the new
147         // action
148         String clear = getFromRequestOrForm("clear", request, form);
149         if (!StringHandling.isNullOrEmpty(clear)) {
150             return "new";
151         }
152         // request parameter overrides current list form
153         String baseClassName = request.getParameter("baseClass");
154         Class newBaseClass = null;
155         if (baseClassName != null) {
156             try {
157                 newBaseClass = Class.forName(baseClassName);
158             } catch (ClassNotFoundException e) {
159                 throw new SystemException(e);
160             }
161         } else if (form == null) {
162             throw new NullPointerException(
163                 "ERROR in ListAction: either the "
164                 + "class request parameter 'baseClass', "
165                 + "or a valid list or mask form must be specified.");
166         } else if (form instanceof InputMaskForm) {
167             InputMaskForm inputMaskForm = (InputMaskForm) form;
168             newBaseClass = inputMaskForm.getBaseClass();
169         }
170         // if we changed to the list view from another view, build the list form
171         if (newBaseClass != null) {
172             PersistenceSession persistenceSession = persistenceManager
173                     .openSession();
174             List valueObjects;
175             try {
176                 valueObjects = persistenceManager.findAll(persistenceSession,
177                         newBaseClass);
178             } finally {
179                 persistenceSession.close();
180             }
181             Mask mask = maskFactory.getMask(newBaseClass, getListMask(request,
182                     form));
183             ListForm listForm = new ListForm(valueObjects, mask, newBaseClass);
184             request.setAttribute(ListForm.REQUEST_ATTRIBUTE, listForm);
185         }
186         return "success";
187     }
188 }
189