Search in sources :

Example 6 with ItemDeltaType

use of com.evolveum.prism.xml.ns._public.types_3.ItemDeltaType in project midpoint by Evolveum.

the class DeltaConvertor method createItemDelta.

public static <IV extends PrismValue, ID extends ItemDefinition> ItemDelta<IV, ID> createItemDelta(ItemDeltaType propMod, PrismContainerDefinition<?> pcDef, boolean allowRawValues) throws SchemaException {
    ItemPathType parentPathType = propMod.getPath();
    ItemPath parentPath = null;
    if (parentPathType != null) {
        parentPath = parentPathType.getItemPath();
    } else {
        throw new IllegalStateException("Path argument in the itemDelta HAVE TO BE specified.");
    }
    if (propMod.getValue() == null) {
        throw new IllegalArgumentException("No value in item delta (path: " + parentPath + ") while creating a property delta");
    }
    ItemDefinition containingPcd = pcDef.findItemDefinition(parentPath);
    PrismContainerDefinition containerDef = null;
    if (containingPcd == null) {
        containerDef = pcDef.findContainerDefinition(parentPath.allUpToLastNamed());
        if (containerDef == null) {
            if (allowRawValues) {
                return null;
            }
            throw new SchemaException("No definition for " + parentPath.allUpToLastNamed().lastNamed().getName() + " (while creating delta for " + pcDef + ")");
        }
    }
    QName elementName = parentPath.lastNamed().getName();
    //propMod.getValue().getParsedValue(containingPcd);
    Item item = RawTypeUtil.getParsedItem(containingPcd, propMod.getValue(), elementName, containerDef);
    ItemDelta<IV, ID> itemDelta = item.createDelta(parentPath);
    if (propMod.getModificationType() == ModificationTypeType.ADD) {
        itemDelta.addValuesToAdd(PrismValue.resetParentCollection(PrismValue.cloneCollection(item.getValues())));
    } else if (propMod.getModificationType() == ModificationTypeType.DELETE) {
        itemDelta.addValuesToDelete(PrismValue.resetParentCollection(PrismValue.cloneCollection(item.getValues())));
    } else if (propMod.getModificationType() == ModificationTypeType.REPLACE) {
        itemDelta.setValuesToReplace(PrismValue.resetParentCollection(PrismValue.cloneCollection(item.getValues())));
    }
    if (!propMod.getEstimatedOldValue().isEmpty()) {
        Item oldItem = RawTypeUtil.getParsedItem(containingPcd, propMod.getEstimatedOldValue(), elementName, containerDef);
        itemDelta.addEstimatedOldValues(PrismValue.resetParentCollection(PrismValue.cloneCollection(oldItem.getValues())));
    }
    return itemDelta;
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) ItemPathType(com.evolveum.prism.xml.ns._public.types_3.ItemPathType) QName(javax.xml.namespace.QName) ItemPath(com.evolveum.midpoint.prism.path.ItemPath)

Example 7 with ItemDeltaType

use of com.evolveum.prism.xml.ns._public.types_3.ItemDeltaType in project midpoint by Evolveum.

the class DeltaConvertor method toItemDeltaTypes.

