[jbpm-commits] JBoss JBPM SVN: r5169 - in jbpm4/trunk/modules/integration: form-plugin/src/main/java/org/jbpm/integration/console/forms and 1 other directories.

do-not-reply at jboss.org do-not-reply at jboss.org
Wed Jul 1 04:12:27 EDT 2009


Author: heiko.braun at jboss.com
Date: 2009-07-01 04:12:26 -0400 (Wed, 01 Jul 2009)
New Revision: 5169

Added:
   jbpm4/trunk/modules/integration/form-plugin/src/main/java/org/jbpm/integration/console/forms/AbstractFormDispatcher.java
   jbpm4/trunk/modules/integration/form-plugin/src/main/java/org/jbpm/integration/console/forms/FormDispatcherComposite.java
   jbpm4/trunk/modules/integration/form-plugin/src/main/java/org/jbpm/integration/console/forms/ProcessFormDispatcher.java
   jbpm4/trunk/modules/integration/form-plugin/src/main/java/org/jbpm/integration/console/forms/TaskFormDispatcher.java
   jbpm4/trunk/modules/integration/form-plugin/src/main/resources/META-INF/services/org.jboss.bpm.console.server.plugin.FormDispatcherPlugin
Removed:
   jbpm4/trunk/modules/integration/form-plugin/src/main/java/org/jbpm/integration/console/forms/TaskDispatcherPluginImpl.java
   jbpm4/trunk/modules/integration/form-plugin/src/main/resources/META-INF/services/org.jboss.bpm.console.server.plugin.TaskDispatcherPlugin
Modified:
   jbpm4/trunk/modules/integration/console/src/main/java/org/jbpm/integration/console/ProcessManagementImpl.java
Log:
JBPM-2233: Start a process with task form

Modified: jbpm4/trunk/modules/integration/console/src/main/java/org/jbpm/integration/console/ProcessManagementImpl.java
===================================================================
--- jbpm4/trunk/modules/integration/console/src/main/java/org/jbpm/integration/console/ProcessManagementImpl.java	2009-07-01 07:17:16 UTC (rev 5168)
+++ jbpm4/trunk/modules/integration/console/src/main/java/org/jbpm/integration/console/ProcessManagementImpl.java	2009-07-01 08:12:26 UTC (rev 5169)
@@ -65,7 +65,8 @@
      
       for(ProcessDefinition processDefinition : activePds)
       {
-        results.add( ModelAdaptor.adoptDefinition(processDefinition) );
+        ProcessDefinitionRef ref = ModelAdaptor.adoptDefinition(processDefinition);
+        results.add(ref);
       }
 
       // suspended  processes
@@ -75,9 +76,9 @@
 
       for(ProcessDefinition p : suspendedPds)
       {
-        ProcessDefinitionRef pref = ModelAdaptor.adoptDefinition(p);
-        pref.setSuspended(true);
-        results.add(pref);
+        ProcessDefinitionRef ref = ModelAdaptor.adoptDefinition(p);
+        ref.setSuspended(true);        
+        results.add(ref);
       }
 
       return results;

