Search in sources :

Example 1 with EventListener

use of org.activiti.bpmn.model.EventListener in project Activiti by Activiti.

the class ActivitiEventListenerParser method parseChildElement.

public void parseChildElement(XMLStreamReader xtr, BaseElement parentElement, BpmnModel model) throws Exception {
    EventListener listener = new EventListener();
    BpmnXMLUtil.addXMLLocation(listener, xtr);
    if (StringUtils.isNotEmpty(xtr.getAttributeValue(null, ATTRIBUTE_LISTENER_CLASS))) {
        listener.setImplementation(xtr.getAttributeValue(null, ATTRIBUTE_LISTENER_CLASS));
        listener.setImplementationType(ImplementationType.IMPLEMENTATION_TYPE_CLASS);
    } else if (StringUtils.isNotEmpty(xtr.getAttributeValue(null, ATTRIBUTE_LISTENER_DELEGATEEXPRESSION))) {
        listener.setImplementation(xtr.getAttributeValue(null, ATTRIBUTE_LISTENER_DELEGATEEXPRESSION));
        listener.setImplementationType(ImplementationType.IMPLEMENTATION_TYPE_DELEGATEEXPRESSION);
    } else if (StringUtils.isNotEmpty(xtr.getAttributeValue(null, ATTRIBUTE_LISTENER_THROW_EVENT_TYPE))) {
        String eventType = xtr.getAttributeValue(null, ATTRIBUTE_LISTENER_THROW_EVENT_TYPE);
        if (ATTRIBUTE_LISTENER_THROW_EVENT_TYPE_SIGNAL.equals(eventType)) {
            listener.setImplementationType(ImplementationType.IMPLEMENTATION_TYPE_THROW_SIGNAL_EVENT);
            listener.setImplementation(xtr.getAttributeValue(null, ATTRIBUTE_LISTENER_THROW_SIGNAL_EVENT_NAME));
        } else if (ATTRIBUTE_LISTENER_THROW_EVENT_TYPE_GLOBAL_SIGNAL.equals(eventType)) {
            listener.setImplementationType(ImplementationType.IMPLEMENTATION_TYPE_THROW_GLOBAL_SIGNAL_EVENT);
            listener.setImplementation(xtr.getAttributeValue(null, ATTRIBUTE_LISTENER_THROW_SIGNAL_EVENT_NAME));
        } else if (ATTRIBUTE_LISTENER_THROW_EVENT_TYPE_MESSAGE.equals(eventType)) {
            listener.setImplementationType(ImplementationType.IMPLEMENTATION_TYPE_THROW_MESSAGE_EVENT);
            listener.setImplementation(xtr.getAttributeValue(null, ATTRIBUTE_LISTENER_THROW_MESSAGE_EVENT_NAME));
        } else if (ATTRIBUTE_LISTENER_THROW_EVENT_TYPE_ERROR.equals(eventType)) {
            listener.setImplementationType(ImplementationType.IMPLEMENTATION_TYPE_THROW_ERROR_EVENT);
            listener.setImplementation(xtr.getAttributeValue(null, ATTRIBUTE_LISTENER_THROW_ERROR_EVENT_CODE));
        } else {
            listener.setImplementationType(ImplementationType.IMPLEMENTATION_TYPE_INVALID_THROW_EVENT);
        }
    }
    listener.setEvents(xtr.getAttributeValue(null, ATTRIBUTE_LISTENER_EVENTS));
    listener.setEntityType(xtr.getAttributeValue(null, ATTRIBUTE_LISTENER_ENTITY_TYPE));
    Process parentProcess = (Process) parentElement;
    parentProcess.getEventListeners().add(listener);
}
Also used : Process(org.activiti.bpmn.model.Process) EventListener(org.activiti.bpmn.model.EventListener)

Example 2 with EventListener

use of org.activiti.bpmn.model.EventListener in project Activiti by Activiti.

the class ActivitiListenerExport method writeEventListeners.

