001: /*
002: * Copyright (c) 2001 - 2005 ivata limited.
003: * All rights reserved.
004: * -----------------------------------------------------------------------------
005: * ivata masks may be redistributed under the GNU General Public
006: * License as published by the Free Software Foundation;
007: * version 2 of the License.
008: *
009: * These programs are free software; you can redistribute them and/or
010: * modify them under the terms of the GNU General Public License
011: * as published by the Free Software Foundation; version 2 of the License.
012: *
013: * These programs are distributed in the hope that they will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: *
017: * See the GNU General Public License in the file LICENSE.txt for more
018: * details.
019: *
020: * If you would like a copy of the GNU General Public License write to
021: *
022: * Free Software Foundation, Inc.
023: * 59 Temple Place - Suite 330
024: * Boston, MA 02111-1307, USA.
025: *
026: *
027: * To arrange commercial support and licensing, contact ivata at
028: * http://www.ivata.com/contact.jsp
029: * -----------------------------------------------------------------------------
030: * $Log: NewAction.java,v $
031: * Revision 1.11 2005/10/11 18:54:06 colinmacleod
032: * Fixed some checkstyle and javadoc issues.
033: *
034: * Revision 1.10 2005/10/09 09:55:17 colinmacleod
035: * Merged changes from ivata masks v0.6.2 into main trunk.
036: *
037: * Revision 1.9 2005/10/03 10:17:25 colinmacleod
038: * Fixed some style and javadoc issues.
039: *
040: * Revision 1.8 2005/10/02 14:06:34 colinmacleod
041: * Added/improved log4j logging.
042: *
043: * Revision 1.7.2.1 2005/10/08 10:54:23 colinmacleod
044: * Added temporary workarounds for bugs in ivata groupware v0.11.x
045: *
046: * Revision 1.7 2005/04/11 14:53:28 colinmacleod
047: * Added defaulting of class name to base class
048: * name.
049: *
050: * Revision 1.6 2005/04/09 18:04:19 colinmacleod
051: * Changed copyright text to GPL v2 explicitly.
052: *
053: * Revision 1.5 2005/01/19 13:14:02 colinmacleod
054: * Renamed CausedByException to SystemException.
055: *
056: * Revision 1.4 2005/01/07 08:08:25 colinmacleod
057: * Moved up a version number.
058: * Changed copyright notices to 2005.
059: * Updated the documentation:
060: * - started working on multiproject:site docu.
061: * - changed the logo.
062: * Added checkstyle and fixed LOADS of style issues.
063: * Added separate thirdparty subproject.
064: * Added struts (in web), util and webgui (in webtheme) from ivata op.
065: *
066: * Revision 1.3 2004/12/29 15:32:20 colinmacleod
067: * List/input mask actions are no longer hard-coded.
068: * Overrides added via request parameters - defaults via new methods on the
069: factory.
070: *
071: * Revision 1.2 2004/05/19 20:34:17 colinmacleod
072: * Added methods to add, amend and remove value objects.
073: *
074: * Revision 1.1.1.1 2004/05/16 20:40:32 colinmacleod
075: * Ready for 0.1 release
076: * -----------------------------------------------------------------------------
077: */
078: package com.ivata.mask.web.struts;
079:
080: import org.apache.log4j.Logger;
081:
082: import javax.servlet.http.HttpServletRequest;
083: import javax.servlet.http.HttpServletResponse;
084: import javax.servlet.http.HttpSession;
085: import org.apache.struts.action.ActionForm;
086: import org.apache.struts.action.ActionMapping;
087: import org.sourceforge.clientsession.ClientSession;
088:
089: import com.ivata.mask.Mask;
090: import com.ivata.mask.MaskFactory;
091: import com.ivata.mask.util.SystemException;
092: import com.ivata.mask.valueobject.ValueObject;
093:
094: /**
095: * <p>
096: * Create a new value object and create a form for it in the request scope.
097: * </p>
098: *
099: * @since ivata masks 0.1 (2004-05-10)
100: * @author Colin MacLeod
101: * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
102: * @version $Revision: 1.11 $
103: */
104: public class NewAction extends MaskAction {
105: /**
106: * Logger for this class.
107: */
108: private static final Logger logger = Logger
109: .getLogger(NewAction.class);
110:
111: /**
112: * <p>
113: * This factory is needed to access the masks and groups of masks.
114: * </p>
115: */
116: private MaskFactory maskFactory;
117:
118: /**
119: * <p>
120: * Construct an action to create new value objects.
121: * </p>
122: *
123: * @param maskFactoryParam {@inheritDoc}
124: * @param authenticatorParam {@inheritDoc}
125: */
126: public NewAction(final MaskFactory maskFactoryParam,
127: final MaskAuthenticator authenticatorParam) {
128: super (maskFactoryParam, authenticatorParam);
129: this .maskFactory = maskFactoryParam;
130: }
131:
132: /**
133: * <p>
134: * Override this method to create a different class of input mask form.
135: * </p>
136: *
137: * @param requestParam
138: * <copyDoc>Refer to {@link InputMaskForm#InputMaskForm}.</copyDoc>
139: * @param valueObjectParam
140: * <copyDoc>Refer to {@link InputMaskForm#InputMaskForm}.</copyDoc>
141: * @param maskParam
142: * <copyDoc>Refer to {@link InputMaskForm#InputMaskForm}.</copyDoc>
143: * @param baseClassParam
144: * <copyDoc>Refer to {@link InputMaskForm#InputMaskForm}.</copyDoc>
145: * @return new input mask form instance.
146: */
147: protected InputMaskForm createInputMaskForm(
148: final HttpServletRequest requestParam,
149: final ValueObject valueObjectParam, final Mask maskParam,
150: final Class baseClassParam) {
151: if (logger.isDebugEnabled()) {
152: logger
153: .debug("createInputMaskForm(HttpServletRequest requestParam = "
154: + requestParam
155: + ", ValueObject valueObjectParam = "
156: + valueObjectParam
157: + ", Mask maskParam = "
158: + maskParam
159: + ", Class baseClassParam = "
160: + baseClassParam + ") - start");
161: }
162:
163: InputMaskForm returnInputMaskForm = new InputMaskForm(
164: valueObjectParam, maskParam, baseClassParam);
165: if (logger.isDebugEnabled()) {
166: logger.debug("createInputMaskForm - end - return value = "
167: + returnInputMaskForm);
168: }
169: return returnInputMaskForm;
170: }
171:
172: /**
173: * <p>
174: * Generic method called by the <strong>Struts </strong> interface. Creates
175: * the value object and a form. Always returns success.
176: * </p>
177: *
178: * @param mapping {@inheritDoc}
179: * @param form {@inheritDoc}
180: * @param request {@inheritDoc}
181: * @param response {@inheritDoc}
182: * @param session {@inheritDoc}
183: * @param clientSession {@inheritDoc}
184: * @throws SystemException If a new value object cannot be created.
185: * @return "success" if successful, otherwise <code>null</code>.
186: */
187: public String execute(final ActionMapping mapping,
188: final ActionForm form, final HttpServletRequest request,
189: final HttpServletResponse response,
190: final HttpSession session, final ClientSession clientSession)
191: throws SystemException {
192: if (logger.isDebugEnabled()) {
193: logger.debug("execute(ActionMapping mapping = " + mapping
194: + ", ActionForm form = " + form
195: + ", HttpServletRequest request = " + request
196: + ", HttpServletResponse response = " + response
197: + ", HttpSession session = " + session
198: + ", ClientSession clientSession = "
199: + clientSession + ") - start");
200: }
201:
202: String className = request.getParameter("className");
203: String baseClassName = request.getParameter("baseClass");
204: if (className == null) {
205: className = baseClassName;
206: }
207: if (className == null) {
208: throw new NullPointerException(
209: "ERROR in NewAction: you must "
210: + "specify a request parameter called 'className'.");
211: }
212: Class valueObjectClass;
213: try {
214: valueObjectClass = Class.forName(className);
215: } catch (ClassNotFoundException e) {
216: logger.error("execute - could not find class '" + className
217: + "'", e);
218: throw new ValueObjectException(e);
219: }
220: ValueObject valueObject;
221: try {
222: valueObject = (ValueObject) valueObjectClass.newInstance();
223: } catch (InstantiationException e) {
224: logger.error("execute - could not instantiate "
225: + valueObjectClass, e);
226: throw new ValueObjectException(e, valueObjectClass);
227: } catch (IllegalAccessException e) {
228: logger.error("execute - could not instantiate "
229: + valueObjectClass, e);
230: throw new ValueObjectException(e, valueObjectClass);
231: }
232: // default the base class name to the class name: if you want, they
233: // can be different, but the class must be a subclass of the base
234: // class
235: if (baseClassName == null) {
236: baseClassName = className;
237: }
238: Class baseClass;
239: try {
240: baseClass = Class.forName(baseClassName);
241: } catch (ClassNotFoundException e) {
242: logger.error("execute - could not create class '"
243: + baseClassName + "'", e);
244: throw new ValueObjectException(e);
245: }
246: Mask mask = maskFactory.getMask(baseClass, getInputMask(
247: request, form));
248: InputMaskForm inputMaskForm;
249: if (form instanceof InputMaskForm) {
250: inputMaskForm = (InputMaskForm) form;
251: } else {
252: inputMaskForm = createInputMaskForm(request, valueObject,
253: mask, baseClass);
254: }
255: request.setAttribute(InputMaskForm.REQUEST_ATTRIBUTE,
256: inputMaskForm);
257:
258: if (logger.isDebugEnabled()) {
259: logger.debug("execute - end - return value = success");
260: }
261: return "success";
262: }
263: }
|