View Javadoc

1   package com.atlassian.jira.web.action.issue;
2   
3   import java.util.Iterator;
4   import java.util.Map;
5   
6   import org.apache.commons.lang.exception.ExceptionUtils;
7   
8   import com.atlassian.jira.exception.CreateException;
9   import com.atlassian.jira.issue.IssueFactory;
10  import com.opensymphony.workflow.InvalidInputException;
11  
12  /**
13   * FQCN : com.atlassian.jira.web.action.issue.ExtendedCreateIssueDetails
14   * @author Kaamelot
15   * @since 3.10.1.30
16   * Description : Extend default CreateIssueDetails in order to extract possible InvalidInputException  
17   */
18  public class ExtendedCreateIssueDetails extends CreateIssueDetails {
19  
20  	/**	 */
21  	private static final long serialVersionUID = 1L;
22  	
23  	public ExtendedCreateIssueDetails(IssueFactory _issueFactory, IssueCreationHelperBean _issueCreationHelperBean) {
24  		super(_issueFactory, _issueCreationHelperBean);
25  	}
26  
27  	/**
28  	 *  Extend doExecute() in order to extract possible InvalidInputException
29  	 */
30  	protected String doExecute() throws Exception {
31  		try {
32  			issueCreationHelperBean.updateIssueFromFieldValuesHolder(getFieldScreenRenderer(), getRemoteUser(), getProject(), getIssuetype(), getIssueObject(), getCustomFieldValuesHolder());
33  
34  			createIssue();
35  
36  			return doPostCreationTasks();
37  		} catch (CreateException e) {
38  			analyseException(e);
39  			return ERROR;
40  		}
41  	}
42  
43  	/** Analyse Exception in order to raise Errors to GUI
44  	 * @param e
45  	 */
46  	protected void analyseException(Throwable e) {
47  		if (extractInvalidInputException(e)) {
48  			addErrorMessage(getText("admin.errors.issues.error.creating","..."));
49  		} else if (e instanceof CreateException) {
50  			log.error(e, e);
51  			String errMsg = getText("admin.errors.issues.error.creating", e.getMessage());
52  			addErrorMessage(errMsg);
53  		} else {
54  			log.error(e, e);
55  			addErrorMessage((e.getMessage() != null ? e.getMessage() : ExceptionUtils.getFullStackTrace(e)));
56  		}
57  	}
58  
59  	/** Extract recursively the InvalidInputException and push they Error by Field Id.
60  	 * @param t Throwable to analyse 
61  	 */
62  	protected boolean extractInvalidInputException(Throwable t) {
63  		boolean hasInvalidInputException = false;
64  		if (t instanceof InvalidInputException) {
65  			hasInvalidInputException = true;
66  			InvalidInputException iie = (InvalidInputException) t;
67  			for (Iterator iterator = iie.getErrors().entrySet().iterator(); iterator.hasNext();) {
68  				Map.Entry entry = (Map.Entry) iterator.next();
69  				addError((String) entry.getKey(), (String) entry.getValue());
70  			}
71  		}
72  		if (t.getCause() != null) {
73  			hasInvalidInputException = hasInvalidInputException || extractInvalidInputException(t.getCause());
74  		}
75  		return hasInvalidInputException;
76  	}
77  
78  }