/src/main/java/com/onresolve/jira/groovy/GroovyConditionFactory.java
Java | 222 lines | 163 code | 34 blank | 25 comment | 28 complexity | 99d0f235bef4086dda9a48ae9aba4b6e MD5 | raw file
- package com.onresolve.jira.groovy;
-
- import com.atlassian.jira.ComponentManager;
- import com.atlassian.jira.plugin.workflow.AbstractWorkflowPluginFactory;
- import com.atlassian.jira.plugin.workflow.WorkflowPluginConditionFactory;
- import com.atlassian.jira.plugin.workflow.WorkflowPluginFunctionFactory;
- import com.atlassian.jira.plugin.workflow.WorkflowPluginValidatorFactory;
- import com.atlassian.jira.util.ErrorCollection;
- import com.opensymphony.workflow.loader.AbstractDescriptor;
- import com.opensymphony.workflow.loader.ConditionDescriptor;
- import com.opensymphony.workflow.loader.FunctionDescriptor;
- import com.opensymphony.workflow.loader.ValidatorDescriptor;
- import org.apache.log4j.Category;
- import webwork.action.ServletActionContext;
-
- import javax.servlet.ServletRequest;
- import javax.servlet.http.HttpServletRequest;
- import java.util.Arrays;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
-
- /**
- * User: echlinj
- * Date: 04-Apr-2008
- * Time: 09:56:00
- */
- public class GroovyConditionFactory extends AbstractWorkflowPluginFactory
- implements WorkflowPluginConditionFactory, WorkflowPluginFunctionFactory, WorkflowPluginValidatorFactory {
-
- Category log = Category.getInstance(GroovyConditionFactory.class);
- ScriptManager scriptManager;
-
- public GroovyConditionFactory(ScriptManager scriptManager) {
- this.scriptManager = scriptManager;
- }
-
- protected void getVelocityParamsForInput(Map velocityParams) {
- ComponentManager.getInstance().getWebResourceManager().requireResource("com.onresolve.jira.groovy.groovyrunner:codemirror");
-
- velocityParams.put("action", new CannedScriptRunner(scriptManager));
- HttpServletRequest httpServletRequest = ServletActionContext.getRequest();
- if (httpServletRequest == null) {
- // Must be the workflow designer
- return;
- }
- velocityParams.put("scriptFileName", "");
- velocityParams.put("adminPanel", false);
- // velocityParams.put("cannedScript", httpServletRequest.getParameter("cannedScript"));
-
- // put stuff for workflow transitions
- // log.debug (httpServletRequest.getRequestURI());
-
- // hacky escaping here but TextUtils replaces to & which seems no good for a url
- velocityParams.put("workflowName", httpServletRequest.getParameter("workflowName").replaceAll("&", "%26"));
- velocityParams.put("workflowMode", httpServletRequest.getParameter("workflowMode"));
- velocityParams.put("workflowStep", httpServletRequest.getParameter("workflowStep"));
- velocityParams.put("atlToken", httpServletRequest.getParameter("atl_token"));
- velocityParams.put("workflowTransition", httpServletRequest.getParameter("workflowTransition"));
- String moduleKey = httpServletRequest.getParameter("pluginModuleKey");
- log.debug ("moduleKey: " + moduleKey);
- velocityParams.put("pluginModuleKey", moduleKey);
-
- // nasty... don't see how else to tell what type of function this is
- if (moduleKey.endsWith("groovy-permission-condition")) {
- velocityParams.put("packageName", "com.onresolve.jira.groovy.canned.workflow.conditions");
- velocityParams.put("actionName", "Condition");
- }
- else if (moduleKey.endsWith("permission-validator")) {
- velocityParams.put("packageName", "com.onresolve.jira.groovy.canned.workflow.validators");
- velocityParams.put("actionName", "Validator");
- }
- else if (moduleKey.endsWith("rungroovy-function")) {
- velocityParams.put("packageName", "com.onresolve.jira.groovy.canned.workflow.postfunctions");
- velocityParams.put("actionName", "Function");
- }
- else {
- throw new IllegalArgumentException("Descriptor must be of one of the supported Descriptor types.");
- }
-
-
- velocityParams.put("cannedScript", httpServletRequest.getParameter("cannedScript"));
- velocityParams.put("isInput", true);
- }
-
- protected void getVelocityParamsForEdit(Map velocityParams, AbstractDescriptor descriptor) {
- ComponentManager.getInstance().getWebResourceManager().requireResource("com.onresolve.jira.groovy.groovyrunner:codemirror");
- getVelocityParamsForView(velocityParams, descriptor);
- velocityParams.put("isInput", false);
- }
-
- protected void getVelocityParamsForView(Map velocityParams, AbstractDescriptor aDescriptor) {
-
- // this seems like bollocks but it's not (quite), because AbstractDescriptor does not impl getArgs
- Map args;
- if (aDescriptor instanceof ConditionDescriptor) {
- ConditionDescriptor descriptor = (ConditionDescriptor) aDescriptor;
- args = descriptor.getArgs();
- velocityParams.put("scriptFileName", (String) args.get("scriptFileName"));
- velocityParams.put("packageName", "com.onresolve.jira.groovy.canned.workflow.conditions");
- }
- else if (aDescriptor instanceof FunctionDescriptor) {
- FunctionDescriptor descriptor = (FunctionDescriptor) aDescriptor;
- args = descriptor.getArgs();
- velocityParams.put("scriptFileName", (String) descriptor.getArgs().get("scriptFileName"));
- velocityParams.put("packageName", "com.onresolve.jira.groovy.canned.workflow.postfunctions");
- }
- else if (aDescriptor instanceof ValidatorDescriptor) {
- ValidatorDescriptor descriptor = (ValidatorDescriptor) aDescriptor;
- args = descriptor.getArgs();
- velocityParams.put("scriptFileName", (String) descriptor.getArgs().get("scriptFileName"));
- velocityParams.put("packageName", "com.onresolve.jira.groovy.canned.workflow.validators");
- }
- else {
- throw new IllegalArgumentException("Descriptor must be of one of the supported Descriptor types.");
- }
-
- velocityParams.put("action", new CannedScriptRunner(scriptManager));
- ServletRequest httpServletRequest = ServletActionContext.getRequest();
- if (httpServletRequest == null) {
- // Must be the workflow designer
- return;
- }
-
- // put stuff for workflow transitions
- velocityParams.put("workflowName", httpServletRequest.getParameter("workflowName").replaceAll("&", "%26"));
- velocityParams.put("workflowMode", httpServletRequest.getParameter("workflowMode"));
- velocityParams.put("workflowStep", httpServletRequest.getParameter("workflowStep"));
- velocityParams.put("workflowTransition", httpServletRequest.getParameter("workflowTransition"));
-
- String moduleKey = httpServletRequest.getParameter("pluginModuleKey");
- log.debug ("moduleKey: " + moduleKey);
- velocityParams.put("pluginModuleKey", moduleKey);
-
-
- log.debug ("httpServletRequest.getParameter(\"workflowName\"): " + httpServletRequest.getParameter("workflowName"));
- String scriptFileName = (String) args.get("scriptFileName");
- log.debug ("cannedScript: " + scriptFileName);
-
- // if we have a script, get the params and merge from template
- if (scriptFileName != null) {
- if (CannedScriptRunner.isCannedScript(scriptFileName)) {
- CannedScriptRunner cannedScriptRunner = new CannedScriptRunner(scriptManager);
- Map params = new HashMap();
- try {
- log.debug ("get script args");
- List<String> expectedArgs = cannedScriptRunner.getScriptArgs(scriptFileName);
- for (String expectedArg : expectedArgs) {
- params.put(expectedArg, args.get(expectedArg));
- }
-
- velocityParams.put("cannedScriptArgs", params);
- velocityParams.put("expectedArgs", expectedArgs);
- velocityParams.put("cannedScript", args.get("scriptFileName"));
-
- // get the args and call the getDescription method
- // cannedScriptRunner.runCannedScript()
- velocityParams.put("cannedScriptPreview", cannedScriptRunner.getCannedScriptHumanDesc(
- (String) args.get("scriptFileName"), params, false));
-
- // check for any validation errors as they seemingly cannot be caught at the time you commit
- ErrorCollection errorCollection = cannedScriptRunner.getValidationErrors(scriptFileName, params, false);
- if (errorCollection != null && errorCollection.hasAnyErrors()) {
- velocityParams.put("errorCollection", errorCollection);
- }
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- }
-
-
- }
- else {
- velocityParams.put("filename", scriptFileName);
- }
- }
-
- velocityParams.put("adminPanel", false);
- }
-
- public Map getDescriptorParams(Map conditionParams) {
- Map params = new HashMap();
- CannedScriptRunner cannedScriptRunner = new CannedScriptRunner(scriptManager);
- String fileName = null;
- try {
- fileName = extractSingleParam(conditionParams, "filename");
- log.debug ("Found fileName " + fileName + " in parameters");
- } catch (Exception e) {
- fileName = extractSingleParam(conditionParams, "cannedScript");
-
- try {
- List<String> expectedArgs = cannedScriptRunner.getScriptArgs(fileName);
-
- if (expectedArgs != null && ! expectedArgs.isEmpty()) {
- log.debug ("expectedArgs: " + expectedArgs);
- log.debug ("conditionParams.keySet: " + conditionParams.keySet());
-
- for (int i = 0; i < expectedArgs.size(); i++) {
- String[] o = (String[]) conditionParams.get("cannedScriptArgs_" + expectedArgs.get(i));
- List<String> ol = Arrays.asList(o);
- log.debug (ol.get(0));
- log.debug ("Add to param: " + expectedArgs.get(i) + " value: " + ol.get(0));
- // log.debug ("script arg: " + conditionParams.get("cannedScriptArgs"));
- params.put(expectedArgs.get(i), ol.get(0));
- }
- }
- } catch (Exception e1) {
- log.error("Error", e1);
- }
- }
-
-
-
- params.put("scriptFileName", fileName);
- /*
- File file = new File(fileName);
- if (! file.canRead() || ! file.isFile()) {
- throw new IllegalArgumentException("Cannot find (or read) script file at: " + fileName);
- }
- */
- return params;
- }
- }