View Javadoc

1   /**
2    * Author  : � Kaamelot - 2006
3    * Projet  : jira-plugin-kaamelot-reporter
4    * Fichier : DefaultIssueBeanHelper.java
5    */
6   package com.atlassian.jira.plugin.report;
7   
8   import java.net.MalformedURLException;
9   import java.net.URL;
10  import java.sql.Timestamp;
11  import java.util.Date;
12  
13  import javax.servlet.http.HttpServletRequest;
14  
15  import org.ofbiz.core.entity.GenericValue;
16  
17  import webwork.action.ActionContext;
18  
19  import com.atlassian.jira.ComponentManager;
20  import com.atlassian.jira.ManagerFactory;
21  import com.atlassian.jira.config.KaamelotKeys;
22  import com.atlassian.jira.config.properties.APKeys;
23  import com.atlassian.jira.config.properties.ApplicationProperties;
24  import com.atlassian.jira.issue.CustomFieldManager;
25  import com.atlassian.jira.issue.Issue;
26  import com.atlassian.jira.issue.IssueConstant;
27  import com.atlassian.jira.issue.IssueManager;
28  import com.atlassian.jira.issue.customfields.impl.DateCFType;
29  import com.atlassian.jira.issue.customfields.impl.DateTimeCFType;
30  import com.atlassian.jira.issue.fields.CustomField;
31  import com.atlassian.jira.issue.worklog.WorklogHelper;
32  import com.atlassian.jira.project.ProjectManager;
33  import com.atlassian.jira.web.util.IssueTableBean;
34  import com.opensymphony.user.User;
35  import com.opensymphony.user.UserManager;
36  import com.opensymphony.util.TextUtils;
37  
38  import fr.kaamelot.addon.PluginInfos;
39  
40  /**
41   * Author  : � Kaamelot - 2006
42   * Projet  : jira-plugin-kaamelot-reporter
43   * Fichier : IssueBeanHelper.java
44   * Description :
45   * 
46   */
47  public abstract class AbstractIssueBeanHelper extends IssueTableBean {
48  
49  	protected static ProjectManager projectManager;
50  	protected static UserManager userManager = UserManager.getInstance();
51  	protected static IssueManager issueManager = ManagerFactory.getIssueManager();
52  	protected static WorklogHelper worklogHelper = (WorklogHelper)ComponentManager.getInstance().getContainer().getComponentInstanceOfType(WorklogHelper.class);
53  	protected static CustomFieldManager customFieldManager = ManagerFactory.getCustomFieldManager();
54  
55  	protected final static ApplicationProperties applicationProperties = ManagerFactory.getApplicationProperties();
56  
57  	boolean hasMultiURL = false;
58  	protected static String JiraUrlBase;
59  
60  	protected final static int SECOND = 1;
61  	protected final static int MINUTE = 60 * SECOND;
62  	protected final static int HOUR = 60 * MINUTE;
63  	
64  	protected static int hoursPerDay = new Integer(applicationProperties.getDefaultBackedString(APKeys.JIRA_TIMETRACKING_HOURS_PER_DAY)).intValue();
65  	protected static int daysPerWeek = new Integer(applicationProperties.getDefaultBackedString(APKeys.JIRA_TIMETRACKING_DAYS_PER_WEEK)).intValue();
66  	protected static int secondsInDay = hoursPerDay * HOUR;
67  	
68  	/** Default Constructor */
69  	protected AbstractIssueBeanHelper() {
70  		projectManager = ManagerFactory.getProjectManager();
71  		hasMultiURL = applicationProperties.getOption(KaamelotKeys.ALLOW_REPORT_ON_CUSTOMFIELD);
72  	}
73  
74  	// Issue Field
75  
76  	public String getSummary(final Issue _issue) throws Exception {
77  		return _issue.getSummary();
78  	}
79  	
80  	public String getIssueKey(final Issue _issue) throws Exception {
81  		return _issue.getKey();
82  	}
83  	
84  	public String getJIRAUrlBase() {
85  		if (JiraUrlBase==null || hasMultiURL) {
86  			HttpServletRequest request = ActionContext.getRequest();
87  			String protocol = "http";
88  			try {
89  				URL url = new URL(request.getRequestURL().toString());
90  				protocol = url.getProtocol();
91  			} catch (MalformedURLException e) {
92  			} 
93  			JiraUrlBase = protocol + "://" + request.getServerName() + ":" + request.getServerPort() +	request.getContextPath();
94  		}
95  		return JiraUrlBase;
96  	}
97  	
98  	public String getHtmlURLtoIssue(final String _issueKey) throws Exception {
99  		return getHtmlURLtoIssue(_issueKey, _issueKey);
100 	}
101 	
102 	public String getHtmlURLtoIssue(final String _issueKey, final String _issueLabel) throws Exception {
103 		return "<a href=\"" + getJIRAUrlBase() + "/browse/" + _issueKey + "\"> " + TextUtils.htmlEncode(_issueLabel) + "</a>";
104 	}
105 	
106 	public String getHtmlURLtoIssueOnSummary(final Issue _issue) throws Exception {
107 		return "<a href=\"" + getJIRAUrlBase() + "/browse/" + _issue.getKey() + "\" title =\""+ _issue.getKey() + "\"> " + TextUtils.htmlEncode(_issue.getSummary()) + "</a>";
108 	}
109 	
110 	public String getHtmlURLtoIssueOnKey(final Issue _issue) throws Exception {
111 		return "<a href=\"" + getJIRAUrlBase() + "/browse/" + _issue.getKey() + "\" title =\""+ TextUtils.htmlEncode(_issue.getSummary()) + "\"> " + _issue.getKey() + "</a>";
112 	}
113 	
114 	public String getHtmlURLtoProject(final Long _projectId) throws Exception {
115 		String projectName = projectManager.getProject(_projectId).getString("name");
116 		return "<a href=\"" + getJIRAUrlBase() + "/secure/BrowseProject.jspa?id=" + _projectId + "\"> " + projectName + "</a>";
117 	}
118 	
119 	public String getHtmlURLtoProject(final String _projectId, final String _projectName) throws Exception {
120 		return "<a href=\"" + getJIRAUrlBase() + "/secure/BrowseProject.jspa?id=" + _projectId + "\"> " + _projectName + "</a>";
121 	}
122 	
123 	public String getHtmlURLtoEditWorkLog(final Issue _issue) throws Exception {
124 		StringBuffer imgSrc = new StringBuffer();
125 
126 		imgSrc.append("<img src=\"").append(getJIRAUrlBase()).append(PluginInfos.getPluginDownloadPath() + "/icons/clock.gif\"")
127 				.append("\" title=\"").append("=\"Edit Worklog\"")
128 				.append("\" border='0'>");
129 		return "<a href=\"" + getJIRAUrlBase() + "/secure/issue/views/EditIssueWorkLog!default.jspa?key=" + _issue.getKey() + "\">" + imgSrc.toString() + "</a>";
130 	}
131 
132 	public String getImgSrctoConstantIcon(final IssueConstant _issueConstant) throws Exception {
133 		StringBuffer imgSrc = new StringBuffer();
134 		
135 		imgSrc.append("<img src=\"").append(getJIRAUrlBase()).append(_issueConstant.getIconUrl())
136 				.append("\" title=\"").append(_issueConstant.getName())
137 				.append("\" border=\"0\" height=\"16\" width=\"16\">");
138 		return imgSrc.toString();
139 	}
140 
141 	public String getImgSrctoIssueTypeIcon(final Issue _issue) throws Exception {
142 		return getImgSrctoConstantIcon(_issue.getIssueTypeObject());
143 	}
144 
145 	public String getImgSrctoStatusIcon(final Issue _issue) throws Exception {
146 		return getImgSrctoConstantIcon(_issue.getStatusObject());
147 	}
148 
149 	public String getHtmlURLtoIssue(final Issue _issue) throws Exception {
150 		return getHtmlURLtoIssue(getIssueKey(_issue)); 
151 	}
152 
153 	public User getReporter(final GenericValue _issue) throws Exception {
154 		return userManager.getUser(_issue.getString("reporter"));
155 	}
156 	
157 	public User getAssignee(final Issue _issue) throws Exception {
158 		return _issue.getAssignee();
159 	}
160 
161 	public String getUserFullName(final User _user) throws Exception {
162 		return (_user!=null) ? _user.getFullName() : "";
163 	}
164 	
165 	public Timestamp getDueDate(final Issue _issue) throws Exception {
166 		return _issue.getDueDate();
167 	}
168 	
169 	public Timestamp getCreationDate(final Issue _issue) throws Exception {
170 		return _issue.getCreated();
171 	}
172 	
173 	// -- Charges in DayFraction
174 
175 	public double getOriginalEstimateDayFraction(final Issue _issue) {
176 		double seconds;
177 		seconds = worklogHelper.getOriginalEstimate(_issue);
178 		return (seconds==0) ? 0 : (seconds / secondsInDay);
179 	}
180 
181 	public double getRemainingDayFraction(final Issue _issue) {
182 		double seconds;
183 		seconds = worklogHelper.getRemainingTime(_issue);
184 		return (seconds==0) ? 0 : (seconds / secondsInDay);
185 	}
186 
187 	public double getSpentDayFraction(final Issue _issue, final Date _untilTs) {
188 		double seconds;
189 		seconds = worklogHelper.getSpentTime(_issue,_untilTs);
190 		return (seconds==0) ? 0 : (seconds / secondsInDay);
191 	}
192 
193 	public double getSpentHourFraction(final Issue _issue, final Date _untilTs) {
194 		double seconds;
195 		seconds = worklogHelper.getSpentTime(_issue,_untilTs);
196 		return (seconds==0) ? 0 : (seconds / HOUR);
197 	}
198 
199 	public long getSecondsInDay(){
200 		return secondsInDay;
201 	}
202 
203 	public static CustomField getCustomField( final String _customFieldName) throws Exception {
204 		return customFieldManager.getCustomFieldObjectByName(_customFieldName);
205 	}
206 	
207 	/**
208 	 * @param _issue Jira Issue to access
209 	 * @param _customFieldName Customfield Name Searched
210 	 * @return Customfield Value 
211 	 * @throws Exception n/a
212 	 */
213 	public Object getCustomFieldValue(final Issue _issue, final String _customFieldName) throws Exception {
214 		if (_issue == null)
215 			throw new IllegalArgumentException("Issue cannot be null.");
216 		Object value = null;
217 		CustomField cf = getCustomField(_customFieldName);
218 		if (cf!=null) {
219 			value = cf.getValue(_issue);
220 		}
221 		return value;
222 	}
223 
224 	/** 
225 	 * Permet de r�cup�rer la valeur par d�faut d'un customfield bien que la demande ne soit pas encore cr�er, 
226 	 * c'est � dire que le genericValue de l'objet Issue n'est pas encore renseign�.
227 	 *  
228 	 * @param _issue
229 	 * @param _customFieldName
230 	 * @return
231 	 * @throws Exception
232 	 */
233 	public Object getCustomFieldDefaultValue(final Issue _issue, final String _customFieldName) throws Exception {
234 		
235 		if (_issue == null)
236 			throw new IllegalArgumentException("Issue cannot be null.");
237 		
238 		Object value = null;
239 		CustomField cf = getCustomField(_customFieldName);
240 		if (cf!=null) {
241 			value = cf.getDefaultValue(_issue);
242 		}
243 		return value;
244 	}
245 	
246 	
247 	/**
248 	 * @param _issue Jira Issue to access
249 	 * @param _customFieldName Customfield Name Searched
250 	 * @return Customfield String Value 
251 	 * @throws Exception n/a
252 	 */
253 	public String getCustomFieldStringValue(final Issue _issue, final String _customFieldName) throws Exception {
254 		return (String)getCustomFieldValue(_issue, _customFieldName);
255 	}
256 
257 	public String getCustomFieldStringDefaultValue(final Issue _issue, final String _customFieldName) throws Exception {
258 		return (String)getCustomFieldDefaultValue(_issue, _customFieldName);
259 	}
260 	
261 	/**
262 	 * @param _issue Jira Issue to access
263 	 * @param _customFieldName Customfield Name Searched
264 	 * @return Customfield Date Value as Date 
265 	 * @throws Exception n/a
266 	 */
267 	public Timestamp getCustomFieldDateValue(final Issue _issue, final String _customFieldName) throws Exception {
268 		if (_issue == null)
269 			throw new IllegalArgumentException("Issue cannot be null.");
270 		Object value = null;
271 		CustomField cf = getCustomField(_customFieldName);
272 		if (cf!=null) {
273 			value = cf.getValue(_issue);
274 			if ( value==null || !(cf.getCustomFieldType() instanceof DateCFType || cf.getCustomFieldType() instanceof DateTimeCFType) ) {
275 				value = null;
276 			}
277 		}
278 		return (Timestamp)value;
279 	}
280 
281 	/**
282 	 * @param _issue Jira Issue to access
283 	 * @param _customFieldName Customfield Name Searched
284 	 * @return Customfield Date Value as Date 
285 	 * @throws Exception n/a
286 	 */
287 	public Timestamp getCustomFieldDateTimeValue(final Issue _issue, final String _customFieldName) throws Exception {
288 		return getCustomFieldDateValue(_issue, _customFieldName);
289 	}
290 	
291 	// ---------------------------
292 	// -- CustomField UserPicker -
293 	// ---------------------------
294 	
295 	public User getCFUserPicker(final Issue _issue, final String _customFieldName) throws Exception {
296 		
297 		return (User)getCustomFieldValue(_issue, _customFieldName);
298 	}
299 
300 	
301 }