Skip to content

Commit

Permalink
fix: update jpa predicates to match filter query intent and avoid sub…
Browse files Browse the repository at this point in the history
…query matching
  • Loading branch information
DropSnorz committed Aug 12, 2024
1 parent d2a14e7 commit 05c45c0
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,15 @@ private void performPackageSearch() {
}
}

List<String> formats = new ArrayList<>();
for (Entry<String, CheckBox> entry : formatsFilterCheckBoxes.entrySet()) {
if (entry.getValue().isSelected()) {
criteriaList.add(new StoreFilterCriteria(entry.getKey(), ExploreFilterCriteriaType.FORMAT));
formats.add(entry.getKey());
}
}
if (formats.size() > 0) {
criteriaList.add(new StoreFilterCriteria(formats, ExploreFilterCriteriaType.FORMAT_LIST));
}

Task<Iterable<RemotePackage>> task = new Task<Iterable<RemotePackage>>() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
package com.owlplug.explore.dao;

import com.owlplug.core.model.PluginType;
import com.owlplug.explore.model.PackageBundle;
import com.owlplug.explore.model.RemotePackage;
import jakarta.persistence.criteria.Fetch;
import jakarta.persistence.criteria.Join;
import jakarta.persistence.criteria.JoinType;
import jakarta.persistence.criteria.Predicate;
Expand Down Expand Up @@ -70,8 +72,24 @@ static Specification<RemotePackage> hasCreator(String creator) {
@SuppressWarnings("unchecked")
static Specification<RemotePackage> hasFormat(String format) {
return (remotePackage, cq, cb) -> {
Join<Object, Object> bundles = (Join<Object, Object>) remotePackage.fetch("bundles");
return cb.isMember(format, bundles.get("formats"));
Join<Object, Object> bundles = (Join<Object, Object>) remotePackage.fetch("bundles", JoinType.INNER);
return bundles.join("formats").in(format);
};
}

/**
* Platform filtering JPA Specification Filter packages matching the given
* platformTag or packages without platform assignment.
*
* @param formatList - The compatible platformTagList to find
* @return The JPA Specification
*/
@SuppressWarnings("unchecked")
static Specification<RemotePackage> hasFormat(List<String> formatList) {
return (remotePackage, cq, cb) -> {
Join<Object, Object> bundles = (Join<Object, Object>) remotePackage.fetch("bundles", JoinType.INNER);
return bundles.join("formats").in(formatList);

};
}

Expand All @@ -84,8 +102,8 @@ static Specification<RemotePackage> hasFormat(String format) {
@SuppressWarnings("unchecked")
static Specification<RemotePackage> hasPlatformTag(String platformTag) {
return (remotePackage, cq, cb) -> {
Join<Object, Object> bundles = (Join<Object, Object>) remotePackage.fetch("bundles");
return cb.isMember(platformTag, bundles.get("targets"));
Join<Object, Object> bundles = (Join<Object, Object>) remotePackage.fetch("bundles", JoinType.INNER);
return bundles.join("targets").in(platformTag);
};
}

Expand All @@ -100,12 +118,8 @@ static Specification<RemotePackage> hasPlatformTag(String platformTag) {
static Specification<RemotePackage> hasPlatformTag(List<String> platformTagList) {
return (remotePackage, cq, cb) -> {
Join<Object, Object> bundles = (Join<Object, Object>) remotePackage.fetch("bundles", JoinType.INNER);
List<Predicate> predicates = new ArrayList<>();
for (String platformTag : platformTagList) {
predicates.add(cb.or(cb.isMember(platformTag, bundles.get("targets"))));
}
cq.distinct(true);
return cb.or(predicates.toArray(new Predicate[predicates.size()]));
return bundles.join("targets").in(platformTagList);

};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@
package com.owlplug.explore.model.search;

public enum ExploreFilterCriteriaType {
NAME, TAG, TYPE, PLATFORM, CREATOR, FORMAT
NAME, TAG, TYPE, PLATFORM, CREATOR, FORMAT, FORMAT_LIST
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ public static Specification<RemotePackage> toSpecification(StoreFilterCriteria c
if (criteria.getFilterType().equals(ExploreFilterCriteriaType.FORMAT)) {
return RemotePackageDAO.hasFormat(String.valueOf(criteria.getValue()));
}
if (criteria.getFilterType().equals(ExploreFilterCriteriaType.FORMAT_LIST)) {
return RemotePackageDAO.hasFormat((List<String>) criteria.getValue());
}
return Specification.where(null);

}
Expand Down
4 changes: 2 additions & 2 deletions owlplug-client/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ spring.main.web-environment=false

# Logger
# Available levels are: TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF
#logging.level.org.hibernate.SQL=DEBUG
#logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE

0 comments on commit 05c45c0

Please sign in to comment.