public static Collection<ItemDeltaType> toItemDeltaTypes(ItemDelta delta, DeltaConversionOptions options) throws SchemaException {
    if (InternalsConfig.consistencyChecks) {
        delta.checkConsistence();
    }
    if (!delta.isEmpty() && delta.getPrismContext() == null) {
        throw new IllegalStateException("Non-empty ItemDelta with no prismContext cannot be converted to ItemDeltaType.");
    }
    Collection<ItemDeltaType> mods = new ArrayList<>();
    ItemPathType path = new ItemPathType(delta.getPath());
    if (delta.getValuesToReplace() != null) {
        ItemDeltaType mod = new ItemDeltaType();
        mod.setPath(path);
        mod.setModificationType(ModificationTypeType.REPLACE);
        try {
            addModValues(delta, mod, delta.getValuesToReplace(), options);
        } catch (SchemaException e) {
            throw new SchemaException(e.getMessage() + " while converting property " + delta.getElementName(), e);
        }
        addOldValues(delta, mod, delta.getEstimatedOldValues(), options);
        mods.add(mod);
    }
    if (delta.getValuesToAdd() != null) {
        ItemDeltaType mod = new ItemDeltaType();
        mod.setPath(path);
        mod.setModificationType(ModificationTypeType.ADD);
        try {
            addModValues(delta, mod, delta.getValuesToAdd(), options);
        } catch (SchemaException e) {
            throw new SchemaException(e.getMessage() + " while converting property " + delta.getElementName(), e);
        }
        addOldValues(delta, mod, delta.getEstimatedOldValues(), options);
        mods.add(mod);
    }
    if (delta.getValuesToDelete() != null) {
        ItemDeltaType mod = new ItemDeltaType();
        mod.setPath(path);
        mod.setModificationType(ModificationTypeType.DELETE);
        try {
            addModValues(delta, mod, delta.getValuesToDelete(), options);
        } catch (SchemaException e) {
            throw new SchemaException(e.getMessage() + " while converting property " + delta.getElementName(), e);
        }
        addOldValues(delta, mod, delta.getEstimatedOldValues(), options);
        mods.add(mod);
    }
    return mods;
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) ItemPathType(com.evolveum.prism.xml.ns._public.types_3.ItemPathType) ArrayList(java.util.ArrayList) ItemDeltaType(com.evolveum.prism.xml.ns._public.types_3.ItemDeltaType)

Example 8 with ItemDeltaType

use of com.evolveum.prism.xml.ns._public.types_3.ItemDeltaType in project midpoint by Evolveum.

the class XPathTest method xpathTest.

/**
     * This is now a proper test yet.
     * It does some operations with XPath. If it does not die, then the
     * code some somehow consistent.
     *
     * It should be improved later.
     */
@Test
public void xpathTest() throws JAXBException, FileNotFoundException, IOException, ParserConfigurationException, SchemaException {
    ObjectModificationType objectModification = PrismTestUtil.parseAtomicValue(new File(FILENAME_CHANGETYPE), ObjectModificationType.COMPLEX_TYPE);
    for (ItemDeltaType change : objectModification.getItemDelta()) {
        ItemPathType pathType = change.getPath();
        System.out.println("  path=" + pathType + " (" + pathType.getClass().getName() + ") " + pathType.toString());
        //            NamedNodeMap attributes = path.getAttributes();
        //            for (int i = 0; i < attributes.getLength(); i++) {
        //                Node n = attributes.item(i);
        //                System.out.println("   A: " + n.getClass().getName() + " " + n.getNodeName() + "(" + n.getPrefix() + " : " + n.getLocalName() + ") = " + n.getNodeValue());
        //            }
        //            List<Object> any = change.getValue().getAny();
        //            for (Object e : any) {
        //                if (e instanceof Element) {
        //                    System.out.println("  E: " + ((Element) e).getLocalName());
        //                }
        //            }
        ItemPath path = pathType.getItemPath();
        XPathHolder xpath = new XPathHolder(path);
        AssertJUnit.assertEquals("c:extension/piracy:ship[2]/c:name", xpath.getXPathWithoutDeclarations());
        System.out.println("XPATH: " + xpath);
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder loader = factory.newDocumentBuilder();
        Document doc = loader.newDocument();
        Element xpathElement = xpath.toElement("http://elelel/", "path", doc);
        Attr nsC = xpathElement.getAttributeNodeNS("http://www.w3.org/2000/xmlns/", "c");
        Attr nsPiracy = xpathElement.getAttributeNodeNS("http://www.w3.org/2000/xmlns/", "piracy");
        System.out.println("c: " + nsC);
        System.out.println("piracy: " + nsPiracy);
        //            AssertJUnit.assertEquals("http://midpoint.evolveum.com/xml/ns/public/common/common-3", nsC.getValue());
        //            AssertJUnit.assertEquals("http://midpoint.evolveum.com/xml/ns/samples/piracy", nsPiracy.getValue());
        System.out.println("XPATH Element: " + xpathElement);
        XPathHolder xpathFromElement = new XPathHolder(xpathElement);
        AssertJUnit.assertEquals(xpath, xpathFromElement);
        //            attributes = xpathElement.getAttributes();
        //            for (int i = 0; i < attributes.getLength(); i++) {
        //                Node n = attributes.item(i);
        //                System.out.println(" A: " + n.getNodeName() + "(" + n.getPrefix() + " : " + n.getLocalName() + ") = " + n.getNodeValue());
        //            }
        List<XPathSegment> segments = xpath.toSegments();
        System.out.println("XPATH segments: " + segments);
        XPathHolder xpathFromSegments = new XPathHolder(segments);
        System.out.println("XPath from segments: " + xpathFromSegments);
        AssertJUnit.assertEquals("c:extension/piracy:ship[2]/c:name", xpathFromSegments.getXPathWithoutDeclarations());
    }
}
Also used : XPathSegment(com.evolveum.midpoint.prism.marshaller.XPathSegment) ObjectModificationType(com.evolveum.midpoint.xml.ns._public.common.api_types_3.ObjectModificationType) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) ItemPathType(com.evolveum.prism.xml.ns._public.types_3.ItemPathType) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) ItemDeltaType(com.evolveum.prism.xml.ns._public.types_3.ItemDeltaType) Attr(org.w3c.dom.Attr) XPathHolder(com.evolveum.midpoint.prism.marshaller.XPathHolder) DocumentBuilder(javax.xml.parsers.DocumentBuilder) File(java.io.File) CanonicalItemPath(com.evolveum.midpoint.prism.path.CanonicalItemPath) ItemPath(com.evolveum.midpoint.prism.path.ItemPath) Test(org.testng.annotations.Test)

