Clover Coverage Report - itracker
Coverage timestamp: Tue May 1 2012 16:42:12 CEST
112   410   71   2.67
46   310   0.63   42
42     1.69  
1    
 
 
  IssueForm       Line # 59 112 71 0% 0.0
 
No Tests
 
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 org.itracker.web.forms;
20   
21    import java.util.HashMap;
22    import java.util.Iterator;
23    import java.util.List;
24    import java.util.Locale;
25    import java.util.ResourceBundle;
26   
27    import javax.servlet.http.HttpServletRequest;
28    import javax.servlet.http.HttpSession;
29   
30    import org.apache.log4j.Logger;
31    import org.apache.struts.action.ActionErrors;
32    import org.apache.struts.action.ActionMapping;
33    import org.apache.struts.action.ActionMessage;
34    import org.apache.struts.action.ActionMessages;
35    import org.apache.struts.upload.FormFile;
36    import org.itracker.core.resources.ITrackerResources;
37    import org.itracker.model.CustomField;
38    import org.itracker.model.Issue;
39    import org.itracker.model.NameValuePair;
40    import org.itracker.model.Project;
41    import org.itracker.model.ProjectScript;
42    import org.itracker.model.Status;
43    import org.itracker.model.User;
44    import org.itracker.services.ITrackerServices;
45    import org.itracker.services.exceptions.IssueException;
46    import org.itracker.services.exceptions.WorkflowException;
47    import org.itracker.services.util.CustomFieldUtilities;
48    import org.itracker.services.util.UserUtilities;
49    import org.itracker.services.util.WorkflowUtilities;
50    import org.itracker.web.actions.project.EditIssueActionUtil;
51    import org.itracker.web.ptos.CreateIssuePTO;
52    import org.itracker.web.util.AttachmentUtilities;
53    import org.itracker.web.util.Constants;
54    import org.itracker.web.util.RequestHelper;
55   
56    /**
57    * This form is by the struts actions to pass issue data.
58    */
 
