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.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.EmployeeData;
32  import fr.hyphonem.conges.data.UserData;
33  import fr.hyphonem.conges.forms.CreateUserForm;
34  
35  /**
36   * @author Stephane Gauchet pour Hyphonem
37   * 
38   */
39  public class ValidateCreateUserAction extends Action {
40  	public ActionForward execute(ActionMapping mapping, ActionForm form,
41  			HttpServletRequest request, HttpServletResponse response)
42  			throws Exception {
43  		HttpSession session = request.getSession();
44  		if (session.getAttribute("user") == null) {
45  			return mapping.findForward("invalidsession");
46  		}
47  
48  		UserData userd = (UserData) session.getAttribute("user");
49  		if (!userd.isModif() && userd.isEmployee()) {
50  			return mapping.findForward("invalidaction");
51  		}
52  
53  		ActionErrors errors = new ActionErrors();
54  		CreateUserForm userForm = (CreateUserForm) form;
55  		UserData user = null;
56  		EmployeeData employee = null;
57  
58  		String login = userForm.getLoginacreer().toUpperCase();
59  		String password = userForm.getPasswordacreer().toUpperCase();
60  		String nom = userForm.getNom();
61  		String prenom = userForm.getPrenom();
62  		String email = userForm.getEmail();
63  		boolean isChief = userForm.isChief();
64  		boolean isEmployee = userForm.isEmployee();
65  		String pays = userForm.getPays();
66  		String team = userForm.getTeam();
67  		String entryDate = userForm.getEntryDate();
68  
69  		// l'une des deux checkbox est obligatoire
70  		if (!isEmployee && !isChief) {
71  			errors.add("one.checkbox.is.required", new ActionError(
72  					"one.checkbox.is.required"));
73  		} else {
74  			// lecture du fichier XML des users
75  			String dataDirPath = (String) session.getAttribute("dataDirPath");
76  			AccessData ad = new AccessDataMng().getInstance(dataDirPath);
77  			try {
78  				user = ad.searchUser(login, password);
79  			} catch (Exception e) {
80  				// System.out.println(e.getMessage());
81  				user = null;
82  			}
83  			try {
84  				// si le user existe deja on envoie une erreur
85  				if (user != null) {
86  					errors.add("user.already.exists", new ActionError(
87  							"user.already.exists"));
88  				} else {
89  					// on cree le nouveau user
90  					user = new UserData();
91  					user.setLogin(login);
92  					user.setPassword(password);
93  					user.setEmail(email);
94  					user.setNom(nom);
95  					user.setPrenom(prenom);
96  					user.setModif(isChief);
97  					user.setEmployee(isEmployee);
98  					user.setTeam(team);
99  
100 					ad.writeUser(user);
101 					if (isEmployee) {
102 						// on cree le nouvel employe
103 						employee = new EmployeeData();
104 						employee.setNom(nom);
105 						employee.setPrenom(prenom);
106 						employee.setPays(pays);
107 						employee.setTeam(team);
108 						employee.setEntryDate(entryDate);
109 						employee.setDayoff1(userForm.isDayoff1());
110 						employee.setDayoff2(userForm.isDayoff2());
111 						employee.setDayoff3(userForm.isDayoff3());
112 						employee.setDayoff4(userForm.isDayoff4());
113 						employee.setDayoff5(userForm.isDayoff5());
114 						employee.setDayoff6(userForm.isDayoff6());
115 						employee.setDayoff7(userForm.isDayoff7());
116 
117 						ad.writeEmployee(employee);
118 					}
119 
120 					// on change la liste des employes qui est deja dans le
121 					// contexte de l'application
122 					Vector employees = ad.readEmployees();
123 					session.getServletContext().setAttribute("employees",
124 							employees);
125 				}
126 			} catch (IOException e) {
127 				errors.add("error.concurrent.access", new ActionError(
128 						"error.concurrent.access"));
129 			} catch (Exception e) {
130 				throw new ServletException(e);
131 			}
132 		}
133 
134 		if (!errors.isEmpty()) {
135 			saveErrors(request, errors);
136 			return mapping.getInputForward();
137 		}
138 
139 		return mapping.findForward("success");
140 	}
141 }