Search in sources :

Example 1 with AnyObjectPatch

use of org.apache.syncope.common.lib.patch.AnyObjectPatch in project syncope by apache.

the class AnyOperations method diff.

/**
 * Calculate modifications needed by first in order to be equal to second.
 *
 * @param updated updated AnyObjectTO
 * @param original original AnyObjectTO
 * @param incremental perform incremental diff (without removing existing info)
 * @return AnyObjectPatch containing differences
 */
public static AnyObjectPatch diff(final AnyObjectTO updated, final AnyObjectTO original, final boolean incremental) {
    AnyObjectPatch result = new AnyObjectPatch();
    diff(updated, original, result, incremental);
    // 1. name
    result.setName(replacePatchItem(updated.getName(), original.getName(), new StringReplacePatchItem()));
    // 2. relationships
    Map<Pair<String, String>, RelationshipTO> updatedRels = EntityTOUtils.buildRelationshipMap(updated.getRelationships());
    Map<Pair<String, String>, RelationshipTO> originalRels = EntityTOUtils.buildRelationshipMap(original.getRelationships());
    updatedRels.entrySet().stream().filter(entry -> (!originalRels.containsKey(entry.getKey()))).forEachOrdered(entry -> {
        result.getRelationships().add(new RelationshipPatch.Builder().operation(PatchOperation.ADD_REPLACE).relationshipTO(entry.getValue()).build());
    });
    if (!incremental) {
        originalRels.keySet().stream().filter(relationship -> !updatedRels.containsKey(relationship)).forEach(key -> {
            result.getRelationships().add(new RelationshipPatch.Builder().operation(PatchOperation.DELETE).relationshipTO(originalRels.get(key)).build());
        });
    }
    // 3. memberships
    Map<String, MembershipTO> updatedMembs = EntityTOUtils.buildMembershipMap(updated.getMemberships());
    Map<String, MembershipTO> originalMembs = EntityTOUtils.buildMembershipMap(original.getMemberships());
    updatedMembs.entrySet().stream().filter(entry -> (!originalMembs.containsKey(entry.getKey()))).forEachOrdered(entry -> {
        result.getMemberships().add(new MembershipPatch.Builder().operation(PatchOperation.ADD_REPLACE).group(entry.getValue().getGroupKey()).build());
    });
    if (!incremental) {
        originalMembs.keySet().stream().filter(membership -> !updatedMembs.containsKey(membership)).forEach(key -> {
            result.getMemberships().add(new MembershipPatch.Builder().operation(PatchOperation.DELETE).group(originalMembs.get(key).getGroupKey()).build());
        });
    }
    return result;
}
Also used : StringPatchItem(org.apache.syncope.common.lib.patch.StringPatchItem) AttrTO(org.apache.syncope.common.lib.to.AttrTO) AnyObjectPatch(org.apache.syncope.common.lib.patch.AnyObjectPatch) LoggerFactory(org.slf4j.LoggerFactory) AnyTO(org.apache.syncope.common.lib.to.AnyTO) HashMap(java.util.HashMap) SerializationUtils(org.apache.commons.lang3.SerializationUtils) BooleanReplacePatchItem(org.apache.syncope.common.lib.patch.BooleanReplacePatchItem) UserPatch(org.apache.syncope.common.lib.patch.UserPatch) RelationshipPatch(org.apache.syncope.common.lib.patch.RelationshipPatch) StringUtils(org.apache.commons.lang3.StringUtils) GroupPatch(org.apache.syncope.common.lib.patch.GroupPatch) MembershipPatch(org.apache.syncope.common.lib.patch.MembershipPatch) Pair(org.apache.commons.lang3.tuple.Pair) Map(java.util.Map) AbstractReplacePatchItem(org.apache.syncope.common.lib.patch.AbstractReplacePatchItem) MembershipTO(org.apache.syncope.common.lib.to.MembershipTO) AnyPatch(org.apache.syncope.common.lib.patch.AnyPatch) Logger(org.slf4j.Logger) Collection(java.util.Collection) Set(java.util.Set) GroupTO(org.apache.syncope.common.lib.to.GroupTO) AttrPatch(org.apache.syncope.common.lib.patch.AttrPatch) PasswordPatch(org.apache.syncope.common.lib.patch.PasswordPatch) RelationshipTO(org.apache.syncope.common.lib.to.RelationshipTO) PatchOperation(org.apache.syncope.common.lib.types.PatchOperation) Optional(java.util.Optional) StringReplacePatchItem(org.apache.syncope.common.lib.patch.StringReplacePatchItem) UserTO(org.apache.syncope.common.lib.to.UserTO) AnyObjectTO(org.apache.syncope.common.lib.to.AnyObjectTO) StringReplacePatchItem(org.apache.syncope.common.lib.patch.StringReplacePatchItem) RelationshipTO(org.apache.syncope.common.lib.to.RelationshipTO) MembershipTO(org.apache.syncope.common.lib.to.MembershipTO) AnyObjectPatch(org.apache.syncope.common.lib.patch.AnyObjectPatch) Pair(org.apache.commons.lang3.tuple.Pair)

