[New] Added StrolchRootElement.setRelation()

This commit is contained in:
Robert von Burg 2020-10-28 13:15:15 +01:00
parent 9f47a72168
commit ad80ad4d79
5 changed files with 79 additions and 5 deletions

View File

@ -1,5 +1,11 @@
package li.strolch.model;
import static li.strolch.model.StrolchModelConstants.*;
import static li.strolch.model.builder.BuilderHelper.buildParamId;
import static li.strolch.model.builder.BuilderHelper.buildParamName;
import li.strolch.model.parameter.StringParameter;
public abstract class AbstractStrolchRootElement extends GroupedParameterizedElement implements StrolchRootElement {
public AbstractStrolchRootElement() {
@ -9,4 +15,34 @@ public abstract class AbstractStrolchRootElement extends GroupedParameterizedEle
public AbstractStrolchRootElement(String id, String name, String type) {
super(id, name, type);
}
public void setRelation(StrolchRootElement element) {
setRelation(buildParamId(element.getType()), element);
}
public void setRelation(String param, StrolchRootElement element) {
StringParameter relationP = relationsBag().getParameter(param);
if (relationP == null) {
String name = buildParamName(param);
relationP = new StringParameter(param, name, "");
switch (element.getObjectType()) {
case Tags.RESOURCE:
relationP.setInterpretation(INTERPRETATION_RESOURCE_REF);
break;
case Tags.ORDER:
relationP.setInterpretation(INTERPRETATION_ORDER_REF);
break;
case Tags.ACTIVITY:
relationP.setInterpretation(INTERPRETATION_ACTIVITY_REF);
break;
}
relationP.setUom(element.getType());
relationsBag().addParameter(relationP);
}
relationP.setValue(element.getId());
}
}

View File

@ -496,7 +496,7 @@ public abstract class GroupedParameterizedElement extends AbstractStrolchElement
bag.setLongList(paramKey, value);
}
private ParameterBag defaultBag() {
protected ParameterBag defaultBag() {
ParameterBag bag = getParameterBag(BAG_PARAMETERS, false);
if (bag == null) {
bag = new ParameterBag(BAG_PARAMETERS, TYPE_PARAMETERS, TYPE_PARAMETERS);
@ -505,6 +505,15 @@ public abstract class GroupedParameterizedElement extends AbstractStrolchElement
return bag;
}
protected ParameterBag relationsBag() {
ParameterBag bag = getParameterBag(BAG_RELATIONS, false);
if (bag == null) {
bag = new ParameterBag(BAG_RELATIONS, TYPE_RELATIONS, TYPE_RELATIONS);
addParameterBag(bag);
}
return bag;
}
///
///

View File

@ -79,6 +79,26 @@ 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
*
* @param param
* the parameter ID on which to set the relations
* @param element
* the element for which to set the relation to
*/
void setRelation(String param, StrolchRootElement element);
/**
* Return a clone of this {@link StrolchElement}
*

View File

@ -0,0 +1,12 @@
package li.strolch.model.builder;
public class BuilderHelper {
public static String buildParamId(String type) {
return Character.toLowerCase(type.charAt(0)) + type.substring(1);
}
public static String buildParamName(String paramId) {
return Character.toUpperCase(paramId.charAt(0)) + paramId.substring(1);
}
}

View File

@ -3,6 +3,7 @@ package li.strolch.model.builder;
import static java.util.Collections.emptyList;
import static li.strolch.model.StrolchModelConstants.*;
import static li.strolch.model.StrolchModelConstants.PolicyConstants.BAG_OBJECTIVES;
import static li.strolch.model.builder.BuilderHelper.buildParamId;
import java.util.HashMap;
import java.util.Map;
@ -65,10 +66,6 @@ public abstract class ParameterBagContainerBuilder<T extends ParameterBagContain
return bagBuilder;
}
private String buildParamId(String type) {
return Character.toLowerCase(type.charAt(0)) + type.substring(1);
}
public T resourceRelation(String type) {
return resourceRelation(buildParamId(type), type, type);
}