deltaspike-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From t...@apache.org
Subject deltaspike git commit: DELTASPIKE-886 ignoreCase for criteria API
Date Thu, 30 Apr 2015 09:49:54 GMT
Repository: deltaspike
Updated Branches:
  refs/heads/master 46ab26f8a -> 32a67c766


DELTASPIKE-886 ignoreCase for criteria API


Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo
Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/32a67c76
Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/32a67c76
Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/32a67c76

Branch: refs/heads/master
Commit: 32a67c766cf504f6bc274528ffa238c2bbec028f
Parents: 46ab26f
Author: Thomas Hug <Thomas.Hug@ctp.com>
Authored: Thu Apr 30 11:49:27 2015 +0200
Committer: Thomas Hug <Thomas.Hug@ctp.com>
Committed: Thu Apr 30 11:49:27 2015 +0200

----------------------------------------------------------------------
 .../deltaspike/data/api/criteria/Criteria.java  | 32 +++++++++++++++
 .../data/impl/criteria/QueryCriteria.java       | 30 ++++++++++++++
 .../impl/criteria/predicate/EqIgnoreCase.java   | 43 ++++++++++++++++++++
 .../data/impl/criteria/predicate/Like.java      | 12 +++++-
 .../criteria/predicate/NotEqIgnoreCase.java     | 43 ++++++++++++++++++++
 .../data/impl/criteria/predicate/NotLike.java   | 12 +++++-
 .../data/impl/criteria/CriteriaTest.java        | 20 +++++++++
 .../test/service/SimpleCriteriaRepository.java  | 18 ++++++--
 8 files changed, 205 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/32a67c76/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/criteria/Criteria.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/criteria/Criteria.java
