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;
}
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);
}
}
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.");
}
}
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.");
}
}
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);
}
}
}
Aggregations