1 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 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 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 ) PropertyUtils.getSimpleProperty(form, "id")); 77 project.setDescription((String ) PropertyUtils.getSimpleProperty(form, "description")); 78 project.setName((String ) PropertyUtils.getSimpleProperty(form, "name")); 79 Integer projectStatus = (Integer ) 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 [] optionValues = (Integer []) 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 [] fieldIds = (Integer []) 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 [] ownerIds = (Integer []) 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 action = (String ) 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 ("Error creating new project."); 120 } 121 ph.setProjectOwners(project, owners); 122 ph.setProjectFields(project, fields); 123 124 Integer [] userIds = (Integer []) PropertyUtils.getSimpleProperty(form, "users"); 125 Integer [] permissions = (Integer []) 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 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 |