Skip to content

Commit

Permalink
Removed deprecated Hibernate-specific code in QueryPredicate (DSpace#…
Browse files Browse the repository at this point in the history
…9381)

 Cleaned up deprecated Hibernate-specific code

---------

Co-authored-by: Jean-François Morin <[email protected]>
  • Loading branch information
jeffmorin and Jean-François Morin authored Feb 29, 2024
1 parent 63cf153 commit 529c495
Showing 1 changed file with 0 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
package org.dspace.contentreport;

import java.util.Arrays;
import java.util.function.BiFunction;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Path;
Expand All @@ -21,10 +20,6 @@
import org.dspace.content.MetadataValue_;
import org.dspace.util.DSpacePostgreSQLDialect;
import org.dspace.util.JpaCriteriaBuilderKit;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.hibernate.type.StandardBasicTypes;

/**
* Operators available for creating predicates to query the
Expand All @@ -34,52 +29,36 @@
public enum QueryOperator {

EXISTS("exists", true, false,
(val, regexClause) -> Property.forName("mv.value").isNotNull(),
(val, regexClause, jpaKit) -> jpaKit.criteriaBuilder().isNotNull(jpaKit.root().get(MetadataValue_.VALUE))),
DOES_NOT_EXIST("doesnt_exist", true, true,
(val, regexClause) -> EXISTS.buildPredicate(val, regexClause),
(val, regexClause, jpaKit) -> EXISTS.buildJpaPredicate(val, regexClause, jpaKit)),
EQUALS("equals", true, false,
(val, regexClause) -> Property.forName("mv.value").eq(val),
(val, regexClause, jpaKit) -> jpaKit.criteriaBuilder().equal(jpaKit.root().get(MetadataValue_.VALUE), val)),
DOES_NOT_EQUAL("not_equals", true, true,
(val, regexClause) -> EQUALS.buildPredicate(val, regexClause),
(val, regexClause, jpaKit) -> EQUALS.buildJpaPredicate(val, regexClause, jpaKit)),
LIKE("like", true, false,
(val, regexClause) -> Property.forName("mv.value").like(val),
(val, regexClause, jpaKit) -> jpaKit.criteriaBuilder().like(jpaKit.root().get(MetadataValue_.VALUE), val)),
NOT_LIKE("not_like", true, true,
(val, regexClause) -> LIKE.buildPredicate(val, regexClause),
(val, regexClause, jpaKit) -> LIKE.buildJpaPredicate(val, regexClause, jpaKit)),
CONTAINS("contains", true, false,
(val, regexClause) -> Property.forName("mv.value").like("%" + val + "%"),
(val, regexClause, jpaKit) -> LIKE.buildJpaPredicate("%" + val + "%", regexClause, jpaKit)),
DOES_NOT_CONTAIN("doesnt_contain", true, true,
(val, regexClause) -> CONTAINS.buildPredicate(val, regexClause),
(val, regexClause, jpaKit) -> CONTAINS.buildJpaPredicate(val, regexClause, jpaKit)),
MATCHES("matches", false, false,
(val, regexClause) -> Restrictions.sqlRestriction(regexClause, val, StandardBasicTypes.STRING),
(val, regexClause, jpaKit) -> regexPredicate(val, DSpacePostgreSQLDialect.REGEX_MATCHES, jpaKit)),
DOES_NOT_MATCH("doesnt_match", false, false,
(val, regexClause) -> Restrictions.not(Restrictions.sqlRestriction(
regexClause, val, StandardBasicTypes.STRING)),
(val, regexClause, jpaKit) -> regexPredicate(val, DSpacePostgreSQLDialect.REGEX_NOT_MATCHES, jpaKit));

private final String code;
/** Criteria builder for the old Hibernate API */
@Deprecated(forRemoval = true)
private final BiFunction<String, String, Criterion> criterionBuilder;
private final TriFunction<String, String, JpaCriteriaBuilderKit<MetadataValue>, Predicate> predicateBuilder;
private final boolean usesRegex;
private final boolean negate;

QueryOperator(String code, boolean usesRegex, boolean negate,
BiFunction<String, String, Criterion> criterionBuilder,
TriFunction<String, String, JpaCriteriaBuilderKit<MetadataValue>, Predicate> predicateBuilder) {
this.code = code;
this.usesRegex = usesRegex;
this.negate = negate;
this.criterionBuilder = criterionBuilder;
this.predicateBuilder = predicateBuilder;
}

Expand All @@ -96,10 +75,6 @@ public boolean getNegate() {
return negate;
}

public Criterion buildPredicate(String val, String regexClause) {
return criterionBuilder.apply(val, regexClause);
}

public Predicate buildJpaPredicate(String val, String regexClause, JpaCriteriaBuilderKit<MetadataValue> jpaKit) {
return predicateBuilder.apply(val, regexClause, jpaKit);
}
Expand All @@ -112,10 +87,6 @@ public static QueryOperator get(String code) {
.orElse(null);
}

public BiFunction<String, String, Criterion> getCriterionBuilder() {
return criterionBuilder;
}

private static Predicate regexPredicate(String val, String regexFunction,
JpaCriteriaBuilderKit<MetadataValue> jpaKit) {
// Source: https://stackoverflow.com/questions/24995881/use-regular-expressions-in-jpa-criteriabuilder
Expand Down

0 comments on commit 529c495

Please sign in to comment.