1 Reply Latest reply on Nov 4, 2005 7:32 AM by Tom Baeyens

    jbpm3.1-alpha1 - Problem with Taskcontroller

    kaobiore Newbie

      Hi!

      I'm trying to write a taskcontroller for jbpm-3.1-alpha1 and have some problems doing this.

      My Taskcontroller looks like this:

      package org.jbpm.taskcontroller;
      
      import java.util.ArrayList;
      import java.util.Iterator;
      import java.util.List;
      
      import org.apache.commons.logging.Log;
      import org.apache.commons.logging.LogFactory;
      import org.dom4j.DocumentException;
      import org.dom4j.DocumentHelper;
      import org.dom4j.Element;
      
      import org.jbpm.context.def.VariableAccess;
      import org.jbpm.context.exe.ContextInstance;
      import org.jbpm.graph.exe.Token;
      import org.jbpm.instantiation.Delegation;
      import org.jbpm.instantiation.XmlInstantiator;
      import org.jbpm.taskmgmt.def.TaskControllerHandler;
      import org.jbpm.taskmgmt.exe.TaskInstance;
      
      public class MyTaskController implements TaskControllerHandler {
      
       private static final long serialVersionUID = 1L;
      
       private Delegation taskControllerDelegation = null;
       private static final Log log = LogFactory.getLog(XmlInstantiator.class);
      
       private List variableAccesses = null;
      
       public MyTaskController(String configuration) {
       System.out.println(configuration);
       Element e = parseConfiguration(configuration);
      
       variableAccesses = readVariableAccesses(e);
       }
      
       public void initializeTaskVariables(TaskInstance taskInstance,
       ContextInstance contextInstance, Token token) {
       if (taskControllerDelegation != null) {
       TaskControllerHandler taskControllerHandler = (TaskControllerHandler) taskControllerDelegation
       .instantiate();
       taskControllerHandler.initializeTaskVariables(taskInstance,
       contextInstance, token);
       } else {
       if (variableAccesses != null) {
       Iterator iter = variableAccesses.iterator();
       while (iter.hasNext()) {
       VariableAccess variableAccess = (VariableAccess) iter
       .next();
       if (variableAccess.isReadable()) {
       String variableName = variableAccess.getVariableName();
       Object value = contextInstance.getVariable(
       variableName, token);
       if (value != null) {
       String mappedName = variableAccess.getMappedName();
       taskInstance.setVariable(mappedName, value);
       }
       }
       }
       }
      
       }
       }
      
       public void submitTaskVariables(TaskInstance taskInstance,
       ContextInstance contextInstance, Token token) {
       if (taskControllerDelegation != null) {
       TaskControllerHandler taskControllerHandler = (TaskControllerHandler) taskControllerDelegation
       .instantiate();
       taskControllerHandler.submitTaskVariables(taskInstance,
       contextInstance, token);
      
       } else {
       if (variableAccesses != null) {
       String missingTaskVariables = null;
       Iterator iter = variableAccesses.iterator();
       while (iter.hasNext()) {
       VariableAccess variableAccess = (VariableAccess) iter
       .next();
       String mappedName = variableAccess.getMappedName();
       // first check if the required variableInstances are present
       if ((variableAccess.isRequired())
       && (!taskInstance.hasVariable(mappedName))) {
       if (missingTaskVariables == null) {
       missingTaskVariables = mappedName;
       } else {
       missingTaskVariables += ", " + mappedName;
       }
       }
       }
      
       // if there are missing, required parameters, throw an
       // IllegalArgumentException
       if (missingTaskVariables != null) {
       throw new IllegalArgumentException(
       "missing task variables: " + missingTaskVariables);
       }
      
       iter = variableAccesses.iterator();
       while (iter.hasNext()) {
       VariableAccess variableAccess = (VariableAccess) iter
       .next();
       String mappedName = variableAccess.getMappedName();
       String variableName = variableAccess.getVariableName();
       if (variableAccess.isWritable()) {
       Object value = taskInstance.getVariable(mappedName);
       if (value != null) {
       contextInstance.setVariable(variableName, value,
       token);
       }
       }
       }
       }
       }
       }
      
       protected Element parseConfiguration(String configuration) {
       Element element = null;
       try {
       element = DocumentHelper.parseText(
       "<action>" + configuration + "</action>").getRootElement();
       } catch (DocumentException e) {
       log
       .error("couldn't parse bean configuration : "
       + configuration, e);
       throw new RuntimeException(e);
       }
      
       return element;
       }
      
       public List readVariableAccesses(Element element) {
       List variableAccesses = new ArrayList();
      
       Iterator iter = element.elementIterator("variable");
       while (iter.hasNext()) {
       Element variableElement = (Element) iter.next();
      
       String variableName = variableElement.attributeValue("name");
       if (variableName == null) {
       // addProblem(new Problem(Problem.LEVEL_WARNING, "the name
       // attribute of a variable element is required:
       // "+variableElement.asXML()));
       }
       String access = variableElement.attributeValue("access",
       "read,write");
       String mappedName = variableElement.attributeValue("mapped-name");
       variableAccesses.add(new VariableAccess(variableName, access,mappedName));
       }
       return variableAccesses;
       }
      }
      


       <start-state name="New Customer">
       <task swimlane="create_customer">
       <controller class='org.jbpm.taskcontroller.MyTaskController' config-type='constructor'>
       <variable name="Salutation" />
       <variable name="County" />
       </controller>
       </task>
       <transition name="tr_collect_cutomer_data_to_check_availability" to="Check"></transition>
       </start-state>
      


      On the webapp are no input-fields or variable names displayed ... i've added two out-prints in the TaskBean for debugging.

       System.out.println("CLASS TASKCONTROLLER ==> "+taskInstance.getTask().getTaskController().getClass());
       System.out.println("NUM VARIABLE ==> "+taskInstance.getTask().getTaskController().getVariableAccesses().size());
      


      This gives me the following output:
      13:49:11,413 INFO [STDOUT] CLASS TASKCONTROLLER ==> class org.jbpm.taskmgmt.def
      .TaskController$$EnhancerByCGLIB$$385b480b
      13:49:11,473 INFO [STDOUT] NUM VARIABLE ==> 0
      


      What am I doing wrong? ... Thanks for your help!

      rgds
      /k