Search in sources :

Example 1 with DataTransformer

use of org.kie.api.runtime.process.DataTransformer in project jbpm by kiegroup.

the class TransformerJSONandXMLTest method testJSONTransformer.

@Test
public void testJSONTransformer() {
    String expectedJson = "{\"name\":\"john\",\"age\":34}";
    DataTransformer transformer = DataTransformerRegistry.get().find("http://www.mvel.org/2.0");
    String expressionR = "new com.fasterxml.jackson.databind.ObjectMapper().readValue(json, clazz)";
    String expressionW = "new com.fasterxml.jackson.databind.ObjectMapper().writeValueAsString(employee)";
    Object compiled = transformer.compile(expressionW, new HashMap<String, Object>());
    Map<String, Object> parameters = new HashMap<String, Object>();
    Person person = new Person();
    person.setAge(34);
    person.setName("john");
    parameters.put("employee", person);
    Object result = transformer.transform(compiled, parameters);
    System.out.println(result);
    assertEquals(expectedJson, result);
    Object compiledR = transformer.compile(expressionR, new HashMap<String, Object>());
    parameters.clear();
    parameters.put("json", result);
    parameters.put("clazz", Person.class);
    Object resultr = transformer.transform(compiledR, parameters);
    System.out.println(resultr);
    assertNotNull(resultr);
    assertTrue(resultr instanceof Person);
    assertEquals("john", ((Person) resultr).getName());
    assertEquals(34, ((Person) resultr).getAge().intValue());
}
Also used : DataTransformer(org.kie.api.runtime.process.DataTransformer) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 2 with DataTransformer

use of org.kie.api.runtime.process.DataTransformer in project jbpm by kiegroup.

the class ActivityTest method testServiceTaskWithCustomTransformation.

@Test
public void testServiceTaskWithCustomTransformation() throws Exception {
    DataTransformerRegistry.get().register("http://custom/transformer", new DataTransformer() {

        @Override
        public Object transform(Object expression, Map<String, Object> parameters) {
            // support only single object
            String value = parameters.values().iterator().next().toString();
            Object result = null;
            if ("caplitalizeFirst".equals(expression)) {
                String first = value.substring(0, 1);
                String main = value.substring(1, value.length());
                result = first.toUpperCase() + main;
            } else if ("caplitalizeLast".equals(expression)) {
                String last = value.substring(value.length() - 1);
                String main = value.substring(0, value.length() - 1);
                result = main + last.toUpperCase();
            } else {
                throw new IllegalArgumentException("Unknown expression " + expression);
            }
            return result;
        }

        @Override
        public Object compile(String expression, Map<String, Object> parameters) {
            // compilation not supported
            return expression;
        }
    });
    KieBase kbase = createKnowledgeBaseWithoutDumper("BPMN2-ServiceProcessWithCustomTransformation.bpmn2");
    ksession = createKnowledgeSession(kbase);
    ksession.getWorkItemManager().registerWorkItemHandler("Service Task", new ServiceTaskHandler());
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("s", "john doe");
    WorkflowProcessInstance processInstance = (WorkflowProcessInstance) ksession.startProcess("ServiceProcess", params);
    assertProcessInstanceFinished(processInstance, ksession);
    assertEquals("John doE", processInstance.getVariable("s"));
}
Also used : ServiceTaskHandler(org.jbpm.bpmn2.handler.ServiceTaskHandler) DataTransformer(org.kie.api.runtime.process.DataTransformer) HashMap(java.util.HashMap) KieBase(org.kie.api.KieBase) WorkflowProcessInstance(org.kie.api.runtime.process.WorkflowProcessInstance) Test(org.junit.Test)

Example 3 with DataTransformer

use of org.kie.api.runtime.process.DataTransformer in project jbpm by kiegroup.

the class TaskHandler method readDataInputAssociation.