Added: jbpm4/trunk/modules/integration/form-plugin/src/main/java/org/jbpm/integration/console/forms/AbstractFormDispatcher.java
===================================================================
--- jbpm4/trunk/modules/integration/form-plugin/src/main/java/org/jbpm/integration/console/forms/AbstractFormDispatcher.java	                        (rev 0)
+++ jbpm4/trunk/modules/integration/form-plugin/src/main/java/org/jbpm/integration/console/forms/AbstractFormDispatcher.java	2009-07-01 08:12:26 UTC (rev 5169)
@@ -0,0 +1,143 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jbpm.integration.console.forms;
+
+import org.jbpm.api.ProcessEngine;
+import org.jbpm.integration.spi.mgmt.ServerConfig;
+import org.jbpm.integration.spi.mgmt.ServerConfigFactory;
+
+import javax.naming.InitialContext;
+import javax.activation.DataHandler;
+import javax.activation.DataSource;
+import java.io.*;
+import java.util.Map;
+
+import freemarker.template.DefaultObjectWrapper;
+import freemarker.template.Template;
+
+/**
+ * @author Heiko.Braun <heiko.braun at jboss.com>
+ */
+public class AbstractFormDispatcher
+{
+  protected final static String WEB_CONTEXT = "/gwt-console-server/rs";
+  
+  protected ProcessEngine processEngine;
+  protected ServerConfig serverConfig = null;
+  protected static final String FORM_DIRECTIVE_KEY = "form";
+  protected static final String OUTCOME_DIRECTIVE_NAME = "outcome";
+
+
+  public AbstractFormDispatcher()
+  {
+    initializeProcessEngine();
+  }
+
+  protected void initializeProcessEngine()
+  {
+    try
+    {
+      InitialContext ctx = new InitialContext();
+      this.processEngine = (ProcessEngine)ctx.lookup("java:/ProcessEngine");
+    }
+    catch (Exception e)
+    {
+      throw new RuntimeException("Failed to lookup process engine", e);
+    }
+  }
+
+  protected ServerConfig getServerConfig()
+  {
+    if(null==serverConfig)
+    {
+      serverConfig = ServerConfigFactory.getServerConfig();
+    }
+    return serverConfig;
+  }
+
+  protected StringBuilder getBaseUrl()
+  {
+    StringBuilder spec = new StringBuilder();
+    spec.append("http://");
+    spec.append(getServerConfig().getWebServiceHost());
+    spec.append(":").append(getServerConfig().getWebServicePort());
+    spec.append(WEB_CONTEXT);
+    return spec;    
+  }
+
+  protected DataHandler processTemplate(
+      final String name, InputStream src,
+      Map<String, Object> renderContext
+  )
+  {
+    DataHandler merged = null;
+
+    try
+    {
+      freemarker.template.Configuration cfg = new freemarker.template.Configuration();
+      cfg.setObjectWrapper(new DefaultObjectWrapper());
+      cfg.setTemplateUpdateDelay(0);
+
+      Template temp = new Template(name, new InputStreamReader(src), cfg);
+      temp.dump(System.out);
+
+      final ByteArrayOutputStream bout = new ByteArrayOutputStream();
+      Writer out = new OutputStreamWriter(bout);
+      temp.process(renderContext, out);
+      out.flush();
+
+      merged = new DataHandler(
+
+          new DataSource()
+          {
+
+            public InputStream getInputStream() throws IOException
+            {
+              return new ByteArrayInputStream(bout.toByteArray());
+            }
+
+            public OutputStream getOutputStream() throws IOException
+            {
+              return bout;
+            }
+
+            public String getContentType()
+            {
+              return "application/octet-stream";
+            }
+
+            public String getName()
+            {
+              return name + "_DataSource";
+            }
+          }
+      );
+
+    }
+    catch (Exception e)
+    {
+      throw new RuntimeException("Failed to process task template", e);
+    }
+
+    return merged;
+  }
+}

Added: jbpm4/trunk/modules/integration/form-plugin/src/main/java/org/jbpm/integration/console/forms/FormDispatcherComposite.java
===================================================================
--- jbpm4/trunk/modules/integration/form-plugin/src/main/java/org/jbpm/integration/console/forms/FormDispatcherComposite.java	                        (rev 0)
+++ jbpm4/trunk/modules/integration/form-plugin/src/main/java/org/jbpm/integration/console/forms/FormDispatcherComposite.java	2009-07-01 08:12:26 UTC (rev 5169)
@@ -0,0 +1,87 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jbpm.integration.console.forms;
+
+import org.jboss.bpm.console.server.plugin.FormDispatcherPlugin;
+import org.jboss.bpm.console.server.plugin.FormAuthorityRef;
+
+import javax.activation.DataHandler;
+import java.net.URL;
+import java.util.Map;
+
+/**
+ * @author Heiko.Braun <heiko.braun at jboss.com>
+ */
+public class FormDispatcherComposite implements FormDispatcherPlugin
+{
+
+  private FormDispatcherPlugin taskDispatcher;
+  private FormDispatcherPlugin processDispatcher;
+
+
+  public FormDispatcherComposite()
+  {
+    this.taskDispatcher = new TaskFormDispatcher();
+    this.processDispatcher = new ProcessFormDispatcher();
+  }
+
+  public URL getDispatchUrl(FormAuthorityRef ref)
+  {
+    switch(ref.getType())
+    {
+      case TASK:
+        return taskDispatcher.getDispatchUrl(ref);
+      case PROCESS:
+        return processDispatcher.getDispatchUrl(ref);
+      default:
+        throw new IllegalArgumentException("Unknown authority type:"+ref.getType());
+    }
+  }
+
+  public DataHandler provideForm(FormAuthorityRef ref)
+  {
+    switch(ref.getType())
+    {
+      case TASK:
+        return taskDispatcher.provideForm(ref);
+      case PROCESS:
+        return processDispatcher.provideForm(ref);
+      default:
+        throw new IllegalArgumentException("Unknown authority type:"+ref.getType());
+    }
+  }
+
+  public void processCompletion(FormAuthorityRef ref, Map<String, Object> payload, String performingUser, String outcome)
+  {
+    switch(ref.getType())
+    {
+      case TASK:
+        taskDispatcher.processCompletion(ref, payload, performingUser, outcome);
+        break;
+      case PROCESS:
+        processDispatcher.processCompletion(ref, payload, performingUser, outcome);
+        break;
+      default:
+        throw new IllegalArgumentException("Unknown authority type:"+ref.getType());
+    }
+  }
+}

