PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/jira-project/jira-components/jira-core/src/main/java/com/atlassian/jira/web/action/admin/workflow/AbstractEditWorkflowTransitionDescriptorParams.java

https://bitbucket.org/ahmed_bilal_360factors/jira7-core
Java | 221 lines | 161 code | 43 blank | 17 comment | 24 complexity | bed344585f32bb0e26edf54ae685ae1f MD5 | raw file
Possible License(s): Apache-2.0
  1. package com.atlassian.jira.web.action.admin.workflow;
  2. import com.atlassian.jira.bc.workflow.WorkflowService;
  3. import com.atlassian.jira.plugin.workflow.AbstractWorkflowModuleDescriptor;
  4. import com.atlassian.jira.plugin.workflow.JiraWorkflowPluginConstants;
  5. import com.atlassian.jira.security.xsrf.RequiresXsrfCheck;
  6. import com.atlassian.jira.workflow.JiraWorkflow;
  7. import com.atlassian.plugin.PluginAccessor;
  8. import com.atlassian.plugin.PluginParseException;
  9. import com.opensymphony.util.TextUtils;
  10. import com.opensymphony.workflow.loader.AbstractDescriptor;
  11. import com.opensymphony.workflow.loader.ActionDescriptor;
  12. import com.opensymphony.workflow.loader.ConditionDescriptor;
  13. import com.opensymphony.workflow.loader.FunctionDescriptor;
  14. import com.opensymphony.workflow.loader.StepDescriptor;
  15. import com.opensymphony.workflow.loader.ValidatorDescriptor;
  16. import org.apache.commons.lang.StringUtils;
  17. import webwork.action.ActionContext;
  18. import java.util.Collection;
  19. import java.util.HashMap;
  20. import java.util.Map;
  21. /**
  22. * Copyright (c) 2002-2004
  23. * All rights reserved.
  24. */
  25. public abstract class AbstractEditWorkflowTransitionDescriptorParams extends AbstractWorkflowTransitionAction {
  26. private AbstractDescriptor workflowDescriptor;
  27. // count needs to be a String to support nested conditions blocks
  28. private String count = "";
  29. private Map descriptorParams;
  30. private AbstractWorkflowModuleDescriptor workflowModuleDescriptor;
  31. public AbstractEditWorkflowTransitionDescriptorParams(JiraWorkflow workflow, StepDescriptor step,
  32. ActionDescriptor transition, PluginAccessor pluginAccessor, WorkflowService workflowService) {
  33. super(workflow, step, transition, pluginAccessor, workflowService);
  34. }
  35. public AbstractEditWorkflowTransitionDescriptorParams(JiraWorkflow workflow, ActionDescriptor transition,
  36. PluginAccessor pluginAccessor, WorkflowService workflowService) {
  37. super(workflow, transition, pluginAccessor, workflowService);
  38. }
  39. public String doDefault() throws Exception {
  40. if (!TextUtils.stringSet(getCount())) {
  41. addErrorMessage(getText("admin.errors.workflows.cannot.find.count"));
  42. }
  43. if (getIndexCount() < 0) {
  44. addErrorMessage(getText("admin.errors.workflows.count.must.be.positive"));
  45. }
  46. if (invalidInput()) {
  47. return ERROR;
  48. }
  49. // Get the workflowDescriptor
  50. setupWorkflowDescriptor();
  51. // Get the module descriptor for the workflow descriptor
  52. setupWorkflowModuleDescriptor();
  53. // We need to display 'input' parameters template for the workflow workflowDescriptor
  54. return super.doDefault();
  55. }
  56. protected void doValidation() {
  57. // Get the workflow descriptor
  58. setupWorkflowDescriptor();
  59. // Get the module descriptor for the workflow descriptor
  60. setupWorkflowModuleDescriptor();
  61. // Setup the parameters returned from the view
  62. setupWorkflowDescriptorParams(ActionContext.getParameters());
  63. }
  64. @RequiresXsrfCheck
  65. protected String doExecute() throws Exception {
  66. // Edit the parameters of the workflow descriptor
  67. editWorkflowDescriptor(workflowDescriptor, descriptorParams);
  68. workflowService.updateWorkflow(getJiraServiceContext(), getWorkflow());
  69. return getRedirect(getRedirectUrl());
  70. }
  71. protected String getRedirectUrl() {
  72. String redirectUrl;
  73. if (getStep() == null) {
  74. redirectUrl = "ViewWorkflowTransition.jspa" + getBasicWorkflowParameters() +
  75. "&workflowTransition=" + getTransition().getId() +
  76. "&currentCount=" + getHighLightParamPrefix() + getCount();
  77. } else {
  78. redirectUrl = "ViewWorkflowTransition.jspa" + getBasicWorkflowParameters() +
  79. "&workflowStep=" + getStep().getId() +
  80. "&workflowTransition=" + getTransition().getId() +
  81. "&currentCount=" + getHighLightParamPrefix() + getCount();
  82. }
  83. if (TextUtils.stringSet(getCount())) {
  84. redirectUrl += "&currentCount=" + getCount();
  85. }
  86. return redirectUrl;
  87. }
  88. protected abstract String getHighLightParamPrefix();
  89. protected void setupWorkflowModuleDescriptor() {
  90. String type = null;
  91. Map args = null;
  92. if (workflowDescriptor instanceof FunctionDescriptor) {
  93. type = ((FunctionDescriptor) workflowDescriptor).getType();
  94. args = ((FunctionDescriptor) workflowDescriptor).getArgs();
  95. } else if (workflowDescriptor instanceof ConditionDescriptor) {
  96. type = ((ConditionDescriptor) workflowDescriptor).getType();
  97. args = ((ConditionDescriptor) workflowDescriptor).getArgs();
  98. } else if (workflowDescriptor instanceof ValidatorDescriptor) {
  99. type = ((ValidatorDescriptor) workflowDescriptor).getType();
  100. args = ((ValidatorDescriptor) workflowDescriptor).getArgs();
  101. } else {
  102. throw new IllegalArgumentException("Invalid workflowDescriptor type");
  103. }
  104. if ("class".equalsIgnoreCase(type) && args.containsKey("class.name")) {
  105. final String className = (String) args.get("class.name");
  106. final String moduleKey = (String) args.get("full.module.key");
  107. try {
  108. workflowModuleDescriptor = (AbstractWorkflowModuleDescriptor) getWorkflowModuleDescriptor(className, moduleKey, getPluginType());
  109. return;
  110. } catch (PluginParseException e) {
  111. final String message = "Cannot find module descriptor.";
  112. log.error(message, e);
  113. throw new RuntimeException(message, e);
  114. }
  115. }
  116. throw new IllegalArgumentException("Cannot find workflowDescriptor.");
  117. }
  118. /**
  119. * Find the module descriptor given a workflow descriptor class name
  120. */
  121. protected AbstractWorkflowModuleDescriptor getWorkflowModuleDescriptor(final String className, final String moduleKey, final String pluginType) throws PluginParseException {
  122. final Collection moduleDescriptors = getPluginAccessor().getEnabledModuleDescriptorsByType(pluginType);
  123. for (final Object moduleDescriptor : moduleDescriptors) {
  124. final AbstractWorkflowModuleDescriptor abstractWorkflowModuleDescriptor = (AbstractWorkflowModuleDescriptor) moduleDescriptor;
  125. if (moduleKey == null) {
  126. if (abstractWorkflowModuleDescriptor.getImplementationClass().getName().equals(className)) {
  127. return abstractWorkflowModuleDescriptor;
  128. }
  129. } else {
  130. if (AbstractWorkflowAction.getFullModuleKey(abstractWorkflowModuleDescriptor.getPluginKey(), abstractWorkflowModuleDescriptor.getKey()).equals(moduleKey) &&
  131. abstractWorkflowModuleDescriptor.getImplementationClass().getName().equals(className)) {
  132. return abstractWorkflowModuleDescriptor;
  133. }
  134. }
  135. }
  136. return null;
  137. }
  138. protected void setupWorkflowDescriptorParams(Map parameters) {
  139. descriptorParams = new HashMap(parameters);
  140. removeKeyOrAddError(descriptorParams, "workflowName", "admin.errors.workflows.cannot.find.name");
  141. removeKeyOrAddError(descriptorParams, "workflowStep", "admin.errors.workflows.cannot.find.step");
  142. removeKeyOrAddError(descriptorParams, "workflowTransition", "admin.errors.workflows.cannot.find.transition");
  143. removeKeyOrAddError(descriptorParams, "count", "admin.errors.workflows.cannot.find.count");
  144. // The Submit button name
  145. descriptorParams.remove("Update");
  146. }
  147. public String getDescriptorHtml() {
  148. if (workflowModuleDescriptor != null) {
  149. // We have a plugin module that can generate HTML for the edit page for us - so use it
  150. return workflowModuleDescriptor.getHtml(JiraWorkflowPluginConstants.RESOURCE_NAME_EDIT_PARAMETERS, workflowDescriptor);
  151. } else {
  152. return null;
  153. }
  154. }
  155. public String getCount() {
  156. return count;
  157. }
  158. public void setCount(String count) {
  159. this.count = count;
  160. }
  161. protected int getIndexCount() {
  162. String[] counts = StringUtils.split(getCount(), ".");
  163. return Integer.parseInt(counts[counts.length - 1]);
  164. }
  165. protected void setWorkflowDescriptor(AbstractDescriptor workflowDescriptor) {
  166. this.workflowDescriptor = workflowDescriptor;
  167. }
  168. protected Map getDescriptorParams() {
  169. return descriptorParams;
  170. }
  171. protected AbstractWorkflowModuleDescriptor getDescriptor() {
  172. return workflowModuleDescriptor;
  173. }
  174. protected abstract void setupWorkflowDescriptor();
  175. public abstract String getWorkflowDescriptorName();
  176. protected abstract String getPluginType();
  177. protected abstract void editWorkflowDescriptor(AbstractDescriptor descriptor, Map params);
  178. }