PageRenderTime 58ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/ahmed_bilal_360factors/jira7-core
Java | 238 lines | 191 code | 37 blank | 10 comment | 22 complexity | 2c0a253d1192fc276c1dd9d32b6134bd MD5 | raw file
Possible License(s): Apache-2.0
  1. package com.atlassian.jira.web.action.issue;
  2. import com.atlassian.jira.bc.issue.IssueService;
  3. import com.atlassian.jira.bc.issue.comment.CommentService;
  4. import com.atlassian.jira.config.SubTaskManager;
  5. import com.atlassian.jira.exception.IssueNotFoundException;
  6. import com.atlassian.jira.exception.IssuePermissionException;
  7. import com.atlassian.jira.issue.Issue;
  8. import com.atlassian.jira.issue.IssueInputParameters;
  9. import com.atlassian.jira.issue.customfields.OperationContext;
  10. import com.atlassian.jira.issue.fields.screen.FieldScreenRenderLayoutItem;
  11. import com.atlassian.jira.issue.fields.screen.FieldScreenRenderTab;
  12. import com.atlassian.jira.issue.fields.screen.FieldScreenRenderer;
  13. import com.atlassian.jira.issue.fields.screen.FieldScreenRendererFactory;
  14. import com.atlassian.jira.issue.operation.IssueOperation;
  15. import com.atlassian.jira.issue.operation.WorkflowIssueOperationImpl;
  16. import com.atlassian.jira.security.plugin.ProjectPermissionKey;
  17. import com.atlassian.jira.security.xsrf.RequiresXsrfCheck;
  18. import com.atlassian.jira.user.util.UserUtil;
  19. import com.atlassian.jira.util.JiraUtils;
  20. import com.atlassian.jira.web.action.issue.util.ScreenTabErrorHelper;
  21. import com.atlassian.jira.web.action.workflow.WorkflowAwareAction;
  22. import com.atlassian.jira.web.action.workflow.WorkflowUIDispatcher;
  23. import com.atlassian.jira.workflow.IssueWorkflowManager;
  24. import com.atlassian.jira.workflow.WorkflowTransitionUtil;
  25. import com.atlassian.jira.workflow.WorkflowTransitionUtilImpl;
  26. import com.opensymphony.workflow.loader.ActionDescriptor;
  27. import webwork.action.ActionContext;
  28. import java.util.Collection;
  29. import java.util.Collections;
  30. import java.util.HashMap;
  31. import java.util.List;
  32. import java.util.Map;
  33. import java.util.Set;
  34. import java.util.SortedSet;
  35. import java.util.TreeSet;
  36. /**
  37. * This beautifully named action is in fact the Issue Transition screen. When a workflow has a screen
  38. * this action is invoked.
  39. * <p>
  40. * We really should rename this one day to say...oooh I dont know....TransitionIssue!
  41. */
  42. public class CommentAssignIssue extends AbstractCommentableAssignableIssue
  43. implements OperationContext, WorkflowAwareAction {
  44. private static final Set<String> EITHER_CURRENT_OR_FUTURE_STATE_PERMISSIONS = Collections.singleton("ADD_COMMENTS");
  45. private final IssueWorkflowManager issueWorkflowManager;
  46. private WorkflowTransitionUtil workflowTransitionUtil;
  47. private final IssueService issueService;
  48. private final Map fieldValuesHolder;
  49. private IssueService.TransitionValidationResult transitionResult;
  50. private int action;
  51. private SortedSet<FieldScreenRenderTab> tabsWithErrors;
  52. private int selectedTab;
  53. public CommentAssignIssue(final SubTaskManager subTaskManager, final FieldScreenRendererFactory fieldScreenRendererFactory,
  54. final CommentService commentService, final IssueService issueService,
  55. final UserUtil userUtil, final IssueWorkflowManager issueWorkflowManager) {
  56. super(subTaskManager, fieldScreenRendererFactory, commentService, userUtil);
  57. this.issueService = issueService;
  58. this.issueWorkflowManager = issueWorkflowManager;
  59. fieldValuesHolder = new HashMap();
  60. }
  61. protected WorkflowTransitionUtil getWorkflowTransitionUtil() {
  62. if (workflowTransitionUtil == null) {
  63. workflowTransitionUtil = JiraUtils.loadComponent(WorkflowTransitionUtilImpl.class);
  64. workflowTransitionUtil.setIssue(getMutableIssue());
  65. workflowTransitionUtil.setAction(getAction());
  66. }
  67. return workflowTransitionUtil;
  68. }
  69. public String doDefault() throws Exception {
  70. try {
  71. super.doDefault();
  72. } catch (IssueNotFoundException e) {
  73. return ISSUE_PERMISSION_ERROR;
  74. } catch (IssuePermissionException e) {
  75. return ISSUE_PERMISSION_ERROR;
  76. }
  77. for (FieldScreenRenderTab fieldScreenRenderTab : getFieldScreenRenderer().getFieldScreenRenderTabs()) {
  78. for (FieldScreenRenderLayoutItem fieldScreenRenderLayoutItem : fieldScreenRenderTab.getFieldScreenRenderLayoutItems()) {
  79. if (fieldScreenRenderLayoutItem.isShow(getIssueObject())) {
  80. fieldScreenRenderLayoutItem.populateFromIssue(getFieldValuesHolder(), getIssueObject());
  81. }
  82. }
  83. }
  84. // validate the transition is still valid else go back to issue screen
  85. if (invalidAction()) {
  86. return WorkflowUIDispatcher.INVALID_ACTION;
  87. }
  88. return INPUT;
  89. }
  90. protected boolean invalidAction() {
  91. return !issueWorkflowManager.isValidAction(getIssueObject(), action, getLoggedInUser());
  92. }
  93. protected FieldScreenRenderer getFieldScreenRenderer() {
  94. return getWorkflowTransitionUtil().getFieldScreenRenderer();
  95. }
  96. public List getFieldScreenRenderTabs() {
  97. return getFieldScreenRenderer().getFieldScreenRenderTabs();
  98. }
  99. public Collection getTabsWithErrors() {
  100. if (tabsWithErrors == null) {
  101. initTabsWithErrors();
  102. }
  103. return tabsWithErrors;
  104. }
  105. private void initTabsWithErrors() {
  106. tabsWithErrors = new TreeSet<>();
  107. selectedTab = new ScreenTabErrorHelper().initialiseTabsWithErrors(tabsWithErrors, getErrors(), getFieldScreenRenderer(), ActionContext.getParameters());
  108. }
  109. public int getSelectedTab() {
  110. // Init tabs - as the first tab with error will be calculated then.
  111. if (tabsWithErrors == null) {
  112. initTabsWithErrors();
  113. }
  114. return selectedTab;
  115. }
  116. protected void doValidation() {
  117. try {
  118. //just checking that the issue exists and that the user has permission to see it.
  119. getIssue();
  120. } catch (IssuePermissionException ipe) {
  121. return;
  122. } catch (IssueNotFoundException infe) {
  123. return;
  124. }
  125. final IssueInputParameters issueInputParameters = issueService.newIssueInputParameters(ActionContext.getParameters());
  126. issueInputParameters.setRetainExistingValuesWhenParameterNotProvided(false);
  127. transitionResult = issueService.validateTransition(getLoggedInUser(), getIssueObject().getId(), action, issueInputParameters);
  128. setFieldValuesHolder(transitionResult.getFieldValuesHolder());
  129. if (!transitionResult.isValid()) {
  130. addErrorCollection(transitionResult.getErrorCollection());
  131. }
  132. }
  133. @RequiresXsrfCheck
  134. protected String doExecute() throws Exception {
  135. if (invalidAction()) {
  136. return WorkflowUIDispatcher.INVALID_ACTION;
  137. }
  138. final IssueService.IssueResult transitionResult = issueService.transition(getLoggedInUser(), this.transitionResult);
  139. if (!transitionResult.isValid()) {
  140. addErrorCollection(transitionResult.getErrorCollection());
  141. return ERROR;
  142. }
  143. if (isInlineDialogMode()) {
  144. return returnComplete();
  145. }
  146. return redirectToView();
  147. }
  148. public int getAction() {
  149. return action;
  150. }
  151. public void setAction(int action) {
  152. this.action = action;
  153. }
  154. public String getI18nTextViaMetaAttr(String key, Object defaultValue) {
  155. final Object i18nKey = getActionDescriptor().getMetaAttributes().get(key);
  156. String localizedString;
  157. if ((i18nKey != null) && (i18nKey instanceof String)) {
  158. localizedString = getText((String) i18nKey);
  159. if (!i18nKey.equals(localizedString)) {
  160. return localizedString;
  161. } else {
  162. log.warn("The i18n key" + i18nKey + " in property '" + key + "' for this transition does not contain a valid i18n key.");
  163. }
  164. }
  165. return defaultValue == null ? "" : defaultValue.toString();
  166. }
  167. public ActionDescriptor getActionDescriptor() {
  168. return getWorkflowTransitionUtil().getActionDescriptor();
  169. }
  170. public Map getCustomFieldValuesHolder() {
  171. return fieldValuesHolder;
  172. }
  173. public IssueOperation getIssueOperation() {
  174. return new WorkflowIssueOperationImpl(getActionDescriptor());
  175. }
  176. public Collection getIgnoreFieldIds() {
  177. return Collections.emptyList();
  178. }
  179. public String getWorkflowTransitionDisplayName() {
  180. return getWorkflowTransitionDisplayName(getActionDescriptor());
  181. }
  182. @Override
  183. public Map<String, Object> getDisplayParams() {
  184. final Map<String, Object> displayParams = new HashMap<String, Object>(super.getDisplayParams());
  185. displayParams.put("theme", "aui");
  186. return displayParams;
  187. }
  188. @Override
  189. public boolean hasIssuePermission(String permissionKey, Issue issue) {
  190. return hasIssuePermission(new ProjectPermissionKey(permissionKey), issue);
  191. }
  192. @Override
  193. public boolean hasIssuePermission(final ProjectPermissionKey permissionKey, final Issue issue) {
  194. if (!EITHER_CURRENT_OR_FUTURE_STATE_PERMISSIONS.contains(permissionKey.permissionKey())) {
  195. return super.hasIssuePermission(permissionKey, issue);
  196. }
  197. // JRA-44123 Allow user to comment if they have permission on either the old status or the new status
  198. return getPermissionManager().hasPermission(permissionKey, issue, getLoggedInUser())
  199. || getPermissionManager().hasPermission(permissionKey, issue, getLoggedInUser(), getActionDescriptor());
  200. }
  201. }