Search in sources :

Example 1 with NodeInstanceEventBody

use of io.automatiko.engine.services.event.impl.NodeInstanceEventBody in project automatiko-engine by automatiko-io.

the class ElasticEventPublisher method publish.

@Override
public void publish(DataEvent<?> event) {
    try {
        Request request;
        Map<String, Object> payload;
        if (event instanceof ProcessInstanceDataEvent) {
            ProcessInstanceDataEvent pevent = (ProcessInstanceDataEvent) event;
            if (config.instance().orElse(true)) {
                Map<String, Object> metadata = new LinkedHashMap<>();
                metadata.put("instanceId", pevent.getData().getId());
                metadata.put("processId", pevent.getData().getProcessId());
                metadata.put("rootInstanceId", pevent.getData().getRootInstanceId());
                metadata.put("rootProcessId", pevent.getData().getRootProcessId());
                metadata.put("parentInstanceId", pevent.getData().getParentInstanceId());
                metadata.put("businessKey", pevent.getData().getBusinessKey());
                metadata.put("state", pevent.getData().getState());
                metadata.put("tags", pevent.getData().getTags());
                if (pevent.getData().getRoles() != null) {
                    metadata.put("roles", pevent.getData().getRoles());
                }
                if (pevent.getData().getVisibleTo() != null) {
                    metadata.put("visibleTo", pevent.getData().getVisibleTo());
                }
                metadata.put("startDate", pevent.getData().getStartDate());
                metadata.put("endDate", pevent.getData().getEndDate());
                payload = new LinkedHashMap<>(pevent.getData().getVariables());
                payload.put("_metadata", metadata);
                request = new Request("PUT", "/" + pevent.getData().sourceInstance().process().id() + "/_doc/" + pevent.getData().getId());
                request.setJsonEntity(mapper.writeValueAsString(payload));
                sendRequest(request, event);
            }
            if (config.audit().orElse(false)) {
                String index = config.auditIndex().orElse("atk_audit");
                StringBuilder bulkRequestBody = new StringBuilder();
                for (NodeInstanceEventBody nevent : pevent.getData().getNodeInstances()) {
                    String actionMetaData = String.format("{ \"index\" : { \"_index\" : \"%s\", \"_id\" : \"%s\" } }%n", index, nevent.getId());
                    Map<String, Object> audit = new LinkedHashMap<>();
                    audit.put("instanceId", pevent.getData().getId());
                    audit.put("processId", pevent.getData().getProcessId());
                    audit.put("rootInstanceId", pevent.getData().getRootInstanceId());
                    audit.put("rootProcessId", pevent.getData().getRootProcessId());
                    audit.put("parentInstanceId", pevent.getData().getParentInstanceId());
                    audit.put("businessKey", pevent.getData().getBusinessKey());
                    audit.put("nodeDefinitionId", nevent.getNodeDefinitionId());
                    audit.put("nodeId", nevent.getNodeId());
                    audit.put("nodeName", nevent.getNodeName());
                    audit.put("nodeType", nevent.getNodeType());
                    audit.put("triggerTime", nevent.getTriggerTime());
                    audit.put("leaveTime", nevent.getLeaveTime());
                    bulkRequestBody.append(actionMetaData);
                    bulkRequestBody.append(mapper.writeValueAsString(audit));
                    bulkRequestBody.append("\n");
                }
                request = new Request("POST", "/" + index + "/_bulk");
                request.setJsonEntity(bulkRequestBody.toString());
                sendRequest(request, event);
            }
        } else if (event instanceof UserTaskInstanceDataEvent && config.tasks().orElse(true)) {
            UserTaskInstanceDataEvent uevent = (UserTaskInstanceDataEvent) event;
            Set<String> potentialOwners = new LinkedHashSet<String>();
            if (uevent.getData().getPotentialUsers() != null) {
                potentialOwners.addAll(uevent.getData().getPotentialUsers());
            }
            if (uevent.getData().getPotentialGroups() != null) {
                potentialOwners.addAll(uevent.getData().getPotentialGroups());
            }
            if (uevent.getData().getAdminUsers() != null) {
                potentialOwners.addAll(uevent.getData().getAdminUsers());
            }
            if (uevent.getData().getAdminGroups() != null) {
                potentialOwners.addAll(uevent.getData().getAdminUsers());
            }
            // remove any excluded users known
            if (uevent.getData().getExcludedUsers() != null) {
                potentialOwners.removeAll(uevent.getData().getExcludedUsers());
            }
            Map<String, Object> metadata = new LinkedHashMap<>();
            metadata.put("processInstanceId", uevent.getData().getProcessInstanceId());
            metadata.put("processId", uevent.getData().getProcessId());
            metadata.put("rootInstanceId", uevent.getData().getRootProcessInstanceId());
            metadata.put("rootProcessId", uevent.getData().getRootProcessId());
            metadata.put("referenceName", uevent.getData().getReferenceName());
            payload = new LinkedHashMap<>();
            payload.put("instanceId", uevent.getData().getId());
            payload.put("name", uevent.getData().getTaskName());
            payload.put("description", uevent.getData().getTaskDescription());
            payload.put("state", uevent.getData().getState());
            payload.put("owner", uevent.getData().getActualOwner());
            payload.put("potentialOwners", potentialOwners);
            payload.put("excludedUsers", uevent.getData().getExcludedUsers());
            payload.put("startDate", uevent.getData().getStartDate());
            payload.put("endDate", uevent.getData().getCompleteDate());
            payload.put("inputs", uevent.getData().getInputs());
            payload.put("outputs", uevent.getData().getOutputs());
            payload.put("_metadata", metadata);
            request = new Request("PUT", "/tasks/_doc/" + uevent.getData().getId());
            request.setJsonEntity(mapper.writeValueAsString(payload));
            sendRequest(request, event);
        } else {
            return;
        }
    } catch (IOException e) {
        LOGGER.error("Error when publishing event to elastic", e);
    }
}
Also used : Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) Request(org.elasticsearch.client.Request) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap) NodeInstanceEventBody(io.automatiko.engine.services.event.impl.NodeInstanceEventBody) UserTaskInstanceDataEvent(io.automatiko.engine.services.event.UserTaskInstanceDataEvent) ProcessInstanceDataEvent(io.automatiko.engine.services.event.ProcessInstanceDataEvent) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

ProcessInstanceDataEvent (io.automatiko.engine.services.event.ProcessInstanceDataEvent)1 UserTaskInstanceDataEvent (io.automatiko.engine.services.event.UserTaskInstanceDataEvent)1 NodeInstanceEventBody (io.automatiko.engine.services.event.impl.NodeInstanceEventBody)1 IOException (java.io.IOException)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 Map (java.util.Map)1 Set (java.util.Set)1 Request (org.elasticsearch.client.Request)1