View Javadoc

1   package com.atlassian.jira.jelly;
2   
3   import org.apache.commons.jelly.JellyContext;
4   import org.apache.commons.jelly.Tag;
5   import org.apache.log4j.Category;
6   import org.ofbiz.core.entity.GenericEntityException;
7   import org.ofbiz.core.entity.GenericValue;
8   
9   import com.atlassian.jira.ManagerFactory;
10  
11  public class ParentIssueContextAccessorImpl implements IssueContextAccessor, IssueAware {
12  
13  	// issue constants
14  	public static final String PARENT_ISSUE_ID = "jelly.parent.issue.id";
15  	public static final String PARENT_ISSUE_KEY = "jelly.parent.issue.key";
16  
17  	private static final Category log = Category.getInstance(IssueContextAccessorImpl.class);
18  	private boolean hasIssueId = false;
19  	private Long issueId = null;
20  	private final Tag tag;
21  
22  	public ParentIssueContextAccessorImpl(Tag tag) {
23  		this.tag = tag;
24  	}
25  
26  	public void setIssue(Long issueId) {
27  		setPreviousIssue();
28  		resetIssueContext();
29  		setIssueContext(issueId);
30  	}
31  
32  	public void setIssue(String issueKey) {
33  		setPreviousIssue();
34  		resetIssueContext();
35  		setIssueContext(issueKey);
36  	}
37  
38  	public void setIssue(GenericValue issue) {
39  		setPreviousIssue();
40  		resetIssueContext();
41  		setIssueContext(issue);
42  	}
43  
44  	public void loadPreviousIssue() {
45  		if (hasIssueId) {
46  			resetIssueContext();
47  			setIssueContext(issueId);
48  			hasIssueId = false;
49  			issueId = null;
50  		}
51  	}
52  
53  	private void setPreviousIssue() {
54  		// Store the old project
55  		if (hasIssue()) {
56  			hasIssueId = true;
57  			this.issueId = getIssueId();
58  		}
59  	}
60  
61  	private void resetIssueContext() {
62  		// Reset the current context
63  		getContext().removeVariable(PARENT_ISSUE_ID);
64  		getContext().removeVariable(PARENT_ISSUE_KEY);
65  	}
66  
67  	private void setIssueContext(Long issueId) {
68  		final GenericValue project = ManagerFactory.getIssueManager().getIssue(issueId);
69  		setIssueContext(project);
70  	}
71  
72  	private void setIssueContext(String issueKey) {
73  		try {
74  			final GenericValue project = ManagerFactory.getIssueManager().getIssue(issueKey);
75  			setIssueContext(project);
76  		} catch (GenericEntityException e) {
77  			log.error(e, e);
78  		}
79  	}
80  
81  	private void setIssueContext(GenericValue issue) {
82  		// Retrieve the new issue
83  		if (issue != null) {
84  			getContext().setVariable(PARENT_ISSUE_ID, issue.getLong("id"));
85  			getContext().setVariable(PARENT_ISSUE_KEY, issue.getString("key"));
86  		}
87  	}
88  
89  	public JellyContext getContext() {
90  		return tag.getContext();
91  	}
92  
93  	public boolean hasIssue() {
94  		return getContext().getVariables().containsKey(PARENT_ISSUE_ID);
95  	}
96  
97  	public Long getIssueId() {
98  		if (hasIssue())
99  			return (Long) getContext().getVariable(PARENT_ISSUE_ID);
100 		else
101 			return null;
102 	}
103 
104 	public GenericValue getIssue() {
105 		return ManagerFactory.getIssueManager().getIssue(getIssueId());
106 	}
107 }