1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
184
185
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
205 session.setAttribute(InputMaskForm.REQUEST_ATTRIBUTE, inputMaskForm);
206 return "success";
207 }
208 }
209