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/EditUserAdminAction.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  
13  
14  public class EditUserAdminAction extends Action {
15  
16    private Logger log = Logger.getLogger(EditUserAdminAction.class);
17  
18    public EditUserAdminAction() {
19      BasicConfigurator.resetConfiguration();
20      PropertyConfigurator.configure(Config.instance().getLogPropertiesFileName());
21    }
22  
23    public ActionForward execute(ActionMapping actionMapping,
24                                 ActionForm actionForm,
25                                 HttpServletRequest httpServletRequest,
26                                 HttpServletResponse httpServletResponse) {
27  
28      EditUserAdminForm editUserAdminForm = (EditUserAdminForm) actionForm;
29  
30      ActionErrors errors;
31  
32      /*We need to check if a User has been selected from the User pull
33        down menu in editUserAdmin.jsp. In other words, we need to check if this
34           Action was invoked by the onChange event in the User pull down or a
35             submit event from the submit button. isButtonSelect will be true if
36           a User has been selected from the User pull down menu...*/
37      if (editUserAdminForm.isButtonSelect()) {
38        editUserAdminForm.setButtonSelect(false);
39        return actionMapping.findForward("failure");
40      }
41  
42      User user = new User();
43      String fieldName;
44      String fieldValue;
45      Vector fieldNameList = new Vector();
46      Vector fieldValueList = new Vector();
47  
48      // the following bit of code uses reflection and the parameter list of the
49      // HttpServletRequest object to construct a user object based on the dynamically
50      // created forms in editUser.jsp
51      // if anything goes wrong during the process, then the user is forwarded to
52      // a failure page
53      for (Enumeration e = httpServletRequest.getParameterNames(); e.hasMoreElements(); ) {
54        String potentialName = (String) e.nextElement();
55        if (potentialName.substring(0, 5).equals("form_")) {
56          fieldValue = httpServletRequest.getParameter(potentialName);
57          fieldName = potentialName.substring(5);
58          fieldName = fieldName.substring(0, 1).toLowerCase() +
59              fieldName.substring(1);
60          try {
61            user.getClass().getField(fieldName).set(user, fieldValue);
62            fieldNameList.addElement(fieldName);
63            fieldValueList.addElement(fieldValue);
64          }
65          catch (Exception ex) {
66            log.warn(ex.getClass() + " encountered in EditUserAdminAction: " + ex.getMessage());
67            errors = new ActionErrors();
68            errors.add("EditUserAction", new ActionError("editUser.caught.exception"));
69            this.saveErrors(httpServletRequest, errors);
70            return (actionMapping.findForward("failure"));
71          }
72        }
73      }
74  
75  
76      /**
77       * now we have a user object with all of the fields filled in from the
78       * dynamically created form in editUser.jsp
79       * @todo updateUser should be called throught UserMgr class.
80       */
81      try {
82        if(AssetDB.instance().updateUser(user) == 0) {
83          errors = new ActionErrors();
84          errors.add("EditUserAction", new ActionError("editUser.caught.exception"));
85          this.saveErrors(httpServletRequest, errors);
86          return (actionMapping.findForward("failure"));
87        }
88      }
89      catch (SQLException ex1) {
90        errors = new ActionErrors();
91        errors.add("EditUserAction", new ActionError("editUser.caught.exception"));
92        this.saveErrors(httpServletRequest, errors);
93        return (actionMapping.findForward("failure"));
94      }
95  
96      // now that the user has been successfully updated in the DB, we attach
97      // the new user object to the session.
98  //    HttpSession s = httpServletRequest.getSession();
99  //    s.setAttribute("User", user);
100     httpServletRequest.getSession().setAttribute("userFieldNameList",fieldNameList);
101     httpServletRequest.getSession().setAttribute("userFieldValueList",fieldValueList);
102     return (actionMapping.findForward("success"));
103 
104   }
105 }