View Javadoc

1   /*Copyright (C) 2004-... Stephane Gauchet for Hyphonem
2   
3    This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
4   
5    This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
6   
7    You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
8    
9    Written by St�phane Gauchet
10   mail me at : sgauchet@free.fr
11   */
12  package fr.hyphonem.conges.forms;
13  
14  import java.text.ParseException;
15  import java.text.SimpleDateFormat;
16  import java.util.logging.Logger;
17  
18  import javax.servlet.http.HttpServletRequest;
19  
20  import org.apache.struts.action.ActionError;
21  import org.apache.struts.action.ActionErrors;
22  import org.apache.struts.action.ActionForm;
23  import org.apache.struts.action.ActionMapping;
24  
25  /**
26   * @author Stephane Gauchet pour Hyphonem
27   * 
28   */
29  public class AbsForm extends ActionForm {
30  	/**
31  	 * 
32  	 */
33  	private static final long serialVersionUID = 1L;
34  
35  	private String id;
36  
37  	private String datedeb;
38  
39  	private String datefin;
40  
41  	private String raison;
42  
43  	private boolean cbmatin;
44  
45  	private boolean cbaprem;
46  
47  	private boolean delete;
48  
49  	private boolean edition;
50  
51  	private String period;
52  
53  	private String perioddatefin;
54  
55  	public String getPerioddatefin() {
56  		return perioddatefin;
57  	}
58  
59  	public void setPerioddatefin(String perioddatefin) {
60  		this.perioddatefin = perioddatefin;
61  	}
62  
63  	/**
64  	 * Constructor for AbsForm.
65  	 */
66  	public AbsForm() {
67  		super();
68  		resetFields();
69  	}
70  
71  	/**
72  	 * Reset all properties to their default values.
73  	 * 
74  	 * @param mapping
75  	 *            The mapping used to select this instance
76  	 * @param request
77  	 *            The servlet request we are processing
78  	 */
79  	public void reset(ActionMapping mapping, HttpServletRequest request) {
80  		resetFields();
81  
82  	}
83  
84  	private void resetFields() {
85  		id = "";
86  		datedeb = "";
87  		datefin = "";
88  		raison = "";
89  		period = "none";
90  		perioddatefin = "";
91  		cbmatin = false;
92  		cbaprem = false;
93  		delete = false;
94  		edition = false;
95  	}
96  
97  	/**
98  	 * Validate the properties that have been set from this HTTP request, and
99  	 * return an <code>ActionErrors</code> object that encapsulates any
100 	 * validation errors that have been found. If no errors are found, return
101 	 * <code>null</code> or an <code>ActionErrors</code> object with no
102 	 * recorded error messages.
103 	 * 
104 	 * @param mapping
105 	 *            The mapping used to select this instance
106 	 * @param request
107 	 *            The servlet request we are processing
108 	 * @return ActionErrors
109 	 */
110 	public ActionErrors validate(ActionMapping mapping,
111 			HttpServletRequest request) {
112 		ActionErrors errors = new ActionErrors();
113 
114 		if (datedeb == null || datedeb.trim().equals("")) {
115 			errors.add("begin.date.required", new ActionError(
116 					"begin.date.required"));
117 		}
118 
119 		if (datefin == null || datefin.trim().equals("")) {
120 			errors.add("end.date.required",
121 					new ActionError("end.date.required"));
122 		}
123 
124 		if (!cbmatin && !cbaprem) {
125 			errors.add("one.of.checkbox.required", new ActionError(
126 					"one.of.checkbox.required"));
127 		}
128 		// test sur le contenu des champs
129 		// on teste les dates via SimpleDateFormat
130 		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
131 
132 		if (!period.equalsIgnoreCase("none")) {
133 			if (perioddatefin == null || perioddatefin.trim().equals("")) {
134 				errors.add("error.period.end.date.not.fill", new ActionError(
135 						"error.period.end.date.not.fill"));
136 			} else if (perioddatefin != null
137 					&& !perioddatefin.trim().equals("")) {
138 				try {
139 					sdf.parse(perioddatefin);
140 				} catch (ParseException pe) {
141 					errors.add("error.date.format", new ActionError(
142 							"error.date.format", "perioddatefin"));
143 				}
144 			}
145 		}
146 
147 		try {
148 			if (datedeb != null && !datedeb.trim().equals("")) {
149 				sdf.parse(datedeb);
150 			}
151 		} catch (ParseException pe) {
152 			errors.add("error.date.format", new ActionError(
153 					"error.date.format", "datedeb"));
154 		}
155 
156 		try {
157 			if (datefin != null && !datefin.trim().equals("")) {
158 				sdf.parse(datefin);
159 			}
160 		} catch (ParseException pe) {
161 			errors.add("error.date.format", new ActionError(
162 					"error.date.format", "datefin"));
163 		}
164 
165 		try {
166 			if (datedeb != null && !datedeb.trim().equals("")
167 					&& datefin != null && !datefin.trim().equals("")
168 					&& sdf.parse(datedeb).after(sdf.parse(datefin))) {
169 				errors.add("error.end.date.before.begin.date", new ActionError(
170 						"error.end.date.before.begin.date"));
171 			}
172 		} catch (ParseException pe) {
173 			Logger.getAnonymousLogger().info(pe.getMessage());
174 		}
175 
176 		request.setAttribute("absForm", this);
177 		return errors;
178 	}
179 
180 	/**
181 	 * Returns the datedeb.
182 	 * 
183 	 * @return datedeb
184 	 */
185 	public String getDatedeb() {
186 		return datedeb;
187 	}
188 
189 	/**
190 	 * Returns the datefin.
191 	 * 
192 	 * @return datefin
193 	 */
194 	public String getDatefin() {
195 		return datefin;
196 	}
197 
198 	/**
199 	 * Returns the raison.
200 	 * 
201 	 * @return raison
202 	 */
203 	public String getRaison() {
204 		return raison;
205 	}
206 
207 	/**
208 	 * Sets the datedeb.
209 	 * 
210 	 * @param datedeb
211 	 *            The datedeb to set
212 	 */
213 	public void setDatedeb(String datedeb) {
214 		this.datedeb = datedeb;
215 	}
216 
217 	/**
218 	 * Sets the datefin.
219 	 * 
220 	 * @param datefin
221 	 *            The datefin to set
222 	 */
223 	public void setDatefin(String datefin) {
224 		this.datefin = datefin;
225 	}
226 
227 	/**
228 	 * Sets the raison.
229 	 * 
230 	 * @param raison
231 	 *            The raison to set
232 	 */
233 	public void setRaison(String raison) {
234 		this.raison = raison;
235 	}
236 
237 	/**
238 	 * Returns the cbaprem.
239 	 * 
240 	 * @return boolean
241 	 */
242 	public boolean isCbaprem() {
243 		return cbaprem;
244 	}
245 
246 	/**
247 	 * Returns the cbmatin.
248 	 * 
249 	 * @return boolean
250 	 */
251 	public boolean isCbmatin() {
252 		return cbmatin;
253 	}
254 
255 	/**
256 	 * Sets the cbaprem.
257 	 * 
258 	 * @param cbaprem
259 	 *            The cbaprem to set
260 	 */
261 	public void setCbaprem(boolean cbaprem) {
262 		this.cbaprem = cbaprem;
263 	}
264 
265 	/**
266 	 * Sets the cbmatin.
267 	 * 
268 	 * @param cbmatin
269 	 *            The cbmatin to set
270 	 */
271 	public void setCbmatin(boolean cbmatin) {
272 		this.cbmatin = cbmatin;
273 	}
274 
275 	/**
276 	 * @return boolean
277 	 */
278 	public boolean isDelete() {
279 		return delete;
280 	}
281 
282 	/**
283 	 * @param b
284 	 */
285 	public void setDelete(boolean b) {
286 		delete = b;
287 	}
288 
289 	/**
290 	 * @return boolean
291 	 */
292 	public boolean isEdition() {
293 		return edition;
294 	}
295 
296 	/**
297 	 * @param b
298 	 */
299 	public void setEdition(boolean b) {
300 		edition = b;
301 	}
302 
303 	public String getId() {
304 		return id;
305 	}
306 
307 	public void setId(String id) {
308 		this.id = id;
309 	}
310 
311 	public String getPeriod() {
312 		return period;
313 	}
314 
315 	public void setPeriod(String period) {
316 		this.period = period;
317 	}
318 
319 }