protected static boolean writeEventListeners(List<EventListener> eventListeners, boolean didWriteExtensionStartElement, XMLStreamWriter xtw) throws Exception {
    if (eventListeners != null && !eventListeners.isEmpty()) {
        for (EventListener eventListener : eventListeners) {
            if (!didWriteExtensionStartElement) {
                xtw.writeStartElement(ELEMENT_EXTENSIONS);
                didWriteExtensionStartElement = true;
            }
            xtw.writeStartElement(ACTIVITI_EXTENSIONS_PREFIX, ELEMENT_EVENT_LISTENER, ACTIVITI_EXTENSIONS_NAMESPACE);
            BpmnXMLUtil.writeDefaultAttribute(ATTRIBUTE_LISTENER_EVENTS, eventListener.getEvents(), xtw);
            BpmnXMLUtil.writeDefaultAttribute(ATTRIBUTE_LISTENER_ENTITY_TYPE, eventListener.getEntityType(), xtw);
            if (ImplementationType.IMPLEMENTATION_TYPE_CLASS.equals(eventListener.getImplementationType())) {
                BpmnXMLUtil.writeDefaultAttribute(ATTRIBUTE_LISTENER_CLASS, eventListener.getImplementation(), xtw);
            } else if (ImplementationType.IMPLEMENTATION_TYPE_DELEGATEEXPRESSION.equals(eventListener.getImplementationType())) {
                BpmnXMLUtil.writeDefaultAttribute(ATTRIBUTE_LISTENER_DELEGATEEXPRESSION, eventListener.getImplementation(), xtw);
            } else if (ImplementationType.IMPLEMENTATION_TYPE_THROW_SIGNAL_EVENT.equals(eventListener.getImplementationType())) {
                BpmnXMLUtil.writeDefaultAttribute(ATTRIBUTE_LISTENER_THROW_SIGNAL_EVENT_NAME, eventListener.getImplementation(), xtw);
                BpmnXMLUtil.writeDefaultAttribute(ATTRIBUTE_LISTENER_THROW_EVENT_TYPE, ATTRIBUTE_LISTENER_THROW_EVENT_TYPE_SIGNAL, xtw);
            } else if (ImplementationType.IMPLEMENTATION_TYPE_THROW_GLOBAL_SIGNAL_EVENT.equals(eventListener.getImplementationType())) {
                BpmnXMLUtil.writeDefaultAttribute(ATTRIBUTE_LISTENER_THROW_SIGNAL_EVENT_NAME, eventListener.getImplementation(), xtw);
                BpmnXMLUtil.writeDefaultAttribute(ATTRIBUTE_LISTENER_THROW_EVENT_TYPE, ATTRIBUTE_LISTENER_THROW_EVENT_TYPE_GLOBAL_SIGNAL, xtw);
            } else if (ImplementationType.IMPLEMENTATION_TYPE_THROW_MESSAGE_EVENT.equals(eventListener.getImplementationType())) {
                BpmnXMLUtil.writeDefaultAttribute(ATTRIBUTE_LISTENER_THROW_MESSAGE_EVENT_NAME, eventListener.getImplementation(), xtw);
                BpmnXMLUtil.writeDefaultAttribute(ATTRIBUTE_LISTENER_THROW_EVENT_TYPE, ATTRIBUTE_LISTENER_THROW_EVENT_TYPE_MESSAGE, xtw);
            } else if (ImplementationType.IMPLEMENTATION_TYPE_THROW_ERROR_EVENT.equals(eventListener.getImplementationType())) {
                BpmnXMLUtil.writeDefaultAttribute(ATTRIBUTE_LISTENER_THROW_ERROR_EVENT_CODE, eventListener.getImplementation(), xtw);
                BpmnXMLUtil.writeDefaultAttribute(ATTRIBUTE_LISTENER_THROW_EVENT_TYPE, ATTRIBUTE_LISTENER_THROW_EVENT_TYPE_ERROR, xtw);
            }
            xtw.writeEndElement();
        }
    }
    return didWriteExtensionStartElement;
}
Also used : EventListener(org.activiti.bpmn.model.EventListener)

Example 3 with EventListener

use of org.activiti.bpmn.model.EventListener in project Activiti by Activiti.

the class EventListenerConverterTest method validateModel.

