Skip to content

Commit

Permalink
searchRefs code/test prototype WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
virgo47 committed Jan 16, 2023
1 parent fec0dac commit 4fe60d3
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,15 @@ public static <S, Q extends FlexibleRelationalPathBase<R>, R> SqaleQueryContext<
SqaleRepoContext sqlRepoContext,
SQLQuery<?> sqlQuery,
SqaleObjectLoader objectLoader) {
return from(sqlRepoContext.getMappingBySchemaType(schemaType),
sqlRepoContext, sqlQuery, objectLoader);
}

SqaleTableMapping<S, Q, R> rootMapping = sqlRepoContext.getMappingBySchemaType(schemaType);
public static <S, Q extends FlexibleRelationalPathBase<R>, R> SqaleQueryContext<S, Q, R> from(
SqaleTableMapping<S, Q, R> rootMapping,
SqaleRepoContext sqlRepoContext,
SQLQuery<?> sqlQuery,
SqaleObjectLoader objectLoader) {
Q rootPath = rootMapping.defaultAlias();
SQLQuery<?> query = sqlQuery.from(rootPath);
// Turns on validations of aliases, does not ignore duplicate JOIN expressions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import com.evolveum.midpoint.repo.sqale.qmodel.org.QOrg;
import com.evolveum.midpoint.repo.sqale.qmodel.org.QOrgClosure;
import com.evolveum.midpoint.repo.sqale.qmodel.org.QOrgMapping;
import com.evolveum.midpoint.repo.sqale.qmodel.ref.MReference;
import com.evolveum.midpoint.repo.sqale.qmodel.ref.QObjectReference;
import com.evolveum.midpoint.repo.sqale.qmodel.ref.QObjectReferenceMapping;
import com.evolveum.midpoint.repo.sqale.update.AddObjectContext;
Expand Down Expand Up @@ -1274,25 +1275,38 @@ private <T extends Containerable> SearchResultList<T> executeSearchContainers(Cl
}
}

// TODO what is the return type?
// Can be: SearchResultList<ObjectReferenceType> of references extracted from objects selected by ref owners
public <T extends Containerable> SearchResultList<ObjectReferenceType> searchReference(
@NotNull Class<T> ownerType, @NotNull ObjectQuery refQuery, @Nullable ObjectFilter parentFilter) {
//Class<ObjectType> ownerType, RefFilter filter) {
// RefFilter or something different? Probably, extended ref filter could work just fine.
ObjectFilter filter = refQuery.getFilter();
if (!(filter instanceof RefFilter)) {
throw new RuntimeException("Only RefFilter is supported for reference query"); // TODO proper exception type
}
System.out.println("ownerType = " + ownerType);
System.out.println("refQuery = " + refQuery);
System.out.println("parentFilter = " + parentFilter);

// 1. select refs, this can be low-level repo based List<MReference>
// 2. collect unique owner OIDs
// 3. select owner objects
// 4. crawl the object to extract the result references from them
return new SearchResultList<>();
public SearchResultList<ObjectReferenceType> searchReferences(
@NotNull ObjectQuery query,
@Nullable Collection<SelectorOptions<GetOperationOptions>> options,
@NotNull OperationResult parentResult) {
try {
// First experiment: query has OwnedBy filter only
ObjectFilter filter = query.getFilter();
if (!(filter instanceof OwnedByFilter)) {
throw new UnsupportedOperationException("wrong filter type");
}

System.out.println("query = " + query);
OwnedByFilter ownedByFilter = (OwnedByFilter) filter;

// TODO later this should be resolved from the ownedBy filter, starting with its type
SqaleQueryContext<Referencable, QObjectReference<MObject>, MReference> queryContext =
SqaleQueryContext.from(
QObjectReferenceMapping.getForRoleMembership(),
sqlRepoContext, sqlRepoContext.newQuery(), null);
SearchResultList<Referencable> list = sqlQueryExecutor.list(queryContext, query, options);
System.out.println("list = " + list);

// 1. select refs, this can be low-level repo based List<MReference>
// 2. collect unique owner OIDs // from here optionally based on options? what if client does not want parents?
// 3. select owner objects
// 4. crawl the object to extract the result references from them
return new SearchResultList<>();
} catch (RepositoryException e) {
throw new RuntimeException(e);
} catch (SchemaException e) {
throw new RuntimeException(e);
}
}
// endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public class SqaleRepoSearchTest extends SqaleRepoBaseTest {
private final String resourceOid = UUID.randomUUID().toString();
private final String connectorHostOid = UUID.randomUUID().toString();

private ItemDefinition<?> shadowAttributeDefinition;
private ItemDefinition<?> shadowAttributeStringMvDefinition;

@BeforeClass
public void initObjects() throws Exception {
Expand Down Expand Up @@ -173,7 +173,7 @@ public void initObjects() throws Exception {
ItemName shadowAttributeName = new ItemName("https://example.com/p", "string-mv");
ShadowAttributesHelper attributesHelper = new ShadowAttributesHelper(shadow1)
.set(shadowAttributeName, DOMUtil.XSD_STRING, "string-value1", "string-value2");
shadowAttributeDefinition = attributesHelper.getDefinition(shadowAttributeName);
shadowAttributeStringMvDefinition = attributesHelper.getDefinition(shadowAttributeName);
shadow1Oid = repositoryService.addObject(shadow1.asPrismObject(), null, result);
// another shadow just to check we don't select shadow1 accidentally/randomly
repositoryService.addObject(
Expand Down Expand Up @@ -2036,7 +2036,7 @@ public void test590SearchObjectWithAssignmentExtension() throws SchemaException
@Test
public void test591SearchShadowWithAttribute() throws SchemaException {
searchObjectTest("with assignment extension item equal to value", ShadowType.class,
f -> f.itemWithDef(shadowAttributeDefinition,
f -> f.itemWithDef(shadowAttributeStringMvDefinition,
ShadowType.F_ATTRIBUTES, new QName("https://example.com/p", "string-mv"))
.eq("string-value2"),
shadow1Oid);
Expand Down Expand Up @@ -2425,16 +2425,24 @@ public void test760SearchUsersWithRightHandReferencedPath() {

// region reference search
@Test // TODO disable if not finished before merging
public void test800SearchReference() {
S_FilterEntryOrEmpty userTypeQuery = prismContext.queryFor(UserType.class);
ObjectQuery refQuery = userTypeQuery
.ref(UserType.F_ROLE_MEMBERSHIP_REF)
public void test800SearchReference() throws SchemaException {
OperationResult operationResult = createOperationResult();
ObjectFilter parentFilter = null;

// ObjectQuery refQuery = prismContext.queryFor(UserType.class)
ObjectQuery refQuery = prismContext.queryForReferenceOwnedBy(UserType.class, UserType.F_ROLE_MEMBERSHIP_REF)
.item(F_NAME).eq("actual-role-name")
.or()
.item(UserType.F_ROLE_MEMBERSHIP_REF).ref("target-oid")
.endBlock()
.and()
.ownedBy(UserType.class)
.id("user-oid-here")
.maxSize(5)
.build();
ObjectFilter parentFilter = userTypeQuery.id("user-oid-here").buildFilter();
// ObjectFilter parentFilter = userTypeQuery.id("user-oid-here").buildFilter();
SearchResultList<ObjectReferenceType> objectReferenceTypes =
repositoryService.searchReference(UserType.class, refQuery, parentFilter);
repositoryService.searchReferences(refQuery, null, operationResult);
}
// endregion

Expand Down

0 comments on commit 4fe60d3

Please sign in to comment.