1
2
3
4
5
6
7
8
9
10
11
12 package fr.hyphonem.conges.actions;
13
14 import java.io.IOException;
15 import java.text.DecimalFormat;
16 import java.text.SimpleDateFormat;
17 import java.util.Calendar;
18 import java.util.Date;
19 import java.util.Enumeration;
20 import java.util.Hashtable;
21 import java.util.Iterator;
22 import java.util.Vector;
23
24 import javax.servlet.ServletException;
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpServletResponse;
27 import javax.servlet.http.HttpSession;
28
29 import org.apache.struts.action.Action;
30 import org.apache.struts.action.ActionError;
31 import org.apache.struts.action.ActionErrors;
32 import org.apache.struts.action.ActionForm;
33 import org.apache.struts.action.ActionForward;
34 import org.apache.struts.action.ActionMapping;
35
36 import fr.hyphonem.conges.AccessData;
37 import fr.hyphonem.conges.AccessDataMng;
38 import fr.hyphonem.conges.CommonCal;
39 import fr.hyphonem.conges.FeryDays;
40 import fr.hyphonem.conges.data.CalendarData;
41 import fr.hyphonem.conges.data.EmployeeData;
42 import fr.hyphonem.conges.data.PendingEventData;
43 import fr.hyphonem.conges.data.StatsData;
44 import fr.hyphonem.conges.forms.AbsForm;
45
46
47
48
49
50 public class ValidateEvtAction extends Action {
51
52
53
54
55 public ValidateEvtAction() {
56 super();
57 }
58
59
60
61
62
63
64
65
66
67 public ActionForward execute(ActionMapping mapping, ActionForm form,
68 HttpServletRequest request, HttpServletResponse response)
69 throws Exception {
70 HttpSession session = request.getSession();
71 if (session.getAttribute("user") == null) {
72 return mapping.findForward("invalidsession");
73 }
74
75 ActionErrors errors = new ActionErrors();
76 EmployeeData employee = (EmployeeData) session.getAttribute("employee");
77 AbsForm absForm = (AbsForm) form;
78
79 String nextID = (String) session.getServletContext().getAttribute(
80 "nextID");
81 int iid = 1;
82 try {
83 iid = new Integer(nextID).intValue();
84 } catch (NumberFormatException e) {
85 iid = 1;
86 }
87
88 String datedeb = absForm.getDatedeb();
89 String datefin = absForm.getDatefin();
90
91
92 SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
93
94 String entrydate = "";
95 Date datedeb_d = sdf.parse(datedeb);
96 Date datefin_d = sdf.parse(datefin);
97 Date entryDate_d = null;
98 if (employee.getEntryDate() != null
99 && !employee.getEntryDate().equals("")) {
100 entryDate_d = sdf.parse(employee.getEntryDate());
101 entrydate = employee.getEntryDate();
102 }
103
104 if (entryDate_d != null) {
105 if (datedeb_d.before(entryDate_d) && datefin_d.before(entryDate_d)) {
106 errors.add("error.abs.before.entry.date", new ActionError(
107 "error.abs.before.entry.date"));
108 } else if (datedeb_d.before(entryDate_d)
109 && datefin_d.after(entryDate_d)) {
110 datedeb = entrydate;
111 }
112 }
113
114 if (!errors.isEmpty()) {
115 saveErrors(request, errors);
116 return mapping.getInputForward();
117 }
118
119
120 String fromDate = (String) session.getAttribute("debPer");
121 String toDate = (String) session.getAttribute("finPer");
122
123 Date fromDate_d = sdf.parse(fromDate);
124 Date toDate_d = sdf.parse(toDate);
125 if (datedeb_d.before(fromDate_d)) {
126 errors.add("error.event.begin.date.before.diplayed.calendar",
127 new ActionError(
128 "error.event.begin.date.before.diplayed.calendar"));
129 }
130 if (datedeb_d.after(toDate_d)) {
131 errors.add("error.event.begin.date.after.diplayed.calendar",
132 new ActionError(
133 "error.event.begin.date.after.diplayed.calendar"));
134 }
135 if (datefin_d.before(fromDate_d)) {
136 errors.add("error.event.end.date.before.diplayed.calendar",
137 new ActionError(
138 "error.event.end.date.before.diplayed.calendar"));
139 }
140 if (datefin_d.after(toDate_d)) {
141 errors.add("error.event.end.date.after.diplayed.calendar",
142 new ActionError(
143 "error.event.end.date.after.diplayed.calendar"));
144 }
145
146 if (!errors.isEmpty()) {
147 saveErrors(request, errors);
148 return mapping.getInputForward();
149 }
150
151 Vector vCalendarData = new Vector();
152
153
154 String codePeriod = absForm.getPeriod();
155 String periodEndDate = absForm.getPerioddatefin();
156 if (codePeriod.equalsIgnoreCase("none")) {
157
158 CalendarData ca = new CalendarData();
159 ca.setId("" + iid);
160 ca.setCbaprem(absForm.isCbaprem());
161 ca.setCbmatin(absForm.isCbmatin());
162 ca.setDatedeb(datedeb);
163 ca.setDatefin(datefin);
164 ca.setRaison(absForm.getRaison());
165 ca.setEmployee(employee);
166 vCalendarData.add(ca);
167 } else {
168 Date periodEndDate_d = sdf.parse(periodEndDate);
169 if (periodEndDate_d.before(fromDate_d)) {
170 errors
171 .add(
172 "error.periodic.end.date.before.diplayed.calendar",
173 new ActionError(
174 "error.periodic.end.date.before.diplayed.calendar"));
175 }
176 if (periodEndDate_d.after(toDate_d)) {
177 errors
178 .add(
179 "error.periodic.end.date.after.diplayed.calendar",
180 new ActionError(
181 "error.periodic.end.date.after.diplayed.calendar"));
182 }
183
184 if (!errors.isEmpty()) {
185 saveErrors(request, errors);
186 return mapping.getInputForward();
187 }
188
189 Calendar cal_periodEndDate = Calendar.getInstance();
190 cal_periodEndDate.setTime(periodEndDate_d);
191
192
193
194 Calendar cal_beginDate = Calendar.getInstance();
195 cal_beginDate.setTime(datedeb_d);
196 Calendar cal_endDate = Calendar.getInstance();
197 cal_endDate.setTime(datefin_d);
198
199 int add_one_to_iid = 0;
200
201 while (cal_beginDate.before(cal_periodEndDate)
202 && cal_endDate.before(cal_periodEndDate)) {
203 int iid_periodic = iid + add_one_to_iid;
204
205 CalendarData ca = new CalendarData();
206 ca.setId("" + iid_periodic);
207 ca.setCbaprem(absForm.isCbaprem());
208 ca.setCbmatin(absForm.isCbmatin());
209 ca.setDatedeb(sdf.format(cal_beginDate.getTime()));
210 ca.setDatefin(sdf.format(cal_endDate.getTime()));
211 ca.setRaison(absForm.getRaison());
212 ca.setEmployee(employee);
213 vCalendarData.add(ca);
214 cal_beginDate.add(Calendar.DAY_OF_MONTH,
215 new Integer(codePeriod).intValue());
216 cal_endDate.add(Calendar.DAY_OF_MONTH, new Integer(codePeriod)
217 .intValue());
218 add_one_to_iid++;
219 }
220 }
221
222 CommonCal commonCal = new CommonCal();
223
224 String codePays = (String) session.getServletContext().getAttribute(
225 "country");
226 if (codePays == null) {
227 codePays = "";
228 }
229 String dataDirPath = (String) session.getAttribute("dataDirPath");
230 Hashtable hJF = new FeryDays().getNationalDays(fromDate, toDate,
231 codePays, dataDirPath);
232
233 AccessData ad = new AccessDataMng().getInstance(dataDirPath);
234
235
236 String nextPEID = (String) session.getServletContext().getAttribute(
237 "nextPEID");
238 int id_i = 1;
239 try {
240 id_i = new Integer(nextPEID).intValue();
241 } catch (NumberFormatException e) {
242 id_i = 1;
243 }
244
245 Vector lstCalendarData = commonCal.getDiscretCalData(vCalendarData,
246 employee, hJF);
247
248
249
250 Vector vLstCal = (Vector) session.getAttribute("lstCalendarData");
251 for (int i = 0; i < lstCalendarData.size(); i++) {
252 CalendarData cal = (CalendarData) lstCalendarData.get(i);
253 for (int j = 0; j < vLstCal.size(); j++) {
254 CalendarData presentAbs = (CalendarData) vLstCal.get(j);
255 if (presentAbs.getDatedeb().equalsIgnoreCase(cal.getDatedeb())
256 && presentAbs.getDatefin().equalsIgnoreCase(
257 cal.getDatefin())
258 && (presentAbs.isCbaprem() == cal.isCbaprem() || presentAbs
259 .isCbmatin() == cal.isCbmatin())
260 && presentAbs.getEmployee().getNom().equalsIgnoreCase(
261 cal.getEmployee().getNom())
262 && presentAbs
263 .getEmployee()
264 .getPrenom()
265 .equalsIgnoreCase(cal.getEmployee().getPrenom())) {
266 errors.add("error.abs.already.exists", new ActionError(
267 "error.abs.already.exists",
268 new String[] { presentAbs.toString() }));
269 }
270 }
271 }
272
273 if (!errors.isEmpty()) {
274 saveErrors(request, errors);
275 return mapping.getInputForward();
276 }
277
278
279 Vector stats = (Vector) session.getAttribute("stats");
280 Iterator iter = stats.iterator();
281 int j = -1;
282 while (iter.hasNext()) {
283 j++;
284 StatsData stat = (StatsData) iter.next();
285 if (stat.getRaison().equals(absForm.getRaison())) {
286 String nbjours = stat.getNbJours();
287 String maxdays = stat.getMaxNBJours();
288
289 double nbj = 0.0;
290 for (Enumeration enumeration = lstCalendarData.elements(); enumeration
291 .hasMoreElements();) {
292 CalendarData cal = (CalendarData) enumeration.nextElement();
293 if (cal.isCbmatin()) {
294 nbj += 0.5;
295 }
296 if (cal.isCbaprem()) {
297 nbj += 0.5;
298 }
299 }
300 double nbtot = new Double(nbjours).doubleValue() + nbj;
301 DecimalFormat df = new DecimalFormat("#,##");
302
303 double maxdays_d = df.parse(maxdays).doubleValue();
304
305 if (nbtot > maxdays_d) {
306 double delta = nbtot - maxdays_d;
307 df = new DecimalFormat("#.##");
308 String s_delta = df.format(delta);
309 errors.add("error.too.much.abs", new ActionError(
310 "error.too.much.abs", new String[] { maxdays,
311 s_delta }));
312 } else {
313 stat.setNbJours("" + nbtot);
314 stats.remove(j);
315 stats.add(stat);
316 session.setAttribute("stats", stats);
317 }
318 break;
319 }
320 }
321
322 if (!errors.isEmpty()) {
323 saveErrors(request, errors);
324 return mapping.getInputForward();
325 }
326
327
328 try {
329 int iterate_id_ped = 0;
330 for (Iterator iterator = vCalendarData.iterator(); iterator
331 .hasNext();) {
332 CalendarData element = (CalendarData) iterator.next();
333 int id_ped = id_i + iterate_id_ped;
334 ad.writePendingEvent("" + id_ped, element, employee, "C",
335 codePays, dataDirPath);
336 iterate_id_ped++;
337 }
338
339 int nextid_i = id_i + iterate_id_ped;
340 session.getServletContext().setAttribute("nextPEID", "" + nextid_i);
341 } catch (IOException e) {
342 errors.add("error.concurrent.access", new ActionError(
343 "error.concurrent.access"));
344 } catch (Exception e) {
345 throw new ServletException(e);
346 }
347
348 if (!errors.isEmpty()) {
349 saveErrors(request, errors);
350 return mapping.getInputForward();
351 }
352
353 for (int i = 0; i < lstCalendarData.size(); i++) {
354 CalendarData cal = (CalendarData) lstCalendarData.get(i);
355 try {
356 ad.writeCalendarData(cal, false);
357 vLstCal.add(cal);
358 if (i == lstCalendarData.size() - 1) {
359 int newID = new Integer(cal.getId()).intValue() + 1;
360 session.getServletContext().setAttribute("nextID",
361 "" + newID);
362 }
363 } catch (IOException e) {
364 errors.add("error.concurrent.access", new ActionError(
365 "error.concurrent.access"));
366 } catch (Exception e) {
367 throw new ServletException(e);
368 }
369 }
370
371 session.setAttribute("lstCalendarData", vLstCal);
372 PendingEventData ped = ad.readPendingEvent("" + id_i);
373 commonCal.sendEmail(ped, employee, request, session, ad, 0);
374
375 if (absForm.isEdition()) {
376 request.setAttribute("raison", absForm.getRaison());
377 request.setAttribute("debDate", absForm.getDatedeb());
378 request.setAttribute("finDate", absForm.getDatefin());
379 String matin = "";
380 String aprem = "";
381 if (absForm.isCbmatin()) {
382 matin = "X";
383 }
384 if (absForm.isCbaprem()) {
385 aprem = "X";
386 }
387
388 String nbJours = "";
389 double nbj = 0.0;
390 for (Enumeration enumeration = lstCalendarData.elements(); enumeration
391 .hasMoreElements();) {
392 CalendarData cal = (CalendarData) enumeration.nextElement();
393 if (cal.isCbmatin()) {
394 nbj += 0.5;
395 }
396 if (cal.isCbaprem()) {
397 nbj += 0.5;
398 }
399 }
400 if (nbj != 0.0) {
401 nbJours = "" + nbj;
402 }
403
404 request.setAttribute("matin", matin);
405 request.setAttribute("aprem", aprem);
406 request.setAttribute("nbjours", nbJours);
407 }
408
409 if (!errors.isEmpty()) {
410 saveErrors(request, errors);
411 return mapping.getInputForward();
412 }
413
414 return mapping.findForward("success");
415 }
416 }