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

Quick Search    Search Deep

Source code: org/acs/damsel/client/add/AddUserAction.java


1   package org.acs.damsel.client.add;
2   
3   import java.sql.*;
4   import java.util.*;
5   import javax.servlet.http.*;
6   
7   import org.acs.damsel.client.*;
8   import org.acs.damsel.srvr.*;
9   import org.acs.damsel.srvr.user.*;
10  import org.apache.log4j.*;
11  import org.apache.struts.action.*;
12  
13  public class AddUserAction
14      extends Action {
15  
16    private Logger log = Logger.getLogger(AddUserAction.class);
17  
18    public AddUserAction() {
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      AddUserForm addUserForm = (AddUserForm) actionForm;
29      String groupName = addUserForm.getGroupName();
30      User user = new User();
31  
32      String fieldName = null;
33      String fieldValue = null;
34      Vector userNameVec = new Vector();
35      Vector userValueVec = new Vector();
36  
37      // The input forms on the edit asset page are dynamically generated according
38      // to the Schema associated with the Collection to which the asset belongs.
39      // The form names are prefixed with "form_" to distinguish
40      // them from other parameters, like the submit button.
41      // This loops through the list of parameters and picks off the form entries.
42      // It then updates the value of each AssetDescriptor with the value entered
43      // into the corresponding textfield.  Note that this does not currently do
44      // any validation.  Once the AssetDescriptors have been updated the entire
45      // asset is updated in the database.
46      for (Enumeration e = httpServletRequest.getParameterNames();
47           e.hasMoreElements(); ) {
48        String potentialName = (String) e.nextElement();
49        if (potentialName.substring(0, 5).equals("form_")) {
50          fieldName = potentialName.substring(5);
51          fieldValue = (String) httpServletRequest.getParameter(potentialName);
52          userNameVec.addElement(fieldName);
53          userValueVec.addElement(fieldValue);
54          try {
55            user.getClass().getField(fieldName).set(user, fieldValue);
56          }
57          catch (Exception ex) {
58            log.warn(ex.getClass() + " encountered in AddUserAction: " +
59                     ex.getMessage());
60            return actionMapping.findForward("failure");
61          }
62        }
63      }
64      //Default password initialized as "password".
65      user.setPassword("password");
66      try {
67        if (ClientApp.instance().getUserMgr().isUserInDB(user)) {
68          ActionErrors errors = new ActionErrors();
69          errors.add("addUser", new ActionError("addUser.user.already.exists"));
70          this.saveErrors(httpServletRequest, errors);
71          return actionMapping.findForward("failure");
72        }
73        if (user.getUserName() == null || user.getUserName().trim().length() == 0) {
74          ActionErrors errors = new ActionErrors();
75          errors.add("addUser", new ActionError("addUser.username.is.null"));
76          this.saveErrors(httpServletRequest, errors);
77          return actionMapping.findForward("failure");
78        }
79        if (user.getFirstName() == null || user.getFirstName().trim().length() == 0) {
80          ActionErrors errors = new ActionErrors();
81          errors.add("addUser", new ActionError("addUser.firstName.is.null"));
82          this.saveErrors(httpServletRequest, errors);
83          return actionMapping.findForward("failure");
84        }
85        if (user.getLastName() == null || user.getLastName().trim().length() == 0) {
86          ActionErrors errors = new ActionErrors();
87          errors.add("addUser", new ActionError("addUser.lastName.is.null"));
88          this.saveErrors(httpServletRequest, errors);
89          return actionMapping.findForward("failure");
90        }
91        if (user.getEmail() == null || user.getEmail().trim().length() == 0) {
92          ActionErrors errors = new ActionErrors();
93          errors.add("addUser", new ActionError("addUser.email.is.null"));
94          this.saveErrors(httpServletRequest, errors);
95          return actionMapping.findForward("failure");
96        }
97  
98  
99  
100 
101       else {
102         ClientApp.instance().getUserMgr().addUser(user);
103         ClientApp.instance().getGroupMgr().addUserToGroup(user.getUserName(),
104             groupName);
105         httpServletRequest.getSession().setAttribute("userNameVec", userNameVec);
106         httpServletRequest.getSession().setAttribute("userValueVec",
107             userValueVec);
108         return actionMapping.findForward("success");
109       }
110     }
111     catch (SQLException ex1) {
112       log.warn("SQLException in AddUserAction: " + ex1.getMessage());
113       return actionMapping.findForward("failure");
114     }
115   }
116 }