[New] Added StrolchRootElement.setRelations()

This commit is contained in:
Robert von Burg 2021-07-22 14:03:34 +02:00
parent 982f3939ea
commit 866c4fc817
2 changed files with 62 additions and 14 deletions

View File

@ -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<? extends StrolchRootElement> elements) {
// validate we have same objects
List<String> objectTypes = elements.stream().map(StrolchRootElement::getObjectType).distinct()
.collect(Collectors.toList());
List<String> 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());
}
}
}

View File

@ -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<? extends StrolchRootElement> elements);
/**
* Return a clone of this {@link StrolchElement}
*