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 Stephane Gauchet
10   mail me at : sgauchet@free.fr
11   */
12  package fr.hyphonem.conges.actions;
13  
14  import java.util.Vector;
15  
16  import javax.servlet.http.HttpServletRequest;
17  import javax.servlet.http.HttpServletResponse;
18  import javax.servlet.http.HttpSession;
19  
20  import org.apache.struts.action.Action;
21  import org.apache.struts.action.ActionError;
22  import org.apache.struts.action.ActionErrors;
23  import org.apache.struts.action.ActionForm;
24  import org.apache.struts.action.ActionForward;
25  import org.apache.struts.action.ActionMapping;
26  
27  import fr.hyphonem.conges.AccessData;
28  import fr.hyphonem.conges.AccessDataMng;
29  import fr.hyphonem.conges.data.PeriodData;
30  import fr.hyphonem.conges.data.UserData;
31  import fr.hyphonem.conges.forms.ValidPeriodParamForm;
32  
33  /**
34   * @author Stephane Gauchet pour Hyphonem
35   */
36  public class ValidPeriodParamAction extends Action {
37  
38  	public ActionForward execute(ActionMapping mapping, ActionForm form,
39  			HttpServletRequest request, HttpServletResponse response)
40  			throws Exception {
41  		HttpSession session = request.getSession();
42  		if (session.getAttribute("user") == null) {
43  			return mapping.findForward("invalidsession");
44  		}
45  
46  		UserData userd = (UserData) session.getAttribute("user");
47  		// admin only
48  		if (userd.isModif() || userd.isEmployee()) {
49  			return mapping.findForward("invalidaction");
50  		}
51  		ActionErrors errors = new ActionErrors();
52  
53  		ValidPeriodParamForm vpd = (ValidPeriodParamForm) form;
54  
55  		Vector periods = (Vector) session.getServletContext().getAttribute(
56  				"periods");
57  
58  		for (int i = 0; i < periods.size(); i++) {
59  			PeriodData element = (PeriodData) periods.get(i);
60  			if (element.getCode().equalsIgnoreCase(vpd.getCode())) {
61  				ActionError error = new ActionError(
62  						"period.code.already.exists");
63  				errors.add("period.code.already.exists", error);
64  				break;
65  			}
66  		}
67  
68  		if (!errors.isEmpty()) {
69  			saveErrors(request, errors);
70  			return mapping.getInputForward();
71  		}
72  
73  		periods.add(new PeriodData(vpd.getCode(), vpd.getLabel()));
74  		// lecture du fichier XML des users
75  		String dataDirPath = (String) session.getAttribute("dataDirPath");
76  		AccessData ad = new AccessDataMng().getInstance(dataDirPath);
77  		try {
78  			ad.writePeriods(periods);
79  		} catch (Exception e) {
80  			e.printStackTrace();
81  		}
82  		session.getServletContext().setAttribute("periods", periods);
83  
84  		return mapping.findForward("success");
85  	}
86  }