Package org.drools.bpmn2.legacy.beta1

Source Code of org.drools.bpmn2.legacy.beta1.UserTaskHandler

/**
* Copyright 2010 JBoss Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.drools.bpmn2.legacy.beta1;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.drools.process.core.Work;
import org.drools.process.core.impl.WorkImpl;
import org.drools.workflow.core.Node;
import org.drools.workflow.core.node.HumanTaskNode;
import org.drools.workflow.core.node.WorkItemNode;
import org.drools.xml.ExtensibleXmlParser;
import org.w3c.dom.Element;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

public class UserTaskHandler extends TaskHandler {
   
    protected Node createNode(Attributes attrs) {
        return new HumanTaskNode();
    }
   
    @SuppressWarnings("unchecked")
  public Class generateNodeFor() {
        return HumanTaskNode.class;
    }

    protected void handleNode(final Node node, final Element element, final String uri,
            final String localName, final ExtensibleXmlParser parser) throws SAXException {
      super.handleNode(node, element, uri, localName, parser);
      HumanTaskNode humanTaskNode = (HumanTaskNode) node;
        Work work = new WorkImpl();
        work.setName("Human Task");
      humanTaskNode.setWork(work);
      Map<String, String> dataInputs = new HashMap<String, String>();
      Map<String, String> dataOutputs = new HashMap<String, String>();
      List<String> owners = new ArrayList<String>();
      org.w3c.dom.Node xmlNode = element.getFirstChild();
        while (xmlNode != null) {
          String nodeName = xmlNode.getNodeName();
          if ("ioSpecification".equals(nodeName)) {
            readIoSpecification(xmlNode, dataInputs, dataOutputs);
          } else if ("dataInputAssociation".equals(nodeName)) {
            readDataInputAssociation(xmlNode, humanTaskNode, dataInputs);
          } else if ("dataOutputAssociation".equals(nodeName)) {
            readDataOutputAssociation(xmlNode, humanTaskNode, dataOutputs);
          } else if ("potentialOwner".equals(nodeName)) {
            owners.add(readPotentialOwner(xmlNode, humanTaskNode));
          }
        xmlNode = xmlNode.getNextSibling();
        }
        if (owners.size() > 0) {
          String owner = owners.get(0);
          for (int i = 1; i < owners.size(); i++) {
            owner += "," + owners.get(i);
          }
          humanTaskNode.getWork().setParameter("ActorId", owner);         
        }
    }
   
    protected String readPotentialOwner(org.w3c.dom.Node xmlNode, HumanTaskNode humanTaskNode) {
    return xmlNode.getFirstChild().getFirstChild().getFirstChild().getTextContent();
    }
   
  public void writeNode(Node node, StringBuilder xmlDump, boolean includeMeta) {
    HumanTaskNode humanTaskNode = (HumanTaskNode) node;
    writeNode("userTask", humanTaskNode, xmlDump, includeMeta);
    xmlDump.append("implementation=\"humanTaskWebService\" >" + EOL);
    writeIO(humanTaskNode, xmlDump);
    String ownerString = (String) humanTaskNode.getWork().getParameter("ActorId");
    if (ownerString != null) {
      String[] owners = ownerString.split(",");
      for (String owner: owners) {
        xmlDump.append(
          "      <potentialOwner resourceRef=\"tns:Actor\" >" + EOL +
          "        <resourceAssignmentExpression>" + EOL +
          "          <formalExpression>" + owner + "</formalExpression>" + EOL +
          "        </resourceAssignmentExpression>" + EOL +
          "      </potentialOwner>" + EOL);
      }
    }
    endNode("userTask", xmlDump);
  }

  protected void writeIO(WorkItemNode workItemNode, StringBuilder xmlDump) {
    xmlDump.append("      <ioSpecification>" + EOL);
    for (Map.Entry<String, String> entry: workItemNode.getInMappings().entrySet()) {
      xmlDump.append("        <dataInput id=\"" + XmlBPMNProcessDumper.getUniqueNodeId(workItemNode) + "_" + entry.getKey() + "Input\" name=\"" + entry.getKey() + "\" />" + EOL);
    }
    for (Map.Entry<String, Object> entry: workItemNode.getWork().getParameters().entrySet()) {
      if (!"ActorId".equals(entry.getKey()) && entry.getValue() != null) {
        xmlDump.append("        <dataInput id=\"" + XmlBPMNProcessDumper.getUniqueNodeId(workItemNode) + "_" + entry.getKey() + "Input\" name=\"" + entry.getKey() + "\" />" + EOL);
      }
    }
    for (Map.Entry<String, String> entry: workItemNode.getOutMappings().entrySet()) {
      xmlDump.append("        <dataOutput id=\"" + XmlBPMNProcessDumper.getUniqueNodeId(workItemNode) + "_" + entry.getKey() + "Output\" name=\"" + entry.getKey() + "\" />" + EOL);
    }
    xmlDump.append("        <inputSet>" + EOL);
    for (Map.Entry<String, String> entry: workItemNode.getInMappings().entrySet()) {
      xmlDump.append("          <dataInputRefs>" + XmlBPMNProcessDumper.getUniqueNodeId(workItemNode) + "_" + entry.getKey() + "Input</dataInputRefs>" + EOL);
    }
    for (Map.Entry<String, Object> entry: workItemNode.getWork().getParameters().entrySet()) {
      if (!"ActorId".equals(entry.getKey()) && entry.getValue() != null) {
        xmlDump.append("          <dataInputRefs>" + XmlBPMNProcessDumper.getUniqueNodeId(workItemNode) + "_" + entry.getKey() + "Input</dataInputRefs>" + EOL);
      }
    }
    xmlDump.append(
      "        </inputSet>" + EOL);
    xmlDump.append("        <outputSet>" + EOL);
    for (Map.Entry<String, String> entry: workItemNode.getOutMappings().entrySet()) {
      xmlDump.append("          <dataOutputRefs>" + XmlBPMNProcessDumper.getUniqueNodeId(workItemNode) + "_" + entry.getKey() + "Output</dataOutputRefs>" + EOL);
    }
    xmlDump.append(
      "        </outputSet>" + EOL);
    xmlDump.append(
      "      </ioSpecification>" + EOL);
    for (Map.Entry<String, Object> entry: workItemNode.getWork().getParameters().entrySet()) {
      if (!"ActorId".equals(entry.getKey()) && entry.getValue() != null) {
        xmlDump.append(
          "      <property id=\"" + XmlBPMNProcessDumper.getUniqueNodeId(workItemNode) + "_" + entry.getKey() + "\" />" + EOL);
      }
    }
    for (Map.Entry<String, String> entry: workItemNode.getInMappings().entrySet()) {
      xmlDump.append("      <dataInputAssociation>" + EOL);
      xmlDump.append(
        "        <sourceRef>" + XmlBPMNProcessDumper.getUniqueNodeId(workItemNode) + "_" + entry.getValue() + "</sourceRef>" + EOL +
        "        <targetRef>" + XmlBPMNProcessDumper.getUniqueNodeId(workItemNode) + "_" + entry.getKey() + "</targetRef>" + EOL);
      xmlDump.append("      </dataInputAssociation>" + EOL);
    }
    for (Map.Entry<String, Object> entry: workItemNode.getWork().getParameters().entrySet()) {
      if (!"ActorId".equals(entry.getKey()) && entry.getValue() != null) {
        xmlDump.append("      <dataInputAssociation>" + EOL);
        xmlDump.append(
          "        <assignment>" + EOL +
          "          <from xs:type=\"tFormalExpression\">" + entry.getValue().toString() + "</from>" + EOL +
          "          <to xs:type=\"tFormalExpression\">" + XmlBPMNProcessDumper.getUniqueNodeId(workItemNode) + "_" + entry.getKey() + "Input</to>" + EOL +
          "        </assignment>" + EOL +
          "        <sourceRef>" + XmlBPMNProcessDumper.getUniqueNodeId(workItemNode) + "_" + entry.getKey() + "</sourceRef>" + EOL +
          "        <targetRef>" + XmlBPMNProcessDumper.getUniqueNodeId(workItemNode) + "_" + entry.getKey() + "Input</targetRef>" + EOL);
        xmlDump.append("      </dataInputAssociation>" + EOL);
      }
    }
    for (Map.Entry<String, String> entry: workItemNode.getOutMappings().entrySet()) {
      xmlDump.append("      <dataOutputAssociation>" + EOL);
      xmlDump.append(
        "        <sourceRef>" + XmlBPMNProcessDumper.getUniqueNodeId(workItemNode) + "_" + entry.getKey() + "</sourceRef>" + EOL +
        "        <targetRef>" + XmlBPMNProcessDumper.getUniqueNodeId(workItemNode) + "_" + entry.getValue() + "</targetRef>" + EOL);
      xmlDump.append("      </dataOutputAssociation>" + EOL);
    }
  }

}
TOP

Related Classes of org.drools.bpmn2.legacy.beta1.UserTaskHandler

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.