Search in sources :

Example 46 with SystemException

use of com.evolveum.midpoint.util.exception.SystemException in project midpoint by Evolveum.

the class ShadowManager method updateShadow.

@SuppressWarnings("unchecked")
public Collection<ItemDelta> updateShadow(ProvisioningContext ctx, PrismObject<ShadowType> resourceShadow, Collection<? extends ItemDelta> aprioriDeltas, OperationResult result) throws SchemaException, ConfigurationException, ObjectNotFoundException, CommunicationException, ExpressionEvaluationException {
    PrismObject<ShadowType> repoShadow = repositoryService.getObject(ShadowType.class, resourceShadow.getOid(), null, result);
    RefinedObjectClassDefinition objectClassDefinition = ctx.getObjectClassDefinition();
    Collection<ItemDelta> repoShadowChanges = new ArrayList<ItemDelta>();
    CachingStategyType cachingStrategy = ProvisioningUtil.getCachingStrategy(ctx);
    for (RefinedAttributeDefinition attrDef : objectClassDefinition.getAttributeDefinitions()) {
        if (ProvisioningUtil.shouldStoreAtributeInShadow(objectClassDefinition, attrDef.getName(), cachingStrategy)) {
            ResourceAttribute<Object> resourceAttr = ShadowUtil.getAttribute(resourceShadow, attrDef.getName());
            PrismProperty<Object> repoAttr = repoShadow.findProperty(new ItemPath(ShadowType.F_ATTRIBUTES, attrDef.getName()));
            PropertyDelta attrDelta;
            if (repoAttr == null && repoAttr == null) {
                continue;
            }
            if (repoAttr == null) {
                attrDelta = attrDef.createEmptyDelta(new ItemPath(ShadowType.F_ATTRIBUTES, attrDef.getName()));
                attrDelta.setValuesToReplace(PrismValue.cloneCollection(resourceAttr.getValues()));
            } else {
                attrDelta = repoAttr.diff(resourceAttr);
            //					LOGGER.trace("DIFF:\n{}\n-\n{}\n=:\n{}", repoAttr==null?null:repoAttr.debugDump(1), resourceAttr==null?null:resourceAttr.debugDump(1), attrDelta==null?null:attrDelta.debugDump(1));
            }
            if (attrDelta != null && !attrDelta.isEmpty()) {
                normalizeDelta(attrDelta, attrDef);
                repoShadowChanges.add(attrDelta);
            }
        }
    }
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("Updating repo shadow {}:\n{}", resourceShadow.getOid(), DebugUtil.debugDump(repoShadowChanges));
    }
    try {
        repositoryService.modifyObject(ShadowType.class, resourceShadow.getOid(), repoShadowChanges, result);
    } catch (ObjectAlreadyExistsException e) {
        // We are not renaming the object here. This should not happen.
        throw new SystemException(e.getMessage(), e);
    }
    return repoShadowChanges;
}
Also used : ArrayList(java.util.ArrayList) ItemDelta(com.evolveum.midpoint.prism.delta.ItemDelta) RefinedObjectClassDefinition(com.evolveum.midpoint.common.refinery.RefinedObjectClassDefinition) SystemException(com.evolveum.midpoint.util.exception.SystemException) RefinedAttributeDefinition(com.evolveum.midpoint.common.refinery.RefinedAttributeDefinition) PrismObject(com.evolveum.midpoint.prism.PrismObject) PropertyDelta(com.evolveum.midpoint.prism.delta.PropertyDelta) ObjectAlreadyExistsException(com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException) ItemPath(com.evolveum.midpoint.prism.path.ItemPath)

Example 47 with SystemException

use of com.evolveum.midpoint.util.exception.SystemException in project midpoint by Evolveum.

the class ShadowManager method addPendingOperationDelta.

private void addPendingOperationDelta(ProvisioningContext ctx, PrismObject<ShadowType> shadow, ObjectDelta<ShadowType> pendingDelta, OperationResult resourceOperationResult, OperationResult parentResult) throws SchemaException, ObjectNotFoundException {
    ObjectDeltaType pendingDeltaType = DeltaConvertor.toObjectDeltaType(pendingDelta);
    PendingOperationType pendingOperation = new PendingOperationType();
    pendingOperation.setDelta(pendingDeltaType);
    pendingOperation.setRequestTimestamp(clock.currentTimeXMLGregorianCalendar());
    pendingOperation.setResultStatus(OperationResultStatusType.IN_PROGRESS);
    pendingOperation.setAsynchronousOperationReference(resourceOperationResult.getAsynchronousOperationReference());
    Collection repoDeltas = new ArrayList<>(1);
    ContainerDelta<PendingOperationType> cdelta = ContainerDelta.createDelta(ShadowType.F_PENDING_OPERATION, shadow.getDefinition());
    cdelta.addValuesToAdd(pendingOperation.asPrismContainerValue());
    repoDeltas.add(cdelta);
    try {
        repositoryService.modifyObject(ShadowType.class, shadow.getOid(), repoDeltas, parentResult);
    } catch (ObjectAlreadyExistsException ex) {
        throw new SystemException(ex);
    }
}
Also used : ObjectDeltaType(com.evolveum.prism.xml.ns._public.types_3.ObjectDeltaType) SystemException(com.evolveum.midpoint.util.exception.SystemException) ArrayList(java.util.ArrayList) Collection(java.util.Collection) ObjectAlreadyExistsException(com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException)