Added: jbpm4/trunk/modules/integration/form-plugin/src/main/java/org/jbpm/integration/console/forms/ProcessFormDispatcher.java
===================================================================
--- jbpm4/trunk/modules/integration/form-plugin/src/main/java/org/jbpm/integration/console/forms/ProcessFormDispatcher.java	                        (rev 0)
+++ jbpm4/trunk/modules/integration/form-plugin/src/main/java/org/jbpm/integration/console/forms/ProcessFormDispatcher.java	2009-07-01 08:12:26 UTC (rev 5169)
@@ -0,0 +1,180 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jbpm.integration.console.forms;
+
+import org.jboss.bpm.console.server.plugin.FormAuthorityRef;
+import org.jboss.bpm.console.server.plugin.FormDispatcherPlugin;
+import org.jbpm.api.ExecutionService;
+import org.jbpm.api.ProcessDefinition;
+import org.jbpm.api.RepositoryService;
+import org.jbpm.pvm.internal.env.Environment;
+import org.jbpm.pvm.internal.env.EnvironmentFactory;
+
+import javax.activation.DataHandler;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.List;
+
+/**
+ * Processes form data to start processes.
+ * 
+ * @author Heiko.Braun <heiko.braun at jboss.com>
+ */
+public class ProcessFormDispatcher extends AbstractFormDispatcher
+    implements FormDispatcherPlugin
+{
+
+  public URL getDispatchUrl(FormAuthorityRef ref)
+  {
+    if(!processHasForm(ref.getReferenceId()))
+      return null;
+    
+    StringBuilder baseUrl = getBaseUrl();
+    baseUrl.append("/form/process/");
+    baseUrl.append( ref.getReferenceId());
+    baseUrl.append("/render");
+
+    try
+    {
+      return new URL(baseUrl.toString());
+    }
+    catch (MalformedURLException e)
+    {
+      throw new RuntimeException("Failed to resolve task dispatch url", e);
+    }
+  }
+
+  private boolean processHasForm(String id)
+  {
+    return getStartFormName(id)!=null;
+  }
+
+  private String getStartFormName(String id)
+  {
+    String name = null;
+
+    Environment env = ((EnvironmentFactory)processEngine).openEnvironment();
+    try
+    {
+      RepositoryService repoService = processEngine.getRepositoryService();
+      List<String> startActivityNames = repoService.getStartActivityNames(id);
+
+      if(null==startActivityNames) // who knows? I'd expect anything from jbpm
+        throw new RuntimeException("Unable to resolve start activity names for process: "+id);
+
+      String defaultActitvity = startActivityNames.get(0);
+      if(startActivityNames.size()>1)
+      {
+        System.out.println("WARN: More then 1 start activity found. Default to " + defaultActitvity + " to resolve the form name.");
+      }
+
+      name = repoService.getStartFormResourceName(id, defaultActitvity);       
+    }
+    finally{
+      env.close();
+    }
+
+    return name;
+  }
+
+  public DataHandler provideForm(FormAuthorityRef ref)
+  {
+    Environment env = ((EnvironmentFactory)processEngine).openEnvironment();
+
+    DataHandler result = null;
+
+    try
+    {
+
+      RepositoryService repoService = processEngine.getRepositoryService();
+
+      // check if a template exists
+      String startFormResourceName = getStartFormName(ref.getReferenceId());
+      if(null==startFormResourceName)
+        throw new IllegalArgumentException("Process " +ref.getReferenceId() + " doesn't provide a start form");
+
+      ProcessDefinition procDef =
+          repoService
+              .createProcessDefinitionQuery()
+              .processDefinitionId(ref.getReferenceId())
+              .uniqueResult();
+
+      InputStream template = repoService.getResourceAsStream(
+          procDef.getDeploymentId(), startFormResourceName
+      );
+
+      // merge template with process variables
+      if(template!=null)
+      {
+        // plugin context
+        StringBuilder action = getBaseUrl();
+        action.append("/form/process/");
+        action.append( ref.getReferenceId() );
+        action.append("/complete");
+
+        Map<String, Object> renderContext = new HashMap<String,Object>();
+
+        // form directive
+        FormDirective formDirective = new FormDirective();
+        formDirective.setAction( action.toString() );
+        renderContext.put(FORM_DIRECTIVE_KEY, formDirective);
+
+        // outcome directive
+        renderContext.put(OUTCOME_DIRECTIVE_NAME, new OutcomeDirective());
+
+        result = processTemplate(startFormResourceName, template, renderContext);
+      }
+
+      return result;
+    }
+    finally{
+      env.close();
+    }
+  }
+
+  public void processCompletion(FormAuthorityRef ref, Map<String, Object> payload, String performingUser, String outcome)
+  {
+    Environment env = ((EnvironmentFactory)processEngine).openEnvironment();
+
+    try
+    {
+      ExecutionService execService = processEngine.getExecutionService();
+      if(payload!=null)
+      {
+        execService.startProcessInstanceById(
+            ref.getReferenceId(), payload
+        );
+      }
+      else
+      {
+        execService.startProcessInstanceById(ref.getReferenceId());
+      }      
+    }
+    finally
+    {
+      env.close();
+    }
+  }
+}

