Search in sources :

Example 61 with PropertyDelta

use of com.evolveum.midpoint.prism.delta.PropertyDelta in project midpoint by Evolveum.

the class ShadowIntegrityCheckResultHandler method checkOrFixActivationItem.

private void checkOrFixActivationItem(ShadowCheckResult checkResult, PrismObject<ShadowType> shadow, PrismContainerValue<ActivationType> activation, QName itemName) {
    PrismProperty property = activation.findProperty(new ItemPath(itemName));
    if (property == null || property.isEmpty()) {
        return;
    }
    checkResult.recordWarning(Statistics.EXTRA_ACTIVATION_DATA, "Unexpected activation item: " + property);
    if (fixExtraData) {
        PropertyDelta delta = PropertyDelta.createReplaceEmptyDelta(shadow.getDefinition(), new ItemPath(ShadowType.F_ACTIVATION, itemName));
        checkResult.addFixDelta(delta, Statistics.EXTRA_ACTIVATION_DATA);
    }
}
Also used : PrismProperty(com.evolveum.midpoint.prism.PrismProperty) PropertyDelta(com.evolveum.midpoint.prism.delta.PropertyDelta) ItemPath(com.evolveum.midpoint.prism.path.ItemPath)

Example 62 with PropertyDelta

use of com.evolveum.midpoint.prism.delta.PropertyDelta in project midpoint by Evolveum.

the class SynchronizationServiceImpl method saveSyncMetadata.

/**
	 * Saves situation, timestamps, kind and intent (if needed)
	 */
private PrismObject<ShadowType> saveSyncMetadata(PrismObject<ShadowType> shadow, SynchronizationSituation situation, ResourceObjectShadowChangeDescription change, ObjectSynchronizationType synchronizationPolicy, Task task, OperationResult parentResult) {
    if (shadow == null) {
        return null;
    }
    ShadowType shadowType = shadow.asObjectable();
    // new situation description
    List<PropertyDelta<?>> deltas = SynchronizationUtils.createSynchronizationSituationAndDescriptionDelta(shadow, situation.getSituation(), change.getSourceChannel(), true);
    if (shadowType.getKind() == null) {
        ShadowKindType kind = synchronizationPolicy.getKind();
        if (kind == null) {
            kind = ShadowKindType.ACCOUNT;
        }
        PropertyDelta<ShadowKindType> kindDelta = PropertyDelta.createReplaceDelta(shadow.getDefinition(), ShadowType.F_KIND, kind);
        deltas.add(kindDelta);
    }
    if (shadowType.getIntent() == null) {
        String intent = synchronizationPolicy.getIntent();
        if (intent == null) {
            intent = SchemaConstants.INTENT_DEFAULT;
        }
        PropertyDelta<String> intentDelta = PropertyDelta.createReplaceDelta(shadow.getDefinition(), ShadowType.F_INTENT, intent);
        deltas.add(intentDelta);
    }
    try {
        repositoryService.modifyObject(shadowType.getClass(), shadow.getOid(), deltas, parentResult);
        ItemDelta.applyTo(deltas, shadow);
        task.recordObjectActionExecuted(shadow, ChangeType.MODIFY, null);
        return shadow;
    } catch (ObjectNotFoundException ex) {
        task.recordObjectActionExecuted(shadow, ChangeType.MODIFY, ex);
        // This may happen e.g. during some recon-livesync interactions.
        // If the shadow is gone then it is gone. No point in recording the
        // situation any more.
        LOGGER.debug("Could not update situation in account, because shadow {} does not exist any more (this may be harmless)", shadow.getOid());
        parentResult.getLastSubresult().setStatus(OperationResultStatus.HANDLED_ERROR);
    } catch (ObjectAlreadyExistsException | SchemaException ex) {
        task.recordObjectActionExecuted(shadow, ChangeType.MODIFY, ex);
        LoggingUtils.logException(LOGGER, "### SYNCHRONIZATION # notifyChange(..): Save of synchronization situation failed: could not modify shadow " + shadow.getOid() + ": " + ex.getMessage(), ex);
        parentResult.recordFatalError("Save of synchronization situation failed: could not modify shadow " + shadow.getOid() + ": " + ex.getMessage(), ex);
        throw new SystemException("Save of synchronization situation failed: could not modify shadow " + shadow.getOid() + ": " + ex.getMessage(), ex);
    } catch (Throwable t) {
        task.recordObjectActionExecuted(shadow, ChangeType.MODIFY, t);
        throw t;
    }
    return null;
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) ShadowType(com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) SystemException(com.evolveum.midpoint.util.exception.SystemException) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) PropertyDelta(com.evolveum.midpoint.prism.delta.PropertyDelta) ShadowKindType(com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowKindType) ObjectAlreadyExistsException(com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException)

