Skip to content

Commit

Permalink
[backend] feat: allow filter on assets from asset groups
Browse files Browse the repository at this point in the history
  • Loading branch information
impolitepanda committed Feb 7, 2025
1 parent dd5a99c commit 3e3b776
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ private SearchUtilsJpa() {}

@SuppressWarnings("unchecked")
public static <T> Specification<T> computeSearchJpa(@Nullable final String search) {

if (!hasText(search)) {
return (Specification<T>) EMPTY_SPECIFICATION;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,31 @@ private static <T, U> Specification<T> computeFilter(
}
List<PropertySchema> filterableProperties = getFilterableProperties(propertySchemas);
PropertySchema filterableProperty = retrieveProperty(filterableProperties, filterKey);

// multiple paths case
if (filterableProperty.getPaths().length > 1) {
List<Predicate> predicates = new ArrayList<>();
for (String path : filterableProperty.getPaths()) {
PropertySchema singlePathPropertySchema =
PropertySchema.builder()
.name(filterableProperty.getName())
.type(filterableProperty.getType())
.path(path)
.build();
Expression<U> paths = toPath(singlePathPropertySchema, root, joinMap);
predicates.add(
toPredicate(
paths,
filter,
cb,
filterableProperty.getJoinTable() != null
? String.class
: filterableProperty.getType()));
}
return cb.or(predicates.toArray(Predicate[]::new));
}

// Single path or no path case
Expression<U> paths = toPath(filterableProperty, root, joinMap);
// In case of join table, we will use ID so type is String
return toPredicate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class PropertySchema {

private final JoinTable joinTable;
private final String path;
private final String[] paths;

@Singular("propertySchema")
private final List<PropertySchema> propertiesSchema;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,11 @@ private static void processAnnotations(
.filterable(queryable.filterable())
.dynamicValues(queryable.dynamicValues())
.sortable(queryable.sortable())
.path(queryable.path());
.path(queryable.path())
.paths(queryable.paths());
if (member instanceof Method) {
builder.type(queryable.clazz()); // Override
} else if (member instanceof Field && hasText(queryable.path())) {
} else if (hasText(queryable.path()) || queryable.paths().length > 0) {
builder.type(queryable.clazz()); // Override
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@

String path() default "";

String[] paths() default {};

Class clazz() default String.class;
}
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,9 @@ public class Inject implements Base, Injection {
inverseJoinColumns = @JoinColumn(name = "asset_id"))
@JsonSerialize(using = MultiIdListDeserializer.class)
@JsonProperty("inject_assets")
@Queryable(filterable = true, path = "assets.name")
@Queryable(
filterable = true,
paths = {"assets.name", "assetGroups.assets.name"})
private List<Asset> assets = new ArrayList<>();

@ArraySchema(schema = @Schema(type = "string"))
Expand Down

0 comments on commit 3e3b776

Please sign in to comment.