io.automatiko.addons.graphql.GraphQLProcessSubscriptionEventPublisher Maven / Gradle / Ivy
package io.automatiko.addons.graphql;
import java.util.function.Function;
import jakarta.enterprise.context.Dependent;
import jakarta.inject.Inject;
import io.automatiko.addons.graphql.internal.SecurityAwareBroadcastProcessor;
import io.automatiko.engine.api.event.DataEvent;
import io.automatiko.engine.api.workflow.ProcessInstance;
import io.automatiko.engine.services.event.ProcessInstanceDataEvent;
@Dependent
public class GraphQLProcessSubscriptionEventPublisher implements GraphQLSubscriptionEventPublisher {
SecurityAwareBroadcastProcessor createdProcessor = SecurityAwareBroadcastProcessor.create();
SecurityAwareBroadcastProcessor completedProcessor = SecurityAwareBroadcastProcessor.create();
SecurityAwareBroadcastProcessor abortedProcessor = SecurityAwareBroadcastProcessor.create();
SecurityAwareBroadcastProcessor inErrorProcessor = SecurityAwareBroadcastProcessor.create();
SecurityAwareBroadcastProcessor changedProcessor = SecurityAwareBroadcastProcessor.create();
GraphQLEventPublisher publisher;
private String processId;
private Function, T> mapper;
@Inject
public GraphQLProcessSubscriptionEventPublisher(GraphQLEventPublisher publisher) {
this.publisher = publisher;
}
@Override
public void process(DataEvent> event) {
if (processId == null || mapper == null) {
return;
}
if (event instanceof ProcessInstanceDataEvent) {
ProcessInstanceDataEvent piEvent = (ProcessInstanceDataEvent) event;
ProcessInstance> instance = piEvent.getData().sourceInstance();
if (instance.process().id().equals(processId)) {
if (instance.status() == ProcessInstance.STATE_COMPLETED) {
completedProcessor.onNext(mapper.apply(instance), piEvent.getData().getVisibleTo());
} else if (instance.status() == ProcessInstance.STATE_ABORTED) {
abortedProcessor.onNext(mapper.apply(instance), piEvent.getData().getVisibleTo());
} else if (instance.status() == ProcessInstance.STATE_ERROR) {
inErrorProcessor.onNext(mapper.apply(instance), piEvent.getData().getVisibleTo());
} else {
changedProcessor.onNext(mapper.apply(instance), piEvent.getData().getVisibleTo());
}
}
}
}
public void configure(String processId, Function, T> mapper) {
this.processId = processId;
this.mapper = mapper;
this.publisher.register(this);
}
public SecurityAwareBroadcastProcessor created(T clazz) {
return createdProcessor;
}
public SecurityAwareBroadcastProcessor completed(T clazz) {
return completedProcessor;
}
public SecurityAwareBroadcastProcessor aborted(T clazz) {
return abortedProcessor;
}
public SecurityAwareBroadcastProcessor inError(T clazz) {
return inErrorProcessor;
}
public SecurityAwareBroadcastProcessor changed(T clazz) {
return changedProcessor;
}
}