Example 63 with PropertyDelta

use of com.evolveum.midpoint.prism.delta.PropertyDelta in project midpoint by Evolveum.

the class AbstractScannerTaskHandler method finish.

@Override
protected void finish(H handler, TaskRunResult runResult, Task task, OperationResult opResult) throws SchemaException {
    super.finish(handler, runResult, task, opResult);
    PrismPropertyDefinition<XMLGregorianCalendar> lastScanTimestampDef = new PrismPropertyDefinitionImpl<>(SchemaConstants.MODEL_EXTENSION_LAST_SCAN_TIMESTAMP_PROPERTY_NAME, DOMUtil.XSD_DATETIME, prismContext);
    PropertyDelta<XMLGregorianCalendar> lastScanTimestampDelta = new PropertyDelta<XMLGregorianCalendar>(new ItemPath(TaskType.F_EXTENSION, SchemaConstants.MODEL_EXTENSION_LAST_SCAN_TIMESTAMP_PROPERTY_NAME), lastScanTimestampDef, prismContext);
    lastScanTimestampDelta.setValueToReplace(new PrismPropertyValue<XMLGregorianCalendar>(handler.getThisScanTimestamp()));
    task.modifyExtension(lastScanTimestampDelta);
}
Also used : XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) PrismPropertyDefinitionImpl(com.evolveum.midpoint.prism.PrismPropertyDefinitionImpl) PropertyDelta(com.evolveum.midpoint.prism.delta.PropertyDelta) ItemPath(com.evolveum.midpoint.prism.path.ItemPath)

Example 64 with PropertyDelta

use of com.evolveum.midpoint.prism.delta.PropertyDelta in project midpoint by Evolveum.

the class ShadowManager method updateShadow.

/**
	 * Updates repository shadow based on shadow from resource. Handles rename cases,
	 * change of auxiliary object classes, etc.
	 * @returns repository shadow as it should look like after the update
	 */
