use of com.evolveum.midpoint.repo.sqale.update.RootUpdateContext in project midpoint by Evolveum.
the class SqaleRepositoryService method executeOverwriteObject.
/**
* Overwrite is more like update than add.
*/
private <T extends ObjectType> String executeOverwriteObject(@NotNull PrismObject<T> newObject) throws SchemaException, RepositoryException, ObjectAlreadyExistsException {
String oid = newObject.getOid();
UUID oidUuid = checkOid(oid);
long opHandle = registerOperationStart(OP_ADD_OBJECT_OVERWRITE, newObject);
try (JdbcSession jdbcSession = sqlRepoContext.newJdbcSession().startTransaction()) {
try {
// noinspection ConstantConditions
RootUpdateContext<T, QObject<MObject>, MObject> updateContext = prepareUpdateContext(jdbcSession, newObject.getCompileTimeClass(), oidUuid);
PrismObject<T> prismObject = updateContext.getPrismObject();
// no precondition check for overwrite
invokeConflictWatchers(w -> w.beforeModifyObject(prismObject));
newObject.setUserData(RepositoryService.KEY_ORIGINAL_OBJECT, prismObject.clone());
ObjectDelta<T> delta = prismObject.diff(newObject, EquivalenceStrategy.LITERAL);
Collection<? extends ItemDelta<?, ?>> modifications = delta.getModifications();
logger.trace("overwriteAddObjectAttempt: originalOid={}, modifications={}", oid, modifications);
Collection<? extends ItemDelta<?, ?>> executedModifications = updateContext.execute(modifications, false);
replaceObject(updateContext, updateContext.getPrismObject());
if (!executedModifications.isEmpty()) {
invokeConflictWatchers((w) -> w.afterModifyObject(oid));
}
logger.trace("OBJECT after:\n{}", prismObject.debugDumpLazily());
} catch (ObjectNotFoundException e) {
// so it is just plain addObject after all
new AddObjectContext<>(sqlRepoContext, newObject).execute(jdbcSession);
invokeConflictWatchers((w) -> w.afterAddObject(oid, newObject));
}
jdbcSession.commit();
return oid;
} catch (RuntimeException e) {
SqaleUtils.handlePostgresException(e);
throw e;
} finally {
registerOperationFinish(opHandle);
}
}
use of com.evolveum.midpoint.repo.sqale.update.RootUpdateContext in project midpoint by Evolveum.
the class SqaleRepositoryService method prepareUpdateContext.
/**
* Read object for update and returns update context that contains it with specific get options.
*/
private <S extends ObjectType, Q extends QObject<R>, R extends MObject> RootUpdateContext<S, Q, R> prepareUpdateContext(@NotNull JdbcSession jdbcSession, @NotNull Class<S> schemaType, @NotNull UUID oid, Collection<SelectorOptions<GetOperationOptions>> getOptions, RepoModifyOptions options) throws SchemaException, ObjectNotFoundException {
SqaleTableMapping<S, QObject<R>, R> rootMapping = sqlRepoContext.getMappingBySchemaType(schemaType);
QObject<R> entityPath = rootMapping.defaultAlias();
Path<?>[] selectExpressions = ObjectArrays.concat(rootMapping.selectExpressions(entityPath, getOptions), entityPath.containerIdSeq);
Tuple result = jdbcSession.newQuery().select(selectExpressions).from(entityPath).where(entityPath.oid.eq(oid)).forUpdate().fetchOne();
if (result == null || result.get(entityPath.fullObject) == null) {
throw new ObjectNotFoundException(schemaType, oid.toString());
}
S object = rootMapping.toSchemaObject(result, entityPath, getOptions, jdbcSession, RepoModifyOptions.isForceReindex(options));
R rootRow = rootMapping.newRowObject();
rootRow.oid = oid;
rootRow.containerIdSeq = result.get(entityPath.containerIdSeq);
// This column is generated, some sub-entities need it, but we can't push it to DB.
rootRow.objectType = MObjectType.fromSchemaType(object.getClass());
return new RootUpdateContext<>(sqlRepoContext, jdbcSession, object, rootRow);
}
Aggregations