private void validateModel(BpmnModel model) {
    Process process = model.getMainProcess();
    assertNotNull(process);
    assertNotNull(process.getEventListeners());
    assertEquals(8, process.getEventListeners().size());
    // Listener with class
    EventListener listener = process.getEventListeners().get(0);
    assertEquals("ENTITY_CREATE", listener.getEvents());
    assertEquals("org.activiti.test.MyListener", listener.getImplementation());
    assertEquals(ImplementationType.IMPLEMENTATION_TYPE_CLASS, listener.getImplementationType());
    // Listener with class, but no specific event (== all events)
    listener = process.getEventListeners().get(1);
    assertNull(listener.getEvents());
    assertEquals("org.activiti.test.AllEventTypesListener", listener.getImplementation());
    assertEquals(ImplementationType.IMPLEMENTATION_TYPE_CLASS, listener.getImplementationType());
    // Listener with delegate expression
    listener = process.getEventListeners().get(2);
    assertEquals("ENTITY_DELETE", listener.getEvents());
    assertEquals("${myListener}", listener.getImplementation());
    assertEquals(ImplementationType.IMPLEMENTATION_TYPE_DELEGATEEXPRESSION, listener.getImplementationType());
    // Listener that throws a signal-event
    listener = process.getEventListeners().get(3);
    assertEquals("ENTITY_DELETE", listener.getEvents());
    assertEquals("theSignal", listener.getImplementation());
    assertEquals(ImplementationType.IMPLEMENTATION_TYPE_THROW_SIGNAL_EVENT, listener.getImplementationType());
    // Listener that throws a global signal-event
    listener = process.getEventListeners().get(4);
    assertEquals("ENTITY_DELETE", listener.getEvents());
    assertEquals("theSignal", listener.getImplementation());
    assertEquals(ImplementationType.IMPLEMENTATION_TYPE_THROW_GLOBAL_SIGNAL_EVENT, listener.getImplementationType());
    // Listener that throws a message-event
    listener = process.getEventListeners().get(5);
    assertEquals("ENTITY_DELETE", listener.getEvents());
    assertEquals("theMessage", listener.getImplementation());
    assertEquals(ImplementationType.IMPLEMENTATION_TYPE_THROW_MESSAGE_EVENT, listener.getImplementationType());
    // Listener that throws an error-event
    listener = process.getEventListeners().get(6);
    assertEquals("ENTITY_DELETE", listener.getEvents());
    assertEquals("123", listener.getImplementation());
    assertEquals(ImplementationType.IMPLEMENTATION_TYPE_THROW_ERROR_EVENT, listener.getImplementationType());
    // Listener restricted to a specific entity
    listener = process.getEventListeners().get(7);
    assertEquals("ENTITY_DELETE", listener.getEvents());
    assertEquals("123", listener.getImplementation());
    assertEquals(ImplementationType.IMPLEMENTATION_TYPE_THROW_ERROR_EVENT, listener.getImplementationType());
    assertEquals("job", listener.getEntityType());
}
Also used : Process(org.activiti.bpmn.model.Process) EventListener(org.activiti.bpmn.model.EventListener)

Example 4 with EventListener

use of org.activiti.bpmn.model.EventListener in project Activiti by Activiti.

the class BpmnJsonConverterUtil method parseEventListeners.

