From 866c4fc8174a6f718de9a6254f49760c4466447f Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Thu, 22 Jul 2021 14:03:34 +0200 Subject: [PATCH] [New] Added StrolchRootElement.setRelations() --- .../model/AbstractStrolchRootElement.java | 54 +++++++++++++++++-- .../li/strolch/model/StrolchRootElement.java | 22 ++++---- 2 files changed, 62 insertions(+), 14 deletions(-) diff --git a/li.strolch.model/src/main/java/li/strolch/model/AbstractStrolchRootElement.java b/li.strolch.model/src/main/java/li/strolch/model/AbstractStrolchRootElement.java index 17b47dac3..e5aadf84d 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/AbstractStrolchRootElement.java +++ b/li.strolch.model/src/main/java/li/strolch/model/AbstractStrolchRootElement.java @@ -1,9 +1,13 @@ package li.strolch.model; +import static java.util.Collections.emptyList; import static li.strolch.model.StrolchModelConstants.*; -import static li.strolch.model.builder.BuilderHelper.buildParamId; import static li.strolch.model.builder.BuilderHelper.buildParamName; +import java.util.List; +import java.util.stream.Collectors; + +import li.strolch.model.parameter.StringListParameter; import li.strolch.model.parameter.StringParameter; public abstract class AbstractStrolchRootElement extends GroupedParameterizedElement implements StrolchRootElement { @@ -16,10 +20,7 @@ public abstract class AbstractStrolchRootElement extends GroupedParameterizedEle super(id, name, type); } - public void setRelation(StrolchRootElement element) { - setRelation(buildParamId(element.getType()), element); - } - + @Override public void setRelation(String param, StrolchRootElement element) { StringParameter relationP = relationsBag().getParameter(param); if (relationP == null) { @@ -45,4 +46,47 @@ public abstract class AbstractStrolchRootElement extends GroupedParameterizedEle relationP.setValue(element.getId()); } + + @Override + public void setRelations(String param, List elements) { + + // validate we have same objects + List objectTypes = elements.stream().map(StrolchRootElement::getObjectType).distinct() + .collect(Collectors.toList()); + List types = elements.stream().map(StrolchRootElement::getType).distinct().collect(Collectors.toList()); + if (objectTypes.size() != 1) + throw new IllegalStateException( + "Only allow to have one type of object: " + elements.stream().map(StrolchElement::getId) + .collect(Collectors.joining(", "))); + if (types.size() != 1) + throw new IllegalStateException( + "Only allow to have one type of object: " + elements.stream().map(StrolchElement::getId) + .collect(Collectors.joining(", "))); + + StringListParameter relationsP = relationsBag().getParameter(param); + if (relationsP == null) { + String name = buildParamName(param); + relationsP = new StringListParameter(param, name, emptyList()); + + switch (objectTypes.get(0)) { + case Tags.RESOURCE: + relationsP.setInterpretation(INTERPRETATION_RESOURCE_REF); + break; + case Tags.ORDER: + relationsP.setInterpretation(INTERPRETATION_ORDER_REF); + break; + case Tags.ACTIVITY: + relationsP.setInterpretation(INTERPRETATION_ACTIVITY_REF); + break; + } + + relationsP.setUom(types.get(0)); + + relationsBag().addParameter(relationsP); + } + + for (StrolchRootElement element : elements) { + relationsP.addValueIfNotContains(element.getId()); + } + } } diff --git a/li.strolch.model/src/main/java/li/strolch/model/StrolchRootElement.java b/li.strolch.model/src/main/java/li/strolch/model/StrolchRootElement.java index fc01463c7..82eee4140 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/StrolchRootElement.java +++ b/li.strolch.model/src/main/java/li/strolch/model/StrolchRootElement.java @@ -17,6 +17,8 @@ package li.strolch.model; import static li.strolch.model.StrolchModelConstants.*; +import java.util.List; + import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonObject; @@ -79,15 +81,6 @@ public interface StrolchRootElement extends StrolchElement, PolicyContainer, Par */ void setVersion(Version version) throws IllegalArgumentException; - /** - * Set a relation to the given element by using the type of the given element as the parameter ID, but lower-ccasing - * the first letter. Should the parameter not exist, then it will be created - * - * @param element - * the element for which to set the relation to - */ - void setRelation(StrolchRootElement element); - /** * Set a relation to the given element by using the given param. Should the parameter not exist, then it will be * created @@ -99,6 +92,17 @@ public interface StrolchRootElement extends StrolchElement, PolicyContainer, Par */ void setRelation(String param, StrolchRootElement element); + /** + * Set relations to the given elements by using the given param. Should the parameter not exist, then it will be + * created + * + * @param param + * the parameter ID on which to set the relations + * @param elements + * the elements for which to set the relations to + */ + void setRelations(String param, List elements); + /** * Return a clone of this {@link StrolchElement} *