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.BufferedReader;
15  import java.io.File;
16  import java.io.FileInputStream;
17  import java.io.InputStreamReader;
18  import java.util.Calendar;
19  import java.util.Hashtable;
20  import java.util.Iterator;
21  import java.util.Vector;
22  import java.util.logging.Logger;
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.CheckData;
39  import fr.hyphonem.conges.data.EmployeeData;
40  import fr.hyphonem.conges.data.ParamsData;
41  import fr.hyphonem.conges.data.ReasonData;
42  import fr.hyphonem.conges.data.UserData;
43  import fr.hyphonem.conges.forms.LoginForm;
44  
45  /**
46   * @author Stephane Gauchet pour Hyphonem
47   */
48  public class LoginAction extends Action {
49  
50  	public ActionForward execute(ActionMapping mapping, ActionForm form,
51  			HttpServletRequest request, HttpServletResponse response)
52  			throws Exception {
53  		HttpSession session = request.getSession();
54  		String dataDirPath = session.getServletContext().getRealPath(
55  				"WEB-INF/data");
56  		// put in session the real path to all personal and app data, will be
57  		// used in the whole session app
58  		session.setAttribute("dataDirPath", dataDirPath);
59  		AccessData ad = new AccessDataMng().getInstance(dataDirPath);
60  
61  		ActionErrors errors = new ActionErrors();
62  		LoginForm loginForm = (LoginForm) form;
63  		UserData user = null;
64  
65  		// verification du couple login/password entre
66  		String login = loginForm.getLogin();
67  		String password = loginForm.getPassword();
68  
69  		// lecture du fichier XML des users
70  		try {
71  			user = ad.searchUser(login.toUpperCase(), password.toUpperCase());
72  		} catch (Exception e) {
73  			// System.out.println(e.getMessage());
74  			user = null;
75  		}
76  		if (user == null) {
77  			errors.add("login.or.passwd.invalid", new ActionError(
78  					"login.or.passwd.invalid"));
79  		} else {
80  			// user logge
81  			session.setAttribute("user", user);
82  
83  			// delete all previous planning images generated by this user and
84  			// export files of his team (or none) generated at least the day
85  			// before
86  			String path = request.getSession().getServletContext().getRealPath(
87  					File.separator);
88  			String endFileName = "_" + user.getNom() + ".jpeg";
89  			String partialExportName = ".csv";
90  			File file = new File(path);
91  			File[] lfiles = file.listFiles();
92  			for (int i = 0; i < lfiles.length; i++) {
93  				File fichier = lfiles[i];
94  				if (fichier.getName().endsWith(endFileName)) {
95  					fichier.delete();
96  				}
97  
98  				Calendar cal = Calendar.getInstance();
99  				cal.add(Calendar.DATE, -1);
100 				if (fichier.getName().endsWith(partialExportName)
101 						&& fichier.lastModified() <= cal.getTimeInMillis()) {
102 					fichier.delete();
103 				}
104 			}
105 
106 			// on charge la liste des employes
107 			Vector employees = ad.readEmployees();
108 
109 			// on cree un employe a partir du user logge
110 			EmployeeData employee = new EmployeeData();
111 			employee.setNom(user.getNom());
112 			employee.setPrenom(user.getPrenom());
113 			// employee.setPays((String)session.getServletContext().getAttribute("country"));
114 
115 			boolean isUserAlsoAnEmployee = false;
116 			int indexEmployee = 0;
117 
118 			// on recherche dans la liste des employes le user logge
119 			for (int i = 0; i < employees.size(); i++) {
120 				EmployeeData emp = (EmployeeData) employees.get(i);
121 
122 				if (emp.getNom().equals(employee.getNom())
123 						&& emp.getPrenom().equals(employee.getPrenom())) {
124 					isUserAlsoAnEmployee = true;
125 					indexEmployee = i;
126 					employee.setPays(emp.getPays());
127 					employee.setTeam(emp.getTeam());
128 					employee.setEntryDate(emp.getEntryDate());
129 					employee.setDayoff1(emp.isDayoff1());
130 					employee.setDayoff2(emp.isDayoff2());
131 					employee.setDayoff3(emp.isDayoff3());
132 					employee.setDayoff4(emp.isDayoff4());
133 					employee.setDayoff5(emp.isDayoff5());
134 					employee.setDayoff6(emp.isDayoff6());
135 					employee.setDayoff7(emp.isDayoff7());
136 				}
137 			}
138 
139 			if (isUserAlsoAnEmployee) {
140 				// employe par defaut = user logge
141 				Logger.getAnonymousLogger().info(
142 						"employee is set to logged user");
143 			} else {
144 				// employe par defaut = premier de la liste
145 				employee = new EmployeeData();
146 				employee = (EmployeeData) employees.get(0);
147 			}
148 
149 			session.setAttribute("indexEmployee", "" + indexEmployee);
150 			session.setAttribute("employee", employee);
151 
152 			errors = CheckData.check(request, employee, errors);
153 			if (errors.isEmpty()) {
154 				Vector params = (Vector) session.getServletContext()
155 						.getAttribute("params");
156 
157 				if (params == null) {
158 					params = new Vector();
159 
160 					File fileParams = new File(dataDirPath + File.separator
161 							+ "params.xml");
162 
163 					if (fileParams.exists()) {
164 						try {
165 							params = ad.readParams();
166 						} catch (Exception e) {
167 							throw new ServletException(e.getMessage());
168 						}
169 					} else {
170 						Vector reasons = (Vector) session.getServletContext()
171 								.getAttribute("reasons");
172 
173 						for (int z = 0; z < reasons.size(); z++) {
174 							ReasonData reason = (ReasonData) reasons.get(z);
175 							String reasonName = reason.getNom();
176 							ParamsData pd = new ParamsData();
177 							pd.setReason(reasonName);
178 							pd.setDateDebut("");
179 							pd.setDateFin("");
180 							pd.setNbJours("");
181 							pd.setType("progressif");
182 							params.add(pd);
183 						}
184 					}
185 
186 					session.getServletContext().setAttribute("params", params);
187 				}
188 
189 				if (session.getServletContext().getAttribute("country") == null) {
190 					String country = ad.readParamPays();
191 					session.getServletContext()
192 							.setAttribute("country", country);
193 				}
194 				if (session.getServletContext().getAttribute("server") == null) {
195 					String server = ad.readParamMail();
196 					session.getServletContext().setAttribute("server", server);
197 				}
198 
199 				if (session.getServletContext().getAttribute("periods") == null) {
200 					Vector periods = ad.readParamPeriods();
201 					session.getServletContext()
202 							.setAttribute("periods", periods);
203 				}
204 
205 				// on charge la liste des employes si elle n'est pas deja en
206 				// application
207 				if (session.getServletContext().getAttribute("employees") == null) {
208 					session.getServletContext().setAttribute("employees",
209 							employees);
210 				}
211 
212 				// on charge la liste des raisons d'absences si elle n'est pas
213 				// deja en application
214 				if (session.getServletContext().getAttribute("reasons") == null) {
215 					Vector reasons = ad.readReasons();
216 					session.getServletContext()
217 							.setAttribute("reasons", reasons);
218 				}
219 				if (session.getServletContext().getAttribute("teams") == null) {
220 					Iterator teams = ad.readTeams();
221 					Vector vteams = new Vector();
222 					while (teams.hasNext()) {
223 						String team = (String) teams.next();
224 						if (team != null && !team.equals("")) {
225 							vteams.add(team);
226 						}
227 					}
228 					session.getServletContext().setAttribute("teams", vteams);
229 				}
230 				if (session.getServletContext().getAttribute("hcountry") == null) {
231 					String pathToCountry = dataDirPath + File.separator
232 							+ "country.txt";
233 					File countryFile = new File(pathToCountry);
234 					Hashtable hCountry = new Hashtable();
235 
236 					if (countryFile.exists()) {
237 						FileInputStream fis = new FileInputStream(countryFile);
238 						BufferedReader dis = new BufferedReader(
239 								new InputStreamReader(fis));
240 						String ligne = dis.readLine();
241 						// 1ere ligne blanche
242 						hCountry.put("", "");
243 
244 						while (ligne != null) {
245 							String country = ligne.substring(0, 48);
246 							// System.out.println("country = "+country);
247 							String code = ligne.substring(48);
248 							// System.out.println("code = "+code);
249 							hCountry.put(code, country);
250 							ligne = dis.readLine();
251 						}
252 					}
253 
254 					session.getServletContext().setAttribute("hcountry",
255 							hCountry);
256 				}
257 				if (user.isModif()) {
258 					Vector listOfPendingEvents = ad.readPendingEvents(user,
259 							user.getTeam());
260 					if (listOfPendingEvents.size() > 0) {
261 						return mapping.findForward("validating");
262 					}
263 				}
264 			}
265 		}
266 
267 		if (!errors.isEmpty()) {
268 			saveErrors(request, errors);
269 			return mapping.getInputForward();
270 		}
271 
272 		return mapping.findForward("success");
273 	}
274 }