public static void parseEventListeners(JsonNode listenersNode, Process process) {
    if (listenersNode == null)
        return;
    listenersNode = validateIfNodeIsTextual(listenersNode);
    for (JsonNode listenerNode : listenersNode) {
        JsonNode eventsNode = listenerNode.get(PROPERTY_EVENTLISTENER_EVENTS);
        if (eventsNode != null && eventsNode.isArray() && eventsNode.size() > 0) {
            EventListener listener = new EventListener();
            StringBuilder eventsBuilder = new StringBuilder();
            for (JsonNode eventNode : eventsNode) {
                JsonNode eventValueNode = eventNode.get(PROPERTY_EVENTLISTENER_EVENT);
                if (eventValueNode != null && eventValueNode.isNull() == false && StringUtils.isNotEmpty(eventValueNode.asText())) {
                    if (eventsBuilder.length() > 0) {
                        eventsBuilder.append(",");
                    }
                    eventsBuilder.append(eventValueNode.asText());
                }
            }
            if (eventsBuilder.length() == 0)
                continue;
            listener.setEvents(eventsBuilder.toString());
            JsonNode rethrowEventNode = listenerNode.get("rethrowEvent");
            if (rethrowEventNode != null && rethrowEventNode.asBoolean()) {
                JsonNode rethrowTypeNode = listenerNode.get("rethrowType");
                if (rethrowTypeNode != null) {
                    if ("error".equalsIgnoreCase(rethrowTypeNode.asText())) {
                        String errorCode = getValueAsString("errorcode", listenerNode);
                        if (StringUtils.isNotEmpty(errorCode)) {
                            listener.setImplementationType(ImplementationType.IMPLEMENTATION_TYPE_THROW_ERROR_EVENT);
                            listener.setImplementation(errorCode);
                        }
                    } else if ("message".equalsIgnoreCase(rethrowTypeNode.asText())) {
                        String messageName = getValueAsString("messagename", listenerNode);
                        if (StringUtils.isNotEmpty(messageName)) {
                            listener.setImplementationType(ImplementationType.IMPLEMENTATION_TYPE_THROW_MESSAGE_EVENT);
                            listener.setImplementation(messageName);
                        }
                    } else if ("signal".equalsIgnoreCase(rethrowTypeNode.asText())) {
                        String signalName = getValueAsString("signalname", listenerNode);
                        if (StringUtils.isNotEmpty(signalName)) {
                            listener.setImplementationType(ImplementationType.IMPLEMENTATION_TYPE_THROW_SIGNAL_EVENT);
                            listener.setImplementation(signalName);
                        }
                    } else if ("globalSignal".equalsIgnoreCase(rethrowTypeNode.asText())) {
                        String signalName = getValueAsString("signalname", listenerNode);
                        if (StringUtils.isNotEmpty(signalName)) {
                            listener.setImplementationType(ImplementationType.IMPLEMENTATION_TYPE_THROW_GLOBAL_SIGNAL_EVENT);
                            listener.setImplementation(signalName);
                        }
                    }
                }
                if (StringUtils.isEmpty(listener.getImplementation())) {
                    continue;
                }
            } else {
                if (StringUtils.isNotEmpty(getValueAsString(PROPERTY_EVENTLISTENER_CLASS_NAME, listenerNode))) {
                    listener.setImplementationType(ImplementationType.IMPLEMENTATION_TYPE_CLASS);
                    listener.setImplementation(getValueAsString(PROPERTY_EVENTLISTENER_CLASS_NAME, listenerNode));
                } else if (StringUtils.isNotEmpty(getValueAsString(PROPERTY_EVENTLISTENER_DELEGATE_EXPRESSION, listenerNode))) {
                    listener.setImplementationType(ImplementationType.IMPLEMENTATION_TYPE_DELEGATEEXPRESSION);
                    listener.setImplementation(getValueAsString(PROPERTY_EVENTLISTENER_DELEGATE_EXPRESSION, listenerNode));
                }
                if (StringUtils.isNotEmpty(getValueAsString(PROPERTY_EVENTLISTENER_ENTITY_TYPE, listenerNode))) {
                    listener.setEntityType(getValueAsString(PROPERTY_EVENTLISTENER_ENTITY_TYPE, listenerNode));
                }
                if (StringUtils.isEmpty(listener.getImplementation())) {
                    continue;
                }
            }
            process.getEventListeners().add(listener);
        }
    }
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) EventListener(org.activiti.bpmn.model.EventListener)

Example 5 with EventListener

use of org.activiti.bpmn.model.EventListener in project Activiti by Activiti.

the class BpmnJsonConverterUtil method convertEventListenersToJson.