Deleted: jbpm4/trunk/modules/integration/form-plugin/src/main/java/org/jbpm/integration/console/forms/TaskDispatcherPluginImpl.java
===================================================================
--- jbpm4/trunk/modules/integration/form-plugin/src/main/java/org/jbpm/integration/console/forms/TaskDispatcherPluginImpl.java	2009-07-01 07:17:16 UTC (rev 5168)
+++ jbpm4/trunk/modules/integration/form-plugin/src/main/java/org/jbpm/integration/console/forms/TaskDispatcherPluginImpl.java	2009-07-01 08:12:26 UTC (rev 5169)
@@ -1,263 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source.
- * Copyright 2006, Red Hat Middleware LLC, and individual contributors
- * as indicated by the @author tags. See the copyright.txt file in the
- * distribution for a full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-package org.jbpm.integration.console.forms;
-
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.OutputStream;
-import java.io.OutputStreamWriter;
-import java.io.Writer;
-import java.net.MalformedURLException;
-import java.net.URL;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
-import javax.activation.DataHandler;
-import javax.activation.DataSource;
-import javax.naming.InitialContext;
-
-import org.jboss.bpm.console.server.plugin.TaskDispatcherPlugin;
-import org.jbpm.api.ExecutionService;
-import org.jbpm.api.ProcessDefinition;
-import org.jbpm.api.ProcessDefinitionQuery;
-import org.jbpm.api.ProcessEngine;
-import org.jbpm.api.RepositoryService;
-import org.jbpm.api.TaskService;
-import org.jbpm.api.task.Task;
-import org.jbpm.integration.spi.mgmt.ServerConfig;
-import org.jbpm.integration.spi.mgmt.ServerConfigFactory;
-import org.jbpm.pvm.internal.env.Environment;
-import org.jbpm.pvm.internal.env.EnvironmentFactory;
-import org.jbpm.pvm.internal.model.ExecutionImpl;
-import org.jbpm.pvm.internal.model.Transition;
-import org.jbpm.pvm.internal.task.TaskImpl;
-
-import freemarker.template.DefaultObjectWrapper;
-import freemarker.template.Template;
-
-/**
- * @author Heiko.Braun <heiko.braun at jboss.com>
- */
-public class TaskDispatcherPluginImpl implements TaskDispatcherPlugin
-{
-  private ProcessEngine processEngine;
-
-  private ServerConfig serverConfig = null;
-
-  public TaskDispatcherPluginImpl()
-  {
-    initializeProcessEngine();
-  }
-
-  public URL getDispatchUrl(long taskId)
-  {
-    StringBuilder spec = new StringBuilder();
-    spec.append("http://");
-    spec.append(getServerConfig().getWebServiceHost());
-    spec.append(":").append(getServerConfig().getWebServicePort());
-    spec.append("/gwt-console-server/rs/task/");
-    spec.append( taskId );
-    spec.append("/render");
-
-    try
-    {
-      return new URL(spec.toString());
-    }
-    catch (MalformedURLException e)
-    {
-      throw new RuntimeException("Failed to resolve task dispatch url", e);
-    }
-  }
-
-
-  private ServerConfig getServerConfig()
-  {
-    if(null==serverConfig)
-    {
-      serverConfig = ServerConfigFactory.getServerConfig();
-    }
-    return serverConfig;
-  }
-
-  public DataHandler provideTaskUI(long taskId)
-  {
-    Environment env = ((EnvironmentFactory)processEngine).openEnvironment();
-
-    DataHandler result = null;
-
-    try
-    {
-      TaskService taskService = processEngine.getTaskService();
-      Task task = taskService.getTask(Long.toString(taskId));
-
-      // access the processdefition
-      TaskImpl cast = ((TaskImpl) task);
-      ExecutionImpl processInstance = cast.getProcessInstance();
-      String processInstanceId =  processInstance.getId();
-      String processId =  processInstance.getProcessDefinition().getId();
-
-      RepositoryService repoService = processEngine.getRepositoryService();
-      ProcessDefinitionQuery query = repoService.createProcessDefinitionQuery();
-      query.processDefinitionId(processId);
-      ProcessDefinition procDef = query.uniqueResult();
-
-      // check if a template exists
-      String name = task.getName() + ".ftl";
-      InputStream template = repoService.getResourceAsStream(
-          procDef.getDeploymentId(), name
-      );
-
-      // merge template with process variables
-      if(template!=null)
-      {
-        ExecutionService execService = processEngine.getExecutionService();
-
-        Set<String> varNames = execService.getVariableNames(processInstanceId);
-        if(varNames!=null)
-        {
-          Map<String, Object> processContext = execService.getVariables(processInstanceId, varNames);
-
-          // plugin context
-
-          StringBuilder action = new StringBuilder();
-          action.append("http://");
-          action.append(getServerConfig().getWebServiceHost());
-          action.append(":").append(getServerConfig().getWebServicePort());
-          action.append("/gwt-console-server/rs/task/");
-          action.append( taskId );
-          action.append("/process");
-
-          Map<String, Object> renderContext = new HashMap<String,Object>();
-
-          // formResourceName directive
-          FormDirective formDirective = new FormDirective();
-          formDirective.setAction( action.toString() );
-          renderContext.put("formResourceName", formDirective);
-
-          // outcome directive
-          // TODO: Fix when https://jira.jboss.org/jira/browse/JBPM-2220 is done
-          OutcomeDirective outcomeDirective = new OutcomeDirective();
-          List<Transition> transitions =
-              ((ExecutionImpl) processInstance).getActivity().getOutgoingTransitions();
-          for(Transition t : transitions)
-          {
-            String outcomeName = t.getName()!=null ? t.getName() : "to_"+t.getDestination().getName();
-            outcomeDirective.getValues().add(outcomeName);
-          }
-          renderContext.put("outcome",outcomeDirective);          
-
-          // process variables
-          renderContext.putAll(processContext);
-
-
-          result = processTemplate(name, template, renderContext);
-        }
-      }
-
-      return result;
-    }
-    finally{
-      env.close();
-    }
-  }
-
-  private DataHandler processTemplate(
-      final String name, InputStream src,
-      Map<String, Object> renderContext
-      )
-  {
-    DataHandler merged = null;
-
-    try
-    {
-      freemarker.template.Configuration cfg = new freemarker.template.Configuration();      
-      cfg.setObjectWrapper(new DefaultObjectWrapper());
-      cfg.setTemplateUpdateDelay(0);
-
-      Template temp = new Template(name, new InputStreamReader(src), cfg);
-      temp.dump(System.out);
-
-      final ByteArrayOutputStream bout = new ByteArrayOutputStream();
-      Writer out = new OutputStreamWriter(bout);
-      temp.process(renderContext, out);
-      out.flush();
-       
-      merged = new DataHandler(
-
-          new DataSource()
-          {
-
-            public InputStream getInputStream() throws IOException
-            {
-              return new ByteArrayInputStream(bout.toByteArray());
-            }
-
-            public OutputStream getOutputStream() throws IOException
-            {
-              return bout;
-            }
-
-            public String getContentType()
-            {
-              return "application/octet-stream";
-            }
-
-            public String getName()
-            {
-              return name + "_DataSource";
-            }
-          }
-      );
-
-    }
-    catch (Exception e)
-    {
-      throw new RuntimeException("Failed to process task template", e);
-    }
-
-    return merged;
-  }
-
-  public void processCompletion(long taskId, String outcome, InputStream payload)
-  {
-
-  }
-
-  protected void initializeProcessEngine()
-  {
-    try
-    {
-      InitialContext ctx = new InitialContext();
-      this.processEngine = (ProcessEngine)ctx.lookup("java:/ProcessEngine");
-    }
-    catch (Exception e)
-    {
-      throw new RuntimeException("Failed to lookup process engine", e);
-    }
-  }
-
-}