59    public class IssueForm extends ITrackerForm {
60   
61    /**
62    *
63    */
64    private static final long serialVersionUID = 1L;
65   
66    private static final Logger log = Logger.getLogger(IssueForm.class);
67   
68    private Integer id = null;
69    private String caller = null;
70    private Integer projectId = null;
71    private Integer creatorId = null;
72    private Integer ownerId = null;
73    private String description = null;
74    private Integer severity = null;
75    private Integer status = null;
76    private Integer prevStatus = null;
77    private String resolution = null;
78    private Integer targetVersion = null;
79    private Integer[] components = new Integer[0];
80    private Integer[] versions = new Integer[0];
81    private String attachmentDescription = null;
82    transient private FormFile attachment = null;
83    private String history = null;
84    // lets try to put Integer,String here:
85    private HashMap<String, String> customFields = new HashMap<String, String>();
86    private Integer relationType = null;
87    private Integer relatedIssueId = null;
88   
 
89  0 toggle public FormFile getAttachment() {
90  0 return attachment;
91    }
92   
 
93  0 toggle public void setAttachment(FormFile attachment) {
94  0 this.attachment = attachment;
95    }
96   
 
97  0 toggle public String getAttachmentDescription() {
98  0 return attachmentDescription;
99    }
100   
 
101  0 toggle public void setAttachmentDescription(String attachmentDescription) {
102  0 this.attachmentDescription = attachmentDescription;
103    }
104   
 
105  0 toggle public String getCaller() {
106  0 return caller;
107    }
108   
 
109  0 toggle public void setCaller(String caller) {
110  0 this.caller = caller;
111    }
112   
 
113  0 toggle public Integer[] getComponents() {
114  0 if (null == components)
115  0 return null;
116  0 return components.clone();
117    }
118   
 
119  0 toggle public void setComponents(Integer[] components) {
120  0 if (null == components)
121  0 this.components = null;
122    else
123  0 this.components = components.clone();
124    }
125   
 
126  0 toggle public Integer getCreatorId() {
127  0 return creatorId;
128    }
129   
 
130  0 toggle public void setCreatorId(Integer creatorId) {
131  0 this.creatorId = creatorId;
132    }
133   
134    // let's try to put Integer,String here:
 
135  0 toggle public HashMap<String, String> getCustomFields() {
136  0 return customFields;
137    }
138   
139    // let's try to put Integer,String here:
 
140  0 toggle public void setCustomFields(HashMap<String, String> customFields) {
141  0 this.customFields = customFields;
142    }
143   
 
144  0 toggle public String getDescription() {
145  0 return description;
146    }
147   
 
148  0 toggle public void setDescription(String description) {
149  0 this.description = description;
150    }
151   
 
152  0 toggle public String getHistory() {
153  0 return history;
154    }
155   
 
156  0 toggle public void setHistory(String history) {
157  0 this.history = history;
158    }
159   
 
160  0 toggle public Integer getId() {
161  0 return id;
162    }
163   
 
164  0 toggle public void setId(Integer id) {
165  0 this.id = id;
166    }
167   
 
168  0 toggle public Integer getOwnerId() {
169  0 return ownerId;
170    }
171   
 
172  0 toggle public void setOwnerId(Integer ownerId) {
173  0 this.ownerId = ownerId;
174    }
175   
 
176  0 toggle public Integer getPrevStatus() {
177  0 return prevStatus;
178    }
179   
 
180  0 toggle public void setPrevStatus(Integer prevStatus) {
181  0 this.prevStatus = prevStatus;
182    }
183   
 
184  0 toggle public Integer getProjectId() {
185  0 return projectId;
186    }
187   
 
188  0 toggle public void setProjectId(Integer projectId) {
189  0 this.projectId = projectId;
190    }
191   
 
192  0 toggle public Integer getRelatedIssueId() {
193  0 return relatedIssueId;
194    }
195   
 
196  0 toggle public void setRelatedIssueId(Integer relatedIssueId) {
197  0 this.relatedIssueId = relatedIssueId;
198    }
199   
 
200  0 toggle public Integer getRelationType() {
201  0 return relationType;
202    }
203   
 
204  0 toggle public void setRelationType(Integer relationType) {
205  0 this.relationType = relationType;
206    }
207   
 
208  0 toggle public String getResolution() {
209  0 return resolution;
210    }
211   
 
212  0 toggle public void setResolution(String resolution) {
213  0 this.resolution = resolution;
214    }
215   
 
216  0 toggle public Integer getSeverity() {
217  0 return severity;
218    }
219   
 
220  0 toggle public void setSeverity(Integer severity) {
221  0 this.severity = severity;
222    }
223   
 
224  0 toggle public Integer getStatus() {
225  0 return status;
226    }
227   
 
228  0 toggle public void setStatus(Integer status) {
229  0 this.status = status;
230    }
231   
 
232  0 toggle public Integer getTargetVersion() {
233  0 return targetVersion;
234    }
235   
 
236  0 toggle public void setTargetVersion(Integer targetVersion) {
237  0 this.targetVersion = targetVersion;
238    }
239   
 
240  0 toggle public Integer[] getVersions() {
241  0 if (null == versions)
242  0 return null;
243  0 return versions.clone();
244    }
245   
 
246  0 toggle public void setVersions(Integer[] versions) {
247  0 if (null == versions)
248  0 this.versions = null;
249    else
250  0 this.versions = versions.clone();
251    }
252   
253    /**
254    * This methods adds in validation for custom fields. It makes sure the
255    * datatype matches and also that all required fields have been populated.
256    *
257    * @param mapping
258    * the ActionMapping object
259    * @param request
260    * the current HttpServletRequest object
261    * @return an ActionErrors object containing any validation errors
262    */
 
263  0 toggle public ActionErrors validate(ActionMapping mapping,
264    HttpServletRequest request) {
265  0 if (log.isDebugEnabled()) {
266  0 log.debug("validate called: mapping: " + mapping + ", request: "
267    + request);
268    }
269  0 ActionErrors errors = super.validate(mapping, request);
270   
271  0 if (log.isDebugEnabled()) {
272  0 log.debug("validate called: mapping: " + mapping + ", request: "
273    + request + ", errors: " + errors);
274    }
275   
276  0 try {
277  0 if (null != getId()) {
278  0 Issue issue = getITrackerServices().getIssueService().getIssue(
279    getId());
280   
281  0 Locale locale = (Locale) request.getSession().getAttribute(
282    Constants.LOCALE_KEY);
283  0 User currUser = (User) request.getSession().getAttribute(
284    Constants.USER_KEY);
285  0 List<NameValuePair> ownersList = UserUtilities
286    .getAssignableIssueOwnersList(issue,
287    issue.getProject(), currUser, locale,
288    getITrackerServices().getUserService(),
289    RequestHelper.getUserPermissions(request
290    .getSession()));
291   
292  0 EditIssueActionUtil.setupJspEnv(mapping, this, request, issue,
293    getITrackerServices().getIssueService(),
294    getITrackerServices().getUserService(), RequestHelper
295    .getUserPermissions(request.getSession()),
296    EditIssueActionUtil.getListOptions(request, issue,
297    ownersList, RequestHelper
298    .getUserPermissions(request
299    .getSession()), issue
300    .getProject(), currUser), errors);
301   
302  0 if (errors.isEmpty() && issue.getProject() == null) {
303  0 if (log.isDebugEnabled()) {
304  0 log.debug("validate: issue project is null: " + issue);
305    }
306  0 errors.add(ActionMessages.GLOBAL_MESSAGE,
307    new ActionMessage(
308    "itracker.web.error.invalidproject"));
309  0 } else if (errors.isEmpty()
310    && issue.getProject().getStatus() != Status.ACTIVE) {
311  0 if (log.isDebugEnabled()) {
312  0 log.debug("validate: issue project is not active: " + issue);
313    }
314  0 errors.add(ActionMessages.GLOBAL_MESSAGE,
315    new ActionMessage(
316    "itracker.web.error.projectlocked"));
317  0 } else if (errors.isEmpty()) {
318  0 if (log.isDebugEnabled()) {
319  0 log.debug("validate: validation had errors for " + issue + ": " + errors);
320    }
321  0 validateProjectScripts(issue.getProject(), errors, this);
322  0 validateAttachment(this.getAttachment(), getITrackerServices(), errors);
323    }
324    } else {
325  0 CreateIssuePTO.setupCreateIssue(request);
326  0 HttpSession session = request.getSession();
327  0 Project project = (Project) session
328    .getAttribute(Constants.PROJECT_KEY);
329  0 if (log.isDebugEnabled()) {
330  0 log.debug("validate: validating create new issue for project: " + page);
331    }
332  0 validateProjectFields(project, request, errors);
333  0 validateProjectScripts(project, errors, this);
334  0 validateAttachment(this.getAttachment(), getITrackerServices(), errors);
335    }
336    } catch (Exception e) {
337  0 e.printStackTrace();
338  0 log.error("validate: unexpected exception", e);
339  0 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
340    "itracker.web.error.system"));
341    }
342  0 if (log.isDebugEnabled()) {
343  0 log.debug("validate: returning errors: " + errors);
344    }
345  0 return errors;
346    }
347   
 
