Skip to content

Commit

Permalink
Issue #598: query docTypeField by docType and parent
Browse files Browse the repository at this point in the history
  • Loading branch information
mrk-vi committed Sep 25, 2023
1 parent 9d29e08 commit 758dcd2
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,20 @@ public Uni<Connection<DocTypeField>> getDocTypeFieldsFromDocType(
docTypeId, after, before, first, last, searchText, sortByList, notEqual);
}

@Query
public Uni<Connection<DocTypeField>> getDocTypeFieldsFromDocTypeByParent(
@Id long docTypeId,
@Description("id of the parent docTypeField (0 if root )") long parentId,
@Description("fetching only nodes after this node (exclusive)") String after,
@Description("fetching only nodes before this node (exclusive)") String before,
@Description("fetching only the first certain number of nodes") Integer first,
@Description("fetching only the last certain number of nodes") Integer last,
String searchText, Set<SortBy> sortByList,
@Description("if notEqual is true, it returns unbound entities") @DefaultValue("false") boolean notEqual) {
return docTypeService.getDocTypeFieldsConnectionByParent(
docTypeId, parentId, after, before, first, last, searchText, sortByList, notEqual);
}

@Query
public Uni<DocType> getDocType(@Id long id) {
return docTypeService.findById(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.openk9.datasource.mapper.DocTypeMapper;
import io.openk9.datasource.model.DocType;
import io.openk9.datasource.model.DocTypeField;
import io.openk9.datasource.model.DocTypeField_;
import io.openk9.datasource.model.DocTypeTemplate;
import io.openk9.datasource.model.DocType_;
import io.openk9.datasource.model.dto.DocTypeDTO;
Expand All @@ -40,6 +41,7 @@
import javax.inject.Inject;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Root;
import java.util.Collection;
import java.util.List;
Expand All @@ -61,12 +63,30 @@ public String[] getSearchFields() {
public Uni<Connection<DocTypeField>> getDocTypeFieldsConnection(
Long id, String after, String before, Integer first, Integer last,
String searchText, Set<SortBy> sortByList, boolean notEqual) {

return findJoinConnection(
id, DocType_.DOC_TYPE_FIELDS, DocTypeField.class,
docTypeFieldService.getSearchFields(), after, before, first, last,
searchText, sortByList, notEqual);
}

public Uni<Connection<DocTypeField>> getDocTypeFieldsConnectionByParent(
long docTypeId, long parentId, String after, String before, Integer first, Integer last,
String searchText, Set<SortBy> sortByList, boolean notEqual) {

return findJoinConnection(
docTypeId, DocType_.DOC_TYPE_FIELDS, DocTypeField.class,
docTypeFieldService.getSearchFields(), after, before, first, last,
searchText, sortByList, notEqual,
(criteriaBuilder, join) -> {
Path<DocTypeField> parentField = join.get(DocTypeField_.parentDocTypeField);

return parentId > 0
? criteriaBuilder.equal(parentField.get(DocTypeField_.id), parentId)
: criteriaBuilder.isNull(parentField);
});
}

public Uni<Page<DocTypeField>> getDocTypeFields(
long docTypeId, Pageable pageable) {
return getDocTypeFields(docTypeId, pageable, Filter.DEFAULT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,22 @@ public <T extends GraphqlId> Uni<Connection<T>> findJoinConnection(

}

public <T extends GraphqlId> Uni<Connection<T>> findJoinConnection(
long entityId, String joinField, Class<T> joinType,
String[] searchFields, String after,
String before, Integer first, Integer last, String searchText,
Set<SortBy> sortByList, boolean not,
BiFunction<CriteriaBuilder, Path<T>, Predicate> whereFun) {

return findJoinConnection(
entityId, joinField, joinType, searchFields, after, before, first,
last, searchText, sortByList, not,
entityRoot -> entityRoot.join(joinField),
(i1, i2) -> List.of(),
whereFun);

}

public <T extends GraphqlId> Uni<Connection<T>> findJoinConnection(
long entityId, String joinField, Class<T> joinType,
String[] searchFields, String after,
Expand All @@ -88,6 +104,24 @@ public <T extends GraphqlId> Uni<Connection<T>> findJoinConnection(
Function<Root<ENTITY>, Path<T>> mapper,
BiFunction<CriteriaBuilder, Root<ENTITY>, List<Order>> defaultOrderFun) {

return findJoinConnection(
entityId, joinField, joinType, searchFields, after, before, first,
last, searchText, sortByList, not,
mapper,
defaultOrderFun,
(criteriaBuilder, tPath) -> criteriaBuilder.conjunction());

}

public <T extends GraphqlId> Uni<Connection<T>> findJoinConnection(
long entityId, String joinField, Class<T> joinType,
String[] searchFields, String after,
String before, Integer first, Integer last, String searchText,
Set<SortBy> sortByList, boolean not,
Function<Root<ENTITY>, Path<T>> mapper,
BiFunction<CriteriaBuilder, Root<ENTITY>, List<Order>> defaultOrderFun,
BiFunction<CriteriaBuilder, Path<T>, Predicate> whereFun) {

CriteriaBuilder builder = getCriteriaBuilder();

CriteriaQuery<T> joinEntityQuery = builder.createQuery(joinType);
Expand All @@ -110,7 +144,9 @@ public <T extends GraphqlId> Uni<Connection<T>> findJoinConnection(

return findConnection(
joinEntityQuery, upperRoot,
builder.in(upperRoot.get(getIdAttribute())).value(subquery).not(),
builder.and(
whereFun.apply(builder, subJoin),
builder.in(upperRoot.get(getIdAttribute())).value(subquery).not()),
searchFields, after, before, first, last, searchText, sortByList);

}
Expand All @@ -124,7 +160,9 @@ public <T extends GraphqlId> Uni<Connection<T>> findJoinConnection(

return findConnection(
joinEntityQuery.select(join), join,
builder.equal(entityRoot.get(getIdAttribute()), entityId),
builder.and(
whereFun.apply(builder, join),
builder.equal(entityRoot.get(getIdAttribute()), entityId)),
searchFields, after, before, first, last, searchText, sortByList);
}

Expand Down

0 comments on commit 758dcd2

Please sign in to comment.