Copied: jbpm4/trunk/modules/integration/form-plugin/src/main/java/org/jbpm/integration/console/forms/TaskFormDispatcher.java (from rev 5155, jbpm4/trunk/modules/integration/form-plugin/src/main/java/org/jbpm/integration/console/forms/TaskDispatcherPluginImpl.java)
===================================================================
--- jbpm4/trunk/modules/integration/form-plugin/src/main/java/org/jbpm/integration/console/forms/TaskFormDispatcher.java	                        (rev 0)
+++ jbpm4/trunk/modules/integration/form-plugin/src/main/java/org/jbpm/integration/console/forms/TaskFormDispatcher.java	2009-07-01 08:12:26 UTC (rev 5169)
@@ -0,0 +1,199 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jbpm.integration.console.forms;
+
+import org.jboss.bpm.console.server.plugin.FormAuthorityRef;
+import org.jboss.bpm.console.server.plugin.FormDispatcherPlugin;
+import org.jbpm.api.*;
+import org.jbpm.api.task.Task;
+import org.jbpm.pvm.internal.env.Environment;
+import org.jbpm.pvm.internal.env.EnvironmentFactory;
+import org.jbpm.pvm.internal.model.ExecutionImpl;
+import org.jbpm.pvm.internal.model.Transition;
+import org.jbpm.pvm.internal.task.TaskImpl;
+
+import javax.activation.DataHandler;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * Processes form data to complete tasks.
+ *
+ * @author Heiko.Braun <heiko.braun at jboss.com>
+ */
+public class TaskFormDispatcher extends AbstractFormDispatcher
+    implements FormDispatcherPlugin
+{
+
+  public TaskFormDispatcher()
+  {
+    super();
+  }
+
+  public URL getDispatchUrl(FormAuthorityRef ref)
+  {
+    if(!taskHasForm(ref.getReferenceId()))
+      return null;
+
+    StringBuilder baseUrl = getBaseUrl();
+    baseUrl.append("/form/task/");
+    baseUrl.append( ref.getReferenceId());
+    baseUrl.append("/render");
+
+    try
+    {
+      return new URL(baseUrl.toString());
+    }
+    catch (MalformedURLException e)
+    {
+      throw new RuntimeException("Failed to resolve task dispatch url", e);
+    }
+  }
+
+  private boolean taskHasForm(String id)
+  {
+    boolean result = false;
+
+    Environment env = ((EnvironmentFactory)processEngine).openEnvironment();
+    try
+    {
+      TaskService taskService = processEngine.getTaskService();
+      Task task = taskService.getTask(id);
+      result = (task.getFormResourceName()!=null);
+    }
+    finally
+    {
+      env.close();
+    }
+
+    return result;
+  }
+
+  public DataHandler provideForm(FormAuthorityRef ref)
+  {
+    Environment env = ((EnvironmentFactory)processEngine).openEnvironment();
+   
+    try
+    {
+      TaskService taskService = processEngine.getTaskService();
+      Task task = taskService.getTask(ref.getReferenceId());
+
+      // access the processdefition
+      TaskImpl cast = ((TaskImpl) task);
+      ExecutionImpl processInstance = cast.getProcessInstance();
+      String processInstanceId =  processInstance.getId();
+      String processId =  processInstance.getProcessDefinition().getId();
+
+      RepositoryService repoService = processEngine.getRepositoryService();
+      ProcessDefinitionQuery query = repoService.createProcessDefinitionQuery();
+      query.processDefinitionId(processId);
+      ProcessDefinition procDef = query.uniqueResult();
+
+      // check if a template exists
+      String name = task.getFormResourceName();
+      InputStream template = repoService.getResourceAsStream(
+          procDef.getDeploymentId(), name
+      );
+
+      // merge template with process variables
+      if(template==null)
+        throw new IllegalArgumentException("Task form resource '"+name+"' doesn't exist.");
+
+      Map<String, Object> processContext = new HashMap<String, Object>(); // empty default
+      ExecutionService execService = processEngine.getExecutionService();
+      Set<String> varNames = execService.getVariableNames(processInstanceId);
+
+      if(varNames!=null)
+        processContext = execService.getVariables(processInstanceId, varNames);
+
+      // plugin context
+      StringBuilder action = getBaseUrl();
+      action.append("/form/task/");
+      action.append( ref.getReferenceId() );
+      action.append("/complete");
+
+      Map<String, Object> renderContext = new HashMap<String,Object>();
+
+      // form directive
+      FormDirective formDirective = new FormDirective();
+      formDirective.setAction( action.toString() );
+      renderContext.put(FORM_DIRECTIVE_KEY, formDirective);
+
+      // outcome directive
+      // TODO: Fix when https://jira.jboss.org/jira/browse/JBPM-2220 is done
+      OutcomeDirective outcomeDirective = new OutcomeDirective();
+      List<Transition> transitions =
+          ((ExecutionImpl) processInstance).getActivity().getOutgoingTransitions();
+      for(Transition t : transitions)
+      {
+        String outcomeName = t.getName()!=null ? t.getName() : "to_"+t.getDestination().getName();
+        outcomeDirective.getValues().add(outcomeName);
+      }
+      renderContext.put(OUTCOME_DIRECTIVE_NAME, outcomeDirective);
+
+      // process variables
+      renderContext.putAll(processContext);
+
+      DataHandler result = processTemplate(name, template, renderContext);
+      return result;
+    }
+    finally
+    {
+      env.close();
+    }
+  }
+
+  public void processCompletion(
+      FormAuthorityRef ref,
+      Map<String,Object> payload,
+      String performingUser,
+      String outcome)
+  {
+    Environment env = ((EnvironmentFactory)processEngine).openEnvironment();
+
+    String taskId = ref.getReferenceId();
+
+    try
+    {
+      TaskService taskService = processEngine.getTaskService();
+
+      if(payload!=null)
+        taskService.setVariables(taskId, payload);
+
+      if(null==outcome)
+        taskService.completeTask(taskId);
+      else
+        taskService.completeTask(taskId, outcome);
+
+    }
+    finally
+    {
+      env.close();
+    }
+  }
+
+}

