1 Reply Latest reply on Sep 8, 2011 11:55 AM by Francesco Pietrobelli

    How can I get a itemDefinition related to a process variable?

    Francesco Pietrobelli Newbie

      Hi guys,

      I would like to build forms for human activities in an automatic and dynamic fashion, but to do this I need to know the data type associated to every dataOutput of the tasks.

       

      My first idea was to retrieve the data type of a process variable inside a WorkItemHandler in the following way:

       

      public void executeWorkItem(WorkItem item, WorkItemManager manager) {
           WorkflowProcessInstance currentProcess = (WorkflowProcessInstance) ksession.getProcessInstance(item.getProcessInstanceId());
           HumanTaskNodeInstance currentNode = (HumanTaskNodeInstance) findNodeInstance(item.getId(), currentProcess);
           Map<String, String> outMappings = ht.getHumanTaskNode().getOutMappings();
           for (String outParameterName : outMappings.keySet()) {
                System.out.println("Type of process variable: "+currentProcess.getVariable(outMappings.get(outParameterName)).getClass());
           }
      }
      
      
      private WorkItemNodeInstance findNodeInstance(long workItemId,NodeInstanceContainer container) {
           for (NodeInstance nodeInstance : container.getNodeInstances()) {
                if (nodeInstance instanceof WorkItemNodeInstance) {
                     WorkItemNodeInstance workItemNodeInstance = (WorkItemNodeInstance) nodeInstance;
                     if (workItemNodeInstance.getWorkItem().getId() == workItemId) {
                          return workItemNodeInstance;
                     }
                }
                if (nodeInstance instanceof NodeInstanceContainer) {
                     WorkItemNodeInstance result = findNodeInstance(workItemId,((NodeInstanceContainer) nodeInstance));
                     if (result != null) {
                          return result;
                     }
                }
           }
           return null;
      }
      
      

       

      But in many case outMappings.get(outParameterName) returns null because process variable wasn't set before and so and i can't invoke getClass() on it.

       

      Does any one have any suggestion? is possible to do this programmatically via API?

       

      Francesco.

       

      P.S: I'am using jBPM 5.1Final

        • 1. Re: How can I get a itemDefinition related to a process variable?
          Francesco Pietrobelli Newbie

          The only solution i found is parsing the bpmn file in which the process is defined in follwiong way:

           

          org.jbpm.workflow.instance.WorkflowProcessInstance currentProcess=(org.jbpm.workflow.instance.WorkflowProcessInstance)ksession.getProcess(processInstanceId);
          DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
          documentBuilderFactory.setNamespaceAware(true);
          DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
          Document doc = documentBuilder.parse(((org.jbpm.process.core.Process) currentProcess.getProcess()).getResource().getInputStream());
          
          
          XPathFactory xFactory = XPathFactory.newInstance();
          XPath xPath = xFactory.newXPath();
          xPath.setNamespaceContext(new BPMN2Namespaces());
          
          Map<String, String> outMap = currentNode.getHumanTaskNode().getOutMappings();
          for (String outParameter : outMap.keySet()) {
               XPathExpression xExpr1 = xPath.compile("//bpmn2:definitions/bpmn2:process/bpmn2:property[@id='"
                                          + outMap.get(outParameter)
                                          + "']/@itemSubjectRef");
               String itemSubjectRef = xExpr1.evaluate(doc);
               XPathExpression xExpr2 = xPath.compile("//bpmn2:definitions/bpmn2:itemDefinition[@id='"
                                          + itemSubjectRef + "']/@structureRef");
               String structureRef = xExpr2.evaluate(doc);
          
               System.out.println("Type of out parameter named '"+outParameter+"': "+structureRef);
          }
          

           

          But if same one know a way to do that without parsing bpmn file is very appreciated....

          Regards,

          Francesco