Skip to content

Commit

Permalink
Fix storing static-class-less PCVs to traces
Browse files Browse the repository at this point in the history
The PrismValue.getRealValue implicitly requires that the type of
prism (container) value is statically defined. So it should be used
with caution.

(cherry picked from commit b76f386)
  • Loading branch information
mederly committed Mar 20, 2020
1 parent ca5728e commit 0c8f92f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 6 deletions.
Expand Up @@ -11,7 +11,10 @@
import com.evolveum.midpoint.prism.equivalence.EquivalenceStrategy;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.util.DebugDumpable;
import com.evolveum.midpoint.util.annotation.Experimental;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.prism.xml.ns._public.types_3.RawType;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -135,9 +138,24 @@ public interface PrismValue extends Visitable, PathVisitable, Serializable, Debu
@Nullable
Class<?> getRealClass();

@Experimental // todo reconsider method name
default boolean hasRealClass() {
return getRealClass() != null;
}

@Nullable
<T> T getRealValue();

@Nullable
@Experimental // todo reconsider method name
default Object getRealValueOrRawType(PrismContext prismContext) {
if (hasRealClass()) {
return getRealValue();
} else {
return new RawType(this, getTypeName(), prismContext);
}
}

// Returns a root of PrismValue tree. For example, if we have a AccessCertificationWorkItemType that has a parent (owner)
// of AccessCertificationCaseType, which has a parent of AccessCertificationCampaignType, this method returns the PCV
// of AccessCertificationCampaignType.
Expand Down
Expand Up @@ -100,16 +100,16 @@ public String toString() {
'}';
}

public static <V extends PrismValue> DeltaSetTripleType fromDeltaSetTriple(PrismValueDeltaSetTriple<V> triple) {
public static <V extends PrismValue> DeltaSetTripleType fromDeltaSetTriple(PrismValueDeltaSetTriple<V> triple, PrismContext prismContext) {
DeltaSetTripleType rv = new DeltaSetTripleType();
for (V v : triple.getZeroSet()) {
rv.zero.add(v.getRealValue());
rv.zero.add(v.getRealValueOrRawType(prismContext));
}
for (V v : triple.getPlusSet()) {
rv.plus.add(v.getRealValue());
rv.plus.add(v.getRealValueOrRawType(prismContext));
}
for (V v : triple.getMinusSet()) {
rv.minus.add(v.getRealValue());
rv.minus.add(v.getRealValueOrRawType(prismContext));
}
return rv;
}
Expand Down
Expand Up @@ -14,6 +14,8 @@
import com.evolveum.midpoint.xml.ns._public.common.common_3.AnyValueType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.NamedValueType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.TracingLevelType;
import com.evolveum.prism.xml.ns._public.types_3.RawType;

import org.jetbrains.annotations.NotNull;

import javax.xml.namespace.QName;
Expand Down Expand Up @@ -55,8 +57,22 @@ public static NamedValueType toNamedValueType(Object object, QName name, PrismCo

private static void setAnyValueTypeContent(Object object, AnyValueType anyValue, PrismContext prismContext) {
if (object instanceof PrismValue) {
object = ((PrismValue) object).getRealValue();
PrismValue prismValue = (PrismValue) object;
if (prismValue.hasRealClass()) {
setAnyValueReal(prismValue.getRealValue(), anyValue, prismContext);
} else {
setAnyValueDynamic(prismValue, anyValue, prismContext);
}
} else {
setAnyValueReal(object, anyValue, prismContext);
}
}

private static void setAnyValueDynamic(PrismValue prismValue, AnyValueType anyValue, PrismContext prismContext) {
anyValue.setValue(new RawType(prismValue, prismValue.getTypeName(), prismContext));
}

private static void setAnyValueReal(Object object, AnyValueType anyValue, PrismContext prismContext) {
if (object != null) {
QName typeName = prismContext.getSchemaRegistry().determineTypeForClass(object.getClass());
if (typeName != null) {
Expand Down
Expand Up @@ -571,7 +571,7 @@ public void evaluateBody(Task task, OperationResult parentResult) throws Express

private void traceOutput() {
if (outputTriple != null) {
trace.setOutput(DeltaSetTripleType.fromDeltaSetTriple(outputTriple));
trace.setOutput(DeltaSetTripleType.fromDeltaSetTriple(outputTriple, prismContext));
}
}

Expand Down

0 comments on commit 0c8f92f

Please sign in to comment.