5 Replies Latest reply on Dec 2, 2009 9:08 AM by aroeder

    Variables keep old values

    aroeder Newbie

      I tried to update my variables after I changed them on a webpage. The code looks basicly like that:

      JbpmContext ctx = JbpmConfiguration.getInstance("jbpm.cfg.xml").createJbpmContext();
      
      TaskInstance currentTI = ctx.getTaskInstance(this.currentTaskInstanceID);
      
      
      Map<String, ? extends Object> variables = this.getFormDescription().getModelMap();
      
      if (variables != null) {
      
       currentTI.getContextInstance().addVariables(variables);
      }
      
      ctx.save(currentTI);
      


      When I debug the code the variables from my model map contain the new values. Also the the values of the variables do change when I call addVariables. After calling
      ctx.save(currentTI);
      I expected that also values would change in the table JBPM_VARIABLEINSTANCE, but they don't.

      What do I miss here?



        • 1. Re: Variables keep old values
          Ronald van Kuijk Master

          please make a full unittest that demonstrates the problem.

          And do you close the context? You should... according to the docs

          • 2. Re: Variables keep old values
            Tej jbpm Novice

            Are you using jbpm 3 version..i remember reading somewhere that only jbpm 4 replace the previously created values..worth checking the user guide again..

            • 3. Re: Variables keep old values
              Ronald van Kuijk Master

              3 should change them as well, but you have to close the context AND NOT set them as local variables

              • 4. Re: Variables keep old values
                aroeder Newbie

                Would do you mean with local variables?

                I also tried:

                currentTI.getProcessInstance().getContextInstance().addVariables(variables);
                


                But that doesn't work neither. Still no value changed in JBPM_VARIABLEINSTANCE nor a new record was added.

                • 5. Re: Variables keep old values
                  aroeder Newbie

                  I wrote a JUnit test for showing my problem:

                  package de.firstdata.test;
                  
                  import java.io.IOException;
                  import java.util.HashMap;
                  import java.util.Map;
                  import java.util.Set;
                  
                  import org.jbpm.graph.def.ProcessDefinition;
                  import org.jbpm.graph.exe.ProcessInstance;
                  import org.jbpm.taskmgmt.exe.TaskInstance;
                  
                  import de.firstdata.config.TicketServiceConfigService;
                  import de.firstdata.dm.JBPMContextWrapper;
                  import de.firstdata.dm.TicketSystemDomainModelFacade;
                  
                  public class JbpmVariablesTest extends ATicketServiceTestA {
                  
                   private TicketSystemDomainModelFacade facade = null;
                  
                   @SuppressWarnings("unchecked")
                   public void testVariables() throws IOException {
                  
                   JBPMContextWrapper ctx = getContextWrapper();
                  
                   try {
                   ProcessDefinition processDefinition = ProcessDefinition
                   .parseXmlString("<process-definition xmlns='' name='ABR'>" + "<start-state name='start-state1'>"
                   + "<transition to='edit'></transition>" + "</start-state>" + "<task-node name='edit'>"
                   + "<task name='edit'>" + " <controller>"
                   + " <variable access='read,write,required' name='inbox'></variable>" + " </controller>"
                   + "</task>" + "<transition to='change' name='tochange'></transition>"
                   + "<transition to='end-state1' name='done'></transition>" + "</task-node>"
                   + "<task-node name='change'>" + "<task name='indexDataChange'>" + " <controller>"
                   + " <variable access='read,write,required' name='inbox'></variable>" + " </controller>"
                   + "</task>" + "<transition to='edit' name='back'></transition>" + "</task-node>"
                   + "<end-state name='end-state1'></end-state>" + "</process-definition>");
                  
                   Map<String, String> myMap = new HashMap<String, String>();
                   myMap.put("inbox", "COMPLAINTS_MANAGEMENT");
                  
                   ctx.deployProcessDefinition(processDefinition);
                   ProcessInstance processInstance = processDefinition.createProcessInstance(myMap);
                   ctx.save(processInstance);
                   processInstance.signal();
                  
                   TaskInstance taskEdit = getTaskInstance(processInstance, "edit");
                   taskEdit.end("tochange");
                  
                   ctx.save(taskEdit);
                  
                   TaskInstance taskChange = getTaskInstance(processInstance, "indexDataChange");
                   Map<String, String> variables = taskChange.getContextInstance().getVariables();
                   variables.put("inbox", "CS_ISO");
                   taskChange.getProcessInstance().getContextInstance().addVariables(variables);
                   ctx.save(taskChange);
                   ctx.close();
                   ctx = getContextWrapper();
                  
                   taskChange.end("back");
                  
                   taskEdit = getTaskInstance(processInstance, "edit");
                   Map<String, String> changedVariables = taskEdit.getProcessInstance().getContextInstance().getVariables();
                  
                   String inbox = (String) changedVariables.get("inbox");
                  
                   taskEdit.end("done");
                   processInstance.end();
                   ctx.deleteProcessDefinition(processDefinition);
                  
                   assertEquals("CS_ISO", inbox);
                   } finally {
                   ctx.close();
                   }
                   }
                  
                   private JBPMContextWrapper getContextWrapper() {
                   if (facade == null) {
                   facade = (TicketSystemDomainModelFacade) TicketServiceConfigService.getConfig().getFacade();
                   }
                   return JBPMContextWrapper.getInstance("jbpm.cfg.xml", facade);
                   }
                  
                   @SuppressWarnings("unchecked")
                   private TaskInstance getTaskInstance(final ProcessInstance processInstance, final String taskInstanceName) {
                   TaskInstance returnInstance = null;
                   Set<TaskInstance> tasks = (Set<TaskInstance>) processInstance.getTaskMgmtInstance().getTaskInstances();
                  
                   for (TaskInstance ti : tasks) {
                   if (ti.getName().equals(taskInstanceName) && !ti.hasEnded()) {
                   returnInstance = ti;
                   }
                   }
                  
                   return returnInstance;
                   }
                  }