Skip to content

Commit

Permalink
[Fix #2169] Alternative approach
Browse files Browse the repository at this point in the history
  • Loading branch information
fjtirado committed Feb 12, 2025
1 parent 4c63ec3 commit 905bffc
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,12 @@
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 @@ -65,7 +63,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((ObjectNode) MergeUtils.merge(JsonObjectUtils.fromValue(data.getMetadata()), instance.getMetadata()));
instance.setMetadata(CommonUtils.mergeMap(Collections.unmodifiableMap(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 @@ -19,11 +19,10 @@
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 @@ -36,7 +35,7 @@ public class ProcessDefinition {
private String source;
private String description;
private Set<String> annotations;
private ObjectNode metadata;
private Map<String, Object> metadata;
private List<Node> nodes;

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

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

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

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

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Optional;
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 @@ -61,7 +62,7 @@ public ProcessDefinition readFrom(ProtoStreamReader reader) throws IOException {
pd.setName(reader.readString(NAME));
pd.setDescription(reader.readString(DESCRIPTION));
pd.setAnnotations(reader.readCollection(ANNOTATIONS, new HashSet<>(), String.class));
pd.setMetadata(buildMetadata(reader));
pd.setMetadata(Collections.unmodifiableMap(buildMetadata(reader)));
pd.setRoles(reader.readCollection(ROLES, new HashSet<>(), String.class));
pd.setAddons(reader.readCollection(ADDONS, new HashSet<>(), String.class));
pd.setType(reader.readString(TYPE));
Expand All @@ -72,14 +73,10 @@ public ProcessDefinition readFrom(ProtoStreamReader reader) throws IOException {
return pd;
}

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;
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);
}

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

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

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

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;
Expand Down Expand Up @@ -74,7 +74,7 @@ void testReadFrom() throws IOException {
.hasFieldOrPropertyWithValue(NAME, "processName")
.hasFieldOrPropertyWithValue(DESCRIPTION, "descr")
.hasFieldOrPropertyWithValue(ANNOTATIONS, singleton("tag1"))
.hasFieldOrPropertyWithValue(METADATA, ObjectMapperFactory.get().createObjectNode().put(metaKey, metaValue))
.hasFieldOrPropertyWithValue(METADATA, Map.of(metaKey, metaValue))
.hasFieldOrPropertyWithValue(ROLES, singleton("admin"))
.hasFieldOrPropertyWithValue(ADDONS, singleton("process-management"))
.hasFieldOrPropertyWithValue(TYPE, "processType");
Expand All @@ -100,7 +100,7 @@ void testWriteTo() throws IOException {
pd.setName("processName");
pd.setDescription("descr");
pd.setAnnotations(singleton("tag1"));
pd.setMetadata(ObjectMapperFactory.get().createObjectNode().put(metaKey, metaValue));
pd.setMetadata(Map.of(metaKey, metaValue));
pd.setRoles(singleton("admin"));
pd.setAddons(singleton("process-management"));
pd.setType("processType");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,19 @@
*/
package org.kie.kogito.index.jpa.mapper;

import java.util.Map;

import org.kie.kogito.index.jpa.model.ProcessDefinitionEntity;
import org.kie.kogito.index.model.ProcessDefinition;
import org.kie.kogito.jackson.utils.JsonObjectUtils;
import org.mapstruct.AfterMapping;
import org.mapstruct.InheritInverseConfiguration;
import org.mapstruct.Mapper;
import org.mapstruct.MappingTarget;

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

@Mapper(componentModel = "cdi", suppressTimestampInGenerated = true)
public interface ProcessDefinitionEntityMapper {

Expand All @@ -41,6 +47,15 @@ default String map(byte[] value) {
return value == null ? null : new String(value);
}

default ObjectNode map(Map<String, Object> model) {
JsonNode entity = JsonObjectUtils.fromValue(model);
return entity == null || !entity.isObject() ? null : (ObjectNode) entity;
}

default Map<String, Object> map(ObjectNode entity) {
return (Map<String, Object>) JsonObjectUtils.convertValue(entity, Map.class);
}

@AfterMapping
default void afterMapping(@MappingTarget ProcessDefinitionEntity entity) {
if (entity.getNodes() != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* 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.
*/

ALTER TABLE definitions ADD COLUMN metadata varchar(max);

DROP TABLE definitions_metadata;
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@

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

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

public class ProcessDefinitionEntity {

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

private Set<String> annotations;

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

private Set<String> roles;

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

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

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

Expand Down

0 comments on commit 905bffc

Please sign in to comment.