Example 2 with AnyObjectPatch

use of org.apache.syncope.common.lib.patch.AnyObjectPatch in project syncope by apache.

the class AnyObjectWizardBuilder method onApplyInternal.

@Override
protected Serializable onApplyInternal(final AnyWrapper<AnyObjectTO> modelObject) {
    final AnyObjectTO inner = modelObject.getInnerObject();
    ProvisioningResult<AnyObjectTO> actual;
    if (inner.getKey() == null) {
        actual = anyObjectRestClient.create(inner);
    } else {
        AnyObjectPatch patch = AnyOperations.diff(inner, getOriginalItem().getInnerObject(), false);
        // update just if it is changed
        if (patch.isEmpty()) {
            actual = new ProvisioningResult<>();
            actual.setEntity(inner);
        } else {
            actual = anyObjectRestClient.update(getOriginalItem().getInnerObject().getETagValue(), patch);
        }
    }
    return actual;
}
Also used : AnyObjectTO(org.apache.syncope.common.lib.to.AnyObjectTO) AnyObjectPatch(org.apache.syncope.common.lib.patch.AnyObjectPatch)

Example 3 with AnyObjectPatch

use of org.apache.syncope.common.lib.patch.AnyObjectPatch in project syncope by apache.

the class DefaultAnyObjectWorkflowAdapter method doUpdate.

@Override
protected WorkflowResult<AnyObjectPatch> doUpdate(final AnyObject anyObject, final AnyObjectPatch anyObjectPatch) {
    AnyObjectTO original = dataBinder.getAnyObjectTO(anyObject, true);
    PropagationByResource propByRes = dataBinder.update(anyObject, anyObjectPatch);
    Set<AttrTO> virAttrs = anyObjectPatch.getVirAttrs();
    AnyObjectTO updated = dataBinder.getAnyObjectTO(anyObjectDAO.save(anyObject), true);
    AnyObjectPatch effectivePatch = AnyOperations.diff(updated, original, false);
    effectivePatch.getVirAttrs().clear();
    effectivePatch.getVirAttrs().addAll(virAttrs);
    return new WorkflowResult<>(effectivePatch, propByRes, "update");
}
Also used : AnyObjectTO(org.apache.syncope.common.lib.to.AnyObjectTO) WorkflowResult(org.apache.syncope.core.provisioning.api.WorkflowResult) AttrTO(org.apache.syncope.common.lib.to.AttrTO) PropagationByResource(org.apache.syncope.core.provisioning.api.PropagationByResource) AnyObjectPatch(org.apache.syncope.common.lib.patch.AnyObjectPatch)

Example 4 with AnyObjectPatch

use of org.apache.syncope.common.lib.patch.AnyObjectPatch in project syncope by apache.

the class CamelAnyObjectProvisioningManager method link.

@Override
public String link(final AnyObjectPatch anyObjectPatch) {
    PollingConsumer pollingConsumer = getConsumer("direct:linkAnyObjectPort");
    sendMessage("direct:linkAnyObject", anyObjectPatch);
    Exchange exchange = pollingConsumer.receive();
    if (exchange.getProperty(Exchange.EXCEPTION_CAUGHT) != null) {
        throw (RuntimeException) exchange.getProperty(Exchange.EXCEPTION_CAUGHT);
    }
    return exchange.getIn().getBody(AnyObjectPatch.class).getKey();
}
Also used : Exchange(org.apache.camel.Exchange) PollingConsumer(org.apache.camel.PollingConsumer) AnyObjectPatch(org.apache.syncope.common.lib.patch.AnyObjectPatch)