@SuppressWarnings("unchecked")
public PrismObject<ShadowType> updateShadow(ProvisioningContext ctx, PrismObject<ShadowType> currentResourceShadow, PrismObject<ShadowType> oldRepoShadow, OperationResult parentResult) throws SchemaException, ObjectNotFoundException, ObjectAlreadyExistsException, ConfigurationException, CommunicationException, ExpressionEvaluationException {
    RefinedObjectClassDefinition ocDef = ctx.computeCompositeObjectClassDefinition(currentResourceShadow);
    ObjectDelta<ShadowType> shadowDelta = oldRepoShadow.createModifyDelta();
    PrismContainer<Containerable> currentResourceAttributesContainer = currentResourceShadow.findContainer(ShadowType.F_ATTRIBUTES);
    PrismContainer<Containerable> oldRepoAttributesContainer = oldRepoShadow.findContainer(ShadowType.F_ATTRIBUTES);
    ShadowType oldRepoShadowType = oldRepoShadow.asObjectable();
    ShadowType currentResourceShadowType = currentResourceShadow.asObjectable();
    if (oldRepoShadowType.isExists() != currentResourceShadowType.isExists()) {
        // Resource object obviously exists when we have got here
        shadowDelta.addModificationReplaceProperty(ShadowType.F_EXISTS, currentResourceShadowType.isExists());
    }
    CachingStategyType cachingStrategy = ProvisioningUtil.getCachingStrategy(ctx);
    for (Item<?, ?> currentResourceItem : currentResourceAttributesContainer.getValue().getItems()) {
        if (currentResourceItem instanceof PrismProperty<?>) {
            PrismProperty<?> currentResourceAttrProperty = (PrismProperty<?>) currentResourceItem;
            RefinedAttributeDefinition<Object> attrDef = ocDef.findAttributeDefinition(currentResourceAttrProperty.getElementName());
            if (ProvisioningUtil.shouldStoreAtributeInShadow(ocDef, attrDef.getName(), cachingStrategy)) {
                MatchingRule matchingRule = matchingRuleRegistry.getMatchingRule(attrDef.getMatchingRuleQName(), attrDef.getTypeName());
                PrismProperty<Object> oldRepoAttributeProperty = oldRepoAttributesContainer.findProperty(currentResourceAttrProperty.getElementName());
                if (oldRepoAttributeProperty == null) {
                    PropertyDelta<?> attrAddDelta = currentResourceAttrProperty.createDelta();
                    for (PrismPropertyValue pval : currentResourceAttrProperty.getValues()) {
                        Object normalizedRealValue;
                        if (matchingRule == null) {
                            normalizedRealValue = pval.getValue();
                        } else {
                            normalizedRealValue = matchingRule.normalize(pval.getValue());
                        }
                        attrAddDelta.addValueToAdd(new PrismPropertyValue(normalizedRealValue));
                        LOGGER.trace("CURRENT ATTR:\n{}\nATTR DELTA:\n{}", currentResourceAttrProperty.debugDump(1), attrAddDelta.debugDump(1));
                    }
                    shadowDelta.addModification(attrAddDelta);
                } else {
                    if (attrDef.isSingleValue()) {
                        Object currentResourceRealValue = currentResourceAttrProperty.getRealValue();
                        Object currentResourceNormalizedRealValue;
                        if (matchingRule == null) {
                            currentResourceNormalizedRealValue = currentResourceRealValue;
                        } else {
                            currentResourceNormalizedRealValue = matchingRule.normalize(currentResourceRealValue);
                        }
                        if (!currentResourceNormalizedRealValue.equals(oldRepoAttributeProperty.getRealValue())) {
                            LOGGER.trace("CURRENT ATTR:\n{}\ncurrentResourceNormalizedRealValue: {}", currentResourceAttrProperty.debugDump(1), currentResourceNormalizedRealValue);
                            shadowDelta.addModificationReplaceProperty(currentResourceAttrProperty.getPath(), currentResourceNormalizedRealValue);
                        }
                    } else {
                        PrismProperty<Object> normalizedCurrentResourceAttrProperty = (PrismProperty<Object>) currentResourceAttrProperty.clone();
                        if (matchingRule != null) {
                            for (PrismPropertyValue pval : normalizedCurrentResourceAttrProperty.getValues()) {
                                Object normalizedRealValue = matchingRule.normalize(pval.getValue());
                                pval.setValue(normalizedRealValue);
                            }
                        }
                        PropertyDelta<Object> attrDiff = oldRepoAttributeProperty.diff(normalizedCurrentResourceAttrProperty);
                        LOGGER.trace("DIFF:\n{}\n-\n{}\n=:\n{}", oldRepoAttributeProperty == null ? null : oldRepoAttributeProperty.debugDump(1), normalizedCurrentResourceAttrProperty == null ? null : normalizedCurrentResourceAttrProperty.debugDump(1), attrDiff == null ? null : attrDiff.debugDump(1));
                        if (attrDiff != null && !attrDiff.isEmpty()) {
                            attrDiff.setParentPath(new ItemPath(ShadowType.F_ATTRIBUTES));
                            shadowDelta.addModification(attrDiff);
                        }
                    }
                }
            }
        }
    }
    for (Item<?, ?> oldRepoItem : oldRepoAttributesContainer.getValue().getItems()) {
        if (oldRepoItem instanceof PrismProperty<?>) {
            PrismProperty<?> oldRepoAttrProperty = (PrismProperty<?>) oldRepoItem;
            RefinedAttributeDefinition<Object> attrDef = ocDef.findAttributeDefinition(oldRepoAttrProperty.getElementName());
            PrismProperty<Object> currentAttribute = currentResourceAttributesContainer.findProperty(oldRepoAttrProperty.getElementName());
            if (attrDef == null || !ProvisioningUtil.shouldStoreAtributeInShadow(ocDef, attrDef.getName(), cachingStrategy) || currentAttribute == null) {
                // No definition for this property it should not be there or no current value: remove it from the shadow
                PropertyDelta<?> oldRepoAttrPropDelta = oldRepoAttrProperty.createDelta();
                oldRepoAttrPropDelta.addValuesToDelete((Collection) PrismPropertyValue.cloneCollection(oldRepoAttrProperty.getValues()));
                shadowDelta.addModification(oldRepoAttrPropDelta);
            }
        }
    }
    PolyString currentShadowName = ShadowUtil.determineShadowName(currentResourceShadow);
    PolyString oldRepoShadowName = oldRepoShadow.getName();
    if (!currentShadowName.equalsOriginalValue(oldRepoShadowName)) {
        PropertyDelta<?> shadowNameDelta = PropertyDelta.createModificationReplaceProperty(ShadowType.F_NAME, oldRepoShadow.getDefinition(), currentShadowName);
        shadowDelta.addModification(shadowNameDelta);
    }
    PropertyDelta<QName> auxOcDelta = (PropertyDelta) PrismProperty.diff(oldRepoShadow.findProperty(ShadowType.F_AUXILIARY_OBJECT_CLASS), currentResourceShadow.findProperty(ShadowType.F_AUXILIARY_OBJECT_CLASS));
    if (auxOcDelta != null) {
        shadowDelta.addModification(auxOcDelta);
    }
    if (cachingStrategy == CachingStategyType.NONE) {
        if (oldRepoShadowType.getCachingMetadata() != null) {
            shadowDelta.addModificationReplaceProperty(ShadowType.F_CACHING_METADATA);
        }
    } else if (cachingStrategy == CachingStategyType.PASSIVE) {
        compareUpdateProperty(shadowDelta, SchemaConstants.PATH_ACTIVATION_ADMINISTRATIVE_STATUS, currentResourceShadow, oldRepoShadow);
        compareUpdateProperty(shadowDelta, SchemaConstants.PATH_ACTIVATION_VALID_FROM, currentResourceShadow, oldRepoShadow);
        compareUpdateProperty(shadowDelta, SchemaConstants.PATH_ACTIVATION_VALID_TO, currentResourceShadow, oldRepoShadow);
        compareUpdateProperty(shadowDelta, SchemaConstants.PATH_ACTIVATION_LOCKOUT_STATUS, currentResourceShadow, oldRepoShadow);
        CachingMetadataType cachingMetadata = new CachingMetadataType();
        cachingMetadata.setRetrievalTimestamp(clock.currentTimeXMLGregorianCalendar());
        shadowDelta.addModificationReplaceProperty(ShadowType.F_CACHING_METADATA, cachingMetadata);
    } else {
        throw new ConfigurationException("Unknown caching strategy " + cachingStrategy);
    }
    if (!shadowDelta.isEmpty()) {
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("Updating repo shadow {} with delta:\n{}", oldRepoShadow, shadowDelta.debugDump(1));
        }
        ConstraintsChecker.onShadowModifyOperation(shadowDelta.getModifications());
        try {
            repositoryService.modifyObject(ShadowType.class, oldRepoShadow.getOid(), shadowDelta.getModifications(), parentResult);
        } catch (ObjectAlreadyExistsException e) {
            // This should not happen for shadows
            throw new SystemException(e.getMessage(), e);
        }
        PrismObject<ShadowType> newRepoShadow = oldRepoShadow.clone();
        shadowDelta.applyTo(newRepoShadow);
        return newRepoShadow;
    } else {
        LOGGER.trace("No need to update repo shadow {} (empty delta)", oldRepoShadow);
        return oldRepoShadow;
    }
}
Also used : RefinedObjectClassDefinition(com.evolveum.midpoint.common.refinery.RefinedObjectClassDefinition) SystemException(com.evolveum.midpoint.util.exception.SystemException) ConfigurationException(com.evolveum.midpoint.util.exception.ConfigurationException) Containerable(com.evolveum.midpoint.prism.Containerable) PropertyDelta(com.evolveum.midpoint.prism.delta.PropertyDelta) ObjectAlreadyExistsException(com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException) PrismPropertyValue(com.evolveum.midpoint.prism.PrismPropertyValue) QName(javax.xml.namespace.QName) PrismProperty(com.evolveum.midpoint.prism.PrismProperty) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) PrismObject(com.evolveum.midpoint.prism.PrismObject) MatchingRule(com.evolveum.midpoint.prism.match.MatchingRule) ItemPath(com.evolveum.midpoint.prism.path.ItemPath)