Example 9 with ItemDeltaType

use of com.evolveum.prism.xml.ns._public.types_3.ItemDeltaType in project midpoint by Evolveum.

the class ObjectTypeUtil method isModificationOf.

//TODO: refactor after new schema
public static boolean isModificationOf(ItemDeltaType modification, QName elementName, ItemPathType path) {
    //        if (path == null && XPathHolder.isDefault(modification.getPath())) {
    //            return (elementName.equals(ObjectTypeUtil.getElementName(modification)));
    //        }
    ItemPathType modificationPath = modification.getPath();
    if (ItemPathUtil.isDefault(modificationPath)) {
        throw new IllegalArgumentException("Path in the delta must not be null");
    }
    if (path == null) {
        return false;
    }
    //        XPathHolder modPath = new XPathHolder(modification.getPath());
    ItemPath full = new ItemPath(path.getItemPath(), elementName);
    ItemPathType fullPath = new ItemPathType(full);
    return fullPath.equivalent(modificationPath);
//        if (fullPath.equals(modificationPath)) {
//            return (elementName.equals(getElementName(modification)));
//        }
//        return false;
}
Also used : ItemPathType(com.evolveum.prism.xml.ns._public.types_3.ItemPathType) ItemPath(com.evolveum.midpoint.prism.path.ItemPath)

Example 10 with ItemDeltaType

use of com.evolveum.prism.xml.ns._public.types_3.ItemDeltaType in project midpoint by Evolveum.

the class TestSanity method test040UnlinkDerbyAccountFromUser.

/**
     * Unlink account by removing the accountRef from the user.
     * The account will not be deleted, just the association to user will be broken.
     */
