Skip to content

Commit

Permalink
[Fix apache#2177] Merge metadata in ProcessEventDefinition
Browse files Browse the repository at this point in the history
  • Loading branch information
fjtirado committed Jan 20, 2025
1 parent bf639a6 commit 0123416
Show file tree
Hide file tree
Showing 12 changed files with 102 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
Expand Down Expand Up @@ -67,6 +69,25 @@ public static TypeDefinitionRegistry loadSchemaDefinitionFile(String fileName) {
}
}

@SuppressWarnings({ "rawtypes", "unchecked" })
public static Map mergeMap(Map source, Map target) {
if (source == null) {
return target;
} else if (target == null) {
return source;
} else {
Map result = new HashMap(target);
source.forEach((key, value) -> result.merge(key, value, (targetValue, srcValue) -> {
if (srcValue instanceof Map && targetValue instanceof Map) {
return mergeMap((Map) srcValue, (Map) targetValue);
} else {
return srcValue;
}
}));
return result;
}
}

private static String getContext(String processId) {
return processId != null && processId.contains(".") ? processId.substring(processId.lastIndexOf('.') + 1) : processId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,18 @@
*/
package org.kie.kogito.index.service;

import java.util.AbstractMap;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

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.json.JsonUtils;
import org.kie.kogito.index.CommonUtils;
import org.kie.kogito.index.model.Node;
import org.kie.kogito.index.model.ProcessDefinition;

import com.fasterxml.jackson.core.JsonProcessingException;

import jakarta.enterprise.context.ApplicationScoped;

import static java.util.stream.Collectors.toList;
Expand Down Expand Up @@ -62,8 +57,9 @@ 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(doMerge(toStringMap(data.getMetadata()), instance.getMetadata()));
instance.setMetadata(CommonUtils.mergeMap(data.getMetadata(), instance.getMetadata()));
instance.setNodes(doMerge(nodeDefinitions(data), instance.getNodes()));

instance.setSource(doMerge(data.getSource(), instance.getSource()));
return instance;
}
Expand All @@ -81,7 +77,7 @@ private static Node nodeDefinition(NodeDefinition definition) {
node.setName(definition.getName());
node.setUniqueId(definition.getUniqueId());
node.setType(definition.getType());
node.setMetadata(toStringMap(definition.getMetadata()));
node.setMetadata(definition.getMetadata());
return node;
}

Expand All @@ -94,22 +90,4 @@ private static <T> T doMerge(T incoming, T current) {
return current;
}

private static Map<String, String> toStringMap(Map<String, ?> input) {
if (input == null) {
return null;
}
return input.entrySet().stream()
.map(entry -> {
if (String.class.isInstance(entry.getValue())) {
return entry;
}
String value = null;
try {
value = JsonUtils.getObjectMapper().writeValueAsString(entry.getValue());
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
return new AbstractMap.SimpleEntry<>(entry.getKey(), value);
}).collect(Collectors.toMap(Map.Entry::getKey, e -> (String) e.getValue()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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;

import java.util.Map;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class CommonUtilsTest {

@Test
void testSimpleMergeMap() {
Map src = Map.of("name", "Javierito", "different", "remain");
Map target = Map.of("name", "Fulanito", "other", "remain");
assertThat(CommonUtils.mergeMap(src, target)).isEqualTo(Map.of("name", "Javierito", "other", "remain", "different", "remain"));
}

@Test
void testComplexMergeMap() {
Map nestedSrc = Map.of("name", "Javierito", "different", "remain");
Map nestedTarget = Map.of("name", "Fulanito", "other", "remain");
Map src = Map.of("nested", nestedSrc);
Map target = Map.of("nested", nestedTarget);
assertThat(CommonUtils.mergeMap(src, target)).isEqualTo(Map.of("nested", Map.of("name", "Javierito", "other", "remain", "different", "remain")));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class Node {
@JsonProperty("nodeType")
private String type;
private String uniqueId;
private Map<String, String> metadata;
private Map<String, ?> metadata;

public String getId() {
return id;
Expand Down Expand Up @@ -64,11 +64,11 @@ public void setUniqueId(String uniqueId) {
this.uniqueId = uniqueId;
}

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class ProcessDefinition {
private String source;
private String description;
private Set<String> annotations;
private Map<String, String> metadata;
private Map<String, ?> metadata;
private List<Node> nodes;

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

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ public Entry readFrom(ProtoStreamReader reader) throws IOException {
@Override
public void writeTo(ProtoStreamWriter writer, Entry entry) throws IOException {
writer.writeString(KEY, entry.getKey());
writer.writeString(VALUE, entry.getValue());
writer.writeString(VALUE, toString(entry.getValue()));
}

private String toString (Object str) {
return str != null ? str.toString() : null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,16 @@ Function<NodeMetadata, Map<String, String>> toNodeMetadataMap() {
};
}

Function<Map<String, String>, NodeMetadata> toNodeMetadata() {
Function<Map<String, ?>, NodeMetadata> toNodeMetadata() {
return m -> {
if (m == null) {
return null;
}
NodeMetadata meta = new NodeMetadata();
meta.setAction(m.get(NodeMetadataMarshaller.ACTION));
meta.setState(m.get(NodeMetadataMarshaller.STATE));
meta.setUniqueId(m.get(NodeMetadataMarshaller.UNIQUE_ID));
meta.setBranch(m.get(NodeMetadataMarshaller.BRANCH));
meta.setAction((String)m.get(NodeMetadataMarshaller.ACTION));
meta.setState((String)m.get(NodeMetadataMarshaller.STATE));
meta.setUniqueId((String)m.get(NodeMetadataMarshaller.UNIQUE_ID));
meta.setBranch((String)m.get(NodeMetadataMarshaller.BRANCH));
return meta;
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public ProcessDefinition readFrom(ProtoStreamReader reader) throws IOException {
return pd;
}

private static Map<String, String> buildMetadata(ProtoStreamReader reader) throws IOException {
private static Map<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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ public class Entry {

private String key;

private String value;
private Object value;

public Entry() {
}

public Entry(String key, String value) {
public Entry(String key, Object value) {
this.key = key;
this.value = value;
}
Expand All @@ -43,7 +43,7 @@ public void setKey(String key) {
this.key = key;
}

public String getValue() {
public Object getValue() {
return value;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class NodeEntity extends AbstractEntity {
foreignKey = @ForeignKey(name = "fk_definitions_nodes_metadata_definitions_nodes"))
@MapKeyColumn(name = "name")
@Column(name = "meta_value")
private Map<String, String> metadata;
private Map<String, ?> metadata;

@Id
@ManyToOne(cascade = CascadeType.ALL, optional = false)
Expand Down Expand Up @@ -96,11 +96,11 @@ public void setType(String type) {
this.type = type;
}

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public class ProcessDefinitionEntity extends AbstractEntity {
foreignKey = @ForeignKey(name = "fk_definitions_metadata"))
@MapKeyColumn(name = "name")
@Column(name = "meta_value")
private Map<String, String> metadata;
private Map<String, ?> metadata;

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

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class ProcessDefinitionEntity {

private Set<String> annotations;

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

private Set<String> roles;

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

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

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

Expand Down Expand Up @@ -193,7 +193,7 @@ public static class NodeEntity {
private String uniqueId;
private String type;

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

public String getId() {
return id;
Expand Down Expand Up @@ -227,11 +227,11 @@ public void setType(String type) {
this.type = type;
}

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

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

Expand Down

0 comments on commit 0123416

Please sign in to comment.