Skip to content

Commit

Permalink
Introduce ItemDefinitionTransformer
Browse files Browse the repository at this point in the history
  • Loading branch information
tonydamage committed May 27, 2021
1 parent c941eb1 commit 4384822
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 13 deletions.
@@ -0,0 +1,22 @@
package com.evolveum.midpoint.prism;


public interface ItemDefinitionTransformer {

<I extends ItemDefinition<?>> I transformItem(ComplexTypeDefinition parentDef, I currentDef);

<T extends TypeDefinition> T applyValue(ComplexTypeDefinition parentDef, ItemDefinition<?> itemDef, T valueDef);


public interface TransformableItem {

void transformDefinition(ComplexTypeDefinition parentDef, ItemDefinitionTransformer transformer);

}

public interface TransformableValue {

void transformDefinition(ComplexTypeDefinition parentDef, ItemDefinition<?> itemDef, ItemDefinitionTransformer transformation);
}

}
Expand Up @@ -224,6 +224,7 @@ <IV extends PrismValue,ID extends ItemDefinition,I extends Item<IV,ID>> I findOr
@Override
PrismContainer<C> cloneComplex(CloneStrategy strategy);

@Deprecated
PrismContainerDefinition<C> deepCloneDefinition(boolean ultraDeep, Consumer<ItemDefinition> postCloneAction);

@Override
Expand Down
Expand Up @@ -55,16 +55,6 @@ default int getMaxOccurs() {
return delegate().getMaxOccurs();
}

@Override
default boolean isSingleValue() {
return delegate().isSingleValue();
}

@Override
default boolean isMultiValue() {
return delegate().isMultiValue();
}

@Override
default boolean isMandatory() {
return delegate().isMandatory();
Expand Down
Expand Up @@ -26,6 +26,8 @@
import javax.xml.namespace.QName;

import com.evolveum.midpoint.prism.*;
import com.evolveum.midpoint.prism.ItemDefinitionTransformer.TransformableItem;
import com.evolveum.midpoint.prism.ItemDefinitionTransformer.TransformableValue;
import com.evolveum.midpoint.prism.util.CloneUtil;
import com.evolveum.midpoint.util.annotation.Experimental;

Expand Down Expand Up @@ -56,7 +58,7 @@
*
* @author Radovan Semancik
*/
public abstract class ItemImpl<V extends PrismValue, D extends ItemDefinition> extends AbstractFreezable implements Item<V, D> {
public abstract class ItemImpl<V extends PrismValue, D extends ItemDefinition> extends AbstractFreezable implements Item<V, D>, TransformableItem {

private static final long serialVersionUID = 510000191615288733L;

Expand Down Expand Up @@ -971,4 +973,18 @@ public Long getHighestId() {
});
return highest.getValue();
}

@Override
public void transformDefinition(ComplexTypeDefinition parent, ItemDefinitionTransformer transformation) {
D newDefinition = transformation.transformItem(parent, definition);
// Do not replace definition with null or run checks if definition is unmodified.
if (newDefinition != null && newDefinition != definition) {
setDefinition(newDefinition);
}
for (V pval : values) {
if (pval instanceof TransformableValue) {
((TransformableValue) pval).transformDefinition(parent, definition, transformation);
}
}
}
}
Expand Up @@ -22,6 +22,7 @@
import org.jetbrains.annotations.Nullable;

import com.evolveum.midpoint.prism.*;
import com.evolveum.midpoint.prism.ItemDefinitionTransformer.TransformableItem;
import com.evolveum.midpoint.prism.delta.ItemDelta;
import com.evolveum.midpoint.prism.equivalence.EquivalenceStrategy;
import com.evolveum.midpoint.prism.equivalence.ParameterizedEquivalenceStrategy;
Expand Down Expand Up @@ -1825,6 +1826,22 @@ public void removeOperationalItems() {
});
}

@Override
public void transformDefinition(ComplexTypeDefinition parentDef, ItemDefinition<?> itemDef,
ItemDefinitionTransformer transformation) {

ComplexTypeDefinition newDefinition = transformation.applyValue(parentDef, itemDef, complexTypeDefinition);
if (newDefinition != null && newDefinition != complexTypeDefinition) {
replaceComplexTypeDefinition(newDefinition);
}

for(Item<?,?> item : items.values()) {
if (item instanceof TransformableItem) {
((TransformableItem) item).transformDefinition(complexTypeDefinition, transformation);
}
}
}

private static class FailOnAddList extends AbstractList<ItemDelta<?, ?>> {

public static final FailOnAddList INSTANCE = new FailOnAddList();
Expand Down
Expand Up @@ -671,4 +671,10 @@ public void performFreeze() {
return null;
}
}

