diff --git a/infra/prism/src/main/java/com/evolveum/midpoint/prism/Item.java b/infra/prism/src/main/java/com/evolveum/midpoint/prism/Item.java index ade3d8f543e..fe9ad747f2f 100644 --- a/infra/prism/src/main/java/com/evolveum/midpoint/prism/Item.java +++ b/infra/prism/src/main/java/com/evolveum/midpoint/prism/Item.java @@ -52,7 +52,7 @@ public abstract class Item imple protected QName elementName; protected PrismValue parent; protected D definition; - @NotNull private final List values = new ArrayList<>(); + @NotNull protected final List values = new ArrayList<>(); private transient Map userData = new HashMap<>();; protected boolean immutable; @@ -392,11 +392,11 @@ public boolean addAll(Collection newValues) throws SchemaException { return changed; } - public boolean add(V newValue) throws SchemaException { + public boolean add(@NotNull V newValue) throws SchemaException { return add(newValue, true); } - public boolean add(V newValue, boolean checkUniqueness) throws SchemaException { + public boolean add(@NotNull V newValue, boolean checkUniqueness) throws SchemaException { checkMutability(); if (newValue.getPrismContext() == null) { newValue.setPrismContext(prismContext); diff --git a/infra/prism/src/main/java/com/evolveum/midpoint/prism/PrismContainer.java b/infra/prism/src/main/java/com/evolveum/midpoint/prism/PrismContainer.java index 7119dc8264c..7333d70a2cb 100644 --- a/infra/prism/src/main/java/com/evolveum/midpoint/prism/PrismContainer.java +++ b/infra/prism/src/main/java/com/evolveum/midpoint/prism/PrismContainer.java @@ -26,6 +26,7 @@ import com.evolveum.midpoint.util.exception.SystemException; import com.evolveum.midpoint.util.logging.Trace; import com.evolveum.midpoint.util.logging.TraceManager; +import org.jetbrains.annotations.NotNull; import javax.xml.namespace.QName; @@ -171,7 +172,7 @@ public PrismContainerValue getValue() { } } - public void setValue(PrismContainerValue value) throws SchemaException { + public void setValue(@NotNull PrismContainerValue value) throws SchemaException { checkMutability(); if (getDefinition() != null) { if (getDefinition().isSingleValue()) { @@ -187,7 +188,7 @@ public void setValue(PrismContainerValue value) throws SchemaException { } @Override - public boolean add(PrismContainerValue newValue, boolean checkUniqueness) throws SchemaException { + public boolean add(@NotNull PrismContainerValue newValue, boolean checkUniqueness) throws SchemaException { checkMutability(); // when a context-less item is added to a contextful container, it is automatically adopted if (newValue.getPrismContext() == null && this.prismContext != null) { @@ -437,21 +438,21 @@ > I findCr return getValue().findCreateItem(itemQName, type, null, create); } - public > I findItem(ItemPath propPath, Class type) { + public > I findItem(ItemPath path, Class type) { try { - return findCreateItem(propPath, type, null, false); + return findCreateItem(path, type, null, false); } catch (SchemaException e) { // This should not happen - throw new SystemException("Internal Error: "+e.getMessage(),e); + throw new SystemException("Internal Error:(path="+path+",type="+type+"): "+e.getMessage(),e); } } - public Item findItem(ItemPath propPath) { + public Item findItem(ItemPath path) { try { - return findCreateItem(propPath, Item.class, null, false); + return findCreateItem(path, Item.class, null, false); } catch (SchemaException e) { // This should not happen - throw new SystemException("Internal Error: "+e.getMessage(),e); + throw new SystemException("Internal Error:(path="+path+"): "+e.getMessage(),e); } } @@ -794,12 +795,22 @@ public void accept(Visitor visitor, ItemPath path, boolean recursive) { } } + /** + * Note: hashcode and equals compare the objects in the "java way". That means the objects must be + * almost precisely equal to match (e.g. including source demarcation in values and other "annotations"). + * For a method that compares the "meaningful" parts of the objects see equivalent(). + */ @Override public int hashCode() { int result = super.hashCode(); return result; } + /** + * Note: hashcode and equals compare the objects in the "java way". That means the objects must be + * almost precisely equal to match (e.g. including source demarcation in values and other "annotations"). + * For a method that compares the "meaningful" parts of the objects see equivalent(). + */ @Override public boolean equals(Object obj) { if (this == obj) diff --git a/infra/prism/src/main/java/com/evolveum/midpoint/prism/PrismContainerValue.java b/infra/prism/src/main/java/com/evolveum/midpoint/prism/PrismContainerValue.java index 8e9c1fca8c7..199f7b29970 100644 --- a/infra/prism/src/main/java/com/evolveum/midpoint/prism/PrismContainerValue.java +++ b/infra/prism/src/main/java/com/evolveum/midpoint/prism/PrismContainerValue.java @@ -51,41 +51,32 @@ public class PrismContainerValue extends PrismValue imp // This is list. We need to maintain the order internally to provide consistent // output in DOM and other ordering-sensitive representations - private List> items = null; + protected List> items = null; private Long id; private C containerable = null; // Definition of this value. Usually it is the same as CTD declared in the parent container. // However, in order to support polymorphism (as well as parent-less values) we distinguish between PC and PCV type definition. - private ComplexTypeDefinition complexTypeDefinition = null; + protected ComplexTypeDefinition complexTypeDefinition = null; public PrismContainerValue() { } public PrismContainerValue(C containerable) { - this(containerable, null, null); + this(containerable, null); } public PrismContainerValue(PrismContext prismContext) { - this(null, null, prismContext); - } - - public PrismContainerValue(ComplexTypeDefinition complexTypeDefinition, PrismContext prismContext) { - this(null, complexTypeDefinition, prismContext); + this(null, prismContext); } public PrismContainerValue(C containerable, PrismContext prismContext) { - this(containerable, null, prismContext); - } - - public PrismContainerValue(C containerable, ComplexTypeDefinition complexTypeDefinition, PrismContext prismContext) { super(prismContext); this.containerable = containerable; - this.complexTypeDefinition = complexTypeDefinition; - if (complexTypeDefinition == null && prismContext != null) { - getComplexTypeDefinition(); // to determine CTD + if (prismContext != null) { + getComplexTypeDefinition(); // to determine CTD (could be also called with null prismContext, but non-null prismContext provides additional information in some cases) } } @@ -1363,8 +1354,8 @@ public String debugDump(int indent) { if (DebugUtil.isDetailedDebugDump()) { DebugUtil.indentDebugDump(sb, indent); wasIndent = true; - sb.append("PCV").append(": "); - } + detailedDebugDumpStart(sb); + } boolean multivalue = true; PrismContainerable parent = getParent(); if (parent != null && parent.getDefinition() != null) { @@ -1376,8 +1367,8 @@ public String debugDump(int indent) { DebugUtil.indentDebugDump(sb, indent); wasIndent = true; } - sb.append("id=").append(PrettyPrinter.prettyPrint(getId())); - } + debugDumpIdentifiers(sb); + } appendOriginDump(sb); List> items = getItems(); if (items != null) { @@ -1396,6 +1387,14 @@ public String debugDump(int indent) { return sb.toString(); } + protected void debugDumpIdentifiers(StringBuilder sb) { + sb.append("id=").append(PrettyPrinter.prettyPrint(getId())); + } + + protected void detailedDebugDumpStart(StringBuilder sb) { + sb.append("PCV").append(": "); + } + @Override public boolean match(PrismValue otherValue) { return equalsRealValue(otherValue); diff --git a/infra/prism/src/main/java/com/evolveum/midpoint/prism/PrismObject.java b/infra/prism/src/main/java/com/evolveum/midpoint/prism/PrismObject.java index e8bd2029068..27654b6873c 100644 --- a/infra/prism/src/main/java/com/evolveum/midpoint/prism/PrismObject.java +++ b/infra/prism/src/main/java/com/evolveum/midpoint/prism/PrismObject.java @@ -43,11 +43,14 @@ * attributes). The QName (namespace and local name) of the element holding the * property is considered to be a property name. * - * This class is named MidPointObject instead of Object to avoid confusion with + * This class is named PrismObject instead of Object to avoid confusion with * java.lang.Object. * * @author Radovan Semancik * + * Class invariant: has at most one value (potentially empty). + * When making this object immutable and there's no value, we create one; in order + * to prevent exceptions on later getValue calls. */ public class PrismObject extends PrismContainer { @@ -55,10 +58,6 @@ public class PrismObject extends PrismContainer { private static final String PROPERTY_NAME_LOCALPART = "name"; - protected String oid; - protected String version; - private O objectable = null; - public PrismObject(QName name, Class compileTimeClass) { super(name, compileTimeClass); } @@ -71,6 +70,54 @@ public PrismObject(QName name, PrismObjectDefinition definition, PrismContext super(name, definition, prismContext); } + public PrismObjectValue createNewValue() { + checkMutability(); + PrismObjectValue newValue = new PrismObjectValue<>(prismContext); + try { + add(newValue, false); + return newValue; + } catch (SchemaException e) { + // This should not happen + throw new SystemException("Internal Error: " + e.getMessage(), e); + } + } + + @NotNull + public PrismObjectValue getValue() { + if (values.isEmpty()) { + return createNewValue(); + } else if (values.size() > 1) { + throw new IllegalStateException("PrismObject with more than one value: " + values); + } + return (PrismObjectValue) values.get(0); + } + + @Override + public void setValue(@NotNull PrismContainerValue value) throws SchemaException { + clear(); + add(value, false); + } + + @Override + public boolean add(@NotNull PrismContainerValue newValue, boolean checkUniqueness) throws SchemaException { + if (!(newValue instanceof PrismObjectValue)) { + throw new IllegalArgumentException("Couldn't add non-PrismObjectValue to a PrismObject: value = " + + newValue + ", object = " + this); + } + if (values.size() > 1) { + throw new IllegalStateException("PrismObject with more than one value: " + this); + } else if (values.size() == 1) { + PrismObjectValue value = (PrismObjectValue) values.get(0); + if (value.isEmpty() && value.getOid() == null) { + clear(); + } else { + throw new IllegalStateException("PrismObject cannot have more than one value. New value = " + newValue + + ", object = " + this); + } + } + return super.add(newValue, checkUniqueness); + } + /** * Returns Object ID (OID). * @@ -79,21 +126,21 @@ public PrismObject(QName name, PrismObjectDefinition definition, PrismContext * @return Object ID (OID) */ public String getOid() { - return oid; + return getValue().getOid(); } public void setOid(String oid) { checkMutability(); - this.oid = oid; + getValue().setOid(oid); } public String getVersion() { - return version; + return getValue().getVersion(); } public void setVersion(String version) { checkMutability(); - this.version = version; + getValue().setVersion(version); } @Override @@ -103,25 +150,7 @@ public PrismObjectDefinition getDefinition() { @NotNull public O asObjectable() { - if (objectable != null) { - return objectable; - } - Class clazz = getCompileTimeClass(); - if (clazz == null) { - throw new SystemException("Unknown compile time class of this prism object '" + getElementName() + "'."); - } - if (Modifier.isAbstract(clazz.getModifiers())) { - throw new SystemException("Can't create instance of class '" + clazz.getSimpleName() + "', it's abstract."); - } - try { - objectable = clazz.newInstance(); - objectable.setupContainer(this); - return (O) objectable; - } catch (SystemException ex) { - throw ex; - } catch (Exception ex) { - throw new SystemException("Couldn't create jaxb object instance of '" + clazz + "': "+ex.getMessage(), ex); - } + return getValue().asObjectable(); } public PolyString getName() { @@ -176,26 +205,6 @@ public void applyDefinition(PrismContainerDefinition definition) throws Schem super.applyDefinition(definition); } - @Override - public > I findItem(ItemPath path, Class type) { - try { - return findCreateItem(path, type, null, false); - } catch (SchemaException e) { - // This should not happen - throw new SystemException("Internal Error:(path="+path+",type="+type+"): "+e.getMessage(),e); - } - } - - @Override - public Item findItem(ItemPath path) { - try { - return findCreateItem(path, Item.class, null, false); - } catch (SchemaException e) { - // This should not happen - throw new SystemException("Internal Error:(path="+path+"): "+e.getMessage(),e); - } - } - @Override public > void removeItem(ItemPath path, Class itemType) { // Objects are only a single-valued containers. The path of the object itself is "empty". @@ -225,8 +234,6 @@ public PrismObject clone() { protected void copyValues(PrismObject clone) { super.copyValues(clone); - clone.oid = this.oid; - clone.version = this.version; } public PrismObjectDefinition deepCloneDefinition(boolean ultraDeep) { @@ -249,7 +256,7 @@ public ObjectDelta diff(PrismObject other, boolean ignoreMetadata, boolean ObjectDelta objectDelta = new ObjectDelta(getCompileTimeClass(), ChangeType.MODIFY, getPrismContext()); objectDelta.setOid(getOid()); - Collection itemDeltas = new ArrayList(); + Collection itemDeltas = new ArrayList<>(); diffInternal(other, itemDeltas, ignoreMetadata, isLiteral); objectDelta.addModifications(itemDeltas); @@ -296,41 +303,6 @@ public ItemPath getPath() { return ItemPath.EMPTY_PATH; } - /** - * Note: hashcode and equals compare the objects in the "java way". That means the objects must be - * almost preciselly equal to match (e.g. including source demarcation in values and other "annotations"). - * For a method that compares the "meaningful" parts of the objects see equivalent(). - */ - @Override - public int hashCode() { - final int prime = 31; - int result = super.hashCode(); - result = prime * result + ((oid == null) ? 0 : oid.hashCode()); - return result; - } - - /** - * Note: hashcode and equals compare the objects in the "java way". That means the objects must be - * almost preciselly equal to match (e.g. including source demarcation in values and other "annotations"). - * For a method that compares the "meaningful" parts of the objects see equivalent(). - */ - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (!super.equals(obj)) - return false; - if (getClass() != obj.getClass()) - return false; - PrismObject other = (PrismObject) obj; - if (oid == null) { - if (other.oid != null) - return false; - } else if (!oid.equals(other.oid)) - return false; - return true; - } - /** * this method ignores some part of the object during comparison (e.g. source demarcation in values) * These methods compare the "meaningful" parts of the objects. @@ -341,11 +313,6 @@ public boolean equivalent(Object obj) { if (getClass() != obj.getClass()) return false; PrismObject other = (PrismObject) obj; - if (oid == null) { - if (other.oid != null) - return false; - } else if (!oid.equals(other.oid)) - return false; ObjectDelta delta = diff(other, true, false); return delta.isEmpty(); } @@ -434,4 +401,21 @@ public String getBusinessDisplayName() { // return doc; // } + @Override + public void checkConsistenceInternal(Itemable rootItem, boolean requireDefinitions, boolean prohibitRaw, + ConsistencyCheckScope scope) { + super.checkConsistenceInternal(rootItem, requireDefinitions, prohibitRaw, scope); + if (size() > 1) { + throw new IllegalStateException("PrismObject holding more than one value: " + size() + ": " + this); + } + getValue(); // checks the type by casting to POV + } + + @Override + public void setImmutable(boolean immutable) { + if (!this.immutable && immutable && values.isEmpty()) { + createNewValue(); + } + super.setImmutable(immutable); + } } diff --git a/infra/prism/src/main/java/com/evolveum/midpoint/prism/PrismObjectDefinition.java b/infra/prism/src/main/java/com/evolveum/midpoint/prism/PrismObjectDefinition.java index 7ac57079dd2..b03f5f573ce 100644 --- a/infra/prism/src/main/java/com/evolveum/midpoint/prism/PrismObjectDefinition.java +++ b/infra/prism/src/main/java/com/evolveum/midpoint/prism/PrismObjectDefinition.java @@ -42,4 +42,7 @@ public interface PrismObjectDefinition extends PrismContai PrismObjectDefinition cloneWithReplacedDefinition(QName itemName, ItemDefinition newDefinition); PrismContainerDefinition getExtensionDefinition(); + + @Override + PrismObjectValue createValue(); } diff --git a/infra/prism/src/main/java/com/evolveum/midpoint/prism/PrismObjectDefinitionImpl.java b/infra/prism/src/main/java/com/evolveum/midpoint/prism/PrismObjectDefinitionImpl.java index 2e29b5792cf..9988e29aa19 100644 --- a/infra/prism/src/main/java/com/evolveum/midpoint/prism/PrismObjectDefinitionImpl.java +++ b/infra/prism/src/main/java/com/evolveum/midpoint/prism/PrismObjectDefinitionImpl.java @@ -123,7 +123,13 @@ public void setExtensionDefinition(ComplexTypeDefinition extensionComplexTypeDef this.complexTypeDefinition = newCtd; } - + + @Override + public PrismObjectValue createValue() { + return new PrismObjectValue<>(prismContext); + } + + private QName getExtensionQName() { String namespace = getName().getNamespaceURI(); return new QName(namespace, PrismConstants.EXTENSION_LOCAL_NAME); diff --git a/infra/prism/src/main/java/com/evolveum/midpoint/prism/PrismObjectValue.java b/infra/prism/src/main/java/com/evolveum/midpoint/prism/PrismObjectValue.java new file mode 100644 index 00000000000..705f883ecf1 --- /dev/null +++ b/infra/prism/src/main/java/com/evolveum/midpoint/prism/PrismObjectValue.java @@ -0,0 +1,169 @@ +/* + * Copyright (c) 2010-2016 Evolveum + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.evolveum.midpoint.prism; + +import com.evolveum.midpoint.prism.polystring.PolyString; +import com.evolveum.midpoint.util.exception.SchemaException; +import org.apache.commons.lang3.StringUtils; +import org.jetbrains.annotations.NotNull; + +import javax.xml.namespace.QName; +import java.util.Objects; + +/** + * Extension of PrismContainerValue that holds object-specific data (OID and version). + * It was created to make methods returning/accepting ItemValue universally usable; + * not losing OID/version data when object values are passed via such interfaces. + * + * This value is to be held by PrismObject. And such object should hold exactly one + * PrismObjectValue. + * + * @author mederly + */ +public class PrismObjectValue extends PrismContainerValue { + + protected String oid; + protected String version; + + public PrismObjectValue() { + } + + public PrismObjectValue(PrismContext prismContext) { + super(prismContext); + } + + public PrismObjectValue(O objectable) { + super(objectable); + } + + public PrismObjectValue(O objectable, PrismContext prismContext) { + super(objectable, prismContext); + } + + private PrismObjectValue(OriginType type, Objectable source, PrismContainerable container, Long id, + ComplexTypeDefinition complexTypeDefinition, PrismContext prismContext, String oid, String version) { + super(type, source, container, id, complexTypeDefinition, prismContext); + this.oid = oid; + this.version = version; + } + + public String getOid() { + return oid; + } + + public void setOid(String oid) { + checkMutability(); + this.oid = oid; + } + + public String getVersion() { + return version; + } + + public void setVersion(String version) { + checkMutability(); + this.version = version; + } + + public O asObjectable() { + return asContainerable(); + } + + public PrismObject asPrismObject() { + return asObjectable().asPrismObject(); + } + + public PolyString getName() { + return asPrismObject().getName(); + } + + public PrismContainer getExtension() { + return asPrismObject().getExtension(); + } + + @Override + public PrismObjectValue clone() { + PrismObjectValue clone = new PrismObjectValue( + getOriginType(), getOriginObject(), getParent(), getId(), null, this.prismContext, oid, version); + copyValues(clone); + return clone; + } + + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (!(o instanceof PrismObjectValue)) + return false; + if (!super.equals(o)) + return false; + PrismObjectValue that = (PrismObjectValue) o; + return Objects.equals(oid, that.oid); + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), oid); + } + + @Override + public boolean equivalent(PrismContainerValue other) { + if (!(other instanceof PrismObjectValue)) { + return false; + } + PrismObjectValue otherPov = (PrismObjectValue) other; + return StringUtils.equals(oid, otherPov.oid) && super.equivalent(other); + } + + @Override + public String toString() { + // we don't delegate to PrismObject, because various exceptions during that process could in turn call this method + StringBuilder sb = new StringBuilder(); + sb.append("POV:"); + if (getParent() != null) { + sb.append(getParent().getElementName().getLocalPart()).append(":"); + } else if (getComplexTypeDefinition() != null) { + sb.append(getComplexTypeDefinition().getTypeName().getLocalPart()).append(":"); + } + sb.append(oid).append("("); + PrismProperty nameProperty = findProperty(new QName(PrismConstants.NAME_LOCAL_NAME)); + sb.append(nameProperty != null ? nameProperty.getRealValue() : null); + sb.append(")"); + return sb.toString(); + } + + @Override + protected void detailedDebugDumpStart(StringBuilder sb) { + sb.append("POV").append(": "); + } + + @Override + protected void debugDumpIdentifiers(StringBuilder sb) { + sb.append("oid=").append(oid); + sb.append(", version=").append(version); + } + + @Override + public String toHumanReadableString() { + return "oid="+oid+": "+items.size()+" items"; + } + + @Override + public PrismContainer asSingleValuedContainer(@NotNull QName itemName) throws SchemaException { + throw new UnsupportedOperationException("Not supported for PrismObjectValue yet."); + } +} diff --git a/infra/prism/src/main/java/com/evolveum/midpoint/prism/PrismReference.java b/infra/prism/src/main/java/com/evolveum/midpoint/prism/PrismReference.java index cecf365e347..a71943fa67e 100644 --- a/infra/prism/src/main/java/com/evolveum/midpoint/prism/PrismReference.java +++ b/infra/prism/src/main/java/com/evolveum/midpoint/prism/PrismReference.java @@ -28,6 +28,7 @@ import com.evolveum.midpoint.util.DebugUtil; import com.evolveum.midpoint.util.MiscUtil; import com.evolveum.midpoint.util.PrettyPrinter; +import org.jetbrains.annotations.NotNull; /** * Object Reference is a property that describes reference to an object. It is @@ -112,7 +113,7 @@ public Collection getRealValues() { } - public boolean add(PrismReferenceValue value) { + public boolean add(@NotNull PrismReferenceValue value) { value.setParent(this); return getValues().add(value); } diff --git a/infra/prism/src/main/java/com/evolveum/midpoint/prism/marshaller/PrismMarshaller.java b/infra/prism/src/main/java/com/evolveum/midpoint/prism/marshaller/PrismMarshaller.java index b6f5ae9d941..216dacbb6a9 100644 --- a/infra/prism/src/main/java/com/evolveum/midpoint/prism/marshaller/PrismMarshaller.java +++ b/infra/prism/src/main/java/com/evolveum/midpoint/prism/marshaller/PrismMarshaller.java @@ -118,9 +118,7 @@ RootXNode marshalAnyData(@NotNull Object object, QName itemName, ItemDefinition return marshalItemAsRoot((Item) object, itemName, itemDefinition, context); } Validate.notNull(itemName, "itemName must be specified for non-Item objects"); - if (object instanceof Objectable) { - return marshalItemAsRoot(((Objectable) object).asPrismObject(), itemName, null, context); - } else if (object instanceof Containerable) { + if (object instanceof Containerable) { return marshalPrismValueAsRoot(((Containerable) object).asPrismContainerValue(), itemName, null, context); } else if (beanMarshaller.canProcess(object.getClass())) { XNode valueNode = beanMarshaller.marshall(object, context); // TODO item definition! @@ -165,9 +163,7 @@ public boolean canSerialize(Object object) { @NotNull private XNode marshalItemContent(@NotNull Item item, ItemDefinition itemDefinition, SerializationContext context) throws SchemaException { - if (item instanceof PrismObject) { - return marshalObjectContent((PrismObject) item, (PrismObjectDefinition) itemDefinition, context); - } else if (item.size() == 1) { + if (item.size() == 1) { return marshalItemValue(item.getValue(0), itemDefinition, null, context); } else { ListXNode xlist = new ListXNode(); @@ -181,12 +177,6 @@ private XNode marshalItemContent(@NotNull Item item, @NotNull private MapXNode marshalObjectContent(@NotNull PrismObject object, @NotNull PrismObjectDefinition objectDefinition, SerializationContext ctx) throws SchemaException { MapXNode xmap = new MapXNode(); - if (object.getOid() != null) { - xmap.put(XNode.KEY_OID, createPrimitiveXNodeStringAttr(object.getOid())); - } - if (object.getVersion() != null) { - xmap.put(XNode.KEY_VERSION, createPrimitiveXNodeStringAttr(object.getVersion())); - } marshalContainerValue(xmap, object.getValue(), objectDefinition, ctx); xmap.setTypeQName(objectDefinition.getTypeName()); // object should have the definition (?) return xmap; @@ -250,6 +240,16 @@ private void marshalContainerValue(MapXNode xmap, Pris if (id != null) { xmap.put(XNode.KEY_CONTAINER_ID, createPrimitiveXNodeAttr(id, DOMUtil.XSD_LONG)); } + if (containerVal instanceof PrismObjectValue) { + PrismObjectValue objectVal = (PrismObjectValue) containerVal; + if (objectVal.getOid() != null) { + xmap.put(XNode.KEY_OID, createPrimitiveXNodeStringAttr(objectVal.getOid())); + } + if (objectVal.getVersion() != null) { + xmap.put(XNode.KEY_VERSION, createPrimitiveXNodeStringAttr(objectVal.getVersion())); + } + } + // We put the explicit type name only if it's different from the parent one // (assuming this value is NOT serialized as a standalone one: in that case its // type must be marshaled in a special way). diff --git a/infra/prism/src/main/java/com/evolveum/midpoint/prism/marshaller/PrismUnmarshaller.java b/infra/prism/src/main/java/com/evolveum/midpoint/prism/marshaller/PrismUnmarshaller.java index 9f3c01204ed..7eb06e10c3b 100644 --- a/infra/prism/src/main/java/com/evolveum/midpoint/prism/marshaller/PrismUnmarshaller.java +++ b/infra/prism/src/main/java/com/evolveum/midpoint/prism/marshaller/PrismUnmarshaller.java @@ -233,17 +233,23 @@ private PrismContainerValue parseContainerValueFrom @NotNull PrismContainerDefinition containerDef, @NotNull ParsingContext pc) throws SchemaException { Long id = getContainerId(map); - // override container definition, if explicit type is specified ComplexTypeDefinition complexTypeDefinition = containerDef.getComplexTypeDefinition(); - if (map.getTypeQName() != null) { - ComplexTypeDefinition specificDef = getSchemaRegistry().findComplexTypeDefinitionByType(map.getTypeQName()); - if (specificDef != null) { - complexTypeDefinition = specificDef; - } else { - pc.warnOrThrow(LOGGER, "Unknown type " + map.getTypeQName() + " in " + map); + + PrismContainerValue cval; + if (containerDef instanceof PrismObjectDefinition) { + cval = ((PrismObjectDefinition) containerDef).createValue(); + } else { + // override container definition, if explicit type is specified + if (map.getTypeQName() != null) { + ComplexTypeDefinition specificDef = getSchemaRegistry().findComplexTypeDefinitionByType(map.getTypeQName()); + if (specificDef != null) { + complexTypeDefinition = specificDef; + } else { + pc.warnOrThrow(LOGGER, "Unknown type " + map.getTypeQName() + " in " + map); + } } + cval = new PrismContainerValue<>(null, null, null, id, complexTypeDefinition, prismContext); } - PrismContainerValue cval = new PrismContainerValue<>(null, null, null, id, complexTypeDefinition, prismContext); for (Entry entry : map.entrySet()) { QName itemName = entry.getKey(); if (itemName == null) { diff --git a/model/model-api/src/main/java/com/evolveum/midpoint/model/api/ScriptExecutionResult.java b/model/model-api/src/main/java/com/evolveum/midpoint/model/api/ScriptExecutionResult.java index aa22be4529c..c8ec483454d 100644 --- a/model/model-api/src/main/java/com/evolveum/midpoint/model/api/ScriptExecutionResult.java +++ b/model/model-api/src/main/java/com/evolveum/midpoint/model/api/ScriptExecutionResult.java @@ -17,6 +17,7 @@ package com.evolveum.midpoint.model.api; import com.evolveum.midpoint.prism.Item; +import com.evolveum.midpoint.prism.PrismValue; import java.util.ArrayList; import java.util.Collections; @@ -30,9 +31,9 @@ public class ScriptExecutionResult { private String consoleOutput; - private List dataOutput; // unmodifiable + always non-null + private List dataOutput; // unmodifiable + always non-null - public ScriptExecutionResult(String consoleOutput, List dataOutput) { + public ScriptExecutionResult(String consoleOutput, List dataOutput) { this.consoleOutput = consoleOutput; if (dataOutput == null) { dataOutput = new ArrayList<>(); @@ -44,7 +45,7 @@ public String getConsoleOutput() { return consoleOutput; } - public List getDataOutput() { + public List getDataOutput() { return dataOutput; } } diff --git a/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/ModelRestService.java b/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/ModelRestService.java index de84e8ddabe..790565db466 100644 --- a/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/ModelRestService.java +++ b/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/ModelRestService.java @@ -43,10 +43,12 @@ import com.evolveum.midpoint.model.api.validator.ValidationResult; import com.evolveum.midpoint.model.impl.util.RestServiceUtil; import com.evolveum.midpoint.prism.Item; +import com.evolveum.midpoint.prism.PrismValue; import com.evolveum.midpoint.prism.path.ItemPath; import com.evolveum.midpoint.prism.query.builder.QueryBuilder; import com.evolveum.midpoint.schema.GetOperationOptions; import com.evolveum.midpoint.schema.SelectorOptions; +import com.evolveum.midpoint.schema.constants.SchemaConstants; import com.evolveum.midpoint.task.api.TaskManager; import com.evolveum.midpoint.util.exception.ObjectNotFoundException; import com.evolveum.midpoint.util.exception.SchemaException; @@ -715,11 +717,11 @@ public Response executeScript(ScriptingExpressionType scr return response; } - private ItemListType prepareXmlData(List output) throws JAXBException, SchemaException { + private ItemListType prepareXmlData(List output) throws JAXBException, SchemaException { ItemListType itemListType = new ItemListType(); if (output != null) { - for (Item item : output) { - RawType rawType = new RawType(prismContext.xnodeSerializer().serialize(item), prismContext); + for (PrismValue value : output) { + RawType rawType = new RawType(prismContext.xnodeSerializer().root(SchemaConstants.C_VALUE).serialize(value), prismContext); itemListType.getItem().add(rawType); } } diff --git a/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/ModelWebService.java b/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/ModelWebService.java index 8618970f3fd..c3f532ffe25 100644 --- a/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/ModelWebService.java +++ b/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/ModelWebService.java @@ -17,10 +17,7 @@ import com.evolveum.midpoint.model.api.*; import com.evolveum.midpoint.model.common.util.AbstractModelWebService; -import com.evolveum.midpoint.prism.Item; -import com.evolveum.midpoint.prism.PrismContext; -import com.evolveum.midpoint.prism.PrismObject; -import com.evolveum.midpoint.prism.PrismProperty; +import com.evolveum.midpoint.prism.*; import com.evolveum.midpoint.prism.delta.ObjectDelta; import com.evolveum.midpoint.prism.query.ObjectQuery; import com.evolveum.midpoint.prism.query.QueryJaxbConvertor; @@ -281,11 +278,11 @@ private ExecuteScriptsResponseType doExecuteScripts(List> scripts return response; } - private ItemListType prepareXmlData(List output) throws JAXBException, SchemaException { + private ItemListType prepareXmlData(List output) throws JAXBException, SchemaException { ItemListType itemListType = new ItemListType(); if (output != null) { - for (Item item : output) { - RawType rawType = new RawType(prismContext.xnodeSerializer().serialize(item), prismContext); + for (PrismValue value: output) { + RawType rawType = new RawType(prismContext.xnodeSerializer().root(SchemaConstants.C_VALUE).serialize(value), prismContext); itemListType.getItem().add(rawType); } } diff --git a/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/Data.java b/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/Data.java index 36c0fcd91d3..36bdd17c9f1 100644 --- a/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/Data.java +++ b/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/Data.java @@ -35,26 +35,20 @@ /** * Data that are passed between individual scripting actions. * - * The content passed between actions (expressions) is a list of prism items - * (object, container, reference, property) - possibly multivalued. They are - * expected to be of the same type. - * - * We insist on the data being prism Items in order to have its - * definition (including data type and name) in place. + * The content passed between actions (expressions) is a list of prism values + * (object, container, reference, property). * * @author mederly */ public class Data implements DebugDumpable { - private static final QName PLAIN_STRING_ELEMENT_NAME = new QName(SchemaConstants.NS_C, "string"); - - private final List data = new ArrayList<>(); // all items are not null + private final List data = new ArrayList<>(); // all items are not null // we want clients to use explicit constructors private Data() { } - public List getData() { + public List getData() { return data; } @@ -78,22 +72,29 @@ public static Data createEmpty() { return new Data(); } - public void addAllFrom(Data data) { - if (data != null) { - for (Item item : data.getData()) { - addItem(item); - } + public void addAllFrom(Data otherData) { + if (otherData != null) { + data.addAll(otherData.getData()); } } - public void addItem(@NotNull Item item) { - data.add(item); + @Deprecated + public void addItem(@NotNull Item item) { + data.addAll(item.getValues()); + } + + public void addValue(PrismValue value) { + data.add(value); + } + + public void addValues(List values) { + data.addAll(values); } public String getDataAsSingleString() throws ScriptExecutionException { if (!data.isEmpty()) { if (data.size() == 1) { - return (String) ((PrismProperty) data.get(0)).getRealValue(); // todo implement some diagnostics when this would fail + return (String) ((PrismPropertyValue) data.get(0)).getRealValue(); // todo implement some diagnostics when this would fail } else { throw new ScriptExecutionException("Multiple values where just one is expected"); } @@ -103,79 +104,82 @@ public String getDataAsSingleString() throws ScriptExecutionException { } public static Data createItem(PrismValue value, PrismContext prismContext) throws SchemaException { - // TODO fix this temporary solution (haven't we somewhere universal method to do this?) - if (value instanceof PrismReferenceValue) { - PrismReference ref = new PrismReference(new QName("reference")); - ref.add((PrismReferenceValue) value); - return create(ref); - } else if (value instanceof PrismContainerValue) { - PrismContainerValue pcv = (PrismContainerValue) value; - return create(pcv.asSingleValuedContainer(new QName("container"))); - } else if (value instanceof PrismPropertyValue) { - if (value.isRaw()) { - throw new IllegalArgumentException("Value cannot be raw at this point: " + value); - } - Class clazz = value.getRealClass(); - assert clazz != null; - PrismPropertyDefinition propertyDefinition; - List defs = prismContext.getSchemaRegistry() - .findItemDefinitionsByCompileTimeClass(clazz, PrismPropertyDefinition.class); - if (defs.size() == 1) { - propertyDefinition = defs.get(0); - } else if (String.class.isAssignableFrom(clazz)) { - propertyDefinition = new PrismPropertyDefinitionImpl<>(PLAIN_STRING_ELEMENT_NAME, DOMUtil.XSD_STRING, prismContext); - } else if (ObjectDeltaType.class.isAssignableFrom(clazz)) { - propertyDefinition = new PrismPropertyDefinitionImpl<>(SchemaConstants.T_OBJECT_DELTA, SchemaConstants.T_OBJECT_DELTA_TYPE, prismContext); - } else if (EventHandlerType.class.isAssignableFrom(clazz)) { - propertyDefinition = new PrismPropertyDefinitionImpl<>(SchemaConstants.C_EVENT_HANDLER, EventHandlerType.COMPLEX_TYPE, prismContext); - } else { - // maybe determine type from class would be sufficient - TypeDefinition td = prismContext.getSchemaRegistry().findTypeDefinitionByCompileTimeClass(clazz, TypeDefinition.class); - if (td != null) { - propertyDefinition = new PrismPropertyDefinitionImpl<>(SchemaConstants.C_VALUE, td.getTypeName(), prismContext); - } else { - throw new IllegalStateException( - "Unsupported data class (to be put into scripting data as property): " + clazz); - } - } - PrismProperty property = propertyDefinition.instantiate(); - property.add((PrismPropertyValue) value); - return create(property); - } else if (value == null) { - return createEmpty(); - } else { - throw new IllegalArgumentException("Unsupported prism value: " + value); + Data data = createEmpty(); + if (value != null) { + data.addValue(value); } + return data; +// // TODO fix this temporary solution (haven't we somewhere universal method to do this?) +// if (value instanceof PrismReferenceValue) { +// PrismReference ref = new PrismReference(new QName("reference")); +// ref.add((PrismReferenceValue) value); +// return create(ref); +// } else if (value instanceof PrismContainerValue) { +// PrismContainerValue pcv = (PrismContainerValue) value; +// return create(pcv.asSingleValuedContainer(new QName("container"))); +// } else if (value instanceof PrismPropertyValue) { +// if (value.isRaw()) { +// throw new IllegalArgumentException("Value cannot be raw at this point: " + value); +// } +// Class clazz = value.getRealClass(); +// assert clazz != null; +// PrismPropertyDefinition propertyDefinition; +// List defs = prismContext.getSchemaRegistry() +// .findItemDefinitionsByCompileTimeClass(clazz, PrismPropertyDefinition.class); +// if (defs.size() == 1) { +// propertyDefinition = defs.get(0); +// } else if (String.class.isAssignableFrom(clazz)) { +// propertyDefinition = new PrismPropertyDefinitionImpl<>(PLAIN_STRING_ELEMENT_NAME, DOMUtil.XSD_STRING, prismContext); +// } else if (ObjectDeltaType.class.isAssignableFrom(clazz)) { +// propertyDefinition = new PrismPropertyDefinitionImpl<>(SchemaConstants.T_OBJECT_DELTA, SchemaConstants.T_OBJECT_DELTA_TYPE, prismContext); +// } else if (EventHandlerType.class.isAssignableFrom(clazz)) { +// propertyDefinition = new PrismPropertyDefinitionImpl<>(SchemaConstants.C_EVENT_HANDLER, EventHandlerType.COMPLEX_TYPE, prismContext); +// } else { +// // maybe determine type from class would be sufficient +// TypeDefinition td = prismContext.getSchemaRegistry().findTypeDefinitionByCompileTimeClass(clazz, TypeDefinition.class); +// if (td != null) { +// propertyDefinition = new PrismPropertyDefinitionImpl<>(SchemaConstants.C_VALUE, td.getTypeName(), prismContext); +// } else { +// throw new IllegalStateException( +// "Unsupported data class (to be put into scripting data as property): " + clazz); +// } +// } +// PrismProperty property = propertyDefinition.instantiate(); +// property.add((PrismPropertyValue) value); +// return create(property); +// } else if (value == null) { +// return createEmpty(); +// } else { +// throw new IllegalArgumentException("Unsupported prism value: " + value); +// } } public Collection getDataAsReferences(QName defaultTargetType) throws ScriptExecutionException { Collection retval = new ArrayList<>(data.size()); - for (Item item : data) { - if (item instanceof PrismObject) { + for (PrismValue value : data) { + if (value instanceof PrismObjectValue) { + PrismObjectValue objectValue = (PrismObjectValue) value; ObjectReferenceType ref = new ObjectReferenceType(); - ref.setType(item.getDefinition().getTypeName()); // todo check the definition is present - ref.setOid(((PrismObject) item).getOid()); // todo check if oid is present + ref.setType(objectValue.asPrismObject().getDefinition().getTypeName()); // todo check the definition is present + ref.setOid(objectValue.getOid()); // todo check if oid is present retval.add(ref); - } else if (item instanceof PrismProperty) { - for (Object value : item.getRealValues()) { - if (value instanceof String) { - ObjectReferenceType ref = new ObjectReferenceType(); - ref.setType(defaultTargetType); - ref.setOid((String) value); // todo implement search by name - retval.add(ref); - } else if (value instanceof ObjectReferenceType) { - retval.add((ObjectReferenceType) value); - } else { - throw new ScriptExecutionException("Unsupported reference type: " + value.getClass()); - } - } - } else if (item instanceof PrismReference) { - PrismReference reference = (PrismReference) item; - for (PrismReferenceValue value : reference.getValues()) { + } else if (value instanceof PrismPropertyValue) { + Object realValue = ((PrismPropertyValue) value).getRealValue(); + if (realValue instanceof String) { ObjectReferenceType ref = new ObjectReferenceType(); - ref.setupReferenceValue(value); + ref.setType(defaultTargetType); + ref.setOid((String) realValue); // todo implement search by name retval.add(ref); + } else if (realValue instanceof ObjectReferenceType) { + retval.add((ObjectReferenceType) realValue); + } else { + throw new ScriptExecutionException("Unsupported reference type: " + value.getClass()); } + } else if (value instanceof PrismReferenceValue) { + PrismReferenceValue referenceValue = (PrismReferenceValue) value; + ObjectReferenceType ref = new ObjectReferenceType(); + ref.setupReferenceValue(referenceValue); + retval.add(ref); } } return retval; diff --git a/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/ExecutionContext.java b/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/ExecutionContext.java index 35810380c2e..b28c00da3aa 100644 --- a/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/ExecutionContext.java +++ b/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/ExecutionContext.java @@ -18,6 +18,7 @@ import com.evolveum.midpoint.model.api.ScriptExecutionResult; import com.evolveum.midpoint.prism.Item; +import com.evolveum.midpoint.prism.PrismValue; import com.evolveum.midpoint.task.api.Task; import com.evolveum.midpoint.util.logging.Trace; import com.evolveum.midpoint.util.logging.TraceManager; @@ -79,7 +80,7 @@ public void setFinalOutput(Data finalOutput) { } public ScriptExecutionResult toExecutionResult() { - List items = null; + List items = null; if (getFinalOutput() != null) { items = getFinalOutput().getData(); } diff --git a/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/AddExecutor.java b/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/AddExecutor.java index 4751bdb81fd..8aa1cfb34af 100644 --- a/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/AddExecutor.java +++ b/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/AddExecutor.java @@ -16,19 +16,19 @@ package com.evolveum.midpoint.model.impl.scripting.actions; +import com.evolveum.midpoint.model.api.ScriptExecutionException; import com.evolveum.midpoint.model.impl.scripting.Data; import com.evolveum.midpoint.model.impl.scripting.ExecutionContext; -import com.evolveum.midpoint.model.api.ScriptExecutionException; import com.evolveum.midpoint.model.impl.scripting.helpers.OperationsHelper; -import com.evolveum.midpoint.prism.Item; import com.evolveum.midpoint.prism.PrismObject; +import com.evolveum.midpoint.prism.PrismObjectValue; +import com.evolveum.midpoint.prism.PrismValue; import com.evolveum.midpoint.prism.delta.ObjectDelta; import com.evolveum.midpoint.schema.result.OperationResult; import com.evolveum.midpoint.util.logging.Trace; import com.evolveum.midpoint.util.logging.TraceManager; import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType; import com.evolveum.midpoint.xml.ns._public.model.scripting_3.ActionExpressionType; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -58,9 +58,9 @@ public Data execute(ActionExpressionType expression, Data input, ExecutionContex boolean raw = getParamRaw(expression, input, context, result); boolean dryRun = getParamDryRun(expression, input, context, result); - for (Item item : input.getData()) { - if (item instanceof PrismObject) { - PrismObject prismObject = (PrismObject) item; + for (PrismValue value : input.getData()) { + if (value instanceof PrismObjectValue) { + PrismObject prismObject = ((PrismObjectValue) value).asPrismObject(); ObjectType objectType = prismObject.asObjectable(); long started = operationsHelper.recordStart(context, objectType); try { @@ -70,9 +70,9 @@ public Data execute(ActionExpressionType expression, Data input, ExecutionContex operationsHelper.recordEnd(context, objectType, started, ex); throw ex; // TODO think about this } - context.println("Added " + item.toString() + rawDrySuffix(raw, dryRun)); + context.println("Added " + prismObject.toString() + rawDrySuffix(raw, dryRun)); } else { - throw new ScriptExecutionException("Item couldn't be added, because it is not a PrismObject: " + item.toString()); + throw new ScriptExecutionException("Item couldn't be added, because it is not a PrismObject: " + value.toString()); } } return Data.createEmpty(); // todo return oid(s) in the future diff --git a/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/AssignExecutor.java b/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/AssignExecutor.java index 4e4880e1722..bda5a565ab5 100644 --- a/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/AssignExecutor.java +++ b/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/AssignExecutor.java @@ -19,8 +19,7 @@ import com.evolveum.midpoint.model.impl.scripting.Data; import com.evolveum.midpoint.model.impl.scripting.ExecutionContext; import com.evolveum.midpoint.model.api.ScriptExecutionException; -import com.evolveum.midpoint.prism.Item; -import com.evolveum.midpoint.prism.PrismObject; +import com.evolveum.midpoint.prism.*; import com.evolveum.midpoint.prism.delta.ObjectDelta; import com.evolveum.midpoint.schema.result.OperationResult; import com.evolveum.midpoint.util.exception.SchemaException; @@ -90,9 +89,9 @@ public Data execute(ActionExpressionType expression, Data input, ExecutionContex throw new ScriptExecutionException("Nothing to assign: neither resource nor role specified"); } - for (Item item : input.getData()) { - if (item instanceof PrismObject && ((PrismObject) item).asObjectable() instanceof FocusType) { - PrismObject prismObject = (PrismObject) item; + for (PrismValue value : input.getData()) { + if (value instanceof PrismObjectValue && ((PrismObjectValue) value).asObjectable() instanceof FocusType) { + PrismObject prismObject = ((PrismObjectValue) value).asPrismObject(); ObjectType objectType = prismObject.asObjectable(); long started = operationsHelper.recordStart(context, objectType); try { @@ -102,9 +101,9 @@ public Data execute(ActionExpressionType expression, Data input, ExecutionContex operationsHelper.recordEnd(context, objectType, started, ex); throw ex; // TODO reconsider this } - context.println("Modified " + item.toString() + rawDrySuffix(raw, dryRun)); + context.println("Modified " + prismObject.toString() + rawDrySuffix(raw, dryRun)); } else { - throw new ScriptExecutionException("Item could not be modified, because it is not a PrismObject of FocusType: " + item.toString()); + throw new ScriptExecutionException("Item could not be modified, because it is not a PrismObject of FocusType: " + value.toString()); } } return Data.createEmpty(); diff --git a/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/DeleteExecutor.java b/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/DeleteExecutor.java index 9e7ba9e7968..cf718203a8d 100644 --- a/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/DeleteExecutor.java +++ b/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/DeleteExecutor.java @@ -22,6 +22,8 @@ import com.evolveum.midpoint.model.impl.scripting.helpers.OperationsHelper; import com.evolveum.midpoint.prism.Item; import com.evolveum.midpoint.prism.PrismObject; +import com.evolveum.midpoint.prism.PrismObjectValue; +import com.evolveum.midpoint.prism.PrismValue; import com.evolveum.midpoint.prism.delta.ObjectDelta; import com.evolveum.midpoint.schema.result.OperationResult; import com.evolveum.midpoint.util.logging.Trace; @@ -58,9 +60,9 @@ public Data execute(ActionExpressionType expression, Data input, ExecutionContex boolean raw = getParamRaw(expression, input, context, result); boolean dryRun = getParamDryRun(expression, input, context, result); - for (Item item : input.getData()) { - if (item instanceof PrismObject) { - PrismObject prismObject = (PrismObject) item; + for (PrismValue item : input.getData()) { + if (item instanceof PrismObjectValue) { + PrismObject prismObject = ((PrismObjectValue) item).asPrismObject(); ObjectType objectType = prismObject.asObjectable(); long started = operationsHelper.recordStart(context, objectType); try { @@ -70,7 +72,7 @@ public Data execute(ActionExpressionType expression, Data input, ExecutionContex operationsHelper.recordEnd(context, objectType, started, ex); throw ex; // TODO think about this } - context.println("Deleted " + item.toString() + rawDrySuffix(raw, dryRun)); + context.println("Deleted " + prismObject.toString() + rawDrySuffix(raw, dryRun)); } else { throw new ScriptExecutionException("Item couldn't be deleted, because it is not a PrismObject: " + item.toString()); } diff --git a/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/DiscoverConnectorsExecutor.java b/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/DiscoverConnectorsExecutor.java index 31662da74d5..0bbf1dd836f 100644 --- a/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/DiscoverConnectorsExecutor.java +++ b/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/DiscoverConnectorsExecutor.java @@ -19,8 +19,7 @@ import com.evolveum.midpoint.model.impl.scripting.Data; import com.evolveum.midpoint.model.impl.scripting.ExecutionContext; import com.evolveum.midpoint.model.api.ScriptExecutionException; -import com.evolveum.midpoint.prism.Item; -import com.evolveum.midpoint.prism.PrismObject; +import com.evolveum.midpoint.prism.*; import com.evolveum.midpoint.prism.delta.ObjectDelta; import com.evolveum.midpoint.prism.delta.ReferenceDelta; import com.evolveum.midpoint.prism.query.AndFilter; @@ -75,9 +74,9 @@ public Data execute(ActionExpressionType expression, Data input, ExecutionContex Data output = Data.createEmpty(); - for (Item item : input.getData()) { - if (item instanceof PrismObject && ((PrismObject) item).asObjectable() instanceof ConnectorHostType) { - PrismObject connectorHostTypePrismObject = (PrismObject) item; + for (PrismValue value: input.getData()) { + if (value instanceof PrismObjectValue && ((PrismObjectValue) value).asObjectable() instanceof ConnectorHostType) { + PrismObject connectorHostTypePrismObject = ((PrismObjectValue) value).asPrismObject(); Set newConnectors; long started = operationsHelper.recordStart(context, connectorHostTypePrismObject.asObjectable()); try { @@ -95,7 +94,7 @@ public Data execute(ActionExpressionType expression, Data input, ExecutionContex rebindConnectors(newConnectors, context, result); } } else { - throw new ScriptExecutionException("Input is not a PrismObject: " + item.toString()); + throw new ScriptExecutionException("Input is not a PrismObject: " + value.toString()); } } return output; diff --git a/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/EnableDisableExecutor.java b/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/EnableDisableExecutor.java index 09a96343164..1312780d067 100644 --- a/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/EnableDisableExecutor.java +++ b/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/EnableDisableExecutor.java @@ -22,6 +22,8 @@ import com.evolveum.midpoint.model.impl.scripting.helpers.OperationsHelper; import com.evolveum.midpoint.prism.Item; import com.evolveum.midpoint.prism.PrismObject; +import com.evolveum.midpoint.prism.PrismObjectValue; +import com.evolveum.midpoint.prism.PrismValue; import com.evolveum.midpoint.prism.delta.ObjectDelta; import com.evolveum.midpoint.prism.path.ItemPath; import com.evolveum.midpoint.schema.result.OperationResult; @@ -66,20 +68,20 @@ public Data execute(ActionExpressionType expression, Data input, ExecutionContex boolean raw = getParamRaw(expression, input, context, result); boolean dryRun = getParamDryRun(expression, input, context, result); - for (Item item : input.getData()) { - if (item instanceof PrismObject) { - PrismObject prismObject = (PrismObject) item; + for (PrismValue value : input.getData()) { + if (value instanceof PrismObjectValue) { + PrismObject prismObject = ((PrismObjectValue) value).asPrismObject(); ObjectType objectType = prismObject.asObjectable(); long started = operationsHelper.recordStart(context, objectType); try { if (objectType instanceof FocusType) { operationsHelper.applyDelta(createEnableDisableDelta((FocusType) objectType, isEnable), operationsHelper.createExecutionOptions(raw), dryRun, context, result); - context.println((isEnable ? "Enabled " : "Disabled ") + item.toString() + rawDrySuffix(raw, dryRun)); + context.println((isEnable ? "Enabled " : "Disabled ") + prismObject.toString() + rawDrySuffix(raw, dryRun)); } else if (objectType instanceof ShadowType) { operationsHelper.applyDelta(createEnableDisableDelta((ShadowType) objectType, isEnable), context, result); - context.println((isEnable ? "Enabled " : "Disabled ") + item.toString() + rawDrySuffix(raw, dryRun)); + context.println((isEnable ? "Enabled " : "Disabled ") + prismObject.toString() + rawDrySuffix(raw, dryRun)); } else { - throw new ScriptExecutionException("Item could not be enabled/disabled, because it is not a FocusType nor ShadowType: " + item.toString()); + throw new ScriptExecutionException("Item could not be enabled/disabled, because it is not a FocusType nor ShadowType: " + value.toString()); } operationsHelper.recordEnd(context, objectType, started, null); } catch (Throwable ex) { @@ -87,7 +89,7 @@ public Data execute(ActionExpressionType expression, Data input, ExecutionContex throw ex; } } else { - throw new ScriptExecutionException("Item could not be enabled/disabled, because it is not a PrismObject: " + item.toString()); + throw new ScriptExecutionException("Item could not be enabled/disabled, because it is not a PrismObject: " + value.toString()); } } return Data.createEmpty(); diff --git a/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/ModifyExecutor.java b/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/ModifyExecutor.java index dc9c33749f7..17f2af8d329 100644 --- a/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/ModifyExecutor.java +++ b/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/ModifyExecutor.java @@ -19,9 +19,7 @@ import com.evolveum.midpoint.model.impl.scripting.Data; import com.evolveum.midpoint.model.impl.scripting.ExecutionContext; import com.evolveum.midpoint.model.api.ScriptExecutionException; -import com.evolveum.midpoint.prism.Item; -import com.evolveum.midpoint.prism.PrismObject; -import com.evolveum.midpoint.prism.PrismProperty; +import com.evolveum.midpoint.prism.*; import com.evolveum.midpoint.prism.delta.ObjectDelta; import com.evolveum.midpoint.schema.DeltaConvertor; import com.evolveum.midpoint.schema.result.OperationResult; @@ -63,9 +61,9 @@ public Data execute(ActionExpressionType expression, Data input, ExecutionContex ActionParameterValueType deltaParameterValue = expressionHelper.getArgument(expression.getParameter(), PARAM_DELTA, true, true, NAME); Data deltaData = expressionHelper.evaluateParameter(deltaParameterValue, ObjectDeltaType.class, input, context, result); - for (Item item : input.getData()) { - if (item instanceof PrismObject) { - PrismObject prismObject = (PrismObject) item; + for (PrismValue value : input.getData()) { + if (value instanceof PrismObjectValue) { + PrismObject prismObject = ((PrismObjectValue) value).asPrismObject(); ObjectType objectType = prismObject.asObjectable(); long started = operationsHelper.recordStart(context, objectType); try { @@ -75,9 +73,9 @@ public Data execute(ActionExpressionType expression, Data input, ExecutionContex operationsHelper.recordEnd(context, objectType, started, ex); throw ex; } - context.println("Modified " + item.toString() + rawDrySuffix(raw, dryRun)); + context.println("Modified " + prismObject.toString() + rawDrySuffix(raw, dryRun)); } else { - throw new ScriptExecutionException("Item could not be modified, because it is not a PrismObject: " + item.toString()); + throw new ScriptExecutionException("Item could not be modified, because it is not a PrismObject: " + value.toString()); } } return Data.createEmpty(); @@ -87,7 +85,7 @@ private ObjectDelta createDelta(ObjectType objectType, Data deltaData) throws Sc if (deltaData.getData().size() != 1) { throw new ScriptExecutionException("Expected exactly one delta to apply, found " + deltaData.getData().size() + " instead."); } - ObjectDeltaType deltaType = ((PrismProperty) deltaData.getData().get(0)).getAnyRealValue().clone(); + ObjectDeltaType deltaType = ((PrismPropertyValue) deltaData.getData().get(0)).clone().getRealValue(); if (deltaType.getChangeType() == null) { deltaType.setChangeType(ChangeTypeType.MODIFY); } diff --git a/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/NotifyExecutor.java b/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/NotifyExecutor.java index eefc009c228..12ef3b99efa 100644 --- a/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/NotifyExecutor.java +++ b/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/NotifyExecutor.java @@ -23,6 +23,7 @@ import com.evolveum.midpoint.notifications.api.events.CustomEvent; import com.evolveum.midpoint.notifications.api.events.Event; import com.evolveum.midpoint.prism.Item; +import com.evolveum.midpoint.prism.PrismValue; import com.evolveum.midpoint.schema.result.OperationResult; import com.evolveum.midpoint.task.api.LightweightIdentifierGenerator; import com.evolveum.midpoint.util.logging.Trace; @@ -91,8 +92,8 @@ public Data execute(ActionExpressionType expression, Data input, ExecutionContex notificationManager.processEvent(event, context.getTask(), result); eventCount++; } else { - for (Item item : input.getData()) { - Event event = new CustomEvent(lightweightIdentifierGenerator, subtype, handler, item, operation, status, context.getChannel()); + for (PrismValue value : input.getData()) { + Event event = new CustomEvent(lightweightIdentifierGenerator, subtype, handler, value, operation, status, context.getChannel()); notificationManager.processEvent(event, context.getTask(), result); eventCount++; } diff --git a/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/PurgeSchemaExecutor.java b/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/PurgeSchemaExecutor.java index 16617008051..7644f59a105 100644 --- a/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/PurgeSchemaExecutor.java +++ b/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/PurgeSchemaExecutor.java @@ -20,9 +20,7 @@ import com.evolveum.midpoint.model.impl.scripting.Data; import com.evolveum.midpoint.model.impl.scripting.ExecutionContext; import com.evolveum.midpoint.model.api.ScriptExecutionException; -import com.evolveum.midpoint.prism.Item; -import com.evolveum.midpoint.prism.PrismContainer; -import com.evolveum.midpoint.prism.PrismObject; +import com.evolveum.midpoint.prism.*; import com.evolveum.midpoint.prism.delta.ObjectDelta; import com.evolveum.midpoint.schema.result.OperationResult; import com.evolveum.midpoint.util.logging.Trace; @@ -55,19 +53,19 @@ public Data execute(ActionExpressionType expression, Data input, ExecutionContex Data output = Data.createEmpty(); - for (Item item : input.getData()) { - if (item instanceof PrismObject && ((PrismObject) item).asObjectable() instanceof ResourceType) { - PrismObject resourceTypePrismObject = (PrismObject) item; + for (PrismValue value : input.getData()) { + if (value instanceof PrismObjectValue && ((PrismObjectValue) value).asObjectable() instanceof ResourceType) { + PrismObject resourceTypePrismObject = ((PrismObjectValue) value).asPrismObject(); ResourceType resourceType = resourceTypePrismObject.asObjectable(); long started = operationsHelper.recordStart(context, resourceType); ObjectDelta delta = createDelta(resourceTypePrismObject.asObjectable()); try { if (delta != null) { operationsHelper.applyDelta(delta, ModelExecuteOptions.createRaw(), context, result); - context.println("Purged schema information from " + item); + context.println("Purged schema information from " + resourceTypePrismObject); output.addItem(operationsHelper.getObject(ResourceType.class, resourceTypePrismObject.getOid(), true, context, result)); } else { - context.println("There's no schema information to be purged in " + item); + context.println("There's no schema information to be purged in " + value); output.addItem(resourceTypePrismObject); } operationsHelper.recordEnd(context, resourceType, started, null); @@ -76,7 +74,7 @@ public Data execute(ActionExpressionType expression, Data input, ExecutionContex throw ex; } } else { - throw new ScriptExecutionException("Couldn't purge resource schema, because input is not a PrismObject: " + item.toString()); + throw new ScriptExecutionException("Couldn't purge resource schema, because input is not a PrismObject: " + value.toString()); } } return output; diff --git a/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/RecomputeExecutor.java b/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/RecomputeExecutor.java index a4eadbdf90e..5a2cbb5dd24 100644 --- a/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/RecomputeExecutor.java +++ b/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/RecomputeExecutor.java @@ -24,6 +24,8 @@ import com.evolveum.midpoint.model.api.ScriptExecutionException; import com.evolveum.midpoint.prism.Item; import com.evolveum.midpoint.prism.PrismObject; +import com.evolveum.midpoint.prism.PrismObjectValue; +import com.evolveum.midpoint.prism.PrismValue; import com.evolveum.midpoint.prism.delta.ChangeType; import com.evolveum.midpoint.prism.delta.ObjectDelta; import com.evolveum.midpoint.schema.result.OperationResult; @@ -63,9 +65,9 @@ public Data execute(ActionExpressionType expression, Data input, ExecutionContex boolean dryRun = getParamDryRun(expression, input, context, result); - for (Item item : input.getData()) { - if (item instanceof PrismObject && FocusType.class.isAssignableFrom(((PrismObject) item).getCompileTimeClass())) { - PrismObject focalPrismObject = (PrismObject) item; + for (PrismValue value : input.getData()) { + if (value instanceof PrismObjectValue && FocusType.class.isAssignableFrom(((PrismObjectValue) value).asPrismObject().getCompileTimeClass())) { + PrismObject focalPrismObject = ((PrismObjectValue) value).asPrismObject(); FocusType focusType = focalPrismObject.asObjectable(); long started = operationsHelper.recordStart(context, focusType); try { @@ -80,9 +82,9 @@ public Data execute(ActionExpressionType expression, Data input, ExecutionContex operationsHelper.recordEnd(context, focusType, started, e); throw e; } - context.println("Recomputed " + item.toString() + drySuffix(dryRun)); + context.println("Recomputed " + focalPrismObject.toString() + drySuffix(dryRun)); } else { - throw new ScriptExecutionException("Item could not be recomputed, because it is not a focal PrismObject: " + item.toString()); + throw new ScriptExecutionException("Item could not be recomputed, because it is not a focal PrismObject: " + value.toString()); } } return Data.createEmpty(); diff --git a/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/ResolveExecutor.java b/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/ResolveExecutor.java index 8cce3ac5363..1c4762578be 100644 --- a/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/ResolveExecutor.java +++ b/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/ResolveExecutor.java @@ -19,10 +19,7 @@ import com.evolveum.midpoint.model.impl.scripting.Data; import com.evolveum.midpoint.model.impl.scripting.ExecutionContext; import com.evolveum.midpoint.model.api.ScriptExecutionException; -import com.evolveum.midpoint.prism.Item; -import com.evolveum.midpoint.prism.PrismObject; -import com.evolveum.midpoint.prism.PrismReference; -import com.evolveum.midpoint.prism.PrismReferenceValue; +import com.evolveum.midpoint.prism.*; import com.evolveum.midpoint.schema.result.OperationResult; import com.evolveum.midpoint.util.logging.Trace; import com.evolveum.midpoint.util.logging.TraceManager; @@ -59,24 +56,22 @@ public Data execute(ActionExpressionType expression, Data input, ExecutionContex Data output = Data.createEmpty(); - for (Item item : input.getData()) { - if (item instanceof PrismReference) { - PrismReference prismReference = (PrismReference) item; - for (PrismReferenceValue prismReferenceValue : prismReference.getValues()) { - String oid = prismReferenceValue.getOid(); - QName targetTypeQName = prismReferenceValue.getTargetType(); - if (targetTypeQName == null) { - throw new ScriptExecutionException("Couldn't resolve reference, because target type is unknown: " + prismReferenceValue); - } - Class typeClass = (Class) prismContext.getSchemaRegistry().determineCompileTimeClass(targetTypeQName); - if (typeClass == null) { - throw new ScriptExecutionException("Couldn't resolve reference, because target type class is unknown for target type " + targetTypeQName); - } - PrismObject prismObject = operationsHelper.getObject(typeClass, oid, noFetch, context, result); - output.addItem(prismObject); + for (PrismValue value : input.getData()) { + if (value instanceof PrismReferenceValue) { + PrismReferenceValue prismReferenceValue = (PrismReferenceValue) value; + String oid = prismReferenceValue.getOid(); + QName targetTypeQName = prismReferenceValue.getTargetType(); + if (targetTypeQName == null) { + throw new ScriptExecutionException("Couldn't resolve reference, because target type is unknown: " + prismReferenceValue); } + Class typeClass = (Class) prismContext.getSchemaRegistry().determineCompileTimeClass(targetTypeQName); + if (typeClass == null) { + throw new ScriptExecutionException("Couldn't resolve reference, because target type class is unknown for target type " + targetTypeQName); + } + PrismObject prismObject = operationsHelper.getObject(typeClass, oid, noFetch, context, result); + output.addValue(prismObject.getValue()); } else { - throw new ScriptExecutionException("Item could not be resolved, because it is not a PrismReference: " + item.toString()); + throw new ScriptExecutionException("Item could not be resolved, because it is not a PrismReference: " + value.toString()); } } return output; diff --git a/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/ScriptExecutor.java b/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/ScriptExecutor.java index d052fb64c10..b598306f9a7 100644 --- a/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/ScriptExecutor.java +++ b/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/ScriptExecutor.java @@ -25,10 +25,7 @@ import com.evolveum.midpoint.model.impl.scripting.Data; import com.evolveum.midpoint.model.impl.scripting.ExecutionContext; import com.evolveum.midpoint.model.impl.util.Utils; -import com.evolveum.midpoint.prism.Item; -import com.evolveum.midpoint.prism.ItemDefinition; -import com.evolveum.midpoint.prism.PrismObject; -import com.evolveum.midpoint.prism.PrismValue; +import com.evolveum.midpoint.prism.*; import com.evolveum.midpoint.schema.constants.ExpressionConstants; import com.evolveum.midpoint.schema.result.OperationResult; import com.evolveum.midpoint.util.QNameUtil; @@ -85,57 +82,54 @@ public Data execute(ActionExpressionType expression, Data input, ExecutionContex } Data output = Data.createEmpty(); - for (Item item : input.getData()) { + for (PrismValue value: input.getData()) { + String valueDescription; long started; - if (item instanceof PrismObject) { - started = operationsHelper.recordStart(context, asObjectType(item)); + if (value instanceof PrismObjectValue) { + started = operationsHelper.recordStart(context, asObjectType(value)); + valueDescription = asObjectType(value).asPrismObject().toString(); } else { started = 0; + valueDescription = value.toHumanReadableString(); } try { - Object outObject = executeScript(scriptExpression, item, context, result); - if (outObject != null && outputDefinition != null) { - output.addItem(createItem(outObject, outputDefinition)); + Object outObject = executeScript(scriptExpression, value, context, result); + if (outObject != null) { + addToData(outObject, output); } - if (item instanceof PrismObject) { - operationsHelper.recordEnd(context, asObjectType(item), started, null); + if (value instanceof PrismObjectValue) { + operationsHelper.recordEnd(context, asObjectType(value), started, null); } } catch (Throwable ex) { - if (item instanceof PrismObject) { - operationsHelper.recordEnd(context, asObjectType(item), started, ex); + if (value instanceof PrismObjectValue) { + operationsHelper.recordEnd(context, asObjectType(value), started, ex); } throw new ScriptExecutionException("Couldn't execute script action: " + ex.getMessage(), ex); } - context.println("Executed script on " + item); + context.println("Executed script on " + valueDescription); } return output; } - // UGLY HACKING - private Item createItem(Object outObject, ItemDefinition itemDefinition) throws SchemaException { - if (outObject instanceof Item) { - return (Item) outObject; // probably won't occur - } - Item item = itemDefinition.instantiate(); - if (outObject instanceof Collection) { + private void addToData(Object outObject, Data output) throws SchemaException { + if (outObject == null) { + return; + } else if (outObject instanceof Collection) { for (Object o : (Collection) outObject) { - addToItem(item, o); + addToData(o, output); } } else { - addToItem(item, outObject); - } - return item; - } - - private void addToItem(Item item, Object value) throws SchemaException { - if (value instanceof PrismValue) { - PrismValue pv = (PrismValue) value; - if (pv.getParent() instanceof PrismObject && item instanceof PrismObject) { - ((PrismObject) item).setOid(((PrismObject) (pv.getParent())).getOid()); + PrismValue value; + if (outObject instanceof PrismValue) { + value = (PrismValue) outObject; + } else if (outObject instanceof Objectable) { + value = new PrismObjectValue<>((Objectable) outObject, prismContext); + } else if (outObject instanceof Containerable) { + value = new PrismContainerValue<>((Containerable) outObject, prismContext); + } else { + value = new PrismPropertyValue<>(outObject, prismContext); } - item.add(((PrismValue) value).clone()); - } else { - throw new UnsupportedOperationException("Adding " + value + " to " + item + " is not supported yet."); + output.addValue(value); } } @@ -156,10 +150,10 @@ private ItemDefinition getItemDefinition(String itemUri) throws ScriptExecuti throw new ScriptExecutionException("Supplied item identification " + itemUri + " corresponds neither to item name nor type name"); } - private Object executeScript(ScriptExpression scriptExpression, Item inputItem, ExecutionContext context, OperationResult result) + private Object executeScript(ScriptExpression scriptExpression, PrismValue prismValue, ExecutionContext context, OperationResult result) throws ExpressionEvaluationException, ObjectNotFoundException, SchemaException { ExpressionVariables variables = new ExpressionVariables(); - variables.addVariableDefinition(ExpressionConstants.VAR_INPUT, inputItem); + variables.addVariableDefinition(ExpressionConstants.VAR_INPUT, prismValue); variables.addVariableDefinition(ExpressionConstants.VAR_PRISM_CONTEXT, prismContext); ExpressionUtil.addActorVariable(variables, securityEnforcer); @@ -174,8 +168,8 @@ private Object executeScript(ScriptExpression scriptExpression, Item inputItem, } } - private ObjectType asObjectType(Item item) { - return (ObjectType) ((PrismObject) item).asObjectable(); + private ObjectType asObjectType(PrismValue value) { + return (ObjectType) ((PrismObjectValue) value).asObjectable(); } } diff --git a/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/TestResourceExecutor.java b/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/TestResourceExecutor.java index 3335a8f6093..09a2df18c52 100644 --- a/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/TestResourceExecutor.java +++ b/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/TestResourceExecutor.java @@ -19,12 +19,9 @@ import com.evolveum.midpoint.model.impl.scripting.Data; import com.evolveum.midpoint.model.impl.scripting.ExecutionContext; import com.evolveum.midpoint.model.api.ScriptExecutionException; -import com.evolveum.midpoint.prism.Item; -import com.evolveum.midpoint.prism.PrismObject; +import com.evolveum.midpoint.prism.*; import com.evolveum.midpoint.prism.delta.ObjectDelta; import com.evolveum.midpoint.prism.delta.ReferenceDelta; -import com.evolveum.midpoint.prism.query.AndFilter; -import com.evolveum.midpoint.prism.query.EqualFilter; import com.evolveum.midpoint.prism.query.ObjectQuery; import com.evolveum.midpoint.prism.query.builder.QueryBuilder; import com.evolveum.midpoint.schema.constants.SchemaConstants; @@ -69,9 +66,9 @@ public Data execute(ActionExpressionType expression, Data input, ExecutionContex Data output = Data.createEmpty(); - for (Item item : input.getData()) { - if (item instanceof PrismObject && ((PrismObject) item).asObjectable() instanceof ResourceType) { - PrismObject resourceTypePrismObject = (PrismObject) item; + for (PrismValue value: input.getData()) { + if (value instanceof PrismObjectValue && ((PrismObjectValue) value).asObjectable() instanceof ResourceType) { + PrismObject resourceTypePrismObject = ((PrismObjectValue) value).asPrismObject(); ResourceType resourceType = resourceTypePrismObject.asObjectable(); long started = operationsHelper.recordStart(context, resourceType); OperationResult testResult; @@ -86,7 +83,7 @@ public Data execute(ActionExpressionType expression, Data input, ExecutionContex context.println("Tested " + resourceTypePrismObject + ": " + testResult.getStatus()); output.addItem(operationsHelper.getObject(ResourceType.class, resourceTypePrismObject.getOid(), false, context, result)); } else { - throw new ScriptExecutionException("Couldn't test a resource, because input is not a PrismObject: " + item.toString()); + throw new ScriptExecutionException("Couldn't test a resource, because input is not a PrismObject: " + value.toString()); } } return output; diff --git a/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/ValidateExecutor.java b/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/ValidateExecutor.java index 9998a59b8a7..ff88c710682 100644 --- a/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/ValidateExecutor.java +++ b/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/actions/ValidateExecutor.java @@ -22,9 +22,7 @@ import com.evolveum.midpoint.model.api.validator.ValidationResult; import com.evolveum.midpoint.model.impl.scripting.Data; import com.evolveum.midpoint.model.impl.scripting.ExecutionContext; -import com.evolveum.midpoint.prism.Item; -import com.evolveum.midpoint.prism.PrismContainer; -import com.evolveum.midpoint.prism.PrismObject; +import com.evolveum.midpoint.prism.*; import com.evolveum.midpoint.schema.SchemaConstantsGenerated; import com.evolveum.midpoint.schema.result.OperationResult; import com.evolveum.midpoint.util.exception.*; @@ -60,9 +58,9 @@ public Data execute(ActionExpressionType expression, Data input, ExecutionContex Data output = Data.createEmpty(); - for (Item item : input.getData()) { - if (item instanceof PrismObject && ((PrismObject) item).asObjectable() instanceof ResourceType) { - PrismObject resourceTypePrismObject = (PrismObject) item; + for (PrismValue value : input.getData()) { + if (value instanceof PrismObjectValue && ((PrismObjectValue) value).asObjectable() instanceof ResourceType) { + PrismObject resourceTypePrismObject = ((PrismObjectValue) value).asPrismObject(); ResourceType resourceType = resourceTypePrismObject.asObjectable(); long started = operationsHelper.recordStart(context, resourceType); @@ -83,7 +81,7 @@ public Data execute(ActionExpressionType expression, Data input, ExecutionContex } } else { - throw new ScriptExecutionException("Couldn't test a resource, because input is not a PrismObject: " + item.toString()); + throw new ScriptExecutionException("Couldn't test a resource, because input is not a PrismObject: " + value.toString()); } } return output; diff --git a/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/expressions/SelectEvaluator.java b/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/expressions/SelectEvaluator.java index 6bd3b64c775..19ed3d38855 100644 --- a/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/expressions/SelectEvaluator.java +++ b/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/expressions/SelectEvaluator.java @@ -20,6 +20,7 @@ import com.evolveum.midpoint.model.impl.scripting.ExecutionContext; import com.evolveum.midpoint.model.api.ScriptExecutionException; import com.evolveum.midpoint.prism.Item; +import com.evolveum.midpoint.prism.PrismValue; import com.evolveum.midpoint.prism.path.ItemPath; import com.evolveum.midpoint.schema.result.OperationResult; import com.evolveum.midpoint.xml.ns._public.model.scripting_3.SelectExpressionType; @@ -42,8 +43,8 @@ public Data evaluate(SelectExpressionType selectExpression, Data input, Executio } ItemPath path = itemPathType.getItemPath(); - for (Item item : input.getData()) { - Object o = item.find(path); + for (PrismValue value : input.getData()) { + Object o = value.find(path); if (o != null) { if (o instanceof Item) { output.addItem((Item) o); diff --git a/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/helpers/ExpressionHelper.java b/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/helpers/ExpressionHelper.java index 439e6a4bfc8..94d52c4d001 100644 --- a/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/helpers/ExpressionHelper.java +++ b/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/scripting/helpers/ExpressionHelper.java @@ -126,17 +126,13 @@ public T getSingleArgumentValue(List arguments, St if (paramData.getData().size() != 1) { throw new ScriptExecutionException("Exactly one item was expected in '" + parameterName + "' parameter. Got " + paramData.getData().size()); } - Item paramItem = paramData.getData().get(0); - if (paramItem == null || paramItem.getValues().size() == 0 || (paramItem.getValues().size() == 1 && paramItem.getValues().get(0) == null)) { + PrismValue prismValue = paramData.getData().get(0); + if (prismValue == null) { if (requiredNonNull) { throw new ScriptExecutionException("A non-null value was expected in '" + parameterName + "' parameter"); } return null; } - if (paramItem.getValues().size() > 1) { - throw new ScriptExecutionException("Exactly one value was expected in '" + parameterName + "' parameter. Got " + paramItem.getValues().size() + "."); - } - PrismValue prismValue = paramItem.getValues().get(0); if (!(prismValue instanceof PrismPropertyValue)) { throw new ScriptExecutionException("A prism property value was expected in '" + parameterName + "' parameter. Got " + prismValue.getClass().getName() + " instead."); } diff --git a/model/model-intest/src/test/java/com/evolveum/midpoint/model/intest/scripting/TestScriptingBasic.java b/model/model-intest/src/test/java/com/evolveum/midpoint/model/intest/scripting/TestScriptingBasic.java index 4511815affa..547e7cecc5d 100644 --- a/model/model-intest/src/test/java/com/evolveum/midpoint/model/intest/scripting/TestScriptingBasic.java +++ b/model/model-intest/src/test/java/com/evolveum/midpoint/model/intest/scripting/TestScriptingBasic.java @@ -21,9 +21,7 @@ import com.evolveum.midpoint.model.impl.scripting.ScriptingExpressionEvaluator; import com.evolveum.midpoint.model.intest.AbstractInitializedModelIntegrationTest; import com.evolveum.midpoint.notifications.api.transports.Message; -import com.evolveum.midpoint.prism.Item; -import com.evolveum.midpoint.prism.PrismObject; -import com.evolveum.midpoint.prism.PrismProperty; +import com.evolveum.midpoint.prism.*; import com.evolveum.midpoint.schema.internals.InternalMonitor; import com.evolveum.midpoint.schema.result.OperationResult; import com.evolveum.midpoint.schema.util.ObjectQueryUtil; @@ -622,7 +620,7 @@ public void test430NotificationAboutJackType2() throws Exception { checkDummyTransportMessages("CustomType2", 1); m = dummyTransport.getMessages("dummy:CustomType2").get(0); - assertEquals("Wrong message body", "[user:c0c010c0-d34d-b33f-f00d-111111111111(jack)]", m.getBody()); + assertEquals("Wrong message body", "[POV:user:c0c010c0-d34d-b33f-f00d-111111111111(jack)]", m.getBody()); assertEquals("Wrong message subject", "Failure notification of type 2", m.getSubject()); } @@ -643,8 +641,8 @@ public void test500ScriptingUsers() throws Exception { Data data = output.getFinalOutput(); assertEquals("Unexpected # of items in output", 5, data.getData().size()); Set realOids = new HashSet<>(); - for (Item item : data.getData()) { - PrismObject user = (PrismObject) item; + for (PrismValue value : data.getData()) { + PrismObject user = ((PrismObjectValue) value).asPrismObject(); assertEquals("Description not set", "Test", user.asObjectable().getDescription()); realOids.add(user.getOid()); } @@ -663,18 +661,18 @@ private void assertNoOutputData(ExecutionContext output) { // the following tests are a bit crude but for now it should be OK - private void assertAttributesNotFetched(List data) { - for (Item item : data) { - if (((PrismObject) item).asObjectable().getAttributes().getAny().size() > 2) { - throw new AssertionError("There are some unexpected attributes present in " + item.debugDump()); + private void assertAttributesNotFetched(List data) { + for (PrismValue value : data) { + if (((PrismObjectValue) value).asObjectable().getAttributes().getAny().size() > 2) { + throw new AssertionError("There are some unexpected attributes present in " + value.debugDump()); } } } - private void assertAttributesFetched(List data) { - for (Item item : data) { - if (((PrismObject) item).asObjectable().getAttributes().getAny().size() <= 2) { - throw new AssertionError("There are no attributes present in " + item.debugDump()); + private void assertAttributesFetched(List data) { + for (PrismValue value : data) { + if (((PrismObjectValue) value).asObjectable().getAttributes().getAny().size() <= 2) { + throw new AssertionError("There are no attributes present in " + value.debugDump()); } } }