KickJava   Java API By Example, From Geeks To Geeks.

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


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
35 import cowsultants.itracker.ejb.client.exceptions.*;
36 import cowsultants.itracker.ejb.client.interfaces.*;
37 import cowsultants.itracker.ejb.client.models.*;
38 import cowsultants.itracker.ejb.client.util.*;
39 import cowsultants.itracker.web.util.*;
40
41
42 public class EditProjectAction extends ITrackerAction {
43
44     public EditProjectAction() {
45     }
46
47     public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
48         ActionErrors errors = new ActionErrors();
49
50         if(! isLoggedIn(request, response)) {
51             return mapping.findForward("login");
52         }
53         if(! isTokenValid(request)) {
54             Logger.logDebug("Invalid request token while editing project.");
55             return mapping.findForward("listprojectsadmin");
56         }
57         resetToken(request);
58
59         ProjectModel project = null;
60         try {
61             InitialContext ic = new InitialContext();
62
63             Object JavaDoc phRef = ic.lookup("java:comp/env/" + ProjectHandler.JNDI_NAME);
64             ProjectHandlerHome phHome = (ProjectHandlerHome) PortableRemoteObject.narrow(phRef, ProjectHandlerHome.class);
65             ProjectHandler ph = phHome.create();
66
67             Object JavaDoc uhRef = ic.lookup("java:comp/env/" + UserHandler.JNDI_NAME);
68             UserHandlerHome uhHome = (UserHandlerHome) PortableRemoteObject.narrow(uhRef, UserHandlerHome.class);
69             UserHandler uh = uhHome.create();
70
71             HttpSession session = request.getSession(true);
72             HashMap userPermissions = (HashMap) session.getAttribute(Constants.PERMISSIONS_KEY);
73             UserModel user = (UserModel) session.getAttribute(Constants.USER_KEY);
74
75             project = new ProjectModel();
76             project.setId((Integer JavaDoc) PropertyUtils.getSimpleProperty(form, "id"));
77             project.setDescription((String JavaDoc) PropertyUtils.getSimpleProperty(form, "description"));
78             project.setName((String JavaDoc) PropertyUtils.getSimpleProperty(form, "name"));
79             Integer JavaDoc projectStatus = (Integer JavaDoc) PropertyUtils.getSimpleProperty(form, "status");
80             if(projectStatus != null) {
81                 project.setStatus(projectStatus.intValue());
82             } else {
83                 project.setStatus(ProjectUtilities.STATUS_ACTIVE);
84             }
85
86             Integer JavaDoc[] optionValues = (Integer JavaDoc[]) PropertyUtils.getSimpleProperty(form, "options");
87             int optionmask = 0;
88             if(optionValues != null) {
89                 for(int i = 0; i < optionValues.length; i++) {
90                     optionmask += optionValues[i].intValue();
91                 }
92             }
93             project.setOptions(optionmask);
94
95             HashSet fields = new HashSet();
96             Integer JavaDoc[] fieldIds = (Integer JavaDoc[]) PropertyUtils.getSimpleProperty(form, "fields");
97             if(fieldIds != null) {
98                 for(int i = 0; i < fieldIds.length; i++) {
99                     fields.add(fieldIds[i]);
100                 }
101             }
102
103             HashSet owners = new HashSet();
104             Integer JavaDoc[] ownerIds = (Integer JavaDoc[]) PropertyUtils.getSimpleProperty(form, "owners");
105             if(ownerIds != null) {
106                 for(int i = 0; i < ownerIds.length; i++) {
107                     owners.add(ownerIds[i]);
108                 }
109             }
110             SessionManager.setAllSessionsNeedsReset();
111
112             String JavaDoc action = (String JavaDoc) request.getParameter("action");
113             if("create".equals(action)) {
114                 if(! user.isSuperUser()) {
115                     return mapping.findForward("unauthorized");
116                 }
117                 project = ph.createProject(project);
118                 if(project == null) {
119                     throw new Exception JavaDoc("Error creating new project.");
120                 }
121                 ph.setProjectOwners(project, owners);
122                 ph.setProjectFields(project, fields);
123
124                 Integer JavaDoc[] userIds = (Integer JavaDoc[]) PropertyUtils.getSimpleProperty(form, "users");
125                 Integer JavaDoc[] permissions = (Integer JavaDoc[]) PropertyUtils.getSimpleProperty(form, "permissions");
126                 if(userIds != null && permissions != null && userIds.length > 0 && permissions.length > 0) {
127                     PermissionModel[] userPermissionModels = new PermissionModel[permissions.length];
128                     for(int i = 0; i < permissions.length; i++) {
129                         userPermissionModels[i] = new PermissionModel(project.getId(), permissions[i].intValue());
130                     }
131                     for(int i = 0; i < userIds.length; i++) {
132                         uh.addUserPermissions(userIds[i], userPermissionModels);
133                     }
134                 }
135             } else if ("update".equals(action)) {
136                 if(! UserUtilities.hasPermission(userPermissions, project.getId(), UserUtilities.PERMISSION_PRODUCT_ADMIN)) {
137                     return mapping.findForward("unauthorized");
138                 }
139                 ph.updateProject(project);
140                 ph.setProjectOwners(project, owners);
141                 ph.setProjectFields(project, fields);
142             }
143             session.removeAttribute(Constants.PROJECT_KEY);
144             return mapping.findForward("listprojectsadmin");
145         } catch(Exception JavaDoc e) {
146             Logger.logError("Exception processing form data", e);
147             errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.system"));
148         }
149
150         if(! errors.isEmpty()) {
151             saveErrors(request, errors);
152         }
153         return mapping.findForward("error");
154     }
155
156 }
157   
Popular Tags