KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cowsultants > itracker > web > forms > IssueForm


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.forms;
20
21 import java.rmi.*;
22 import java.util.*;
23 import javax.ejb.*;
24 import javax.rmi.*;
25 import javax.naming.*;
26 import javax.servlet.http.*;
27
28 import org.apache.commons.beanutils.*;
29 import org.apache.struts.action.*;
30 import org.apache.struts.validator.*;
31 import org.apache.struts.util.*;
32
33 import cowsultants.itracker.ejb.client.exceptions.*;
34 import cowsultants.itracker.ejb.client.interfaces.*;
35 import cowsultants.itracker.ejb.client.models.*;
36 import cowsultants.itracker.ejb.client.resources.*;
37 import cowsultants.itracker.ejb.client.util.*;
38 import cowsultants.itracker.web.util.*;
39
40
41 /**
42   * This form is by the struts actions to pass issue data.
43   */

44 public class IssueForm extends DynaValidatorForm {
45
46     /**
47       * This methods adds in validation for custom fields. It makes sure the datatype
48       * matches and also that all required fields have been populated.
49       * @param mapping the ActionMapping object
50       * @param request the current HttpServletRequest object
51       * @return an ActionErrors object containing any validation errors
52       */

53     public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
54         ActionErrors errors = super.validate(mapping, request);
55
56         Logger.logDebug("Errors contains " + errors.size() + " errors.");
57         try {
58             ProjectModel project = null;
59             Integer JavaDoc projectId = (Integer JavaDoc) PropertyUtils.getSimpleProperty(this, "projectId");
60             if(projectId == null) {
61                 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.invalidproject"));
62             } else {
63                 InitialContext ic = new InitialContext();
64                 Object JavaDoc phRef = ic.lookup("java:comp/env/" + ProjectHandler.JNDI_NAME);
65                 ProjectHandlerHome phHome = (ProjectHandlerHome) PortableRemoteObject.narrow(phRef, ProjectHandlerHome.class);
66                 ProjectHandler ph = phHome.create();
67
68                 project = ph.getProject(projectId);
69             }
70
71             if(errors.isEmpty() && project == null) {
72                 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.invalidproject"));
73             } else if(errors.isEmpty() && project.getStatus() != ProjectUtilities.STATUS_ACTIVE) {
74                 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.projectlocked"));
75             } else if(errors.isEmpty()) {
76                 Locale currLocale = ITrackerResources.getLocale();
77
78                 request.setAttribute(Constants.PROJECT_KEY, project);
79
80                 CustomFieldModel[] projectFields = project.getCustomFields();
81                 if(projectFields.length > 0) {
82                     HttpSession session = request.getSession();
83                     if(session != null) {
84                         currLocale = (Locale) session.getAttribute(Constants.LOCALE_KEY);
85                     }
86
87                     for(int i = 0; i < projectFields.length; i++) {
88                         String JavaDoc fieldValue = request.getParameter("customFields(" + projectFields[i].getId() +")");
89                         if(fieldValue != null && ! fieldValue.equals("")) {
90                             IssueFieldModel issueField = new IssueFieldModel(projectFields[i]);
91                             try {
92                                 issueField.setValue(fieldValue, currLocale);
93                             } catch(IssueException ie) {
94                                 String JavaDoc label = CustomFieldUtilities.getCustomFieldName(projectFields[i].getId(), currLocale);
95                                 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(ie.getType(), label));
96                             }
97                         } else if(projectFields[i].isRequired()) {
98                             String JavaDoc label = CustomFieldUtilities.getCustomFieldName(projectFields[i].getId(), currLocale);
99                             errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(IssueException.TYPE_CF_REQ_FIELD, label));
100                         }
101                     }
102                 }
103             }
104         } catch(Exception JavaDoc e) {
105             e.printStackTrace();
106             errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.system"));
107         }
108
109         return errors;
110     }
111
112 }
113
Popular Tags