5 Replies Latest reply on May 5, 2011 3:21 AM by melc

    Java code accessing task variables

    npereira

      Hi forum,

       

      I'm having some trouble accessing the variables in my BPMN.

       

      The problem is the following, I have a Human task, that has some "on entry actions".

      In these on entry actions I generate a variable, named myVariable, that is local to the task.

       

      Now my problem is when I'm on my Java code, accessing through TaskSummary:

       

                TaskClient client = new TaskClient(new MinaTaskClientConnector("client 1", new           MinaTaskClientHandler(SystemEventListenerFactory.getSystemEventListener())));

                  if(client.connect("127.0.0.1", 9123))

                  {

                      BlockingTaskSummaryResponseHandler taskSummaryResponseHandler = new BlockingTaskSummaryResponseHandler();

                      client.getTasksAssignedAsPotentialOwner("krisv", "en-UK", taskSummaryResponseHandler);

       

                      List<TaskSummary> tasks = taskSummaryResponseHandler.getResults();

                      Iterator itr = tasks.iterator();

                      while(itr.hasNext())

                      {

                          TaskSummary t = (TaskSummary)itr.next();

                          System.out.println(t.getId());

                          ...

                      }

       

       

      How can I access these variables?

      Is it possible and how?

       

      Regards,

      Nelson

        • 1. Java code accessing task variables
          npereira

          Hi forum,

           

          I have been around this issue for some time now and I really fail to see a way out of this one.

           

          The bottom line is that what I want to do, is once I have the Task object, I want to access the parameters that I maped for this particular Task.

           

          I have tried the Task object and also TaskData object, but I fail to see a method like GetParameters or GetVariable? Is there such a method in the JBPM5 API? if so how do I access a particular parameter on a Task?

           

          Regards,

          Nelson

          • 2. Re: Java code accessing task variables
            loga07

            Hi,

             

            In order to access varaibles inside the Human Task, you have to map those variables in to the Human Task in workflow definition using Parameter Mapping attribute in the properties tab. Note that the parameter map for the Human Task should be of 'Content' only.

             

            Also in the code, you have to invoke taskData.getDocumentContentId() to get the content which contains the parameter values that mapped in the definition. The sample code is as follows:

             

            public static Object getContent(long contentId) {

                    connect();

                    Object obj = null;

                    try {

                        BlockingGetContentResponseHandler responseHandler = new BlockingGetContentResponseHandler();

                        client.getContent(contentId, responseHandler);

                        Content content = responseHandler.getContent();

                        byte[] byteContent = content.getContent();

                        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(

                                byteContent);

             

                        ObjectInputStream inputStream = new ObjectInputStream(

                                byteArrayInputStream);

                        obj = inputStream.readObject();

                    } catch (Throwable t) {

                        t.printStackTrace();

                    }

                    return obj;

                }

             

             

            Hope this helps to solve your problem.

             

            Regards,

            Loga

            • 3. Re: Java code accessing task variables
              melc

              If i'm not mistaken when using a task any variables i.e. comming from parammeter mapping, or even entry actions etc should be placed in content then the content is accessed like below,

              long contentId = task.getTaskData().getDocumentContentId();

                              if (contentId != -1) {

                                      BlockingGetContentResponseHandler getContentResponseHandler = new BlockingGetContentResponseHandler();

                          client.getContent(contentId, getContentResponseHandler);

                          Content content = getContentResponseHandler.getContent();

               

              all other arguments of task are access from the methods of TaskData , i.e. task.getTaskData().getComments(); etc

               

              Otherwise a custom workitem has method getParameter for the custom arguments specified for this workitem.But again the variables from parameter mapping and entry actions will have to be placed to the specific argument of that workitem.

               

               

              Please correct me if there is a better approach....

               

              p.s. window open toooo long just noticed LOGANATHAN's reply.... sorry

              • 4. Java code accessing task variables
                npereira

                Hi Guys,

                 

                Thanks very much for your replies, they really did help me.

                I got it to work.

                 

                But for future reference, this will only work if the "Result Mapping" of the Task is set, because what is returned from

                 

                Content content = responseHandler.getContent();

                 

                is whatever is in the "Result Mapping".

                 

                At least this is what I gather from my experiments! But feel free to correct me if I'm wrong.

                • 5. Re: Java code accessing task variables
                  melc

                  Good i'm glad it worked out for

                   

                  Actually, you can use several methods to set variables and parameters for example,

                   

                  1. result mapping (as you already mentioned), if you want to map a process variable to a value from the result parameter of the task

                  2. parameter mapping (as loganathan mentioned) - here you can map parameters of the task with process variables

                  3. set a task parameter i.e. in the properties of the task at the content parameter field of the task place a notation like #{some_value_from_variable}

                  4. set process variables within on entry actions and on exit actions and then use the parameter mapping to map those variables to your task parameters and use them in your task i.e. (from the sample.bpmn of the jbpm5 installation),

                   

                  on entry action

                  java.util.Map contentParam = new java.util.HashMap();

                  contentParam.put("reason", reason);

                  contentParam.put("performance", performance);

                  kcontext.setVariable("content", contentParam);

                   

                  Parameter mapping

                  {Content=content}

                   

                  5. on exit action you can set parameters to variables as shown in the code above