Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/acs/damsel/client/edit/EditUserAction.java


1   package org.acs.damsel.client.edit;
2   
3   import java.sql.*;
4   import java.util.*;
5   import javax.servlet.http.*;
6   
7   import org.acs.damsel.srvr.*;
8   import org.acs.damsel.srvr.db.*;
9   import org.acs.damsel.srvr.user.*;
10  import org.apache.log4j.*;
11  import org.apache.struts.action.*;
12  import org.acs.damsel.client.ClientApp;
13  
14  public class EditUserAction
15      extends Action {
16  
17    private Logger log = Logger.getLogger(EditUserAction.class);
18  
19    public EditUserAction() {
20      BasicConfigurator.resetConfiguration();
21      PropertyConfigurator.configure(Config.instance().getLogPropertiesFileName());
22    }
23  
24    public ActionForward execute(ActionMapping actionMapping,
25                                 ActionForm actionForm,
26                                 HttpServletRequest httpServletRequest,
27                                 HttpServletResponse httpServletResponse) {
28  
29      EditUserForm editUserForm = (EditUserForm) actionForm;
30      User user = new User();
31      String fieldName;
32      String fieldValue;
33  
34      ActionErrors errors;
35  
36      // the following bit of code uses reflection and the parameter list of the
37      // HttpServletRequest object to construct a user object based on the dynamically
38      // created forms in editUser.jsp
39      // if anything goes wrong during the process, then the user is forwarded to
40      // a failure page
41      for (Enumeration e = httpServletRequest.getParameterNames();
42           e.hasMoreElements(); ) {
43        String potentialName = (String) e.nextElement();
44        if (potentialName.substring(0, 5).equals("form_")) {
45          fieldValue = httpServletRequest.getParameter(potentialName);
46          fieldName = potentialName.substring(5);
47          fieldName = fieldName.substring(0, 1).toLowerCase() +
48              fieldName.substring(1);
49          try {
50            user.getClass().getField(fieldName).set(user, fieldValue);
51          }
52          catch (Exception ex) {
53            log.warn(ex.getClass() + " encountered in EditUserAction: " +
54                     ex.getMessage());
55            errors = new ActionErrors();
56            errors.add("EditUserAction",
57                       new ActionError("editUser.caught.exception"));
58            this.saveErrors(httpServletRequest, errors);
59            return (actionMapping.findForward("failure"));
60          }
61        }
62      }
63  
64      /**
65       * now we have a user object with all of the fields filled in from the
66       * dynamically created form in editUser.jsp
67       * @todo updateUser should be called throught UserMgr class.
68       */
69      try {
70        if (ClientApp.instance().getUserMgr().updateUser(user) == 0) {
71          errors = new ActionErrors();
72          errors.add("EditUserAction",
73                     new ActionError("editUser.caught.exception"));
74          this.saveErrors(httpServletRequest, errors);
75          return (actionMapping.findForward("failure"));
76        }
77        if (user.firstName.equals("")) {
78          errors = new ActionErrors();
79          errors.add("EditUserAction",
80                     new ActionError("editUser.firstName.is.null"));
81          this.saveErrors(httpServletRequest, errors);
82          return (actionMapping.findForward("failure"));
83        }
84        if (user.lastName.equals("")) {
85          errors = new ActionErrors();
86          errors.add("EditUserAction",
87                     new ActionError("editUser.lastName.is.null"));
88          this.saveErrors(httpServletRequest, errors);
89          return (actionMapping.findForward("failure"));
90        }
91        if (user.email.equals("")) {
92          errors = new ActionErrors();
93          errors.add("EditUserAction", new ActionError("editUser.email.is.null"));
94          this.saveErrors(httpServletRequest, errors);
95          return (actionMapping.findForward("failure"));
96        }
97  
98      }
99      catch (SQLException ex1) {
100       errors = new ActionErrors();
101       errors.add("EditUserAction", new ActionError("editUser.caught.exception"));
102       this.saveErrors(httpServletRequest, errors);
103       return (actionMapping.findForward("failure"));
104     }
105 
106     // now that the user has been successfully updated in the DB, we attach
107     // the new user object to the session.
108     HttpSession s = httpServletRequest.getSession();
109     s.setAttribute("User", user);
110     return (actionMapping.findForward("success"));
111   }
112 }