use of com.evolveum.midpoint.repo.sqale.qmodel.object.QObject 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.qmodel.object.QObject 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);
}
use of com.evolveum.midpoint.repo.sqale.qmodel.object.QObject in project midpoint by Evolveum.
the class SqaleRepositoryService method readByOid.
/**
* Read object using provided {@link JdbcSession} as a part of already running transaction.
*/
private <S extends ObjectType> S readByOid(@NotNull JdbcSession jdbcSession, @NotNull Class<S> schemaType, @NotNull UUID oid, Collection<SelectorOptions<GetOperationOptions>> options) throws SchemaException, ObjectNotFoundException {
SqaleTableMapping<S, QObject<MObject>, MObject> rootMapping = sqlRepoContext.getMappingBySchemaType(schemaType);
QObject<MObject> root = rootMapping.defaultAlias();
Tuple result = jdbcSession.newQuery().from(root).select(rootMapping.selectExpressions(root, options)).where(root.oid.eq(oid)).fetchOne();
if (result == null || result.get(root.fullObject) == null) {
throw new ObjectNotFoundException(schemaType, oid.toString());
}
return rootMapping.toSchemaObject(result, root, options, jdbcSession, false);
}
use of com.evolveum.midpoint.repo.sqale.qmodel.object.QObject in project midpoint by Evolveum.
the class SqaleRepositoryService method executeGetVersion.
private <T extends ObjectType> String executeGetVersion(Class<T> type, UUID oid) throws ObjectNotFoundException {
long opHandle = registerOperationStart(OP_GET_VERSION, type);
try (JdbcSession jdbcSession = sqlRepoContext.newJdbcSession().startReadOnlyTransaction()) {
SqaleTableMapping<T, QObject<MObject>, MObject> rootMapping = sqlRepoContext.getMappingBySchemaType(type);
QObject<MObject> root = rootMapping.defaultAlias();
Integer version = jdbcSession.newQuery().select(root.version).from(root).where(root.oid.eq(oid)).fetchOne();
if (version == null) {
throw new ObjectNotFoundException(type, oid.toString());
}
String versionString = version.toString();
invokeConflictWatchers((w) -> w.afterGetVersion(oid.toString(), versionString));
return versionString;
} finally {
registerOperationFinish(opHandle);
}
}
use of com.evolveum.midpoint.repo.sqale.qmodel.object.QObject in project midpoint by Evolveum.
the class OrgFilterProcessor method process.
@Override
public Predicate process(OrgFilter filter) throws QueryException {
// necessary for lazy refresh of org closure
context.markContainsOrgFilter();
FlexibleRelationalPathBase<?> path = context.root();
if (!(path instanceof QObject)) {
throw new QueryException("Org filter can only be used for objects," + " not for path " + path + " of type " + path.getClass());
}
QObject<?> objectPath = (QObject<?>) path;
if (filter.isRoot()) {
QObjectReference<MObject> ref = getNewRefAlias();
return subQuery(ref).where(ref.ownerOid.eq(objectPath.oid)).notExists();
}
if (filter.getOrgRef() == null) {
throw new QueryException("No organization reference defined in the search query.");
}
String oidParam = filter.getOrgRef().getOid();
if (oidParam == null) {
throw new QueryException("No oid specified in organization reference " + filter.getOrgRef().debugDump());
}
QName relation = filter.getOrgRef().getRelation();
// null means ANY (not "default") here, so we only search/normalize non-nulls
Integer relationId = relation != null ? context.repositoryContext().searchCachedRelationId(relation) : null;
if (filter.getScope() == OrgFilter.Scope.ONE_LEVEL) {
QObjectReference<MObject> ref = getNewRefAlias();
SQLQuery<?> subQuery = subQuery(ref).where(ref.ownerOid.eq(objectPath.oid).and(ref.targetOid.eq(UUID.fromString(oidParam))));
if (relationId != null) {
subQuery.where(ref.relationId.eq(relationId));
}
return subQuery.exists();
} else if (filter.getScope() == OrgFilter.Scope.SUBTREE) {
QObjectReference<MObject> ref = getNewRefAlias();
QOrgClosure oc = getNewClosureAlias();
SQLQuery<?> subQuery = subQuery(ref).join(oc).on(oc.descendantOid.eq(ref.targetOid)).where(ref.ownerOid.eq(objectPath.oid).and(oc.ancestorOid.eq(UUID.fromString(oidParam))));
if (relationId != null) {
subQuery.where(ref.relationId.eq(relationId));
}
return subQuery.exists();
} else if (filter.getScope() == OrgFilter.Scope.ANCESTORS) {
QOrgClosure oc = getNewClosureAlias();
return subQuery(oc).where(oc.ancestorOid.eq(objectPath.oid).and(oc.descendantOid.eq(UUID.fromString(oidParam))).and(oc.ancestorOid.ne(oc.descendantOid))).exists();
} else {
throw new QueryException("Unknown scope if org filter: " + filter);
}
}
Aggregations