KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cowsultants > itracker > web > actions > EditUserFormAction


1 /*
2  * This software was designed and created by Jason Carroll.
3  * Copyright (c) 2002, 2003, 2004 Jason Carroll.
4  * The author can be reached at jcarroll@cowsultants.com
5  * ITracker website: http://www.cowsultants.com
6  * ITracker forums: http://www.cowsultants.com/phpBB/index.php
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it only under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  */

18
19 package cowsultants.itracker.web.actions;
20
21 import java.io.*;
22 import java.rmi.*;
23 import java.util.*;
24 import javax.ejb.*;
25 import javax.rmi.*;
26 import javax.naming.*;
27 import javax.servlet.*;
28 import javax.servlet.http.*;
29
30 import org.apache.commons.beanutils.*;
31 import org.apache.struts.action.*;
32 import org.apache.struts.upload.*;
33 import org.apache.struts.util.*;
34 import org.apache.struts.validator.*;
35
36 import cowsultants.itracker.ejb.client.exceptions.*;
37 import cowsultants.itracker.ejb.client.interfaces.*;
38 import cowsultants.itracker.ejb.client.models.*;
39 import cowsultants.itracker.ejb.client.util.*;
40 import cowsultants.itracker.web.forms.*;
41 import cowsultants.itracker.web.util.*;
42
43
44 public class EditUserFormAction extends ITrackerAction {
45
46     public EditUserFormAction() {
47     }
48
49     public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
50         ActionErrors errors = new ActionErrors();
51
52         if(! isLoggedIn(request, response)) {
53             return mapping.findForward("login");
54         }
55
56         if(! hasPermission(UserUtilities.PERMISSION_USER_ADMIN, request, response)) {
57             return mapping.findForward("unauthorized");
58         }
59
60         HttpSession session = request.getSession(true);
61
62         try {
63             InitialContext ic = new InitialContext();
64
65             Object JavaDoc uhRef = ic.lookup("java:comp/env/" + UserHandler.JNDI_NAME);
66             UserHandlerHome uhHome = (UserHandlerHome) PortableRemoteObject.narrow(uhRef, UserHandlerHome.class);
67             UserHandler uh = uhHome.create();
68
69             UserModel editUser = null;
70             HashMap userPermissions = new HashMap();
71             UserForm userForm = (UserForm) form;
72             if(userForm == null) {
73                 userForm = new UserForm();
74             }
75
76             String JavaDoc action = (String JavaDoc) request.getParameter("action");
77             if("create".equals(action)) {
78                 if(! uh.allowProfileCreation(null, null, UserUtilities.AUTH_TYPE_UNKNOWN, UserUtilities.REQ_SOURCE_WEB)) {
79                     errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.noprofilecreates"));
80                     saveErrors(request, errors);
81                     return mapping.findForward("error");
82                 }
83
84                 editUser = new UserModel();
85                 editUser.setId(new Integer JavaDoc(-1));
86                 editUser.setStatus(UserUtilities.STATUS_ACTIVE);
87                 userForm.setAction("create");
88                 userForm.setId(editUser.getId());
89             } else if ("update".equals(action)) {
90                 Integer JavaDoc userId = userForm.getId();
91                 if(userId == null) {
92                     errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.invaliduser"));
93                 } else {
94                     editUser = uh.getUser(userId);
95                     if(editUser == null) {
96                         errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.invaliduser"));
97                     } else {
98                         userForm.setAction("update");
99                         userForm.setId(editUser.getId());
100                         userForm.setLogin(editUser.getLogin());
101                         userForm.setFirstName(editUser.getFirstName());
102                         userForm.setLastName(editUser.getLastName());
103                         userForm.setEmail(editUser.getEmail());
104                         userForm.setSuperUser(editUser.isSuperUser());
105
106                         PermissionModel[] permissionArray = uh.getPermissionsByUserId(editUser.getId());
107                         HashMap formPermissions = new HashMap();
108                         for(int i = 0; i < permissionArray.length; i++) {
109                             if(userPermissions.get(permissionArray[i].getProjectId()) == null) {
110                                 HashMap projectPermissions = new HashMap();
111                                 userPermissions.put(permissionArray[i].getProjectId(), projectPermissions);
112                             }
113                             formPermissions.put("Perm" + permissionArray[i].getPermissionType() + "Proj" + permissionArray[i].getProjectId(), "on");
114                             ((HashMap) userPermissions.get(permissionArray[i].getProjectId())).put(Integer.toString(permissionArray[i].getPermissionType()), permissionArray[i]);
115                         }
116                         userForm.setPermissions(formPermissions);
117                     }
118                 }
119             } else {
120                 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.invalidaction"));
121             }
122
123             if(errors.isEmpty()) {
124                 request.setAttribute("userForm", userForm);
125                 session.setAttribute(Constants.EDIT_USER_KEY, editUser);
126                 session.setAttribute(Constants.EDIT_USER_PERMS_KEY, userPermissions);
127                 saveToken(request);
128                 return mapping.getInputForward();
129             }
130         } catch(Exception JavaDoc e) {
131             Logger.logError("Exception while creating edit user form.", e);
132             errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.system"));
133         }
134
135         if(! errors.isEmpty()) {
136             saveErrors(request, errors);
137         }
138
139         return mapping.findForward("error");
140     }
141
142 }
143   
Popular Tags