@Override
public void transformDefinition(ComplexTypeDefinition parentDef, ItemDefinition<?> itemDef,
ItemDefinitionTransformer transformation) {
// NOOP
}
}
Expand Up @@ -30,7 +30,6 @@
import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -80,6 +79,7 @@ public PrismReferenceValueImpl(String oid, OriginType type, Objectable source) {
*
* @return the target oid
*/
@Override
public String getOid() {
if (oid != null) {
return oid;
Expand All @@ -90,6 +90,7 @@ public String getOid() {
return null;
}

@Override
public void setOid(String oid) {
checkMutable();
this.oid = oid;
Expand All @@ -104,10 +105,12 @@ public void setOid(String oid) {
* expect that the object can disappear when serialization boundary is crossed.
* The client must expect that the object is null.
*/
@Override
public PrismObject getObject() {
return object;
}

@Override
public void setObject(PrismObject object) {
checkMutable();
this.object = object;
Expand All @@ -121,6 +124,7 @@ public void setObject(PrismObject object) {
*
* @return the target type name
*/
@Override
public QName getTargetType() {
if (targetType != null) {
return targetType;
Expand All @@ -131,13 +135,15 @@ public QName getTargetType() {
return null;
}

@Override
public void setTargetType(QName targetType) {
setTargetType(targetType, false);
}

/**
* @param allowEmptyNamespace This is an ugly hack. See comment in DOMUtil.validateNonEmptyQName.
*/
@Override
public void setTargetType(QName targetType, boolean allowEmptyNamespace) {
checkMutable();
// Null value is OK
Expand All @@ -158,6 +164,7 @@ public void setTargetType(QName targetType, boolean allowEmptyNamespace) {
* mechanism.
* @return cached name of the target object.
*/
@Override
public PolyString getTargetName() {
if (targetName != null) {
return targetName;
Expand All @@ -168,11 +175,13 @@ public PolyString getTargetName() {
return null;
}

@Override
public void setTargetName(PolyString name) {
checkMutable();
this.targetName = name;
}

@Override
public void setTargetName(PolyStringType name) {
checkMutable();
if (name == null) {
Expand All @@ -183,11 +192,13 @@ public void setTargetName(PolyStringType name) {
}

// The PRV (this object) should have a parent with a prism context
@Override
public Class<Objectable> getTargetTypeCompileTimeClass() {
PrismContext prismContext = getPrismContext();
return prismContext != null ? getTargetTypeCompileTimeClass(prismContext) : null;
}

@Override
public Class<Objectable> getTargetTypeCompileTimeClass(PrismContext prismContext) {
QName type = getTargetType();
if (type == null) {
Expand All @@ -198,42 +209,51 @@ public Class<Objectable> getTargetTypeCompileTimeClass(PrismContext prismContext
}
}

@Override
public QName getRelation() {
return relation;
}

@Override
public void setRelation(QName relation) {
checkMutable();
this.relation = relation;
}

@Override
public PrismReferenceValueImpl relation(QName relation) {
setRelation(relation);
return this;
}

@Override
public String getDescription() {
return description;
}

@Override
public void setDescription(String description) {
checkMutable();
this.description = description;
}

@Override
public SearchFilterType getFilter() {
return filter;
}

@Override
public void setFilter(SearchFilterType filter) {
checkMutable();
this.filter = filter;
}

@Override
public EvaluationTimeType getResolutionTime() {
return resolutionTime;
}

@Override
public void setResolutionTime(EvaluationTimeType resolutionTime) {
checkMutable();
this.resolutionTime = resolutionTime;
Expand Down Expand Up @@ -311,6 +331,7 @@ public void applyDefinition(ItemDefinition definition, boolean force) throws Sch
applyDefinition((PrismReferenceDefinition)definition, force);
}

@Override
public void applyDefinition(PrismReferenceDefinition definition, boolean force) throws SchemaException {
super.applyDefinition(definition, force);
if (object == null) {
Expand Down Expand Up @@ -395,6 +416,7 @@ public boolean isEmpty() {
* Returns a version of this value that is canonical, that means it has the minimal form.
* E.g. it will have only OID and no object.
*/
@Override
public PrismReferenceValueImpl toCanonical() {
PrismReferenceValueImpl can = new PrismReferenceValueImpl();
can.setOid(getOid());
Expand All @@ -407,6 +429,7 @@ public PrismReferenceValueImpl toCanonical() {
return can;
}

@Override
public boolean equals(PrismValue other, @NotNull ParameterizedEquivalenceStrategy strategy) {
return other instanceof PrismReferenceValue && equals((PrismReferenceValue) other, strategy);
}
Expand Down Expand Up @@ -550,6 +573,7 @@ public String toString() {
return sb.toString();
}

@Override
public Referencable asReferencable() {
if (referencable != null) {
return referencable;
Expand Down Expand Up @@ -598,6 +622,7 @@ public String debugDump(int indent) {
return debugDump(indent, false);
}

@Override
public String debugDump(int indent, boolean expandObject) {
StringBuilder sb = new StringBuilder();
DebugUtil.indentDebugDump(sb, indent);
Expand Down Expand Up @@ -709,4 +734,10 @@ public void accept(Visitor visitor) {
object.accept(visitor);
}
}

@Override
public void transformDefinition(ComplexTypeDefinition parentDef, ItemDefinition<?> itemDef,
ItemDefinitionTransformer transformation) {
// NOOP
}
}
Expand Up @@ -7,6 +7,7 @@
package com.evolveum.midpoint.prism.impl;

import com.evolveum.midpoint.prism.*;
import com.evolveum.midpoint.prism.ItemDefinitionTransformer.TransformableValue;
import com.evolveum.midpoint.prism.delta.ItemDelta;
import com.evolveum.midpoint.prism.equivalence.EquivalenceStrategy;
import com.evolveum.midpoint.prism.equivalence.ParameterizedEquivalenceStrategy;
Expand All @@ -28,7 +29,7 @@
* @author semancik
*
*/
public abstract class PrismValueImpl extends AbstractFreezable implements PrismValue {
public abstract class PrismValueImpl extends AbstractFreezable implements PrismValue, TransformableValue {

private OriginType originType;
private Objectable originObject;
Expand Down Expand Up @@ -468,4 +469,5 @@ public boolean isTransient() {
public void setTransient(boolean value) {
isTransient = value;
}

}

0 comments on commit 4384822

Please sign in to comment.