Copied: jbpm4/trunk/modules/integration/form-plugin/src/main/resources/META-INF/services/org.jboss.bpm.console.server.plugin.FormDispatcherPlugin (from rev 5146, jbpm4/trunk/modules/integration/form-plugin/src/main/resources/META-INF/services/org.jboss.bpm.console.server.plugin.TaskDispatcherPlugin)
===================================================================
--- jbpm4/trunk/modules/integration/form-plugin/src/main/resources/META-INF/services/org.jboss.bpm.console.server.plugin.FormDispatcherPlugin	                        (rev 0)
+++ jbpm4/trunk/modules/integration/form-plugin/src/main/resources/META-INF/services/org.jboss.bpm.console.server.plugin.FormDispatcherPlugin	2009-07-01 08:12:26 UTC (rev 5169)
@@ -0,0 +1 @@
+org.jbpm.integration.console.forms.FormDispatcherComposite
\ No newline at end of file

Deleted: jbpm4/trunk/modules/integration/form-plugin/src/main/resources/META-INF/services/org.jboss.bpm.console.server.plugin.TaskDispatcherPlugin
===================================================================
--- jbpm4/trunk/modules/integration/form-plugin/src/main/resources/META-INF/services/org.jboss.bpm.console.server.plugin.TaskDispatcherPlugin	2009-07-01 07:17:16 UTC (rev 5168)
+++ jbpm4/trunk/modules/integration/form-plugin/src/main/resources/META-INF/services/org.jboss.bpm.console.server.plugin.TaskDispatcherPlugin	2009-07-01 08:12:26 UTC (rev 5169)
@@ -1 +0,0 @@
-org.jbpm.integration.console.forms.TaskDispatcherPluginImpl
\ No newline at end of file




More information about the jbpm-commits mailing list