protected void readDataInputAssociation(org.w3c.dom.Node xmlNode, WorkItemNode workItemNode, Map<String, String> dataInputs) {
    // sourceRef
    org.w3c.dom.Node subNode = xmlNode.getFirstChild();
    if ("sourceRef".equals(subNode.getNodeName())) {
        String source = subNode.getTextContent();
        // targetRef
        subNode = subNode.getNextSibling();
        String target = subNode.getTextContent();
        // transformation
        Transformation transformation = null;
        subNode = subNode.getNextSibling();
        if (subNode != null && "transformation".equals(subNode.getNodeName())) {
            String lang = subNode.getAttributes().getNamedItem("language").getNodeValue();
            String expression = subNode.getTextContent();
            DataTransformer transformer = transformerRegistry.find(lang);
            if (transformer == null) {
                throw new IllegalArgumentException("No transformer registered for language " + lang);
            }
            transformation = new Transformation(lang, expression);
            // transformation.setCompiledExpression(transformer.compile(expression));
            subNode = subNode.getNextSibling();
        }
        // assignments
        List<Assignment> assignments = new LinkedList<Assignment>();
        while (subNode != null) {
            org.w3c.dom.Node ssubNode = subNode.getFirstChild();
            String from = ssubNode.getTextContent();
            String to = ssubNode.getNextSibling().getTextContent();
            assignments.add(new Assignment("XPath", from, to));
            subNode = subNode.getNextSibling();
        }
        workItemNode.addInAssociation(new DataAssociation(source, dataInputs.get(target), assignments, transformation));
    } else {
        // targetRef
        String to = subNode.getTextContent();
        // assignment
        subNode = subNode.getNextSibling();
        if (subNode != null) {
            org.w3c.dom.Node subSubNode = subNode.getFirstChild();
            NodeList nl = subSubNode.getChildNodes();
            if (nl.getLength() > 1) {
                // not supported ?
                workItemNode.getWork().setParameter(dataInputs.get(to), subSubNode.getTextContent());
                return;
            } else if (nl.getLength() == 0) {
                return;
            }
            Object result = null;
            Object from = nl.item(0);
            if (from instanceof Text) {
                String text = ((Text) from).getTextContent();
                if (text.startsWith("\"") && text.endsWith("\"")) {
                    result = text.substring(1, text.length() - 1);
                } else {
                    result = text;
                }
            } else {
                result = nl.item(0);
            }
            workItemNode.getWork().setParameter(dataInputs.get(to), result);
        }
    }
}
Also used : Transformation(org.jbpm.workflow.core.node.Transformation) DataAssociation(org.jbpm.workflow.core.node.DataAssociation) NodeList(org.w3c.dom.NodeList) Text(org.w3c.dom.Text) LinkedList(java.util.LinkedList) Assignment(org.jbpm.workflow.core.node.Assignment) DataTransformer(org.kie.api.runtime.process.DataTransformer)

Example 4 with DataTransformer

use of org.kie.api.runtime.process.DataTransformer in project jbpm by kiegroup.

the class BusinessRuleTaskHandler method readDataInputAssociation.

protected void readDataInputAssociation(org.w3c.dom.Node xmlNode, RuleSetNode ruleSetNode, Map<String, String> dataInputs) {
    // sourceRef
    org.w3c.dom.Node subNode = xmlNode.getFirstChild();
    if ("sourceRef".equals(subNode.getNodeName())) {
        String source = subNode.getTextContent();
        // targetRef
        subNode = subNode.getNextSibling();
        String target = subNode.getTextContent();
        // transformation
        Transformation transformation = null;
        subNode = subNode.getNextSibling();
        if (subNode != null && "transformation".equals(subNode.getNodeName())) {
            String lang = subNode.getAttributes().getNamedItem("language").getNodeValue();
            String expression = subNode.getTextContent();
            DataTransformer transformer = transformerRegistry.find(lang);
            if (transformer == null) {
                throw new IllegalArgumentException("No transformer registered for language " + lang);
            }
            transformation = new Transformation(lang, expression);
            subNode = subNode.getNextSibling();
        }
        // assignments
        List<Assignment> assignments = new LinkedList<Assignment>();
        while (subNode != null) {
            org.w3c.dom.Node ssubNode = subNode.getFirstChild();
            String from = ssubNode.getTextContent();
            String to = ssubNode.getNextSibling().getTextContent();
            assignments.add(new Assignment("XPath", from, to));
            subNode = subNode.getNextSibling();
        }
        ruleSetNode.addInAssociation(new DataAssociation(source, dataInputs.get(target), assignments, transformation));
    } else {
        // targetRef
        String to = subNode.getTextContent();
        // assignment
        subNode = subNode.getNextSibling();
        if (subNode != null) {
            org.w3c.dom.Node subSubNode = subNode.getFirstChild();
            NodeList nl = subSubNode.getChildNodes();
            if (nl.getLength() > 1) {
                // not supported ?
                ruleSetNode.setParameter(dataInputs.get(to), subSubNode.getTextContent());
                return;
            } else if (nl.getLength() == 0) {
                return;
            }
            Object result = null;
            Object from = nl.item(0);
            if (from instanceof Text) {
                String text = ((Text) from).getTextContent();
                if (text.startsWith("\"") && text.endsWith("\"")) {
                    result = text.substring(1, text.length() - 1);
                } else {
                    result = text;
                }
            } else {
                result = nl.item(0);
            }
            ruleSetNode.setParameter(dataInputs.get(to), result);
        }
    }
}
Also used : Transformation(org.jbpm.workflow.core.node.Transformation) DataAssociation(org.jbpm.workflow.core.node.DataAssociation) NodeList(org.w3c.dom.NodeList) Text(org.w3c.dom.Text) LinkedList(java.util.LinkedList) Assignment(org.jbpm.workflow.core.node.Assignment) DataTransformer(org.kie.api.runtime.process.DataTransformer)

