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.io.IOException;
15  import java.util.Vector;
16  
17  import javax.servlet.ServletException;
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.data.UserData;
32  import fr.hyphonem.conges.forms.TeamForm;
33  
34  /**
35   * @author Stephane Gauchet pour Hyphonem
36   */
37  public class ValidTeamsAction extends Action {
38  
39  	/**
40  	 * 
41  	 */
42  	public ValidTeamsAction() {
43  		super();
44  	}
45  
46  	public ActionForward execute(ActionMapping mapping, ActionForm form,
47  			HttpServletRequest request, HttpServletResponse response)
48  			throws IOException, ServletException {
49  		HttpSession session = request.getSession();
50  		if (session.getAttribute("user") == null) {
51  			return mapping.findForward("invalidsession");
52  		}
53  
54  		UserData userd = (UserData) session.getAttribute("user");
55  		// admin only
56  		if (userd.isModif() || userd.isEmployee()) {
57  			return mapping.findForward("invalidaction");
58  		}
59  		ActionErrors errors = new ActionErrors();
60  		TeamForm mteam = (TeamForm) form;
61  
62  		String nom0 = mteam.getNomEquipe0();
63  		String oldnom0 = mteam.getOldNomEquipe0();
64  
65  		String nom1 = mteam.getNomEquipe1();
66  		String oldnom1 = mteam.getOldNomEquipe1();
67  
68  		String nom2 = mteam.getNomEquipe2();
69  		String oldnom2 = mteam.getOldNomEquipe2();
70  
71  		String nom3 = mteam.getNomEquipe3();
72  		String oldnom3 = mteam.getOldNomEquipe3();
73  
74  		String nom4 = mteam.getNomEquipe4();
75  		String oldnom4 = mteam.getOldNomEquipe4();
76  
77  		String nom5 = mteam.getNomEquipe5();
78  		String oldnom5 = mteam.getOldNomEquipe5();
79  
80  		String nom6 = mteam.getNomEquipe6();
81  		String oldnom6 = mteam.getOldNomEquipe6();
82  
83  		String nom7 = mteam.getNomEquipe7();
84  		String oldnom7 = mteam.getOldNomEquipe7();
85  
86  		Vector teams = new Vector();
87  		teams.add(nom0);
88  		teams.add(nom1);
89  		teams.add(nom2);
90  		teams.add(nom3);
91  		teams.add(nom4);
92  		teams.add(nom5);
93  		teams.add(nom6);
94  		teams.add(nom7);
95  
96  		session.getServletContext().setAttribute("teams", teams);
97  
98  		String dataDirPath = (String) session.getAttribute("dataDirPath");
99  		AccessData ad = new AccessDataMng().getInstance(dataDirPath);
100 		try {
101 			ad.writeTeams(teams);
102 		} catch (IOException e) {
103 			errors.add("error.concurrent.access", new ActionError(
104 					"error.concurrent.access"));
105 		} catch (Exception e) {
106 			throw new ServletException(e);
107 		}
108 
109 		Vector employees = (Vector) session.getServletContext().getAttribute(
110 				"employees");
111 		try {
112 			if (!nom0.equals(oldnom0)) {
113 				ad.replaceOldWithNewTeam(oldnom0, nom0, employees);
114 			}
115 			if (!nom1.equals(oldnom1)) {
116 				ad.replaceOldWithNewTeam(oldnom1, nom1, employees);
117 			}
118 			if (!nom2.equals(oldnom2)) {
119 				ad.replaceOldWithNewTeam(oldnom2, nom2, employees);
120 			}
121 			if (!nom3.equals(oldnom3)) {
122 				ad.replaceOldWithNewTeam(oldnom3, nom3, employees);
123 			}
124 			if (!nom4.equals(oldnom4)) {
125 				ad.replaceOldWithNewTeam(oldnom4, nom4, employees);
126 			}
127 			if (!nom5.equals(oldnom5)) {
128 				ad.replaceOldWithNewTeam(oldnom5, nom5, employees);
129 			}
130 			if (!nom6.equals(oldnom6)) {
131 				ad.replaceOldWithNewTeam(oldnom6, nom6, employees);
132 			}
133 			if (!nom7.equals(oldnom7)) {
134 				ad.replaceOldWithNewTeam(oldnom7, nom7, employees);
135 			}
136 		} catch (IOException e) {
137 			errors.add("error.concurrent.access", new ActionError(
138 					"error.concurrent.access"));
139 		} catch (Exception e) {
140 			throw new ServletException(e);
141 		}
142 
143 		if (!errors.isEmpty()) {
144 			saveErrors(request, errors);
145 			return mapping.getInputForward();
146 		}
147 		return mapping.findForward("success");
148 	}
149 }