[Lada-commits] [PATCH 4 of 5] Implement negation of query filter closer to underlying API

Wald Commits scm-commit at wald.intevation.org
Fri Dec 16 12:19:55 CET 2016


# HG changeset patch
# User Tom Gottfried <tom at intevation.de>
# Date 1481886394 -3600
# Node ID 86ef81592f67abe22be5a4e2c69236cfaa5a97bd
# Parent  b652f4c9b75c50eefc308744bf14131346fa162e
Implement negation of query filter closer to underlying API.

Previously andNot() negated the whole filter and it's usage in
IsUnique was actually a misunderstanding. Having not() as in
javax.persistence.criteria.Predicate seems to be more intuitive for
negating the whole filter and more flexible.

diff -r b652f4c9b75c -r 86ef81592f67 src/main/java/de/intevation/lada/rest/stamm/DeskriptorService.java
--- a/src/main/java/de/intevation/lada/rest/stamm/DeskriptorService.java	Thu Dec 15 16:16:32 2016 +0100
+++ b/src/main/java/de/intevation/lada/rest/stamm/DeskriptorService.java	Fri Dec 16 12:06:34 2016 +0100
@@ -104,7 +104,7 @@
         QueryBuilder<Deskriptoren> builder = new QueryBuilder<Deskriptoren>(
             repository.entityManager("stamm"),
             Deskriptoren.class);
-        builder.andNot("sn", 0);
+        builder.and("sn", 0).not();
         if (params.containsKey("layer") &&
             !params.containsKey("parents")) {
             String layer = params.getFirst("layer");
diff -r b652f4c9b75c -r 86ef81592f67 src/main/java/de/intevation/lada/util/data/QueryBuilder.java
--- a/src/main/java/de/intevation/lada/util/data/QueryBuilder.java	Thu Dec 15 16:16:32 2016 +0100
+++ b/src/main/java/de/intevation/lada/util/data/QueryBuilder.java	Fri Dec 16 12:06:34 2016 +0100
@@ -79,20 +79,14 @@
     }
 
     /**
-     * Logical AND NOT operation.
+     * Negate filter.
      *
-     * @param id    The database column name.
-     * @param value The filter value
-     * @return The builder itself.
      */
-    public QueryBuilder<T> andNot(String id, Object value) {
-        Predicate p = this.builder.equal(this.root.get(id), value);
-        if (this.filter != null) {
-            this.filter = this.builder.and(this.filter, p).not();
+    public QueryBuilder<T> not() {
+        if (this.filter == null) {
+            return this;
         }
-        else {
-            this.filter = this.builder.and(p).not();
-        }
+        this.filter = this.filter.not();
         return this;
     }
 
diff -r b652f4c9b75c -r 86ef81592f67 src/main/java/de/intevation/lada/validation/rules/ort/IsUnique.java
--- a/src/main/java/de/intevation/lada/validation/rules/ort/IsUnique.java	Thu Dec 15 16:16:32 2016 +0100
+++ b/src/main/java/de/intevation/lada/validation/rules/ort/IsUnique.java	Fri Dec 16 12:06:34 2016 +0100
@@ -31,12 +31,12 @@
 
         QueryBuilder<Ort> builder = new QueryBuilder<Ort>(
             repository.entityManager("stamm"), Ort.class);
+        if (ort.getId() != null) {
+            // Consider UPDATE
+            builder.and("id", ort.getId()).not();
+        }
         builder.and("netzbetreiberId", ort.getNetzbetreiberId());
         builder.and("ortId", ort.getOrtId());
-        if (ort.getId() != null) {
-            // Consider UPDATE
-            builder.andNot("id", ort.getId());
-        }
         if (!repository.filterPlain(builder.getQuery(), "stamm").isEmpty()) {
             Violation violation = new Violation();
             violation.addError("ortId", 672);


More information about the Lada-commits mailing list