Example 5 with DataTransformer

use of org.kie.api.runtime.process.DataTransformer in project jbpm by kiegroup.

the class BusinessRuleTaskHandler method readDataOutputAssociation.

protected void readDataOutputAssociation(org.w3c.dom.Node xmlNode, RuleSetNode ruleSetNode, Map<String, String> dataOutputs) {
    // sourceRef
    org.w3c.dom.Node subNode = xmlNode.getFirstChild();
    String source = subNode.getTextContent();
    // targetRef
    subNode = subNode.getNextSibling();
    String target = subNode.getTextContent();
    // transformation
    Transformation transformation = null;
    subNode = subNode.getNextSibling();
    if (subNode != null && "transformation".equals(subNode.getNodeName())) {
        String lang = subNode.getAttributes().getNamedItem("language").getNodeValue();
        String expression = subNode.getTextContent();
        DataTransformer transformer = transformerRegistry.find(lang);
        if (transformer == null) {
            throw new IllegalArgumentException("No transformer registered for language " + lang);
        }
        transformation = new Transformation(lang, expression, source);
        subNode = subNode.getNextSibling();
    }
    // assignments
    List<Assignment> assignments = new LinkedList<Assignment>();
    while (subNode != null) {
        org.w3c.dom.Node ssubNode = subNode.getFirstChild();
        String from = ssubNode.getTextContent();
        String to = ssubNode.getNextSibling().getTextContent();
        assignments.add(new Assignment("XPath", from, to));
        subNode = subNode.getNextSibling();
    }
    ruleSetNode.addOutAssociation(new DataAssociation(dataOutputs.get(source), target, assignments, transformation));
}
Also used : Assignment(org.jbpm.workflow.core.node.Assignment) Transformation(org.jbpm.workflow.core.node.Transformation) DataTransformer(org.kie.api.runtime.process.DataTransformer) DataAssociation(org.jbpm.workflow.core.node.DataAssociation) LinkedList(java.util.LinkedList)

Aggregations

DataTransformer (org.kie.api.runtime.process.DataTransformer)25 Transformation (org.jbpm.workflow.core.node.Transformation)22 DataAssociation (org.jbpm.workflow.core.node.DataAssociation)13 HashMap (java.util.HashMap)12 VariableScopeInstance (org.jbpm.process.instance.context.variable.VariableScopeInstance)6 WorkflowProcess (org.jbpm.workflow.core.WorkflowProcess)6 Assignment (org.jbpm.workflow.core.node.Assignment)5 LinkedList (java.util.LinkedList)4 Map (java.util.Map)3 EventTransformerImpl (org.jbpm.process.core.event.EventTransformerImpl)3 NodeInstanceResolverFactory (org.jbpm.workflow.instance.impl.NodeInstanceResolverFactory)3 Test (org.junit.Test)3 NodeList (org.w3c.dom.NodeList)3 Text (org.w3c.dom.Text)3 Iterator (java.util.Iterator)2 Matcher (java.util.regex.Matcher)2 WorkItem (org.drools.core.process.instance.WorkItem)2 ProcessDialect (org.jbpm.process.builder.dialect.ProcessDialect)2 Variable (org.jbpm.process.core.context.variable.Variable)2 DataType (org.jbpm.process.core.datatype.DataType)2