changeset 303:c96fcc4dd5fd

New filter method with free parameters and resultset.
author Raimund Renkert <rrenkert@intevation.de>
date Fri, 16 Aug 2013 14:39:27 +0200
parents a529909cffeb
children 2d3f293899e8
files src/main/java/de/intevation/lada/data/LProbeRepository.java
diffstat 1 files changed, 37 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/de/intevation/lada/data/LProbeRepository.java	Thu Aug 15 15:20:41 2013 +0200
+++ b/src/main/java/de/intevation/lada/data/LProbeRepository.java	Fri Aug 16 14:39:27 2013 +0200
@@ -1,5 +1,7 @@
 package de.intevation.lada.data;
 
+import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
@@ -9,9 +11,12 @@
 import javax.inject.Named;
 import javax.persistence.EntityExistsException;
 import javax.persistence.EntityManager;
+import javax.persistence.Query;
 import javax.persistence.TransactionRequiredException;
 import javax.persistence.criteria.CriteriaQuery;
+import javax.ws.rs.core.MultivaluedMap;
 
+import de.intevation.lada.auth.Authorization;
 import de.intevation.lada.manage.Manager;
 import de.intevation.lada.model.LProbe;
 import de.intevation.lada.model.LProbeInfo;
@@ -47,6 +52,10 @@
     @Named("lprobevalidator")
     private Validator validator;
 
+    @Inject
+    @Named("dataauthorization")
+    private Authorization authorization;
+
     public EntityManager getEntityManager() {
         return this.em;
     }
@@ -62,6 +71,34 @@
         return new Response(true, 200, result);
     }
 
+    public Response filterFree(
+        String sql,
+        List<String> filters,
+        List<String> results,
+        MultivaluedMap<String, String> params) {
+        Query query = em.createNativeQuery(sql);
+        for (String filter: filters) {
+            query.setParameter(filter, params.get(filter));
+        }
+        List<Object[]> result = query.getResultList();
+        List<Map<String, Object>> res = new ArrayList<Map<String, Object>>();
+        for (Object[] row: result) {
+            Map<String, Object> set = new HashMap<String, Object>();
+            for (int i = 0; i < row.length; i++) {
+                set.put(results.get(i), row[i]);
+                if (results.get(i).equals("probeId")) {
+                    if (authorization.isReadOnly((String)row[i])) {
+                        set.put("readonly", Boolean.TRUE);
+                    }
+                    else {
+                        set.put("readonly", Boolean.FALSE);
+                    }
+                }
+            }
+            res.add(set);
+        }
+        return new Response(true, 200, res);
+    }
     /**
      * Get all objects.
      *
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)