Skip to content

Commit

Permalink
[Fix #2170] Changing metadata schema
Browse files Browse the repository at this point in the history
  • Loading branch information
fjtirado committed Feb 7, 2025
1 parent e60dca5 commit 33d634c
Show file tree
Hide file tree
Showing 11 changed files with 148 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@
import org.kie.kogito.event.process.NodeDefinition;
import org.kie.kogito.event.process.ProcessDefinitionDataEvent;
import org.kie.kogito.event.process.ProcessDefinitionEventBody;
import org.kie.kogito.index.CommonUtils;
import org.kie.kogito.index.json.JsonUtils;
import org.kie.kogito.index.model.Node;
import org.kie.kogito.index.model.ProcessDefinition;
import org.kie.kogito.jackson.utils.JsonObjectUtils;
import org.kie.kogito.jackson.utils.MergeUtils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.node.ObjectNode;

import jakarta.enterprise.context.ApplicationScoped;

Expand Down Expand Up @@ -63,7 +65,7 @@ public static ProcessDefinition merge(ProcessDefinition instance, ProcessDefinit
instance.setEndpoint(doMerge(data.getEndpoint(), instance.getEndpoint()));
instance.setDescription(doMerge(data.getDescription(), instance.getDescription()));
instance.setAnnotations(doMerge(data.getAnnotations(), instance.getAnnotations()));
instance.setMetadata(CommonUtils.mergeMap(toStringMap(data.getMetadata()), instance.getMetadata()));
instance.setMetadata((ObjectNode) MergeUtils.merge(JsonObjectUtils.fromValue(data.getMetadata()), instance.getMetadata()));
instance.setNodes(doMerge(nodeDefinitions(data), instance.getNodes()));
instance.setSource(doMerge(data.getSource(), instance.getSource()));
return instance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ input ProcessDefinitionArgument {
id: StringArgument
name: StringArgument
version: StringArgument
metadata: JSON
}

type ProcessInstance {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@
package org.kie.kogito.index.model;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import com.fasterxml.jackson.databind.node.ObjectNode;

public class ProcessDefinition {

private String id;
Expand All @@ -35,7 +36,7 @@ public class ProcessDefinition {
private String source;
private String description;
private Set<String> annotations;
private Map<String, String> metadata;
private ObjectNode metadata;
private List<Node> nodes;

public String getId() {
Expand Down Expand Up @@ -126,11 +127,11 @@ public void setAnnotations(Set<String> annotations) {
this.annotations = annotations;
}

public Map<String, String> getMetadata() {
public ObjectNode getMetadata() {
return metadata;
}

public void setMetadata(Map<String, String> metadata) {
public void setMetadata(ObjectNode metadata) {
this.metadata = metadata;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.stream.Collectors;

import org.infinispan.protostream.MessageMarshaller;
import org.kie.kogito.index.model.Entry;
import org.kie.kogito.index.model.Node;
import org.kie.kogito.index.model.ProcessDefinition;
import org.kie.kogito.jackson.utils.ObjectMapperFactory;
import org.kie.kogito.persistence.infinispan.protostream.AbstractMarshaller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

public class ProcessDefinitionMarshaller extends AbstractMarshaller implements MessageMarshaller<ProcessDefinition> {

Expand Down Expand Up @@ -72,10 +72,14 @@ public ProcessDefinition readFrom(ProtoStreamReader reader) throws IOException {
return pd;
}

private static Map<String, String> buildMetadata(ProtoStreamReader reader) throws IOException {
return Optional.ofNullable(reader.readCollection(METADATA, new HashSet<>(), Entry.class))
.map(entries -> entries.stream().collect(Collectors.toMap(Entry::getKey, Entry::getValue)))
.orElse(null);
private static ObjectNode buildMetadata(ProtoStreamReader reader) throws IOException {
Set<Entry> set = reader.readCollection(METADATA, new HashSet<>(), Entry.class);
if (set == null) {
return null;
}
ObjectNode node = ObjectMapperFactory.get().createObjectNode();
set.forEach(e -> node.put(e.getKey(), e.getValue()));
return node;
}

@Override
Expand All @@ -95,10 +99,13 @@ public void writeTo(ProtoStreamWriter writer, ProcessDefinition pd) throws IOExc
}

private static Set<Entry> buildMetadata(ProcessDefinition pd) {
return Optional.ofNullable(pd.getMetadata())
.map(Map::entrySet)
.map(entries -> entries.stream().map(e -> new Entry(e.getKey(), e.getValue())).collect(Collectors.toSet()))
.orElse(null);
return pd.getMetadata() == null ? null : buildMetadata(pd.getMetadata());
}

private static Set<Entry> buildMetadata(ObjectNode node) {
Set<Entry> result = new LinkedHashSet<Entry>();
node.fields().forEachRemaining(e -> result.add(new Entry(e.getKey(), e.getValue().asText())));
return result;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@

import java.io.IOException;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.infinispan.protostream.MessageMarshaller;
import org.junit.jupiter.api.Test;
import org.kie.kogito.index.model.Entry;
import org.kie.kogito.index.model.ProcessDefinition;
import org.kie.kogito.jackson.utils.ObjectMapperFactory;
import org.mockito.InOrder;

import static java.util.Collections.singleton;
import static java.util.stream.Collectors.toSet;
import static org.assertj.core.api.Assertions.assertThat;
import static org.kie.kogito.index.infinispan.protostream.ProcessDefinitionMarshaller.ADDONS;
import static org.kie.kogito.index.infinispan.protostream.ProcessDefinitionMarshaller.ANNOTATIONS;
Expand All @@ -48,6 +48,9 @@

class ProcessDefinitionMarshallerTest {

private static final String metaKey = "key1";
private final String metaValue = "value1";

@Test
void testReadFrom() throws IOException {
MessageMarshaller.ProtoStreamReader reader = mock(MessageMarshaller.ProtoStreamReader.class);
Expand All @@ -56,7 +59,7 @@ void testReadFrom() throws IOException {
when(reader.readString(NAME)).thenReturn("processName");
when(reader.readString(DESCRIPTION)).thenReturn("descr");
when(reader.readCollection(eq(ANNOTATIONS), any(), eq(String.class))).thenReturn(new HashSet<>(singleton("tag1")));
when(reader.readCollection(eq(METADATA), any(), eq(Entry.class))).thenReturn(new HashSet<>(singleton(new Entry("key1", "value1"))));
when(reader.readCollection(eq(METADATA), any(), eq(Entry.class))).thenReturn(new HashSet<>(singleton(new Entry(metaKey, metaValue))));
when(reader.readCollection(eq(ROLES), any(), eq(String.class))).thenReturn(new HashSet<>(singleton("admin")));
when(reader.readCollection(eq(ADDONS), any(), eq(String.class))).thenReturn(new HashSet<>(singleton("process-management")));
when(reader.readString(TYPE)).thenReturn("processType");
Expand All @@ -71,7 +74,7 @@ void testReadFrom() throws IOException {
.hasFieldOrPropertyWithValue(NAME, "processName")
.hasFieldOrPropertyWithValue(DESCRIPTION, "descr")
.hasFieldOrPropertyWithValue(ANNOTATIONS, singleton("tag1"))
.hasFieldOrPropertyWithValue(METADATA, Map.of("key1", "value1"))
.hasFieldOrPropertyWithValue(METADATA, ObjectMapperFactory.get().createObjectNode().put(metaKey, metaValue))
.hasFieldOrPropertyWithValue(ROLES, singleton("admin"))
.hasFieldOrPropertyWithValue(ADDONS, singleton("process-management"))
.hasFieldOrPropertyWithValue(TYPE, "processType");
Expand All @@ -90,13 +93,14 @@ void testReadFrom() throws IOException {

@Test
void testWriteTo() throws IOException {

ProcessDefinition pd = new ProcessDefinition();
pd.setId("processId");
pd.setVersion("1.0");
pd.setName("processName");
pd.setDescription("descr");
pd.setAnnotations(singleton("tag1"));
pd.setMetadata(Map.of("key1", "value1"));
pd.setMetadata(ObjectMapperFactory.get().createObjectNode().put(metaKey, metaValue));
pd.setRoles(singleton("admin"));
pd.setAddons(singleton("process-management"));
pd.setType("processType");
Expand All @@ -112,7 +116,7 @@ void testWriteTo() throws IOException {
inOrder.verify(writer).writeString(NAME, pd.getName());
inOrder.verify(writer).writeString(DESCRIPTION, pd.getDescription());
inOrder.verify(writer).writeCollection(ANNOTATIONS, pd.getAnnotations(), String.class);
inOrder.verify(writer).writeCollection(METADATA, pd.getMetadata().entrySet().stream().map(e -> new Entry(e.getKey(), e.getValue())).collect(toSet()), Entry.class);
inOrder.verify(writer).writeCollection(METADATA, Set.of(new Entry(metaKey, metaValue)), Entry.class);
inOrder.verify(writer).writeCollection(ROLES, pd.getRoles(), String.class);
inOrder.verify(writer).writeCollection(ADDONS, pd.getAddons(), String.class);
inOrder.verify(writer).writeString(TYPE, pd.getType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,24 @@
package org.kie.kogito.index.jpa.model;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import org.kie.kogito.index.model.ProcessDefinitionKey;
import org.kie.kogito.persistence.postgresql.hibernate.JsonBinaryConverter;

import com.fasterxml.jackson.databind.node.ObjectNode;

import jakarta.persistence.CascadeType;
import jakarta.persistence.CollectionTable;
import jakarta.persistence.Column;
import jakarta.persistence.Convert;
import jakarta.persistence.ElementCollection;
import jakarta.persistence.Entity;
import jakarta.persistence.ForeignKey;
import jakarta.persistence.Id;
import jakarta.persistence.IdClass;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.MapKeyColumn;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;

Expand Down Expand Up @@ -74,13 +76,9 @@ public class ProcessDefinitionEntity extends AbstractEntity {
@JoinColumn(name = "process_version", referencedColumnName = "version") }, foreignKey = @ForeignKey(name = "fk_definitions_annotations"))
@Column(name = "annotation")
private Set<String> annotations;
@ElementCollection
@CollectionTable(name = "definitions_metadata", joinColumns = {
@JoinColumn(name = "process_id", referencedColumnName = "id"), @JoinColumn(name = "process_version", referencedColumnName = "version") },
foreignKey = @ForeignKey(name = "fk_definitions_metadata"))
@MapKeyColumn(name = "name")
@Column(name = "meta_value")
private Map<String, String> metadata;
@Convert(converter = JsonBinaryConverter.class)
@Column(columnDefinition = "jsonb")
private ObjectNode metadata;

@Override
public String getId() {
Expand Down Expand Up @@ -171,11 +169,11 @@ public void setAnnotations(Set<String> annotations) {
this.annotations = annotations;
}

public Map<String, String> getMetadata() {
public ObjectNode getMetadata() {
return metadata;
}

public void setMetadata(Map<String, String> metadata) {
public void setMetadata(ObjectNode metadata) {
this.metadata = metadata;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@
import org.kie.kogito.index.model.ProcessDefinition;
import org.kie.kogito.index.model.ProcessDefinitionKey;

import io.quarkus.arc.DefaultBean;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.transaction.Transactional;

@ApplicationScoped
@DefaultBean
public class ProcessDefinitionEntityStorage extends AbstractStorage<ProcessDefinitionKey, ProcessDefinitionEntity, ProcessDefinition> {

protected ProcessDefinitionEntityStorage() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

import org.bson.codecs.pojo.annotations.BsonId;

import com.fasterxml.jackson.databind.node.ObjectNode;

public class ProcessDefinitionEntity {

@BsonId
Expand All @@ -40,7 +42,7 @@ public class ProcessDefinitionEntity {

private Set<String> annotations;

private Map<String, String> metadata;
private ObjectNode metadata;

private Set<String> roles;

Expand Down Expand Up @@ -148,11 +150,11 @@ public void setAnnotations(Set<String> annotations) {
this.annotations = annotations;
}

public Map<String, String> getMetadata() {
public ObjectNode getMetadata() {
return metadata;
}

public void setMetadata(Map<String, String> metadata) {
public void setMetadata(ObjectNode metadata) {
this.metadata = metadata;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,18 @@
*/
package org.kie.kogito.index.postgresql;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.kie.kogito.persistence.api.query.AttributeFilter;

import jakarta.persistence.PersistenceException;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.Expression;
import jakarta.persistence.criteria.Join;
import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Root;

Expand Down Expand Up @@ -101,11 +105,26 @@ private static Expression buildObjectExpression(CriteriaBuilder builder, Object

private static Expression buildPathExpression(CriteriaBuilder builder, Root<?> root, String attributeName, boolean isStr) {
String[] attributes = attributeName.split("\\.");
Expression<?>[] arguments = new Expression[attributes.length];
arguments[0] = root.get(attributes[0]);
for (int i = 1; i < attributes.length; i++) {
arguments[i] = builder.literal(attributes[i]);

Collection<Expression> arguments = new ArrayList<>();
if (attributes.length == 1)
return root.get(attributeName);
int startIndex;
// Check if the first attribute is a join, if it is, assume next attribute is the json property (not sure thats necessarily correct but it will work)
try {
Join join = root.join(attributes[0]);
arguments.add(join.get(attributes[1]));
startIndex = 2;
} catch (PersistenceException ex) {
// If not, the first attribute is the json one,
arguments.add(root.get(attributes[0]));
startIndex = 1;
}

for (int i = startIndex; i < attributes.length; i++) {
arguments.add(builder.literal(attributes[i]));
}
return isStr ? builder.function("jsonb_extract_path_text", String.class, arguments) : builder.function("jsonb_extract_path", Object.class, arguments);
return isStr ? builder.function("jsonb_extract_path_text", String.class, arguments.toArray(new Expression[arguments.size()]))
: builder.function("jsonb_extract_path", Object.class, arguments.toArray(new Expression[arguments.size()]));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.kie.kogito.index.postgresql;

import org.kie.kogito.index.jpa.mapper.ProcessDefinitionEntityMapper;
import org.kie.kogito.index.jpa.model.ProcessDefinitionEntityRepository;
import org.kie.kogito.index.jpa.storage.ProcessDefinitionEntityStorage;
import org.kie.kogito.index.model.ProcessDefinition;
import org.kie.kogito.persistence.api.query.Query;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;

@ApplicationScoped
public class PostgresqlProcessDefinitionEntityStorage extends ProcessDefinitionEntityStorage {

@Inject
public PostgresqlProcessDefinitionEntityStorage(ProcessDefinitionEntityRepository repository, ProcessDefinitionEntityMapper mapper) {
super(repository, mapper);
}

@Override
public Query<ProcessDefinition> query() {
return new PostgresqlJsonJPAQuery<>(repository, mapToModel, entityClass);
}
}
Loading

0 comments on commit 33d634c

Please sign in to comment.