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 package com.ivata.mask.web.struts;
72 import javax.servlet.http.HttpServletRequest;
73 import javax.servlet.http.HttpServletResponse;
74 import javax.servlet.http.HttpSession;
75
76 import org.apache.log4j.Logger;
77 import org.apache.struts.action.ActionErrors;
78 import org.apache.struts.action.ActionForm;
79 import org.apache.struts.action.ActionMapping;
80 import com.ivata.mask.Mask;
81 import com.ivata.mask.MaskFactory;
82 import com.ivata.mask.persistence.FinderException;
83 import com.ivata.mask.persistence.PersistenceManager;
84 import com.ivata.mask.persistence.PersistenceSession;
85 import com.ivata.mask.util.SystemException;
86 import com.ivata.mask.util.StringHandling;
87 import com.ivata.mask.valueobject.ValueObject;
88 /***
89 * <p>
90 * View a value object in a mask for display or deletion.
91 * </p>
92 *
93 * @since ivata masks 0.1 (2004-05-10)
94 * @author Colin MacLeod
95 * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
96 * @version $Revision: 1.8.2.1 $
97 */
98 public class FindAction extends MaskAction {
99 /***
100 * Refer to {@link Logger}.
101 */
102 private static Logger log = Logger.getLogger(FindAction.class);
103 /***
104 * Class of value object we are to find.
105 */
106 private String baseClassName;
107 /***
108 * <code>true</code> if we should only display the value object (rather than
109 * edit it).
110 */
111 private Boolean displayOnly;
112 /***
113 * Unique identifier of the value object.
114 */
115 private String idString;
116 /***
117 * <p>
118 * This factory is needed to access the masks and groups of masks.
119 * </p>
120 */
121 private MaskFactory maskFactory;
122 /***
123 * <p>
124 * Used to locate the value objects by their unique identifier.
125 * </p>
126 */
127 private PersistenceManager persistenceManager;
128 /***
129 * <p>
130 * Create a new AddAction with the given value object locator.
131 * </p>
132 *
133 * @param persistenceManagerParam
134 * used to locate the value objects by their unique identifier.
135 * @param maskFactoryParam
136 * This factory is needed to access the masks and groups of
137 * masks.
138 * @param authenticatorParam
139 * used to confirm whether or not the user should be allowed to
140 * continue, in the <code>execute</code> method.
141 */
142 public FindAction(final PersistenceManager persistenceManagerParam,
143 final MaskFactory maskFactoryParam,
144 final MaskAuthenticator authenticatorParam) {
145 super(maskFactoryParam, authenticatorParam);
146 this.maskFactory = maskFactoryParam;
147 this.persistenceManager = persistenceManagerParam;
148 }
149 /***
150 * <p>
151 * Override this method to create a different class of input mask form.
152 * </p>
153 *
154 * @param requestParam
155 * request we are processing.
156 * @param valueObjectParam
157 * value object to be displayed/edited.
158 * @param maskParam
159 * mask to be edited.
160 * @param baseClassParam
161 * base class of all value objects to show in the list or
162 * associated with this mask.
163 * @since ivata op (0.10) (2004-12-31)
164 * @return new instance of <code>InputMaskForm</code>.
165 */
166 protected InputMaskForm createInputMaskForm(
167 final HttpServletRequest requestParam,
168 final ValueObject valueObjectParam, final Mask maskParam,
169 final Class baseClassParam) {
170 return new InputMaskForm(valueObjectParam, maskParam, baseClassParam);
171 }
172 /***
173 * <p>
174 * Generic method called by the <strong>Struts </strong> interface. Looks
175 * for a request parameter called "idString" and removes the value
176 * object with this id.
177 * </p>
178 *
179 * <p>
180 * Additionally, specifying a request parameter called "readOnly"
181 * to <code>true</code> will cause the mask form to be created in read
182 * only (display) mode.
183 * </p>
184 *
185 * <p>
186 * Refer to {@link com.ivata.mask.web.struts.MaskAction#execute}.
187 * </p>
188 *
189 * @param mapping
190 * Refer to {@link com.ivata.mask.web.struts.MaskAction#execute}.
191 * @param errors
192 * Refer to {@link com.ivata.mask.web.struts.MaskAction#execute}.
193 * @param form
194 * Refer to {@link com.ivata.mask.web.struts.MaskAction#execute}.
195 * @param request
196 * Refer to {@link com.ivata.mask.web.struts.MaskAction#execute}.
197 * @param response
198 * Refer to {@link com.ivata.mask.web.struts.MaskAction#execute}.
199 * @param session
200 * Refer to {@link com.ivata.mask.web.struts.MaskAction#execute}.
201 * @return Refer to {@link com.ivata.mask.web.struts.MaskAction#execute}.
202 * @throws SystemException
203 * Refer to {@link com.ivata.mask.web.struts.MaskAction#execute}.
204 */
205 public String execute(final ActionMapping mapping,
206 final ActionErrors errors, final ActionForm form,
207 final HttpServletRequest request,
208 final HttpServletResponse response, final HttpSession session)
209 throws SystemException {
210
211
212 String baseClassNameRequest = request.getParameter("baseClass");
213 if (baseClassNameRequest != null) {
214 baseClassName = baseClassNameRequest;
215 }
216 String idStringRequest = request.getParameter("idString");
217 if (idStringRequest != null) {
218 idString = idStringRequest;
219 }
220 String displayOnlyRequest = request.getParameter("displayOnly");
221 if ((displayOnlyRequest != null)
222 || (displayOnly == null)) {
223 displayOnly = new Boolean("true".equals(displayOnlyRequest));
224 }
225 if (idString == null) {
226 throw new ValueObjectException(
227 "You must specify a request parameter called 'idString'");
228 }
229 if (baseClassName == null) {
230 throw new ValueObjectException(
231 "You must specify a request parameter called 'baseClass'");
232 }
233 Class baseClass;
234 try {
235 baseClass = Class.forName(baseClassName);
236 } catch (ClassNotFoundException e) {
237 throw new ValueObjectException(e);
238 }
239 PersistenceSession persistenceSession = persistenceManager
240 .openSession();
241 try {
242 assert (!StringHandling.isNullOrEmpty(idString));
243 ValueObject valueObject;
244 try {
245 valueObject = persistenceManager.findByPrimaryKey(
246 persistenceSession, baseClass, idString);
247 } catch(FinderException e) {
248 valueObject = null;
249 }
250 if ((valueObject == null)
251 && log.isDebugEnabled()) {
252 log.debug("No value object found with id string '"
253 + idString
254 + "'");
255 }
256 Mask mask = maskFactory.getMask(baseClass, getInputMask(request,
257 form));
258 if (mask == null) {
259 throw new NullPointerException(
260 "ERROR in ViewAction: no mask for baseClass '"
261 + baseClass + "', type 'mask'");
262 }
263 InputMaskForm maskForm;
264 if (form instanceof InputMaskForm) {
265 maskForm = (InputMaskForm) form;
266 } else {
267 maskForm = createInputMaskForm(request, valueObject, mask,
268 baseClass);
269 }
270
271
272 if (displayOnly.booleanValue()) {
273 maskForm.setDisplayOnly(true);
274 }
275 request.setAttribute(InputMaskForm.REQUEST_ATTRIBUTE, maskForm);
276
277 session.setAttribute(InputMaskForm.REQUEST_ATTRIBUTE, maskForm);
278 return "success";
279 } finally {
280 persistenceSession.close();
281 }
282 }
283 /***
284 * Class of value object we are to find.
285 *
286 * @return Returns the baseClassName.
287 */
288 protected String getBaseClassName() {
289 return baseClassName;
290 }
291 /***
292 * <code>true</code> if we should only display the value object (rather than
293 * edit it).
294 *
295 * @return Returns the displayOnly.
296 */
297 protected Boolean getDisplayOnly() {
298 return displayOnly;
299 }
300 /***
301 * Unique identifier of the value object.
302 *
303 * @return Returns the idString.
304 */
305 protected String getIdString() {
306 return idString;
307 }
308 /***
309 * Class of value object we are to find.
310 *
311 * @param baseClassNameParam The baseClassName to set.
312 */
313 protected void setBaseClassName(String baseClassNameParam) {
314 if (log.isDebugEnabled()) {
315 log.debug("setBaseClassName before: '" + baseClassName
316 + "', after: '" + baseClassNameParam + "'");
317 }
318
319 baseClassName = baseClassNameParam;
320 }
321 /***
322 * <code>true</code> if we should only display the value object (rather than
323 * edit it).
324 *
325 * @param displayOnlyParam The displayOnly to set.
326 */
327 protected void setDisplayOnly(Boolean displayOnlyParam) {
328 if (log.isDebugEnabled()) {
329 log.debug("setDisplayOnly before: '" + displayOnly + "', after: '"
330 + displayOnlyParam + "'");
331 }
332
333 displayOnly = displayOnlyParam;
334 }
335 /***
336 * Unique identifier of the value object.
337 *
338 * @param idStringParam The idString to set.
339 */
340 protected void setIdString(String idStringParam) {
341 if (log.isDebugEnabled()) {
342 log.debug("setIdString before: '" + idString + "', after: '"
343 + idStringParam + "'");
344 }
345
346 idString = idStringParam;
347 }
348 }
349