View Javadoc

1   // Created on 24.11.2003
2   package com.pnpconsult.zeiterfassung.actions.admin;
3   
4   import java.io.Serializable;
5   import java.io.UnsupportedEncodingException;
6   import java.net.URLEncoder;
7   import java.text.ParseException;
8   import java.util.Collection;
9   import java.util.HashSet;
10  import java.util.Iterator;
11  import java.util.LinkedList;
12  import java.util.Set;
13  
14  import javax.servlet.http.HttpServletRequest;
15  
16  import jface.util.Strings;
17  import net.sf.hibernate.HibernateException;
18  
19  import org.apache.commons.lang.ObjectUtils;
20  import org.apache.commons.lang.StringUtils;
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.apache.struts.action.ActionErrors;
24  import org.apache.struts.action.ActionMapping;
25  
26  import com.pnpconsult.zeiterfassung.actions.EditForm;
27  import com.pnpconsult.zeiterfassung.helper.UserManager;
28  import com.pnpconsult.zeiterfassung.model.Project;
29  import com.pnpconsult.zeiterfassung.model.Role;
30  import com.pnpconsult.zeiterfassung.model.User;
31  import com.pnpconsult.zeiterfassung.model.UserProject;
32  import com.pnpconsult.zeiterfassung.tags.UserProjectBillingFactorTag;
33  import com.pnpconsult.zeiterfassung.util.ActionErrorUtils;
34  import com.pnpconsult.zeiterfassung.util.NumberUtils;
35  import com.pnpconsult.zeiterfassung.util.SimpleExpressionParser;
36  
37  /***
38   * @author <a href="mailto:powerpete@users.sf.net">M. Petersen</a>
39   * @version $Id: EditUserForm.java,v 1.9 2004/06/24 20:54:18 powerpete Exp $
40   * 
41   * @struts.form name="editUserForm"
42   */
43  public class EditUserForm extends EditForm
44  {
45      private static final Log LOG = LogFactory.getLog(EditUserForm.class);
46      private String firstName;
47      private boolean male;
48      private String lastName;
49      private String login;
50      private String password;
51      private String passwordRepeat;
52      private float rate;
53      private long[] projectIds = new long[0];
54      private Collection userProjects = new LinkedList();
55      private boolean userRole;
56      private boolean managerRole;
57      private boolean adminRole;
58      private Collection customers;
59  
60      /***
61       * @see com.pnpconsult.zeiterfassung.actions.EditForm#save()
62       */
63      public void save()
64      {
65          super.save();
66      }
67  
68      /***
69       * @see com.pnpconsult.zeiterfassung.actions.EditForm#update()
70       */
71      public void update()
72      {
73          super.update();
74      }
75  
76      public Collection getCustomers()
77      {
78          if (customers == null)
79          {
80              customers = new AdminMenuForm().getAllCustomers();
81          }
82          return customers;
83      }
84  
85      public ActionErrors validate(
86          ActionMapping mapping,
87          HttpServletRequest request)
88      {
89          ActionErrors errors = new ActionErrors();
90          if (StringUtils.isBlank(login))
91          {
92              ActionErrorUtils.add(errors, "errors.secure.admin.user.login");
93          }
94          else if (containsSpecialCharacters(login))
95          {
96              ActionErrorUtils.add(errors, "errors.secure.admin.user.login_format_error");
97          }
98          else
99          {
100             if (isNew)
101             {
102                 try
103                 {
104                     if (new UserManager().exists(login))
105                     {
106                         ActionErrorUtils.add(errors, "errors.secure.admin.user.login_duplicate");
107                     }
108                 }
109                 catch (HibernateException e)
110                 {
111                     ActionErrorUtils.add(errors, "errors.hibernate", e.getLocalizedMessage());
112                 }
113             }
114         }
115         if (StringUtils.isBlank(firstName))
116         {
117             ActionErrorUtils.add(errors, "errors.secure.admin.user.firstName");
118         }
119         if (StringUtils.isBlank(lastName))
120         {
121             ActionErrorUtils.add(errors, "errors.secure.admin.user.lastName");
122         }
123         if (!isPasswordValid())
124         {
125             ActionErrorUtils.add(errors, "errors.secure.admin.user.password");
126         }
127         if (errors.isEmpty())
128         {
129             userProjects.clear();
130             for (int i = 0; i < projectIds.length; i++)
131             {
132                 Project project = new Project();
133                 project.setId(projectIds[i]);
134                 float billingFactor =
135                     UserProjectBillingFactorTag.lookupBillingFactor(
136                         request,
137                         "userProjectBillingFactor",
138                         projectIds[i]);
139                 UserProject userProject = new UserProject();
140                 userProject.setBillingFactor(billingFactor);
141                 userProject.setProject(project);
142                 userProjects.add(userProject);
143             }
144         }
145         return errors;
146     }
147 
148     /***
149      * @return
150      * @throws UnsupportedEncodingException
151      */
152     private boolean containsSpecialCharacters(String str)
153     {
154         try
155         {
156             return !str.equals(URLEncoder.encode(str, "UTF-8"));
157         }
158         catch (UnsupportedEncodingException e)
159         {
160             // It contains so bad special characters, I can't even convert it!
161             return true;
162         }
163     }
164 
165     boolean isPasswordValid()
166     {
167         if (isNew)
168         {
169             if (Strings.isEmpty(password, true))
170             {
171                 return false;
172             }
173             if (Strings.isEmpty(passwordRepeat, true))
174             {
175                 return false;
176             }
177         }
178         else
179         {
180             if (Strings.isEmpty(password, true)
181                 && Strings.isEmpty(passwordRepeat, true))
182             {
183                 return true;
184             }
185         }
186         if (containsSpecialCharacters(password))
187         {
188             return false;
189         }
190         return ObjectUtils.equals(password, passwordRepeat);
191     }
192 
193     protected void writeToDataObject(Object obj)
194     {
195         User user = (User) obj;
196         LOG.debug("Input: " + user + ", userProjects = " + user.getUserProjects());
197         user.setFirstName(firstName);
198         user.setLastName(lastName);
199         user.setLogin(login);
200         user.setMale(male);
201 
202         if (!Strings.isEmpty(password, true))
203         {
204             user.setPassword(password);
205         }
206         user.setRate(rate);
207         Set roles = new HashSet();
208         if (userRole)
209         {
210             Role role = new Role();
211             role.setName("user");
212             roles.add(role);
213         }
214         if (managerRole)
215         {
216             Role role = new Role();
217             role.setName("manager");
218             roles.add(role);
219         }
220         if (adminRole)
221         {
222             Role role = new Role();
223             role.setName("admin");
224             roles.add(role);
225         }
226         user.setRoles(roles);
227         try
228         {
229             new UserManager().setUserProjects(user, userProjects);
230         }
231         catch (Exception e)
232         {
233             LOG.fatal("setUserProjects(" + user + ", " + userProjects + ")", e);
234         }
235     }
236 
237     protected void readFromDataObject(Object obj)
238     {
239         User user = (User) obj;
240         firstName = user.getFirstName();
241         lastName = user.getLastName();
242         login = user.getLogin();
243         male = user.isMale();
244         password = user.getPassword();
245         rate = user.getRate();
246         Set userRoles = user.getRoles();
247         if (userRoles != null)
248         {
249             userRole = false;
250             managerRole = false;
251             adminRole = false;
252             for (Iterator it = user.getRoles().iterator(); it.hasNext();)
253             {
254                 Role role = (Role) it.next();
255                 userRole |= "user".equals(role.getName());
256                 managerRole |= "manager".equals(role.getName());
257                 adminRole |= "admin".equals(role.getName());
258             }
259         }
260         Collection userProjects = user.getUserProjects();
261         projectIds = new long[userProjects.size()];
262         int i = 0;
263         for (Iterator it = userProjects.iterator(); it.hasNext();)
264         {
265             UserProject userProject = (UserProject) it.next();
266             projectIds[i++] = userProject.getProject().getId();
267         }
268     }
269 
270     public String getFirstName()
271     {
272         return firstName;
273     }
274 
275     public void setFirstName(String firstName)
276     {
277         this.firstName = firstName;
278     }
279 
280     public String getLastName()
281     {
282         return lastName;
283     }
284 
285     public void setLastName(String lastName)
286     {
287         this.lastName = lastName;
288     }
289 
290     public String getLogin()
291     {
292         return login;
293     }
294 
295     public void setLogin(String login)
296     {
297         this.login = login;
298     }
299 
300     public boolean isMale()
301     {
302         return male;
303     }
304 
305     public void setMale(boolean male)
306     {
307         this.male = male;
308     }
309 
310     public String getPassword()
311     {
312         return "";
313     }
314 
315     public void setPassword(String password)
316     {
317         this.password = password;
318     }
319 
320     public String getPasswordRepeat()
321     {
322         return "";
323     }
324 
325     public void setPasswordRepeat(String passwordRepeat)
326     {
327         this.passwordRepeat = passwordRepeat;
328     }
329 
330     public String getRate()
331     {
332         return NumberUtils.formatLong(rate);
333     }
334 
335     public void setRate(String rate)
336     {
337         try
338         {
339             this.rate = (float) SimpleExpressionParser.parse(rate);
340         }
341         catch (ParseException e)
342         {
343             LOG.info("User entered unparseable data: " + rate, e);
344         }
345     }
346 
347     /***
348      * @see com.pnpconsult.zeiterfassung.actions.EditForm#newDataObject()
349      */
350     protected Object newDataObject()
351     {
352         return new User();
353     }
354 
355     /***
356      * @see com.pnpconsult.zeiterfassung.actions.EditForm#dataObjectType()
357      */
358     protected Class dataObjectType()
359     {
360         return User.class;
361     }
362 
363     /***
364      * @see com.pnpconsult.zeiterfassung.actions.EditForm#dataObjectKey()
365      */
366     protected Serializable dataObjectKey()
367     {
368         return login;
369     }
370     /***
371      * @return Returns the adminRole.
372      */
373     public boolean isAdminRole()
374     {
375         return adminRole;
376     }
377 
378     /***
379      * @param adminRole The adminRole to set.
380      */
381     public void setAdminRole(boolean adminRole)
382     {
383         this.adminRole = adminRole;
384     }
385 
386     /***
387      * @return Returns the managerRole.
388      */
389     public boolean isManagerRole()
390     {
391         return managerRole;
392     }
393 
394     /***
395      * @param managerRole The managerRole to set.
396      */
397     public void setManagerRole(boolean managerRole)
398     {
399         this.managerRole = managerRole;
400     }
401 
402     /***
403      * @return Returns the userRole.
404      */
405     public boolean isUserRole()
406     {
407         return userRole;
408     }
409 
410     /***
411      * @param userRole The userRole to set.
412      */
413     public void setUserRole(boolean userRole)
414     {
415         this.userRole = userRole;
416     }
417     /***
418      * @return Returns the projectIds.
419      */
420     public long[] getProjectIds()
421     {
422         return projectIds;
423     }
424 
425     /***
426      * @param projectIds The projectIds to set.
427      */
428     public void setProjectIds(long[] projectIds)
429     {
430         this.projectIds = projectIds;
431     }
432 
433 }