Example 48 with SystemException

use of com.evolveum.midpoint.util.exception.SystemException in project midpoint by Evolveum.

the class SequenceHelper method returnUnusedValuesToSequenceAttempt.

public void returnUnusedValuesToSequenceAttempt(String oid, Collection<Long> unusedValues, OperationResult result) throws ObjectNotFoundException, SchemaException, SerializationRelatedException {
    LOGGER.debug("Returning unused values of {} to a sequence with oid '{}'.", unusedValues, oid);
    LOGGER_PERFORMANCE.debug("> return unused values, oid={}, values={}", oid, unusedValues);
    Session session = null;
    try {
        session = baseHelper.beginTransaction();
        PrismObject<SequenceType> prismObject = objectRetriever.getObjectInternal(session, SequenceType.class, oid, null, true, result);
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("OBJECT before:\n{}", prismObject.debugDump());
        }
        SequenceType sequence = prismObject.asObjectable();
        int maxUnusedValues = sequence.getMaxUnusedValues() != null ? sequence.getMaxUnusedValues() : 0;
        Iterator<Long> valuesToReturnIterator = unusedValues.iterator();
        while (valuesToReturnIterator.hasNext() && sequence.getUnusedValues().size() < maxUnusedValues) {
            Long valueToReturn = valuesToReturnIterator.next();
            if (valueToReturn == null) {
                // sanity check
                continue;
            }
            if (!sequence.getUnusedValues().contains(valueToReturn)) {
                sequence.getUnusedValues().add(valueToReturn);
            } else {
                LOGGER.warn("UnusedValues in sequence {} already contains value of {} - ignoring the return request", oid, valueToReturn);
            }
        }
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("OBJECT after:\n{}", prismObject.debugDump());
        }
        // merge and update object
        LOGGER.trace("Translating JAXB to data type.");
        RObject rObject = objectUpdater.createDataObjectFromJAXB(prismObject, PrismIdentifierGenerator.Operation.MODIFY);
        rObject.setVersion(rObject.getVersion() + 1);
        objectUpdater.updateFullObject(rObject, prismObject);
        session.merge(rObject);
        LOGGER.trace("Before commit...");
        session.getTransaction().commit();
        LOGGER.trace("Committed!");
    } catch (ObjectNotFoundException ex) {
        baseHelper.rollbackTransaction(session, ex, result, true);
        throw ex;
    } catch (SchemaException ex) {
        baseHelper.rollbackTransaction(session, ex, result, true);
        throw ex;
    } catch (DtoTranslationException | RuntimeException ex) {
        // should always throw an exception
        baseHelper.handleGeneralException(ex, session, result);
        // ...so this shouldn't occur at all
        throw new SystemException("Exception " + ex + " was not handled correctly", ex);
    } finally {
        baseHelper.cleanupSessionAndResult(session, result);
        LOGGER.trace("Session cleaned up.");
    }
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) SequenceType(com.evolveum.midpoint.xml.ns._public.common.common_3.SequenceType) DtoTranslationException(com.evolveum.midpoint.repo.sql.util.DtoTranslationException) SystemException(com.evolveum.midpoint.util.exception.SystemException) RObject(com.evolveum.midpoint.repo.sql.data.common.RObject) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) Session(org.hibernate.Session)

Example 49 with SystemException

use of com.evolveum.midpoint.util.exception.SystemException in project midpoint by Evolveum.

the class SequenceHelper method advanceSequenceAttempt.

