PageRenderTime 45ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/com/onresolve/jira/groovy/GroovyConditionFactory.java

https://bitbucket.org/sorin/jira-plugin-intellij
Java | 222 lines | 163 code | 34 blank | 25 comment | 28 complexity | 99d0f235bef4086dda9a48ae9aba4b6e MD5 | raw file
  1. package com.onresolve.jira.groovy;
  2. import com.atlassian.jira.ComponentManager;
  3. import com.atlassian.jira.plugin.workflow.AbstractWorkflowPluginFactory;
  4. import com.atlassian.jira.plugin.workflow.WorkflowPluginConditionFactory;
  5. import com.atlassian.jira.plugin.workflow.WorkflowPluginFunctionFactory;
  6. import com.atlassian.jira.plugin.workflow.WorkflowPluginValidatorFactory;
  7. import com.atlassian.jira.util.ErrorCollection;
  8. import com.opensymphony.workflow.loader.AbstractDescriptor;
  9. import com.opensymphony.workflow.loader.ConditionDescriptor;
  10. import com.opensymphony.workflow.loader.FunctionDescriptor;
  11. import com.opensymphony.workflow.loader.ValidatorDescriptor;
  12. import org.apache.log4j.Category;
  13. import webwork.action.ServletActionContext;
  14. import javax.servlet.ServletRequest;
  15. import javax.servlet.http.HttpServletRequest;
  16. import java.util.Arrays;
  17. import java.util.HashMap;
  18. import java.util.List;
  19. import java.util.Map;
  20. /**
  21. * User: echlinj
  22. * Date: 04-Apr-2008
  23. * Time: 09:56:00
  24. */
  25. public class GroovyConditionFactory extends AbstractWorkflowPluginFactory
  26. implements WorkflowPluginConditionFactory, WorkflowPluginFunctionFactory, WorkflowPluginValidatorFactory {
  27. Category log = Category.getInstance(GroovyConditionFactory.class);
  28. ScriptManager scriptManager;
  29. public GroovyConditionFactory(ScriptManager scriptManager) {
  30. this.scriptManager = scriptManager;
  31. }
  32. protected void getVelocityParamsForInput(Map velocityParams) {
  33. ComponentManager.getInstance().getWebResourceManager().requireResource("com.onresolve.jira.groovy.groovyrunner:codemirror");
  34. velocityParams.put("action", new CannedScriptRunner(scriptManager));
  35. HttpServletRequest httpServletRequest = ServletActionContext.getRequest();
  36. if (httpServletRequest == null) {
  37. // Must be the workflow designer
  38. return;
  39. }
  40. velocityParams.put("scriptFileName", "");
  41. velocityParams.put("adminPanel", false);
  42. // velocityParams.put("cannedScript", httpServletRequest.getParameter("cannedScript"));
  43. // put stuff for workflow transitions
  44. // log.debug (httpServletRequest.getRequestURI());
  45. // hacky escaping here but TextUtils replaces to & which seems no good for a url
  46. velocityParams.put("workflowName", httpServletRequest.getParameter("workflowName").replaceAll("&", "%26"));
  47. velocityParams.put("workflowMode", httpServletRequest.getParameter("workflowMode"));
  48. velocityParams.put("workflowStep", httpServletRequest.getParameter("workflowStep"));
  49. velocityParams.put("atlToken", httpServletRequest.getParameter("atl_token"));
  50. velocityParams.put("workflowTransition", httpServletRequest.getParameter("workflowTransition"));
  51. String moduleKey = httpServletRequest.getParameter("pluginModuleKey");
  52. log.debug ("moduleKey: " + moduleKey);
  53. velocityParams.put("pluginModuleKey", moduleKey);
  54. // nasty... don't see how else to tell what type of function this is
  55. if (moduleKey.endsWith("groovy-permission-condition")) {
  56. velocityParams.put("packageName", "com.onresolve.jira.groovy.canned.workflow.conditions");
  57. velocityParams.put("actionName", "Condition");
  58. }
  59. else if (moduleKey.endsWith("permission-validator")) {
  60. velocityParams.put("packageName", "com.onresolve.jira.groovy.canned.workflow.validators");
  61. velocityParams.put("actionName", "Validator");
  62. }
  63. else if (moduleKey.endsWith("rungroovy-function")) {
  64. velocityParams.put("packageName", "com.onresolve.jira.groovy.canned.workflow.postfunctions");
  65. velocityParams.put("actionName", "Function");
  66. }
  67. else {
  68. throw new IllegalArgumentException("Descriptor must be of one of the supported Descriptor types.");
  69. }
  70. velocityParams.put("cannedScript", httpServletRequest.getParameter("cannedScript"));
  71. velocityParams.put("isInput", true);
  72. }
  73. protected void getVelocityParamsForEdit(Map velocityParams, AbstractDescriptor descriptor) {
  74. ComponentManager.getInstance().getWebResourceManager().requireResource("com.onresolve.jira.groovy.groovyrunner:codemirror");
  75. getVelocityParamsForView(velocityParams, descriptor);
  76. velocityParams.put("isInput", false);
  77. }
  78. protected void getVelocityParamsForView(Map velocityParams, AbstractDescriptor aDescriptor) {
  79. // this seems like bollocks but it's not (quite), because AbstractDescriptor does not impl getArgs
  80. Map args;
  81. if (aDescriptor instanceof ConditionDescriptor) {
  82. ConditionDescriptor descriptor = (ConditionDescriptor) aDescriptor;
  83. args = descriptor.getArgs();
  84. velocityParams.put("scriptFileName", (String) args.get("scriptFileName"));
  85. velocityParams.put("packageName", "com.onresolve.jira.groovy.canned.workflow.conditions");
  86. }
  87. else if (aDescriptor instanceof FunctionDescriptor) {
  88. FunctionDescriptor descriptor = (FunctionDescriptor) aDescriptor;
  89. args = descriptor.getArgs();
  90. velocityParams.put("scriptFileName", (String) descriptor.getArgs().get("scriptFileName"));
  91. velocityParams.put("packageName", "com.onresolve.jira.groovy.canned.workflow.postfunctions");
  92. }
  93. else if (aDescriptor instanceof ValidatorDescriptor) {
  94. ValidatorDescriptor descriptor = (ValidatorDescriptor) aDescriptor;
  95. args = descriptor.getArgs();
  96. velocityParams.put("scriptFileName", (String) descriptor.getArgs().get("scriptFileName"));
  97. velocityParams.put("packageName", "com.onresolve.jira.groovy.canned.workflow.validators");
  98. }
  99. else {
  100. throw new IllegalArgumentException("Descriptor must be of one of the supported Descriptor types.");
  101. }
  102. velocityParams.put("action", new CannedScriptRunner(scriptManager));
  103. ServletRequest httpServletRequest = ServletActionContext.getRequest();
  104. if (httpServletRequest == null) {
  105. // Must be the workflow designer
  106. return;
  107. }
  108. // put stuff for workflow transitions
  109. velocityParams.put("workflowName", httpServletRequest.getParameter("workflowName").replaceAll("&", "%26"));
  110. velocityParams.put("workflowMode", httpServletRequest.getParameter("workflowMode"));
  111. velocityParams.put("workflowStep", httpServletRequest.getParameter("workflowStep"));
  112. velocityParams.put("workflowTransition", httpServletRequest.getParameter("workflowTransition"));
  113. String moduleKey = httpServletRequest.getParameter("pluginModuleKey");
  114. log.debug ("moduleKey: " + moduleKey);
  115. velocityParams.put("pluginModuleKey", moduleKey);
  116. log.debug ("httpServletRequest.getParameter(\"workflowName\"): " + httpServletRequest.getParameter("workflowName"));
  117. String scriptFileName = (String) args.get("scriptFileName");
  118. log.debug ("cannedScript: " + scriptFileName);
  119. // if we have a script, get the params and merge from template
  120. if (scriptFileName != null) {
  121. if (CannedScriptRunner.isCannedScript(scriptFileName)) {
  122. CannedScriptRunner cannedScriptRunner = new CannedScriptRunner(scriptManager);
  123. Map params = new HashMap();
  124. try {
  125. log.debug ("get script args");
  126. List<String> expectedArgs = cannedScriptRunner.getScriptArgs(scriptFileName);
  127. for (String expectedArg : expectedArgs) {
  128. params.put(expectedArg, args.get(expectedArg));
  129. }
  130. velocityParams.put("cannedScriptArgs", params);
  131. velocityParams.put("expectedArgs", expectedArgs);
  132. velocityParams.put("cannedScript", args.get("scriptFileName"));
  133. // get the args and call the getDescription method
  134. // cannedScriptRunner.runCannedScript()
  135. velocityParams.put("cannedScriptPreview", cannedScriptRunner.getCannedScriptHumanDesc(
  136. (String) args.get("scriptFileName"), params, false));
  137. // check for any validation errors as they seemingly cannot be caught at the time you commit
  138. ErrorCollection errorCollection = cannedScriptRunner.getValidationErrors(scriptFileName, params, false);
  139. if (errorCollection != null && errorCollection.hasAnyErrors()) {
  140. velocityParams.put("errorCollection", errorCollection);
  141. }
  142. } catch (Exception e) {
  143. log.error(e.getMessage(), e);
  144. }
  145. }
  146. else {
  147. velocityParams.put("filename", scriptFileName);
  148. }
  149. }
  150. velocityParams.put("adminPanel", false);
  151. }
  152. public Map getDescriptorParams(Map conditionParams) {
  153. Map params = new HashMap();
  154. CannedScriptRunner cannedScriptRunner = new CannedScriptRunner(scriptManager);
  155. String fileName = null;
  156. try {
  157. fileName = extractSingleParam(conditionParams, "filename");
  158. log.debug ("Found fileName " + fileName + " in parameters");
  159. } catch (Exception e) {
  160. fileName = extractSingleParam(conditionParams, "cannedScript");
  161. try {
  162. List<String> expectedArgs = cannedScriptRunner.getScriptArgs(fileName);
  163. if (expectedArgs != null && ! expectedArgs.isEmpty()) {
  164. log.debug ("expectedArgs: " + expectedArgs);
  165. log.debug ("conditionParams.keySet: " + conditionParams.keySet());
  166. for (int i = 0; i < expectedArgs.size(); i++) {
  167. String[] o = (String[]) conditionParams.get("cannedScriptArgs_" + expectedArgs.get(i));
  168. List<String> ol = Arrays.asList(o);
  169. log.debug (ol.get(0));
  170. log.debug ("Add to param: " + expectedArgs.get(i) + " value: " + ol.get(0));
  171. // log.debug ("script arg: " + conditionParams.get("cannedScriptArgs"));
  172. params.put(expectedArgs.get(i), ol.get(0));
  173. }
  174. }
  175. } catch (Exception e1) {
  176. log.error("Error", e1);
  177. }
  178. }
  179. params.put("scriptFileName", fileName);
  180. /*
  181. File file = new File(fileName);
  182. if (! file.canRead() || ! file.isFile()) {
  183. throw new IllegalArgumentException("Cannot find (or read) script file at: " + fileName);
  184. }
  185. */
  186. return params;
  187. }
  188. }