348  0 toggle private static void validateAttachment(FormFile attachment, ITrackerServices services, ActionMessages errors) {
349  0 if (null != attachment) {
350  0 ActionMessages msg = AttachmentUtilities.validate(attachment, services);
351  0 if (!msg.isEmpty()) {
352  0 if (log.isDebugEnabled()) {
353  0 log.debug("validateAttachment: failed to validate, " + msg);
354    }
355  0 errors.add(msg);
356    }
357    }
358    }
359   
 
360  0 toggle private static void validateProjectFields(Project project,
361    HttpServletRequest request, ActionErrors errors) {
362  0 List<CustomField> projectFields = project.getCustomFields();
363  0 if (null != projectFields && projectFields.size() > 0) {
364  0 HttpSession session = request.getSession();
365   
366  0 Locale locale = ITrackerResources.getLocale();
367  0 if (session != null) {
368  0 locale = (Locale) session.getAttribute(Constants.LOCALE_KEY);
369    }
370   
371  0 ResourceBundle bundle = ITrackerResources.getBundle(locale);
372  0 Iterator<CustomField> it = projectFields.iterator();
373  0 while (it.hasNext()) {
374  0 CustomField customField = it.next();
375  0 String fieldValue = request.getParameter("customFields("
376    + customField.getId() + ")");
377  0 if (fieldValue != null && !fieldValue.equals("")) {
378   
379    // Don't create an IssueField only so that we can call
380    // setValue to validate the value!
381    // IssueField issueField = new
382    // IssueField(projectFields.get(i));
383  0 try {
384  0 customField.checkAssignable(fieldValue, locale, bundle);
385    } catch (IssueException ie) {
386  0 String label = CustomFieldUtilities.getCustomFieldName(
387    customField.getId(), locale);
388  0 errors.add(ActionMessages.GLOBAL_MESSAGE,
389    new ActionMessage(ie.getType(), label));
390    }
391  0 } else if (customField.isRequired()) {
392  0 String label = CustomFieldUtilities.getCustomFieldName(
393    customField.getId(), locale);
394  0 errors.add(ActionMessages.GLOBAL_MESSAGE,
395    new ActionMessage(IssueException.TYPE_CF_REQ_FIELD,
396    label));
397    }
398    }
399    }
400    }
401   
 
402  0 toggle private static void validateProjectScripts(Project project, ActionErrors errors, IssueForm form)
403    throws WorkflowException {
404   
405  0 List<ProjectScript> scripts = project.getScripts();
406  0 WorkflowUtilities.processFieldScripts(scripts,
407    WorkflowUtilities.EVENT_FIELD_ONVALIDATE, null, errors, form);
408    }
409   
410    }