b/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/criteria/Criteria.java
index 6ff411b..72ab661 100644
--- a/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/criteria/Criteria.java
+++ b/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/criteria/Criteria.java
@@ -203,6 +203,14 @@ public interface Criteria<C, R>
     <P> Criteria<C, R> eq(SingularAttribute<? super C, P> att, P value);
 
     /**
+     * Equals predicate, case insensitive.
+     * @param att           The attribute to compare with.
+     * @param value         The comparison value.
+     * @return              Fluent API: Criteria instance.
+     */
+    <P> Criteria<C, R> eqIgnoreCase(SingularAttribute<? super C, String>
att, String value);
+
+    /**
      * Not Equals predicate.
      * @param att           The attribute to compare with.
      * @param value         The comparison value.
@@ -211,6 +219,14 @@ public interface Criteria<C, R>
     <P> Criteria<C, R> notEq(SingularAttribute<? super C, P> att, P value);
 
     /**
+     * Not Equals predicate, case insensitive.
+     * @param att           The attribute to compare with.
+     * @param value         The comparison value.
+     * @return              Fluent API: Criteria instance.
+     */
+    <P> Criteria<C, R> notEqIgnoreCase(SingularAttribute<? super C, String>
att, String value);
+
+    /**
      * Like predicate.
      * @param att           The attribute to compare with.
      * @param value         The comparison value.
@@ -219,6 +235,14 @@ public interface Criteria<C, R>
     <P> Criteria<C, R> like(SingularAttribute<? super C, String> att, String
value);
 
     /**
+     * Like predicate, case insensitive.
+     * @param att           The attribute to compare with.
+     * @param value         The comparison value.
+     * @return              Fluent API: Criteria instance.
+     */
+    <P> Criteria<C, R> likeIgnoreCase(SingularAttribute<? super C, String>
att, String value);
+
+    /**
      * Not like predicate.
      * @param att           The attribute to compare with.
      * @param value         The comparison value.
@@ -227,6 +251,14 @@ public interface Criteria<C, R>
     <P> Criteria<C, R> notLike(SingularAttribute<? super C, String> att,
String value);
 
     /**
+     * Not like predicate, case insensitive.
+     * @param att           The attribute to compare with.
+     * @param value         The comparison value.
+     * @return              Fluent API: Criteria instance.
+     */
+    <P> Criteria<C, R> notLikeIgnoreCase(SingularAttribute<? super C, String>
att, String value);
+
+    /**
      * Less than predicate.
      * @param att           The attribute to compare with.
      * @param value         The comparison value.

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/32a67c76/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/criteria/QueryCriteria.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/criteria/QueryCriteria.java
b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/criteria/QueryCriteria.java
index b530280..79ffbed 100644
--- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/criteria/QueryCriteria.java
+++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/criteria/QueryCriteria.java
@@ -48,6 +48,7 @@ import org.apache.deltaspike.data.api.criteria.QuerySelection;
 import org.apache.deltaspike.data.impl.builder.OrderDirection;
 import org.apache.deltaspike.data.impl.criteria.predicate.Between;
 import org.apache.deltaspike.data.impl.criteria.predicate.Eq;
+import org.apache.deltaspike.data.impl.criteria.predicate.EqIgnoreCase;
 import org.apache.deltaspike.data.impl.criteria.predicate.FetchBuilder;
 import org.apache.deltaspike.data.impl.criteria.predicate.GreaterThan;
 import org.apache.deltaspike.data.impl.criteria.predicate.GreaterThanOrEqual;
@@ -61,6 +62,7 @@ import org.apache.deltaspike.data.impl.criteria.predicate.LessThan;
 import org.apache.deltaspike.data.impl.criteria.predicate.LessThanOrEqual;
 import org.apache.deltaspike.data.impl.criteria.predicate.Like;
 import org.apache.deltaspike.data.impl.criteria.predicate.NotEq;
+import org.apache.deltaspike.data.impl.criteria.predicate.NotEqIgnoreCase;
 import org.apache.deltaspike.data.impl.criteria.predicate.NotLike;
 import org.apache.deltaspike.data.impl.criteria.predicate.OrBuilder;
 import org.apache.deltaspike.data.impl.criteria.predicate.PredicateBuilder;
@@ -367,6 +369,13 @@ public class QueryCriteria<C, R> implements Criteria<C, R>
     }
 
     @Override
+    public <P> Criteria<C, R> eqIgnoreCase(SingularAttribute<? super C, String>
att, String value)
+    {
+        add(new EqIgnoreCase<C>(att, value), value);
+        return this;
+    }
+
+    @Override
     public <P> Criteria<C, R> notEq(SingularAttribute<? super C, P> att,
P value)
     {
         add(new NotEq<C, P>(att, value), value);
@@ -374,6 +383,13 @@ public class QueryCriteria<C, R> implements Criteria<C, R>
     }
 
     @Override
+    public <P> Criteria<C, R> notEqIgnoreCase(SingularAttribute<? super C,
String> att, String value)
+    {
+        add(new NotEqIgnoreCase<C>(att, value), value);
+        return this;
+    }
+
+    @Override
     public <P> Criteria<C, R> like(SingularAttribute<? super C, String>
att, String value)
     {
         add(new Like<C>(att, value), value);
@@ -381,6 +397,13 @@ public class QueryCriteria<C, R> implements Criteria<C, R>
     }
 
     @Override
+    public <P> Criteria<C, R> likeIgnoreCase(SingularAttribute<? super C,
String> att, String value)
+    {
+        add(new Like<C>(att, value, true), value);
+        return this;
+    }
+
+    @Override
     public <P> Criteria<C, R> notLike(SingularAttribute<? super C, String>
att, String value)
     {
         add(new NotLike<C>(att, value), value);
@@ -388,6 +411,13 @@ public class QueryCriteria<C, R> implements Criteria<C, R>
     }
 
     @Override
+    public <P> Criteria<C, R> notLikeIgnoreCase(SingularAttribute<? super
C, String> att, String value)
+    {
+        add(new NotLike<C>(att, value, true), value);
+        return this;
+    }
+
+    @Override
     public <P extends Comparable<? super P>> Criteria<C, R> lt(SingularAttribute<?
super C, P> att, P value)
     {
         add(new LessThan<C, P>(att, value), value);

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/32a67c76/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/criteria/predicate/EqIgnoreCase.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/criteria/predicate/EqIgnoreCase.java
b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/criteria/predicate/EqIgnoreCase.java
new file mode 100644
index 0000000..2f93954
--- /dev/null
+++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/criteria/predicate/EqIgnoreCase.java
@@ -0,0 +1,43 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.deltaspike.data.impl.criteria.predicate;
+
+import java.util.Arrays;
+import java.util.List;
+
+import javax.persistence.criteria.CriteriaBuilder;
+import javax.persistence.criteria.Path;
+import javax.persistence.criteria.Predicate;
+import javax.persistence.metamodel.SingularAttribute;
+
+public class EqIgnoreCase<E> extends SingleValueBuilder<E, String>
+{
+
+    public EqIgnoreCase(SingularAttribute<? super E, String> att, String value)
+    {
+        super(att, value);
+    }
+
+    @Override
+    public List<Predicate> build(CriteriaBuilder builder, Path<E> path)
+    {
+        return Arrays.asList(builder.equal(builder.upper(path.<String>get(getAtt())),
getValue().toUpperCase()));
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/32a67c76/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/criteria/predicate/Like.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/criteria/predicate/Like.java
b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/criteria/predicate/Like.java
index 4445f38..ba852b3 100644
--- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/criteria/predicate/Like.java
+++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/criteria/predicate/Like.java
@@ -29,15 +29,25 @@ import javax.persistence.metamodel.SingularAttribute;
 public class Like<E> extends SingleValueBuilder<E, String>
 {
 
+    private final boolean caseInsensitive;
+
     public Like(SingularAttribute<? super E, String> att, String value)
     {
+        this(att, value, false);
+    }
+
+    public Like(SingularAttribute<? super E, String> att, String value, boolean caseInsensitive)
+    {
         super(att, value);
+        this.caseInsensitive = caseInsensitive;
     }
 
     @Override
     public List<Predicate> build(CriteriaBuilder builder, Path<E> path)
     {
-        return Arrays.asList(builder.like(path.get(getAtt()), getValue()));
+        return Arrays.asList(builder.like(
+                caseInsensitive ? builder.upper(path.<String>get(getAtt())) : path.get(getAtt()),
+                caseInsensitive ? getValue().toUpperCase() : getValue()));
     }
 
 }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/32a67c76/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/criteria/predicate/NotEqIgnoreCase.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/criteria/predicate/NotEqIgnoreCase.java
b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/criteria/predicate/NotEqIgnoreCase.java
new file mode 100644
index 0000000..9cb26ba
--- /dev/null
+++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/criteria/predicate/NotEqIgnoreCase.java
@@ -0,0 +1,43 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.deltaspike.data.impl.criteria.predicate;
+
+import java.util.Arrays;
+import java.util.List;
+
+import javax.persistence.criteria.CriteriaBuilder;
+import javax.persistence.criteria.Path;
+import javax.persistence.criteria.Predicate;
+import javax.persistence.metamodel.SingularAttribute;
+
+public class NotEqIgnoreCase<E> extends SingleValueBuilder<E, String>
+{
+
+    public NotEqIgnoreCase(SingularAttribute<? super E, String> att, String value)
+    {
+        super(att, value);
+    }
+
+    @Override
+    public List<Predicate> build(CriteriaBuilder builder, Path<E> path)
+    {
+        return Arrays.asList(builder.notEqual(builder.upper(path.<String>get(getAtt())),
getValue().toUpperCase()));
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/32a67c76/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/criteria/predicate/NotLike.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/criteria/predicate/NotLike.java
b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/criteria/predicate/NotLike.java
index 6c5244f..ee25070 100644
--- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/criteria/predicate/NotLike.java
+++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/criteria/predicate/NotLike.java
@@ -29,15 +29,25 @@ import javax.persistence.metamodel.SingularAttribute;
 public class NotLike<E> extends SingleValueBuilder<E, String>
 {
 
+    private final boolean caseInsensitive;
+
     public NotLike(SingularAttribute<? super E, String> att, String value)
     {
+        this(att, value, false);
+    }
+
+    public NotLike(SingularAttribute<? super E, String> att, String value, boolean
caseInsensitive)
+    {
         super(att, value);
+        this.caseInsensitive = caseInsensitive;
     }
 
     @Override
     public List<Predicate> build(CriteriaBuilder builder, Path<E> path)
     {
-        return Arrays.asList(builder.notLike(path.get(getAtt()), getValue()));
+        return Arrays.asList(builder.notLike(
+                caseInsensitive ? builder.upper(path.<String>get(getAtt())) : path.get(getAtt()),
+                caseInsensitive ? getValue().toUpperCase() : getValue()));
     }
 
 }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/32a67c76/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/criteria/CriteriaTest.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/criteria/CriteriaTest.java
b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/criteria/CriteriaTest.java
index 1efd4ac..44a3f32 100644
--- a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/criteria/CriteriaTest.java
+++ b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/criteria/CriteriaTest.java
@@ -89,6 +89,26 @@ public class CriteriaTest extends TransactionalTestCase
     }
 
     @Test
+    public void should_query_with_ignore_case()
+    {
+        // given
+        final String name = "TEST_query_EQ_with_igNOREe_case";
+        final String nameLike = "TEST_query_LIKE_with_igNOREe_case";
+        createSimple(name, 155);
+        createSimple(nameLike, 166);
+
+        // when
+        List<Simple> result1 = repo.queryByIgnoreCase(name.toLowerCase(), "no_match");
+        List<Simple> result2 = repo.queryByIgnoreCase("no_match", "%" + nameLike.substring(5,
22) + "%");
+
+        // then
+        assertEquals(1, result1.size());
+        assertEquals(Integer.valueOf(155), result1.get(0).getCounter());
+        assertEquals(1, result2.size());
+        assertEquals(Integer.valueOf(166), result2.get(0).getCounter());
+    }
+
+    @Test
     public void should_create_join_criteria_query()
     {
         // given

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/32a67c76/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/service/SimpleCriteriaRepository.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/service/SimpleCriteriaRepository.java
b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/service/SimpleCriteriaRepository.java
index acbaeb1..35c87fd 100644
--- a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/service/SimpleCriteriaRepository.java
+++ b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/service/SimpleCriteriaRepository.java
@@ -29,6 +29,7 @@ import org.apache.deltaspike.data.test.domain.Simple_;
 import org.apache.deltaspike.data.test.domain.SuperSimple_;
 
 @Repository
+@SuppressWarnings("unchecked")
 public abstract class SimpleCriteriaRepository extends AbstractEntityRepository<Simple,
Long>
         implements CriteriaSupport<Simple>
 {
@@ -42,6 +43,20 @@ public abstract class SimpleCriteriaRepository extends AbstractEntityRepository<
                 .getResultList();
     }
 
+    public List<Simple> queryByIgnoreCase(String name, String nameLike)
+    {
+        return criteria()
+                .or(
+                    criteria()
+                        .eqIgnoreCase(Simple_.name, name)
+                        .notEqIgnoreCase(Simple_.name, nameLike),
+                    criteria()
+                        .likeIgnoreCase(Simple_.name, nameLike)
+                        .notLikeIgnoreCase(Simple_.name, name)
+                 )
+                .getResultList();
+    }
+
     public Simple queryOptional(String name)
     {
         return criteria()
@@ -77,7 +92,6 @@ public abstract class SimpleCriteriaRepository extends AbstractEntityRepository<
                 .getSingleResult();
     }
 
-    @SuppressWarnings("unchecked")
     public Statistics queryWithSelect(String name)
     {
         return criteria()
@@ -86,7 +100,6 @@ public abstract class SimpleCriteriaRepository extends AbstractEntityRepository<
                 .getSingleResult();
     }
 
-    @SuppressWarnings("unchecked")
     public Object[] queryWithSelectAggregateReturnArray(String name)
     {
         return criteria()
@@ -97,7 +110,6 @@ public abstract class SimpleCriteriaRepository extends AbstractEntityRepository<
                 .getSingleResult();
     }
 
-    @SuppressWarnings("unchecked")
     public List<Object[]> queryWithSelectAttributes(String name)
     {
         return criteria()


Mime
View raw message