PageRenderTime 65ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/jira-project/jira-components/jira-core/src/main/java/com/atlassian/jira/web/action/issue/AbstractWorklogAction.java

https://bitbucket.org/ahmed_bilal_360factors/jira7-core
Java | 253 lines | 204 code | 40 blank | 9 comment | 19 complexity | 89a75ae5a22e7173268dbfa65a70f8be MD5 | raw file
Possible License(s): Apache-2.0
  1. package com.atlassian.jira.web.action.issue;
  2. import com.atlassian.jira.bc.issue.comment.CommentService;
  3. import com.atlassian.jira.bc.issue.worklog.WorklogService;
  4. import com.atlassian.jira.config.FeatureManager;
  5. import com.atlassian.jira.datetime.DateTimeFormatterFactory;
  6. import com.atlassian.jira.datetime.DateTimeStyle;
  7. import com.atlassian.jira.exception.DataAccessException;
  8. import com.atlassian.jira.issue.Issue;
  9. import com.atlassian.jira.issue.IssueFieldConstants;
  10. import com.atlassian.jira.issue.RendererManager;
  11. import com.atlassian.jira.issue.fields.CommentVisibility;
  12. import com.atlassian.jira.issue.fields.layout.field.FieldLayoutItem;
  13. import com.atlassian.jira.issue.fields.layout.field.FieldLayoutManager;
  14. import com.atlassian.jira.plugin.renderer.JiraRendererModuleDescriptor;
  15. import com.atlassian.jira.security.roles.ProjectRole;
  16. import com.atlassian.jira.security.roles.ProjectRoleManager;
  17. import com.atlassian.jira.user.util.UserUtil;
  18. import com.atlassian.jira.util.JiraDurationUtils;
  19. import com.atlassian.jira.util.collect.MapBuilder;
  20. import com.atlassian.jira.web.FieldVisibilityManager;
  21. import com.opensymphony.util.TextUtils;
  22. import java.util.Collection;
  23. import java.util.Collections;
  24. import java.util.Date;
  25. import java.util.Map;
  26. public abstract class AbstractWorklogAction extends AbstractIssueSelectAction {
  27. private Long worklogId;
  28. private String timeLogged;
  29. private String startDate;
  30. private String workType;
  31. private String newEstimate;
  32. private String adjustmentAmount;
  33. private String commentLevel;
  34. private String comment;
  35. // how to adjust the timeestimate - allowed values are auto, new, manual & leave
  36. protected static final String ADJUST_ESTIMATE_AUTO = "auto";
  37. protected static final String ADJUST_ESTIMATE_NEW = "new";
  38. protected static final String ADJUST_ESTIMATE_MANUAL = "manual";
  39. // selected adjustment type - defaults to auto
  40. protected String adjustEstimate = ADJUST_ESTIMATE_AUTO;
  41. protected final WorklogService worklogService;
  42. protected final CommentService commentService;
  43. protected final ProjectRoleManager projectRoleManager;
  44. protected final JiraDurationUtils jiraDurationUtils;
  45. protected final DateTimeFormatterFactory dateTimeFormatterFactory;
  46. protected final FieldVisibilityManager fieldVisibilityManager;
  47. private final FieldLayoutManager fieldLayoutManager;
  48. private final RendererManager rendererManager;
  49. private final UserUtil userUtil;
  50. private final FeatureManager featureManager;
  51. private CommentVisibility commentVisibility;
  52. public AbstractWorklogAction(WorklogService worklogService, CommentService commentService,
  53. ProjectRoleManager projectRoleManager, JiraDurationUtils jiraDurationUtils,
  54. final FieldVisibilityManager fieldVisibilityManager,
  55. final FieldLayoutManager fieldLayoutManager, final RendererManager rendererManager,
  56. UserUtil userUtil, final FeatureManager featureManager,
  57. DateTimeFormatterFactory dateTimeFormatterFactory) {
  58. this.worklogService = worklogService;
  59. this.commentService = commentService;
  60. this.projectRoleManager = projectRoleManager;
  61. this.jiraDurationUtils = jiraDurationUtils;
  62. this.fieldVisibilityManager = fieldVisibilityManager;
  63. this.fieldLayoutManager = fieldLayoutManager;
  64. this.rendererManager = rendererManager;
  65. this.userUtil = userUtil;
  66. this.featureManager = featureManager;
  67. this.dateTimeFormatterFactory = dateTimeFormatterFactory;
  68. }
  69. public String getComment() {
  70. return comment;
  71. }
  72. public String getEstimate() throws Exception {
  73. final Long estimate = getIssueObject().getEstimate();
  74. return estimate == null ? null : jiraDurationUtils.getFormattedDuration(estimate);
  75. }
  76. public boolean isTimeTrackingFieldHidden(Issue issue) {
  77. return fieldVisibilityManager.isFieldHidden(IssueFieldConstants.TIMETRACKING, issue);
  78. }
  79. public String getTimeLogged() {
  80. return timeLogged;
  81. }
  82. public void setTimeLogged(String timeLogged) {
  83. this.timeLogged = timeLogged;
  84. }
  85. public String getWorkType() {
  86. return workType;
  87. }
  88. public void setWorkType(String workType) {
  89. this.workType = workType;
  90. }
  91. public String getNewEstimate() {
  92. return newEstimate;
  93. }
  94. public void setNewEstimate(String newEstimate) {
  95. this.newEstimate = newEstimate;
  96. }
  97. public String getAdjustmentAmount() {
  98. return adjustmentAmount;
  99. }
  100. public void setAdjustmentAmount(final String adjustmentAmount) {
  101. this.adjustmentAmount = adjustmentAmount;
  102. }
  103. public String getAdjustEstimate() {
  104. return adjustEstimate;
  105. }
  106. public void setAdjustEstimate(String adjustEstimate) {
  107. this.adjustEstimate = adjustEstimate;
  108. }
  109. public boolean isLevelSelected(String visibilityLevel) {
  110. return getCommentLevel() != null && getCommentLevel().equals(visibilityLevel);
  111. }
  112. public String getSelectedLevelName() {
  113. if (getCommentLevel() == null) {
  114. return getText("security.level.viewable.by.all");
  115. }
  116. final Collection<ProjectRole> roleLevels = getRoleLevels();
  117. for (ProjectRole roleLevel : roleLevels) {
  118. if (getCommentLevel().equals("role:" + roleLevel.getId().toString())) {
  119. return getText("security.level.restricted.to", TextUtils.htmlEncode(roleLevel.getName()));
  120. }
  121. }
  122. final Collection<String> groupLevels = getGroupLevels();
  123. for (String groupLevel : groupLevels) {
  124. if (getCommentLevel().equals("group:" + groupLevel)) {
  125. return getText("security.level.restricted.to", TextUtils.htmlEncode(groupLevel));
  126. }
  127. }
  128. return getText("security.level.viewable.by.all");
  129. }
  130. public String getCommentLevel() {
  131. return commentLevel;
  132. }
  133. public void setCommentLevel(String commentLevel) {
  134. this.commentLevel = commentLevel;
  135. }
  136. public void setComment(String comment) {
  137. this.comment = comment;
  138. }
  139. public Collection getGroupLevels() {
  140. Collection groups;
  141. if (getLoggedInUser() == null || !commentService.isGroupVisibilityEnabled()) {
  142. groups = Collections.emptyList();
  143. } else {
  144. groups = userUtil.getGroupNamesForUser(getLoggedInUser().getName());
  145. }
  146. return groups;
  147. }
  148. public Collection<ProjectRole> getRoleLevels() {
  149. Collection<ProjectRole> roleLevels;
  150. if (commentService.isProjectRoleVisibilityEnabled()) {
  151. roleLevels = projectRoleManager.getProjectRoles(getLoggedInUser(), getIssueObject().getProjectObject());
  152. } else {
  153. roleLevels = Collections.emptyList();
  154. }
  155. return roleLevels;
  156. }
  157. public String getStartDate() {
  158. return startDate;
  159. }
  160. public void setStartDate(String startDate) {
  161. this.startDate = startDate;
  162. }
  163. protected CommentVisibility getCommentVisibility() {
  164. if (commentVisibility == null) {
  165. commentVisibility = new CommentVisibility(commentLevel);
  166. }
  167. return commentVisibility;
  168. }
  169. protected Date getParsedStartDate() {
  170. try {
  171. return (getStartDate() == null) ? null : dateTimeFormatterFactory.formatter().forLoggedInUser()
  172. .withStyle(DateTimeStyle.DATE_TIME_PICKER).parse(getStartDate());
  173. } catch (IllegalArgumentException e) {
  174. // Its cool to let this be null the service validation will add the correct error
  175. return null;
  176. }
  177. }
  178. protected String getFormattedStartDate(Date date) {
  179. return dateTimeFormatterFactory.formatter().forLoggedInUser()
  180. .withStyle(DateTimeStyle.DATE_TIME_PICKER).format(date);
  181. }
  182. public Long getWorklogId() {
  183. return worklogId;
  184. }
  185. public void setWorklogId(Long worklogId) {
  186. this.worklogId = worklogId;
  187. }
  188. /**
  189. * Renders the input control for the Work Description (i.e. Comment) of the Log Work form. The control will be different
  190. * depending on which renderer is chosen for the Log Work field for this specific issue context.
  191. *
  192. * @return the HTML of the input control to be displayed on the form.
  193. */
  194. public String getWorkDescriptionEditHtml() {
  195. try {
  196. final String rendererType = getRendererType();
  197. final JiraRendererModuleDescriptor rendererDescriptor = rendererManager.getRendererForType(rendererType).getDescriptor();
  198. final Map<Object, Object> rendererParams = MapBuilder.newBuilder().add("rows", "10").add("cols", "60").add("wrap", "virtual").add("class", "long-field").toMutableMap();
  199. return rendererDescriptor.getEditVM(getComment(), getIssueObject().getKey(), rendererType, "comment", "comment", rendererParams, false);
  200. } catch (DataAccessException e) {
  201. log.error("Could not render edit template for work description", e);
  202. return "";
  203. }
  204. }
  205. public String getRendererType() {
  206. final FieldLayoutItem worklogFieldLayoutItem = fieldLayoutManager.getFieldLayout(getIssueObject()).getFieldLayoutItem(IssueFieldConstants.WORKLOG);
  207. return (worklogFieldLayoutItem != null) ? worklogFieldLayoutItem.getRendererType() : null;
  208. }
  209. @Deprecated
  210. public boolean isOnDemand() {
  211. return false;
  212. }
  213. }