1 /**
2 * Copyright 2005-2006 the original author or authors.
3 *
4 * Licensed under the Gnu General Pubic License, Version 2.0 (the
5 * "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.opensource.org/licenses/gpl-license.php
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the Gnu General Public License for more details.
14 */
15 package org.figure8.join.control;
16
17 import org.apache.struts.Globals;
18 import org.apache.struts.action.ActionForm;
19 import org.apache.struts.action.ActionErrors;
20 import org.apache.struts.action.ActionMapping;
21 import org.apache.struts.action.ActionMessage;
22 import org.apache.struts.util.MessageResources;
23
24 import javax.servlet.http.HttpServletRequest;
25
26 import java.io.Serializable;
27 /**
28 * An abstract ActinoForm class that all Join form classes should extend.
29 * This class implements Struts validate() method as a wrapper to a new
30 * doValidate() abstract method. This wrapper is intended to perform some
31 * pre-processing (such as retrieving the specified operation to invoke and
32 * initializing errors array) and providing some helpful methods (such as
33 * retrieving resource bundles and adding validation errors to array).
34 * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
35 * @version $Revision: 1.2 $
36 */
37 public abstract class JoinForm extends ActionForm implements Serializable{
38
39
40
41 /** Static memeber for keeping resources of default bundle. */
42 private static MessageResources msgResources = null;
43 /** Static memeber for keeping resources of GUI bundle. */
44 private static MessageResources guiResources = null;
45
46
47
48
49 /** The identifier of EntityObject associated */
50 private long id = 0;
51 /** The string representation of EntityObject identifier */
52 private String idStr = null;
53 /** The <code>ActionErrors</code> associated to this form validation. */
54 protected ActionErrors errors = null;
55
56
57
58
59 /**
60 * Abstract method that subclasses must implement. The real validation
61 * must be done here, according to the <b>operation</b> and using the
62 * helpful addActionError() methods.
63 * @param operation String representing the operation to invoke on Action
64 * @param mapping Mapping between forwards name and path for this action
65 * @param request The servlet container request wrapper
66 */
67 public abstract void doValidate(String operation, ActionMapping mapping, HttpServletRequest request);
68
69
70
71
72 /** @return Retrieve the default resource bundle. */
73 public MessageResources getMessageResources(){
74
75 if (msgResources == null)
76 msgResources = (MessageResources)servlet.getServletContext().getAttribute(Globals.MESSAGES_KEY);
77
78 return msgResources;
79 }
80 /** @return Retrieve the resource bundle containing GUI resources. */
81 public MessageResources getGuiMessageResources(){
82
83 if (guiResources == null)
84 guiResources = (MessageResources)servlet.getServletContext().getAttribute(JoinAction.GUI_KEY);
85
86 return guiResources;
87 }
88
89 /** @return Identifier of EntityObject associated with this form */
90 public long getId(){
91 return id;
92 }
93 /** @param id Identifier of EntityObject associated with this form */
94 public void setId(long id){
95 this.id = id;
96 this.idStr = String.valueOf(id);
97 }
98
99 /** @return Identifier of EntityObject associated with form (should be a long) */
100 public String getIdStr(){
101 return idStr;
102 }
103 /** @param idStr String representation of EntityObject identifier (must be a long) */
104 public void setIdStr(String idStr){
105 this.idStr = idStr;
106 }
107
108
109
110
111 /**
112 * Add an action validation error to current error array.
113 * @param errorType Key representing the error type into default resource bundle
114 * @param param1 Parameter to inject into error type label
115 */
116 protected void addActionError(String errorType, String param1){
117
118 ActionMessage msg = new ActionMessage(errorType, param1);
119 errors.add(ActionErrors.GLOBAL_MESSAGE, msg);
120 }
121 /**
122 * Add an action validation error to current error array.
123 * @param errorType Key representing the error type into default resource bundle
124 * @param param1 Parameter to inject into error type label
125 * @param param2 Parameter to inject into error type label
126 */
127 protected void addActionError(String errorType, String param1, String param2){
128
129 ActionMessage msg = new ActionMessage(errorType, param1, param2);
130 errors.add(ActionErrors.GLOBAL_MESSAGE, msg);
131 }
132 /**
133 * Add an action validation error to current error array.
134 * @param errorType Key representing the error type into default resource bundle
135 * @param param1 Parameter to inject into error type label
136 * @param param2 Parameter to inject into error type label
137 * @param param3 Parameter to inject into error type label
138 */
139 protected void addActionError(String errorType, String param1, String param2, String param3){
140
141 ActionMessage msg = new ActionMessage(errorType, param1, param2, param3);
142 errors.add(ActionErrors.GLOBAL_MESSAGE, msg);
143 }
144
145 /**
146 * Validate the identifier of {@link org.figure8.join.core.EntityObject} associated
147 * with this form. This takes place in 2 step : ensure that string representation of
148 * if is not null nor empty, ensure that it represents a long value.
149 */
150 protected void validateEntityObjectId(){
151
152 if (idStr == null || idStr.length() == 0)
153 addActionError("errors.required", getGuiMessageResources().getMessage("label.global.id"));
154 else{
155
156 try {id = Long.valueOf(idStr).longValue();}
157 catch (Exception e){
158 addActionError("errors.range", getGuiMessageResources().getMessage("label.global.id"),
159 "1", String.valueOf(Long.MAX_VALUE));
160 }
161 }
162 }
163
164
165
166 /**
167 * Validation of the form attributes. This method acts as a wrapper around
168 * doValidate() abstract method. First, it initializes ActionErrors and
169 * retrieves operation to invoke onto Action (if any). Then, it calls
170 * doValidate() passing around this operation. Lastly, it returns ActionErros
171 * that has been completed by doValidate() implementation.
172 * @param mapping Mapping between forwards name and path for this action
173 * @param request The servlet container request wrapper
174 * @return The ActionErrors list raised during validation
175 */
176 public ActionErrors validate(ActionMapping mapping, HttpServletRequest request){
177
178 errors = new ActionErrors();
179
180 String operation = request.getParameter(JoinAction.OP_PARAMETER);
181
182 doValidate(operation, mapping, request);
183
184 return errors;
185 }
186
187 /**
188 * Reset form attributes.
189 * @param mapping Mapping between forwards name and path for this action
190 * @param request The servlet container request wrapper
191 */
192 public void reset(ActionMapping mapping, HttpServletRequest request){
193 id = 0;
194 idStr = null;
195 idStr = null;
196 }
197 }