public long advanceSequenceAttempt(String oid, OperationResult result) throws ObjectNotFoundException, SchemaException, SerializationRelatedException {
    long returnValue;
    LOGGER.debug("Advancing sequence with oid '{}'.", oid);
    LOGGER_PERFORMANCE.debug("> advance sequence, oid={}", oid);
    Session session = null;
    try {
        session = baseHelper.beginTransaction();
        PrismObject<SequenceType> prismObject = objectRetriever.getObjectInternal(session, SequenceType.class, oid, null, true, result);
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("OBJECT before:\n{}", prismObject.debugDump());
        }
        SequenceType sequence = prismObject.asObjectable();
        if (!sequence.getUnusedValues().isEmpty()) {
            returnValue = sequence.getUnusedValues().remove(0);
        } else {
            long counter = sequence.getCounter() != null ? sequence.getCounter() : 0L;
            long maxCounter = sequence.getMaxCounter() != null ? sequence.getMaxCounter() : Long.MAX_VALUE;
            boolean allowRewind = Boolean.TRUE.equals(sequence.isAllowRewind());
            if (counter < maxCounter) {
                returnValue = counter;
                sequence.setCounter(counter + 1);
            } else if (counter == maxCounter) {
                returnValue = counter;
                if (allowRewind) {
                    sequence.setCounter(0L);
                } else {
                    // will produce exception during next run
                    sequence.setCounter(counter + 1);
                }
            } else {
                // i.e. counter > maxCounter
                if (allowRewind) {
                    // shouldn't occur but...
                    LOGGER.warn("Sequence {} overflown with allowRewind set to true. Rewinding.", oid);
                    returnValue = 0;
                    sequence.setCounter(1L);
                } else {
                    // TODO some better exception...
                    throw new SystemException("No (next) value available from sequence " + oid + ". Current counter = " + sequence.getCounter() + ", max value = " + sequence.getMaxCounter());
                }
            }
        }
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("Return value = {}, OBJECT after:\n{}", returnValue, prismObject.debugDump());
        }
        // merge and update object
        LOGGER.trace("Translating JAXB to data type.");
        RObject rObject = objectUpdater.createDataObjectFromJAXB(prismObject, PrismIdentifierGenerator.Operation.MODIFY);
        rObject.setVersion(rObject.getVersion() + 1);
        objectUpdater.updateFullObject(rObject, prismObject);
        session.merge(rObject);
        LOGGER.trace("Before commit...");
        session.getTransaction().commit();
        LOGGER.trace("Committed!");
        return returnValue;
    } catch (ObjectNotFoundException ex) {
        baseHelper.rollbackTransaction(session, ex, result, true);
        throw ex;
    } catch (SchemaException ex) {
        baseHelper.rollbackTransaction(session, ex, result, true);
        throw ex;
    } catch (DtoTranslationException | RuntimeException ex) {
        // should always throw an exception
        baseHelper.handleGeneralException(ex, session, result);
        // ...so this shouldn't occur at all
        throw new SystemException("Exception " + ex + " was not handled correctly", ex);
    } finally {
        baseHelper.cleanupSessionAndResult(session, result);
        LOGGER.trace("Session cleaned up.");
    }
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) DtoTranslationException(com.evolveum.midpoint.repo.sql.util.DtoTranslationException) SystemException(com.evolveum.midpoint.util.exception.SystemException) RObject(com.evolveum.midpoint.repo.sql.data.common.RObject) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) SequenceType(com.evolveum.midpoint.xml.ns._public.common.common_3.SequenceType) Session(org.hibernate.Session)

Example 50 with SystemException

use of com.evolveum.midpoint.util.exception.SystemException in project midpoint by Evolveum.

the class RUtil method fixCompositeIdentifierInMetaModel.

private static void fixCompositeIdentifierInMetaModel(SessionFactory sessionFactory, Class clazz) {
    ClassMetadata classMetadata = sessionFactory.getClassMetadata(clazz);
    if (classMetadata instanceof AbstractEntityPersister) {
        AbstractEntityPersister persister = (AbstractEntityPersister) classMetadata;
        EntityMetamodel model = persister.getEntityMetamodel();
        IdentifierProperty identifier = model.getIdentifierProperty();
        try {
            Field field = IdentifierProperty.class.getDeclaredField("hasIdentifierMapper");
            field.setAccessible(true);
            field.set(identifier, true);
            field.setAccessible(false);
        } catch (Exception ex) {
            throw new SystemException("Attempt to fix entity meta model with hack failed, reason: " + ex.getMessage(), ex);
        }
    }
}
Also used : ClassMetadata(org.hibernate.metadata.ClassMetadata) Field(java.lang.reflect.Field) SystemException(com.evolveum.midpoint.util.exception.SystemException) IdentifierProperty(org.hibernate.tuple.IdentifierProperty) EntityMetamodel(org.hibernate.tuple.entity.EntityMetamodel) AbstractEntityPersister(org.hibernate.persister.entity.AbstractEntityPersister) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) SystemException(com.evolveum.midpoint.util.exception.SystemException)

Aggregations

SystemException (com.evolveum.midpoint.util.exception.SystemException)201 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)113 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)76 ObjectNotFoundException (com.evolveum.midpoint.util.exception.ObjectNotFoundException)65 ObjectAlreadyExistsException (com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException)35 PrismObject (com.evolveum.midpoint.prism.PrismObject)32 CommunicationException (com.evolveum.midpoint.util.exception.CommunicationException)32 ExpressionEvaluationException (com.evolveum.midpoint.util.exception.ExpressionEvaluationException)31 QName (javax.xml.namespace.QName)28 ConfigurationException (com.evolveum.midpoint.util.exception.ConfigurationException)26 SecurityViolationException (com.evolveum.midpoint.util.exception.SecurityViolationException)26 Task (com.evolveum.midpoint.task.api.Task)23 ArrayList (java.util.ArrayList)21 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)16 GenericFrameworkException (com.evolveum.midpoint.provisioning.ucf.api.GenericFrameworkException)16 ShadowType (com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType)14 Test (org.testng.annotations.Test)14 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)12 ObjectQuery (com.evolveum.midpoint.prism.query.ObjectQuery)12 ResultHandler (com.evolveum.midpoint.schema.ResultHandler)12