Example 5 with AnyObjectPatch

use of org.apache.syncope.common.lib.patch.AnyObjectPatch in project syncope by apache.

the class SearchITCase method issueSYNCOPE980.

@Test
public void issueSYNCOPE980() {
    AnyTypeTO service = new AnyTypeTO();
    service.setKey("SERVICE");
    service.setKind(AnyTypeKind.ANY_OBJECT);
    Response response = anyTypeService.create(service);
    assertEquals(Response.Status.CREATED.getStatusCode(), response.getStatusInfo().getStatusCode());
    String serviceKey = null;
    try {
        AnyObjectTO anyObjectTO = new AnyObjectTO();
        anyObjectTO.setName("one");
        anyObjectTO.setRealm(SyncopeConstants.ROOT_REALM);
        anyObjectTO.setType(service.getKey());
        anyObjectTO.getMemberships().add(new MembershipTO.Builder().group("29f96485-729e-4d31-88a1-6fc60e4677f3").build());
        serviceKey = createAnyObject(anyObjectTO).getEntity().getKey();
        AnyObjectPatch anyObjectPatch = new AnyObjectPatch();
        anyObjectPatch.setKey("fc6dbc3a-6c07-4965-8781-921e7401a4a5");
        anyObjectPatch.getMemberships().add(new MembershipPatch.Builder().group("29f96485-729e-4d31-88a1-6fc60e4677f3").build());
        updateAnyObject(anyObjectPatch);
        PagedResult<AnyObjectTO> matching = anyObjectService.search(new AnyQuery.Builder().fiql(SyncopeClient.getAnyObjectSearchConditionBuilder(service.getKey()).inGroups("29f96485-729e-4d31-88a1-6fc60e4677f3").query()).build());
        assertEquals(1, matching.getSize());
        assertEquals(serviceKey, matching.getResult().get(0).getKey());
    } finally {
        if (serviceKey != null) {
            anyObjectService.delete(serviceKey);
        }
        anyTypeService.delete(service.getKey());
    }
}
Also used : Response(javax.ws.rs.core.Response) AnyObjectTO(org.apache.syncope.common.lib.to.AnyObjectTO) MembershipPatch(org.apache.syncope.common.lib.patch.MembershipPatch) MembershipTO(org.apache.syncope.common.lib.to.MembershipTO) AnyTypeTO(org.apache.syncope.common.lib.to.AnyTypeTO) AnyObjectPatch(org.apache.syncope.common.lib.patch.AnyObjectPatch) AnyQuery(org.apache.syncope.common.rest.api.beans.AnyQuery) Test(org.junit.jupiter.api.Test)

Aggregations

AnyObjectPatch (org.apache.syncope.common.lib.patch.AnyObjectPatch)23 AnyObjectTO (org.apache.syncope.common.lib.to.AnyObjectTO)14 List (java.util.List)5 AttrPatch (org.apache.syncope.common.lib.patch.AttrPatch)5 MembershipTO (org.apache.syncope.common.lib.to.MembershipTO)5 Map (java.util.Map)4 Pair (org.apache.commons.lang3.tuple.Pair)4 GroupPatch (org.apache.syncope.common.lib.patch.GroupPatch)4 UserPatch (org.apache.syncope.common.lib.patch.UserPatch)4 AttrTO (org.apache.syncope.common.lib.to.AttrTO)4 PatchOperation (org.apache.syncope.common.lib.types.PatchOperation)4 Test (org.junit.jupiter.api.Test)4 Collection (java.util.Collection)3 HashMap (java.util.HashMap)3 Optional (java.util.Optional)3 Set (java.util.Set)3 SerializationUtils (org.apache.commons.lang3.SerializationUtils)3 StringUtils (org.apache.commons.lang3.StringUtils)3 AnyPatch (org.apache.syncope.common.lib.patch.AnyPatch)3 MembershipPatch (org.apache.syncope.common.lib.patch.MembershipPatch)3