@Test
public void test040UnlinkDerbyAccountFromUser() throws FileNotFoundException, JAXBException, FaultMessage, ObjectNotFoundException, SchemaException, DirectoryException, SQLException {
    TestUtil.displayTestTile("test040UnlinkDerbyAccountFromUser");
    // GIVEN
    ObjectDeltaType objectChange = new ObjectDeltaType();
    objectChange.setOid(USER_JACK_OID);
    ItemDeltaType modificationDeleteAccountRef = new ItemDeltaType();
    modificationDeleteAccountRef.setModificationType(ModificationTypeType.DELETE);
    ObjectReferenceType accountRefToDelete = new ObjectReferenceType();
    accountRefToDelete.setOid(accountShadowOidDerby);
    RawType modificationValue = new RawType(((PrismContextImpl) prismContext).getBeanMarshaller().marshall(accountRefToDelete), prismContext);
    modificationDeleteAccountRef.getValue().add(modificationValue);
    modificationDeleteAccountRef.setPath(new ItemPathType(new ItemPath(UserType.F_LINK_REF)));
    objectChange.getItemDelta().add(modificationDeleteAccountRef);
    objectChange.setChangeType(ChangeTypeType.MODIFY);
    objectChange.setObjectType(UserType.COMPLEX_TYPE);
    displayJaxb("modifyObject input", objectChange, new QName(SchemaConstants.NS_C, "change"));
    assertNoRepoCache();
    // WHEN ObjectTypes.USER.getTypeQName(), 
    OperationResultType result = modifyObjectViaModelWS(objectChange);
    // THEN
    assertNoRepoCache();
    displayJaxb("modifyObject result", result, SchemaConstants.C_RESULT);
    TestUtil.assertSuccess("modifyObject has failed", result);
    // Check if user object was modified in the repo
    OperationResult repoResult = new OperationResult("getObject");
    PrismObject<UserType> uObject = repositoryService.getObject(UserType.class, USER_JACK_OID, null, repoResult);
    UserType repoUser = uObject.asObjectable();
    repoResult.computeStatus();
    display("User (repository)", repoUser);
    List<ObjectReferenceType> accountRefs = repoUser.getLinkRef();
    // only OpenDJ account should be left now
    assertEquals(1, accountRefs.size());
    ObjectReferenceType ref = accountRefs.get(0);
    assertEquals("Wrong OID in accountRef in " + repoUser, accountShadowOidOpendj, ref.getOid());
}
Also used : ItemPathType(com.evolveum.prism.xml.ns._public.types_3.ItemPathType) QName(javax.xml.namespace.QName) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ItemDeltaType(com.evolveum.prism.xml.ns._public.types_3.ItemDeltaType) ObjectDeltaType(com.evolveum.prism.xml.ns._public.types_3.ObjectDeltaType) ObjectReferenceType(com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType) OperationResultType(com.evolveum.midpoint.xml.ns._public.common.common_3.OperationResultType) RawType(com.evolveum.prism.xml.ns._public.types_3.RawType) UserType(com.evolveum.midpoint.xml.ns._public.common.common_3.UserType) ItemPath(com.evolveum.midpoint.prism.path.ItemPath) Test(org.testng.annotations.Test) AbstractModelIntegrationTest(com.evolveum.midpoint.model.test.AbstractModelIntegrationTest)

Aggregations

ItemDeltaType (com.evolveum.prism.xml.ns._public.types_3.ItemDeltaType)39 ObjectDeltaType (com.evolveum.prism.xml.ns._public.types_3.ObjectDeltaType)28 RawType (com.evolveum.prism.xml.ns._public.types_3.RawType)14 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)11 ObjectDeltaListType (com.evolveum.midpoint.xml.ns._public.common.api_types_3.ObjectDeltaListType)11 UserType (com.evolveum.midpoint.xml.ns._public.common.common_3.UserType)11 ItemPathType (com.evolveum.prism.xml.ns._public.types_3.ItemPathType)11 Test (org.testng.annotations.Test)11 ItemDelta (com.evolveum.midpoint.prism.delta.ItemDelta)8 QName (javax.xml.namespace.QName)8 Document (org.w3c.dom.Document)8 AbstractModelIntegrationTest (com.evolveum.midpoint.model.test.AbstractModelIntegrationTest)7 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)7 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)7 PrismAsserts.assertEqualsPolyString (com.evolveum.midpoint.prism.util.PrismAsserts.assertEqualsPolyString)6 ObjectDeltaOperationListType (com.evolveum.midpoint.xml.ns._public.common.api_types_3.ObjectDeltaOperationListType)6 XNode (com.evolveum.midpoint.prism.xnode.XNode)4 ObjectModificationType (com.evolveum.midpoint.xml.ns._public.common.api_types_3.ObjectModificationType)4 ObjectDeltaOperationType (com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectDeltaOperationType)4 ResourceObjectShadowChangeDescriptionType (com.evolveum.midpoint.xml.ns._public.common.common_3.ResourceObjectShadowChangeDescriptionType)4