changeset 788:7133f6114920

Added 'order by' for multiple columns.
author Raimund Renkert <raimund.renkert@intevation.de>
date Wed, 18 Nov 2015 15:38:18 +0100
parents 7033810468d9
children dea5ed4b7fa4
files src/main/java/de/intevation/lada/util/data/QueryBuilder.java
diffstat 1 files changed, 21 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/de/intevation/lada/util/data/QueryBuilder.java	Wed Nov 18 15:37:32 2015 +0100
+++ b/src/main/java/de/intevation/lada/util/data/QueryBuilder.java	Wed Nov 18 15:38:18 2015 +0100
@@ -7,12 +7,15 @@
  */
 package de.intevation.lada.util.data;
 
+import java.util.ArrayList;
 import java.util.List;
+import java.util.Map;
 
 import javax.persistence.EntityManager;
 import javax.persistence.criteria.CriteriaBuilder;
 import javax.persistence.criteria.CriteriaQuery;
 import javax.persistence.criteria.Expression;
+import javax.persistence.criteria.Order;
 import javax.persistence.criteria.Path;
 import javax.persistence.criteria.Predicate;
 import javax.persistence.criteria.Root;
@@ -301,6 +304,24 @@
     }
 
     /**
+     * Order result by the specified column name
+     *
+     * @param ids   Map of column names and boolean for asc/desc.
+     */
+    public void orderBy(Map<String, Boolean> ids) {
+        List<Order> order = new ArrayList<Order>();
+        for (String id : ids.keySet()) {
+            if (ids.get(id)) {
+                order.add(this.builder.asc(this.root.get(id)));
+            }
+            else {
+                order.add(this.builder.desc(this.root.get(id)));
+            }
+        }
+        this.query.orderBy(order);
+    }
+
+    /**
      * Get an empty instance of this builder to create subfilters.
      *
      * @return An empty instance of this builder.
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)