Skip to content

Commit

Permalink
KOGITO-9737 - Automatically register process definitions using Data I…
Browse files Browse the repository at this point in the history
…ndex addon
  • Loading branch information
cristianonicolai committed Sep 4, 2023
1 parent 0ef7fb7 commit b3f58ea
Show file tree
Hide file tree
Showing 51 changed files with 605 additions and 221 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,12 @@ public ProcessInstance apply(ProcessInstanceDataEvent event) {
pi.setAddons(isNullOrEmpty(event.getKogitoAddons()) ? null : Set.of(event.getKogitoAddons().split(",")));
pi.setEndpoint(event.getSource() == null ? null : event.getSource().toString());
pi.setLastUpdate(toZonedDateTime(event.getTime()));
pi.setDefinition(definitions().apply(event));
pi.setVersion(event.getData().getVersion());
pi.setDefinition(definition().apply(event));
return pi;
}

private Function<ProcessInstanceDataEvent, ProcessDefinition> definitions() {
private Function<ProcessInstanceDataEvent, ProcessDefinition> definition() {
return event -> {
ProcessDefinition pd = new ProcessDefinition();
pd.setId(event.getData().getProcessId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ public void indexProcessInstance(ProcessInstance pi) {
pi.getNodes().addAll(nodes);
}
ProcessDefinition definition = pi.getDefinition();
manager.getProcessDefinitionsCache().put(definition.getKey(), definition);
if (!manager.getProcessDefinitionsCache().containsKey(definition.getKey())) {
manager.getProcessDefinitionsCache().put(definition.getKey(), definition);
}
manager.getProcessInstancesCache().put(pi.getId(), pi);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ public String getProcessInstanceServiceUrl(DataFetchingEnvironment env) {
return getServiceUrl(source.getEndpoint(), source.getProcessId());
}

public ProcessDefinition getProcessDefinition(DataFetchingEnvironment env) {
ProcessInstance source = env.getSource();
return cacheService.getProcessDefinitionsCache().get(ProcessDefinition.toKey(source.getProcessId(), source.getVersion()));
}

protected String getServiceUrl(String endpoint, String processId) {
LOGGER.debug("Process endpoint {}", endpoint);
if (endpoint.startsWith("/")) {
Expand Down Expand Up @@ -210,7 +215,12 @@ public CompletableFuture<String> getProcessInstanceDiagram(DataFetchingEnvironme

public CompletableFuture<String> getProcessInstanceSourceFileContent(DataFetchingEnvironment env) {
ProcessInstance pi = env.getSource();
return dataIndexApiExecutor.getProcessDefinitionSourceFileContent(getServiceUrl(pi.getEndpoint(), pi.getProcessId()), pi.getProcessId());
ProcessDefinition pd = cacheService.getProcessDefinitionsCache().get(ProcessDefinition.toKey(pi.getProcessId(), pi.getVersion()));
if (pd == null) {
return dataIndexApiExecutor.getProcessDefinitionSourceFileContent(getServiceUrl(pi.getEndpoint(), pi.getProcessId()), pi.getProcessId());
} else {
return getProcessDefinitionSource(pd);
}
}

public CompletableFuture<List<Node>> getProcessInstanceNodes(DataFetchingEnvironment env) {
Expand All @@ -219,8 +229,17 @@ public CompletableFuture<List<Node>> getProcessInstanceNodes(DataFetchingEnviron
}

public CompletableFuture<String> getProcessDefinitionSourceFileContent(DataFetchingEnvironment env) {
ProcessDefinition pd = env.getSource();
return dataIndexApiExecutor.getProcessDefinitionSourceFileContent(getServiceUrl(pd.getEndpoint(), pd.getId()), pd.getId());
return getProcessDefinitionSource(env.getSource());
}

private CompletableFuture<String> getProcessDefinitionSource(ProcessDefinition pd) {
if (pd == null) {
return CompletableFuture.completedFuture(null);
} else if (pd.getSource() == null) {
return dataIndexApiExecutor.getProcessDefinitionSourceFileContent(getServiceUrl(pd.getEndpoint(), pd.getId()), pd.getId());
} else {
return CompletableFuture.completedFuture(new String(pd.getSource()));
}
}

public CompletableFuture<List<Node>> getProcessDefinitionNodes(DataFetchingEnvironment env) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ input ProcessDefinitionArgument {
type ProcessInstance {
id: String!
processId: String!
version: String
processName: String
parentProcessInstanceId: String
rootProcessInstanceId: String
Expand All @@ -67,6 +68,7 @@ type ProcessInstance {
nodeDefinitions: [Node!]
diagram: String
source: String
definition: ProcessDefinition
}

type ProcessInstanceError {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ public GraphQLSchema createSchema() {
builder.dataFetcher("diagram", this::getProcessInstanceDiagram);
builder.dataFetcher("source", this::getProcessInstanceSourceFileContent);
builder.dataFetcher("nodeDefinitions", this::getProcessInstanceNodes);
builder.dataFetcher("definition", this::getProcessDefinition);
return builder;
})
.type("UserTaskInstance", builder -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ input KogitoMetadataArgument {
type ProcessInstanceMeta {
id: String!
processId: String!
version: String
processName: String
parentProcessInstanceId: String
rootProcessInstanceId: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ protected void validateProcessInstance(String query, ProcessInstanceDataEvent ev
.body("data.ProcessInstances[0].id", is(event.getData().getId()))
.body("data.ProcessInstances[0].processId", is(event.getData().getProcessId()))
.body("data.ProcessInstances[0].processName", is(event.getData().getProcessName()))
.body("data.ProcessInstances[0].version", is(event.getData().getVersion()))
.body("data.ProcessInstances[0].rootProcessId", is(event.getData().getRootProcessId()))
.body("data.ProcessInstances[0].rootProcessInstanceId", is(event.getData().getRootInstanceId()))
.body("data.ProcessInstances[0].parentProcessInstanceId", is(event.getData().getParentInstanceId()))
Expand Down Expand Up @@ -183,7 +184,10 @@ protected void validateProcessInstance(String query, ProcessInstanceDataEvent ev
.body("data.ProcessInstances[0].milestones.size()", is(event.getData().getMilestones().size()))
.body("data.ProcessInstances[0].milestones[0].id", is(event.getData().getMilestones().stream().findFirst().get().getId()))
.body("data.ProcessInstances[0].milestones[0].name", is(event.getData().getMilestones().stream().findFirst().get().getName()))
.body("data.ProcessInstances[0].milestones[0].status", is(event.getData().getMilestones().stream().findFirst().get().getStatus())));
.body("data.ProcessInstances[0].milestones[0].status", is(event.getData().getMilestones().stream().findFirst().get().getStatus()))
.body("data.ProcessInstances[0].definition.id", is(event.getData().getProcessId()))
.body("data.ProcessInstances[0].definition.version", is(event.getData().getVersion()))
.body("data.ProcessInstances[0].definition.name", is(event.getData().getProcessName())));
}

protected void validateProcessInstance(String query, ProcessInstanceDataEvent event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class GraphQLUtils {
static {
QUERY_FIELDS.put(ProcessDefinition.class, getAllFieldsList(ProcessDefinition.class).map(getFieldName()).collect(joining(", ")));
QUERY_FIELDS.put(UserTaskInstance.class, getAllFieldsList(UserTaskInstance.class).map(getFieldName()).collect(joining(", ")));
QUERY_FIELDS.put(ProcessInstance.class, getAllFieldsList(ProcessInstance.class).filter(field -> !field.getName().equals("definition")).map(getFieldName()).collect(joining(", ")));
QUERY_FIELDS.put(ProcessInstance.class, getAllFieldsList(ProcessInstance.class).map(getFieldName()).collect(joining(", ")));
QUERY_FIELDS.put(Job.class, getAllFieldsList(Job.class).map(getFieldName()).collect(joining(", ")));
QUERY_FIELDS.computeIfPresent(ProcessInstance.class, (k, v) -> v + ", serviceUrl");
QUERY_FIELDS.computeIfPresent(ProcessInstance.class, (k, v) -> v + ", childProcessInstances { id, processName }");
Expand Down Expand Up @@ -230,7 +230,7 @@ private static String getQuery(String name, Class clazz, String... args) {
}

private static Stream<Field> getAllFieldsList(Class clazz) {
return FieldUtils.getAllFieldsList(clazz).stream().filter(f -> getJacocoPredicate().test(f));
return FieldUtils.getAllFieldsList(clazz).stream().filter(getSoourcePredicate().or(getSoourcePredicate()));
}

private static Function<Field, String> getFieldName() {
Expand Down Expand Up @@ -263,4 +263,8 @@ private static Function<Field, String> getFieldName() {
private static Predicate<Field> getJacocoPredicate() {
return field -> !field.getName().equals("$jacocoData");
}

private static Predicate<Field> getSoourcePredicate() {
return field -> !(field.getDeclaringClass().equals(ProcessDefinition.class) && field.getName().equals("source"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public class ProcessDefinition {

private String endpoint;

private String source;

public String getId() {
return id;
}
Expand Down Expand Up @@ -85,8 +87,20 @@ public void setEndpoint(String endpoint) {
this.endpoint = endpoint;
}

public String getSource() {
return source;
}

public void setSource(String source) {
this.source = source;
}

public String getKey() {
return String.format("%s-%s", id, version);
return toKey(id, version);
}

public static String toKey(String processId, String version){
return String.format("%s-%s", processId, version);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public class ProcessInstanceMeta {
private String id;
private String processId;
private String processName;

private String version;
private Integer state;
private String businessKey;
private String endpoint;
Expand Down Expand Up @@ -153,6 +155,14 @@ public void setLastUpdate(ZonedDateTime lastUpdate) {
this.lastUpdate = lastUpdate;
}

public String getVersion() {
return version;
}

public void setVersion(String version) {
this.version = version;
}

@Override
public String toString() {
return "ProcessInstanceMeta{" +
Expand All @@ -169,6 +179,7 @@ public String toString() {
", rootProcessId='" + rootProcessId + '\'' +
", parentProcessInstanceId='" + parentProcessInstanceId + '\'' +
", lastUpdated=" + lastUpdate +
", version=" + version +
'}';
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public class ProcessDefinitionMarshaller extends AbstractMarshaller implements M
protected static final String TYPE = "type";
protected static final String ENDPOINT = "endpoint";

protected static final String SOURCE = "source";

public ProcessDefinitionMarshaller(ObjectMapper mapper) {
super(mapper);
}
Expand All @@ -48,6 +50,8 @@ public ProcessDefinition readFrom(ProtoStreamReader reader) throws IOException {
pd.setAddons(reader.readCollection(ADDONS, new HashSet<>(), String.class));
pd.setType(reader.readString(TYPE));
pd.setEndpoint(reader.readString(ENDPOINT));
byte[] bytes = reader.readBytes(SOURCE);
pd.setSource(bytes == null ? null : new String(bytes));
return pd;
}

Expand All @@ -60,6 +64,7 @@ public void writeTo(ProtoStreamWriter writer, ProcessDefinition pd) throws IOExc
writer.writeCollection(ADDONS, pd.getAddons(), String.class);
writer.writeString(TYPE, pd.getType());
writer.writeString(ENDPOINT, pd.getEndpoint());
writer.writeBytes(SOURCE, pd.getSource() == null ? null : pd.getSource().getBytes());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class ProcessInstanceMarshaller extends AbstractMarshaller implements Mes
protected static final String ROOT_PROCESS_ID = "rootProcessId";
protected static final String PARENT_PROCESS_INSTANCE_ID = "parentProcessInstanceId";
protected static final String PROCESS_NAME = "processName";
protected static final String VERSION = "version";
protected static final String ERROR = "error";
protected static final String ADDONS = "addons";
protected static final String LAST_UPDATE = "lastUpdate";
Expand Down Expand Up @@ -75,6 +76,7 @@ public ProcessInstance readFrom(ProtoStreamReader reader) throws IOException {
pi.setLastUpdate(dateToZonedDateTime(reader.readDate(LAST_UPDATE)));
pi.setBusinessKey(reader.readString(BUSINESS_KEY));
pi.setMilestones(reader.readCollection(MILESTONES, new ArrayList<>(), Milestone.class));
pi.setVersion(reader.readString(VERSION));
return pi;
}

Expand All @@ -98,6 +100,7 @@ public void writeTo(ProtoStreamWriter writer, ProcessInstance pi) throws IOExcep
writer.writeDate(LAST_UPDATE, zonedDateTimeToDate(pi.getLastUpdate()));
writer.writeString(BUSINESS_KEY, pi.getBusinessKey());
writer.writeCollection(MILESTONES, pi.getMilestones(), Milestone.class);
writer.writeString(VERSION, pi.getVersion());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public class ProcessDefinitionEntity {

private String endpoint;

private byte[] source;

public String getId() {
return id;
}
Expand Down Expand Up @@ -102,6 +104,14 @@ public void setEndpoint(String endpoint) {
this.endpoint = endpoint;
}

public byte[] getSource() {
return source;
}

public void setSource(byte[] source) {
this.source = source;
}

@Override
public boolean equals(Object o) {
if (this == o)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public ProcessDefinitionEntity mapToEntity(String key, ProcessDefinition pd) {
entity.setAddons(pd.getAddons());
entity.setType(pd.getType());
entity.setEndpoint(pd.getEndpoint());
entity.setSource(pd.getSource() == null ? null : pd.getSource().getBytes());
return entity;
}

Expand All @@ -58,6 +59,7 @@ public ProcessDefinition mapToModel(ProcessDefinitionEntity entity) {
pd.setAddons(entity.getAddons());
pd.setType(entity.getType());
pd.setEndpoint(entity.getEndpoint());
pd.setSource(entity.getSource() == null ? null : new String(entity.getSource()));
return pd;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public class ProcessInstanceEntity {

String processName;

String version;

ProcessInstanceErrorEntity error;

Set<String> addons;
Expand Down Expand Up @@ -206,6 +208,14 @@ public void setMilestones(List<MilestoneEntity> milestones) {
this.milestones = milestones;
}

public String getVersion() {
return version;
}

public void setVersion(String version) {
this.version = version;
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public ProcessInstanceEntity mapToEntity(String key, ProcessInstance instance) {
entity.setLastUpdate(zonedDateTimeToInstant(instance.getLastUpdate()));
entity.setBusinessKey(instance.getBusinessKey());
entity.setMilestones(Optional.ofNullable(instance.getMilestones()).map(milestones -> milestones.stream().map(this::fromMilestone).collect(toList())).orElse(null));
entity.setVersion(instance.getVersion());
return entity;
}

Expand Down Expand Up @@ -99,7 +100,8 @@ public ProcessInstance mapToModel(ProcessInstanceEntity entity) {
instance.setAddons(entity.getAddons());
instance.setLastUpdate(instantToZonedDateTime(entity.getLastUpdate()));
instance.setBusinessKey(entity.getBusinessKey());
instance.setMilestones(Optional.ofNullable(entity.getMilestones()).map(milesteons -> milesteons.stream().map(this::toMilestone).collect(toList())).orElse(null));
instance.setMilestones(Optional.ofNullable(entity.getMilestones()).map(milestones -> milestones.stream().map(this::toMilestone).collect(toList())).orElse(null));
instance.setVersion(entity.getVersion());
return instance;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,11 @@ public interface ProcessDefinitionEntityMapper {
@InheritInverseConfiguration
ProcessDefinition mapToModel(ProcessDefinitionEntity pd);

default byte[] map(String value) {
return value == null ? null : value.getBytes();
};

default String map(byte[] value) {
return value == null ? null : new String(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.Table;

import org.hibernate.annotations.OnDelete;
Expand All @@ -47,6 +48,8 @@ public class ProcessDefinitionEntity extends AbstractEntity {

private String endpoint;

private byte[] source;

@ElementCollection
@JoinColumn(name = "id")
@CollectionTable(name = "definitions_roles", joinColumns = { @JoinColumn(name = "process_id"),
Expand Down Expand Up @@ -120,6 +123,14 @@ public void setEndpoint(String endpoint) {
this.endpoint = endpoint;
}

public byte[] getSource() {
return source;
}

public void setSource(byte[] source) {
this.source = source;
}

@Override
public boolean equals(Object o) {
if (this == o)
Expand Down
Loading

0 comments on commit b3f58ea

Please sign in to comment.