Example 65 with PropertyDelta

use of com.evolveum.midpoint.prism.delta.PropertyDelta in project midpoint by Evolveum.

the class ShadowManager method fixShadow.

/**
	 * Re-reads the shadow, re-evaluates the identifiers and stored values, updates them if necessary. Returns
	 * fixed shadow.  
	 */
public PrismObject<ShadowType> fixShadow(ProvisioningContext ctx, PrismObject<ShadowType> origRepoShadow, OperationResult parentResult) throws ObjectNotFoundException, SchemaException, ConfigurationException, CommunicationException, ExpressionEvaluationException {
    PrismObject<ShadowType> currentRepoShadow = repositoryService.getObject(ShadowType.class, origRepoShadow.getOid(), null, parentResult);
    ProvisioningContext shadowCtx = ctx.spawn(currentRepoShadow);
    RefinedObjectClassDefinition ocDef = shadowCtx.getObjectClassDefinition();
    PrismContainer<Containerable> attributesContainer = currentRepoShadow.findContainer(ShadowType.F_ATTRIBUTES);
    if (attributesContainer != null && attributesContainer.getValue() != null) {
        ObjectDelta<ShadowType> shadowDelta = currentRepoShadow.createModifyDelta();
        for (Item<?, ?> item : attributesContainer.getValue().getItems()) {
            if (item instanceof PrismProperty<?>) {
                PrismProperty<?> attrProperty = (PrismProperty<?>) item;
                RefinedAttributeDefinition<Object> attrDef = ocDef.findAttributeDefinition(attrProperty.getElementName());
                if (attrDef == null) {
                    // No definition for this property, it should not be in the shadow
                    PropertyDelta<?> oldRepoAttrPropDelta = attrProperty.createDelta();
                    oldRepoAttrPropDelta.addValuesToDelete((Collection) PrismPropertyValue.cloneCollection(attrProperty.getValues()));
                    shadowDelta.addModification(oldRepoAttrPropDelta);
                } else {
                    MatchingRule matchingRule = matchingRuleRegistry.getMatchingRule(attrDef.getMatchingRuleQName(), attrDef.getTypeName());
                    List<PrismPropertyValue> valuesToAdd = null;
                    List<PrismPropertyValue> valuesToDelete = null;
                    for (PrismPropertyValue attrVal : attrProperty.getValues()) {
                        Object currentRealValue = attrVal.getValue();
                        Object normalizedRealValue = matchingRule.normalize(currentRealValue);
                        if (!normalizedRealValue.equals(currentRealValue)) {
                            if (attrDef.isSingleValue()) {
                                shadowDelta.addModificationReplaceProperty(attrProperty.getPath(), normalizedRealValue);
                                break;
                            } else {
                                if (valuesToAdd == null) {
                                    valuesToAdd = new ArrayList<>();
                                }
                                valuesToAdd.add(new PrismPropertyValue(normalizedRealValue));
                                if (valuesToDelete == null) {
                                    valuesToDelete = new ArrayList<>();
                                }
                                valuesToDelete.add(new PrismPropertyValue(currentRealValue));
                            }
                        }
                    }
                    PropertyDelta attrDelta = attrProperty.createDelta(attrProperty.getPath());
                    if (valuesToAdd != null) {
                        attrDelta.addValuesToAdd(valuesToAdd);
                    }
                    if (valuesToDelete != null) {
                        attrDelta.addValuesToDelete(valuesToDelete);
                    }
                    shadowDelta.addModification(attrDelta);
                }
            }
        }
        if (!shadowDelta.isEmpty()) {
            if (LOGGER.isTraceEnabled()) {
                LOGGER.trace("Fixing shadow {} with delta:\n{}", origRepoShadow, shadowDelta.debugDump());
            }
            try {
                repositoryService.modifyObject(ShadowType.class, origRepoShadow.getOid(), shadowDelta.getModifications(), parentResult);
            } catch (ObjectAlreadyExistsException e) {
                // This should not happen for shadows
                throw new SystemException(e.getMessage(), e);
            }
            shadowDelta.applyTo(currentRepoShadow);
        } else {
            LOGGER.trace("No need to fixing shadow {} (empty delta)", origRepoShadow);
        }
    } else {
        LOGGER.trace("No need to fixing shadow {} (no atttributes)", origRepoShadow);
    }
    return currentRepoShadow;
}
Also used : RefinedObjectClassDefinition(com.evolveum.midpoint.common.refinery.RefinedObjectClassDefinition) PrismProperty(com.evolveum.midpoint.prism.PrismProperty) SystemException(com.evolveum.midpoint.util.exception.SystemException) Containerable(com.evolveum.midpoint.prism.Containerable) PrismObject(com.evolveum.midpoint.prism.PrismObject) PropertyDelta(com.evolveum.midpoint.prism.delta.PropertyDelta) MatchingRule(com.evolveum.midpoint.prism.match.MatchingRule) ObjectAlreadyExistsException(com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException) PrismPropertyValue(com.evolveum.midpoint.prism.PrismPropertyValue)

Aggregations

PropertyDelta (com.evolveum.midpoint.prism.delta.PropertyDelta)83 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)51 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)36 ShadowType (com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType)25 QName (javax.xml.namespace.QName)24 Test (org.testng.annotations.Test)24 ArrayList (java.util.ArrayList)22 Task (com.evolveum.midpoint.task.api.Task)21 ItemDelta (com.evolveum.midpoint.prism.delta.ItemDelta)19 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)18 ObjectDelta (com.evolveum.midpoint.prism.delta.ObjectDelta)16 ObjectNotFoundException (com.evolveum.midpoint.util.exception.ObjectNotFoundException)11 SystemException (com.evolveum.midpoint.util.exception.SystemException)11 ObjectType (com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType)11 Collection (java.util.Collection)11 PrismPropertyValue (com.evolveum.midpoint.prism.PrismPropertyValue)10 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)10 ObjectAlreadyExistsException (com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException)10 SecurityViolationException (com.evolveum.midpoint.util.exception.SecurityViolationException)9 PolicyViolationException (com.evolveum.midpoint.util.exception.PolicyViolationException)8