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
67
68
69
70
71
72
73
74
75
76
77
78
79
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
224
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
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