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.actions;
13  
14  import java.util.Iterator;
15  import java.util.Vector;
16  import java.util.logging.Logger;
17  
18  import javax.servlet.http.HttpServletRequest;
19  import javax.servlet.http.HttpServletResponse;
20  import javax.servlet.http.HttpSession;
21  
22  import org.apache.struts.action.Action;
23  import org.apache.struts.action.ActionError;
24  import org.apache.struts.action.ActionErrors;
25  import org.apache.struts.action.ActionForm;
26  import org.apache.struts.action.ActionForward;
27  import org.apache.struts.action.ActionMapping;
28  
29  import fr.hyphonem.conges.AccessData;
30  import fr.hyphonem.conges.AccessDataMng;
31  import fr.hyphonem.conges.CommonCal;
32  import fr.hyphonem.conges.data.CalendarData;
33  import fr.hyphonem.conges.data.EmployeeData;
34  import fr.hyphonem.conges.data.EmployeeIdCalData;
35  import fr.hyphonem.conges.data.PendingEventData;
36  import fr.hyphonem.conges.data.StatsData;
37  import fr.hyphonem.conges.data.UserData;
38  import fr.hyphonem.conges.forms.ValidatingForm;
39  
40  /**
41   * @author Stephane Gauchet pour Hyphonem
42   */
43  public class ValidValidatingAction extends Action {
44  
45  	public ActionForward execute(ActionMapping mapping, ActionForm form,
46  			HttpServletRequest request, HttpServletResponse response)
47  			throws Exception {
48  		if (request.getParameter("forward").toString().equals("accept")) {
49  			return accept(mapping, form, request, response);
50  		} else if (request.getParameter("forward").toString().equals("refuse")) {
51  			return refuse(mapping, form, request, response);
52  		} else {
53  			return mapping.getInputForward();
54  		}
55  	}
56  
57  	/**
58  	 * @param mapping
59  	 * @param form
60  	 * @param request
61  	 * @param response
62  	 * @return ActionForward
63  	 * @throws Exception
64  	 */
65  	public ActionForward accept(ActionMapping mapping, ActionForm form,
66  			HttpServletRequest request, HttpServletResponse response)
67  			throws Exception {
68  
69  		HttpSession session = request.getSession();
70  
71  		if (session.getAttribute("user") == null) {
72  			return mapping.findForward("invalidsession");
73  		}
74  
75  		UserData userd = (UserData) session.getAttribute("user");
76  		if (!userd.isModif() && userd.isEmployee()) {
77  			return mapping.findForward("invalidaction");
78  		}
79  
80  		ActionErrors errors = new ActionErrors();
81  		ActionForward forward = new ActionForward();
82  
83  		ValidatingForm vform = (ValidatingForm) form;
84  		String[] checks = vform.getCheckboxes();
85  		for (int i = 0; i < checks.length; i++) {
86  			String id = checks[i];
87  			String dataDirPath = (String) session.getAttribute("dataDirPath");
88  			AccessData ad = new AccessDataMng().getInstance(dataDirPath);
89  
90  			// lire le pendingEvent
91  			PendingEventData ped = ad.readPendingEvent(id);
92  			// supprimer le pendingEvent
93  			EmployeeIdCalData eid = ad.deletePendingEvent(id);
94  
95  			if (ped.getType().equalsIgnoreCase("C")) {
96  				// on ne fait rien car les données sont déjà stockées dans le
97  				// fichier
98  				Logger.getAnonymousLogger()
99  						.info("data are already in the file");
100 			} else {
101 				// on est dans le cas M ou D
102 
103 				// il faut modifier les données avec celle du ped
104 				Vector stats = (Vector) session.getAttribute("stats");
105 
106 				CalendarData cda = new CalendarData();
107 				// on modifie les données du calendarData avec celles du
108 				// pendingEvent
109 				cda.setCbaprem(ped.isCbaprem());
110 				cda.setCbmatin(ped.isCbmatin());
111 				cda.setId(ped.getIdcal());
112 				cda.setRaison(ped.getRaison());
113 				cda.setDatedeb(ped.getDatedeb());
114 				cda.setDatefin(ped.getDatefin());
115 				EmployeeData employee = new EmployeeData();
116 				employee.setNom(eid.getNom());
117 				employee.setPrenom(eid.getPrenom());
118 				cda.setEmployee(employee);
119 
120 				if (ped.getType().equalsIgnoreCase("M")) {
121 					// verifions si on ne dépasse le maximum de jours alloués.
122 
123 					Iterator iter1 = stats.iterator();
124 					int j = -1;
125 					while (iter1.hasNext()) {
126 						j++;
127 						StatsData stat = (StatsData) iter1.next();
128 						if (stat.getRaison().equals(ped.getRaison())) {
129 
130 							String nbjours = stat.getNbJours();
131 							String maxdays = stat.getMaxNBJours();
132 
133 							double nbj = 0.0;
134 
135 							// CalendarData cal =
136 							// (CalendarData)enumeration.nextElement();
137 							if (cda.isCbmatin()) {
138 								nbj += 0.5;
139 							}
140 							if (cda.isCbaprem()) {
141 								nbj += 0.5;
142 							}
143 
144 							double nbtot = new Double(nbjours).doubleValue()
145 									+ nbj;
146 							double maxdays_d = new Double(maxdays)
147 									.doubleValue();
148 
149 							if (nbtot > maxdays_d) {
150 								double delta = nbtot - maxdays_d;
151 								String s_delta = "" + delta;
152 								errors
153 										.add("error.too.much.abs",
154 												new ActionError(
155 														"error.too.much.abs",
156 														new String[] { maxdays,
157 																s_delta }));
158 							} else {
159 								stat.setNbJours("" + nbtot);
160 								stats.remove(j);
161 								stats.add(stat);
162 							}
163 							break;
164 						}
165 					}
166 
167 					if (!errors.isEmpty()) {
168 						saveErrors(request, errors);
169 						return mapping.getInputForward();
170 					}
171 
172 					// on créé le calendarData (modifié)
173 					ad.writeCalendarData(cda, false);
174 
175 					// supprimer l'evenement original du fichier temporaire, la
176 					// modification ayant été acceptée
177 					ad.deleteTemporarilyOriginalEvent(ped.getIdcal());
178 				}
179 				if (ped.getType().equalsIgnoreCase("D")) {
180 					// on supprime les calendarData
181 					// on ne fait rien car il a déjà été supprimer précédemment
182 					// supprimer l'evenement original du fichier temporaire, la
183 					// modification ayant été acceptée
184 					ad.deleteTemporarilyOriginalEvent(ped.getIdcal());
185 				}
186 				// session.setAttribute("lstCalendarData",ad.readCalendarData(cda.getEmployee()));
187 
188 				session.setAttribute("stats", stats);
189 			}
190 			// prévenir l'employé que son absence a été validée
191 			new CommonCal().sendEmailValidation(ped, eid, request, session, ad,
192 					0);
193 		}
194 
195 		if (!errors.isEmpty()) {
196 			saveErrors(request, errors);
197 
198 			// Forward control to the appropriate 'failure' URI (change name as
199 			// desired)
200 			forward = mapping.getInputForward();
201 		} else {
202 
203 			// Forward control to the appropriate 'success' URI (change name as
204 			// desired)
205 			forward = mapping.findForward("success");
206 		}
207 
208 		// Finish with
209 		return (forward);
210 	}
211 
212 	/**
213 	 * @param mapping
214 	 * @param form
215 	 * @param request
216 	 * @param response
217 	 * @return ActionForward
218 	 * @throws Exception
219 	 */
220 	public ActionForward refuse(ActionMapping mapping, ActionForm form,
221 			HttpServletRequest request, HttpServletResponse response)
222 			throws Exception {
223 
224 		HttpSession session = request.getSession();
225 
226 		if (session.getAttribute("user") == null) {
227 			return mapping.findForward("invalidsession");
228 		}
229 
230 		UserData userd = (UserData) session.getAttribute("user");
231 		if (!userd.isModif() && userd.isEmployee()) {
232 			return mapping.findForward("invalidaction");
233 		}
234 
235 		ActionErrors errors = new ActionErrors();
236 		ActionForward forward = new ActionForward();
237 
238 		ValidatingForm vform = (ValidatingForm) form;
239 		String[] checks = vform.getCheckboxes();
240 		String dataDirPath = (String) session.getAttribute("dataDirPath");
241 		AccessData ad = new AccessDataMng().getInstance(dataDirPath);
242 		for (int i = 0; i < checks.length; i++) {
243 			String id = checks[i];
244 
245 			// lire le pendingEvent
246 			PendingEventData ped = ad.readPendingEvent(id);
247 
248 			// supprimer le pendingEvent
249 			EmployeeIdCalData eid = ad.deletePendingEvent(id);
250 
251 			// si c'�tait une creation on supprime
252 			if (ped.getType().equalsIgnoreCase("C")) {
253 				// supprimer la ou les absence(s) créée(s)
254 				Vector cals = ad.searchCalendarData(eid);
255 				Iterator iter = cals.iterator();
256 				while (iter.hasNext()) {
257 					CalendarData cd = (CalendarData) iter.next();
258 					ad.writeCalendarData(cd, false, true);
259 				}
260 			} else {
261 				// on lit le fichier temporaire qui contient les evenements
262 				// originaux que l'on remet dans le fichier personnel
263 				CalendarData cal = ad.searchTemporarilyOriginalEvents(ped
264 						.getIdcal());
265 				ad.writeCalendarData(cal, false);
266 
267 				// puis on supprime les events du fichier temporaire
268 				ad.deleteTemporarilyOriginalEvent(ped.getIdcal());
269 			}
270 			// pr�venir l'employ� que son absence a �t� refus�e
271 			new CommonCal().sendEmailValidation(ped, eid, request, session, ad,
272 					1);
273 		}
274 
275 		if (!errors.isEmpty()) {
276 			saveErrors(request, errors);
277 
278 			// Forward control to the appropriate 'failure' URI (change name as
279 			// desired)
280 			forward = mapping.getInputForward();
281 
282 		} else {
283 
284 			// Forward control to the appropriate 'success' URI (change name as
285 			// desired)
286 			forward = mapping.findForward("success");
287 
288 		}
289 
290 		// Finish with
291 		return (forward);
292 	}
293 }