public static void convertEventListenersToJson(List<EventListener> listeners, ObjectNode propertiesNode) {
    ObjectNode listenersNode = objectMapper.createObjectNode();
    ArrayNode itemsNode = objectMapper.createArrayNode();
    for (EventListener listener : listeners) {
        ObjectNode propertyItemNode = objectMapper.createObjectNode();
        if (StringUtils.isNotEmpty(listener.getEvents())) {
            ArrayNode eventArrayNode = objectMapper.createArrayNode();
            String[] eventArray = listener.getEvents().split(",");
            for (String eventValue : eventArray) {
                if (StringUtils.isNotEmpty(eventValue.trim())) {
                    ObjectNode eventNode = objectMapper.createObjectNode();
                    eventNode.put(PROPERTY_EVENTLISTENER_EVENT, eventValue.trim());
                    eventArrayNode.add(eventNode);
                }
            }
            propertyItemNode.put(PROPERTY_EVENTLISTENER_EVENT, listener.getEvents());
            propertyItemNode.put(PROPERTY_EVENTLISTENER_EVENTS, eventArrayNode);
        }
        String implementationText = null;
        if (ImplementationType.IMPLEMENTATION_TYPE_CLASS.equals(listener.getImplementationType())) {
            propertyItemNode.put(PROPERTY_EVENTLISTENER_CLASS_NAME, listener.getImplementation());
            implementationText = listener.getImplementation();
        } else if (ImplementationType.IMPLEMENTATION_TYPE_DELEGATEEXPRESSION.equals(listener.getImplementationType())) {
            propertyItemNode.put(PROPERTY_EVENTLISTENER_DELEGATE_EXPRESSION, listener.getImplementation());
            implementationText = listener.getImplementation();
        } else if (ImplementationType.IMPLEMENTATION_TYPE_THROW_ERROR_EVENT.equals(listener.getImplementationType())) {
            propertyItemNode.put(PROPERTY_EVENTLISTENER_RETHROW_EVENT, true);
            propertyItemNode.put(PROPERTY_EVENTLISTENER_RETHROW_TYPE, "error");
            propertyItemNode.put(PROPERTY_EVENTLISTENER_ERROR_CODE, listener.getImplementation());
            implementationText = "Rethrow as error " + listener.getImplementation();
        } else if (ImplementationType.IMPLEMENTATION_TYPE_THROW_MESSAGE_EVENT.equals(listener.getImplementationType())) {
            propertyItemNode.put(PROPERTY_EVENTLISTENER_RETHROW_EVENT, true);
            propertyItemNode.put(PROPERTY_EVENTLISTENER_RETHROW_TYPE, "message");
            propertyItemNode.put(PROPERTY_EVENTLISTENER_MESSAGE_NAME, listener.getImplementation());
            implementationText = "Rethrow as message " + listener.getImplementation();
        } else if (ImplementationType.IMPLEMENTATION_TYPE_THROW_SIGNAL_EVENT.equals(listener.getImplementationType())) {
            propertyItemNode.put(PROPERTY_EVENTLISTENER_RETHROW_EVENT, true);
            propertyItemNode.put(PROPERTY_EVENTLISTENER_RETHROW_TYPE, "signal");
            propertyItemNode.put(PROPERTY_EVENTLISTENER_SIGNAL_NAME, listener.getImplementation());
            implementationText = "Rethrow as signal " + listener.getImplementation();
        } else if (ImplementationType.IMPLEMENTATION_TYPE_THROW_GLOBAL_SIGNAL_EVENT.equals(listener.getImplementationType())) {
            propertyItemNode.put(PROPERTY_EVENTLISTENER_RETHROW_EVENT, true);
            propertyItemNode.put(PROPERTY_EVENTLISTENER_RETHROW_TYPE, "globalSignal");
            propertyItemNode.put(PROPERTY_EVENTLISTENER_SIGNAL_NAME, listener.getImplementation());
            implementationText = "Rethrow as signal " + listener.getImplementation();
        }
        if (StringUtils.isNotEmpty(implementationText)) {
            propertyItemNode.put(PROPERTY_EVENTLISTENER_IMPLEMENTATION, implementationText);
        }
        if (StringUtils.isNotEmpty(listener.getEntityType())) {
            propertyItemNode.put(PROPERTY_EVENTLISTENER_ENTITY_TYPE, listener.getEntityType());
        }
        itemsNode.add(propertyItemNode);
    }
    listenersNode.put(PROPERTY_EVENTLISTENER_VALUE, itemsNode);
    propertiesNode.put(PROPERTY_EVENT_LISTENERS, listenersNode);
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) EventListener(org.activiti.bpmn.model.EventListener)

Aggregations

EventListener (org.activiti.bpmn.model.EventListener)5 Process (org.activiti.bpmn.model.Process)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1