changeset 306:b9295222ff06

merged.
author Raimund Renkert <rrenkert@intevation.de>
date Fri, 16 Aug 2013 14:42:37 +0200
parents f0e1caad6027 (diff) 71284b42ba39 (current diff)
children 82bea32eddb7
files src/main/java/de/intevation/lada/utils/QueryTools.java
diffstat 3 files changed, 87 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/de/intevation/lada/data/LProbeRepository.java	Fri Aug 16 12:27:43 2013 +0200
+++ b/src/main/java/de/intevation/lada/data/LProbeRepository.java	Fri Aug 16 14:42:37 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.
      *
--- a/src/main/java/de/intevation/lada/rest/LProbeService.java	Fri Aug 16 12:27:43 2013 +0200
+++ b/src/main/java/de/intevation/lada/rest/LProbeService.java	Fri Aug 16 14:42:37 2013 +0200
@@ -2,6 +2,7 @@
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Map;
 import java.util.logging.Logger;
 
 import javax.enterprise.context.RequestScoped;
@@ -19,16 +20,23 @@
 import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.core.UriInfo;
 
+import org.apache.commons.io.IOUtils;
+import org.jboss.resteasy.plugins.providers.multipart.InputPart;
+import org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput;
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+
 import de.intevation.lada.auth.Authentication;
 import de.intevation.lada.auth.AuthenticationException;
 import de.intevation.lada.auth.AuthenticationResponse;
 import de.intevation.lada.auth.Authorization;
+import de.intevation.lada.data.LProbeRepository;
 import de.intevation.lada.data.QueryBuilder;
 import de.intevation.lada.data.Repository;
 import de.intevation.lada.model.LProbe;
 import de.intevation.lada.model.LProbeInfo;
-
-import org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput;
+import de.intevation.lada.utils.QueryTools;
 
 /**
 * This class produces a RESTful service to read, write and update
@@ -134,38 +142,52 @@
                 new QueryBuilder<LProbeInfo>(
                     repository.getEntityManager(),
                     LProbeInfo.class);
-            builder.or("netzbetreiberId", auth.getNetzbetreiber());
-            builder.or("fertig", Boolean.TRUE);
+
             MultivaluedMap<String, String> params = info.getQueryParameters();
-            if (params.isEmpty()) {
+            if (params.isEmpty() || !params.containsKey("qid")) {
                 return repository.filter(builder.getQuery());
             }
-            QueryBuilder<LProbeInfo> mstBuilder = builder.getEmptyBuilder();
-            if (params.keySet().contains("mstId")) {
-                String[] paramValues = params.getFirst("mstId").split(",");
-                for (String pv: paramValues) {
-                    mstBuilder.or("mstId", pv);
+            String qid = params.getFirst("qid");
+            JSONObject query = QueryTools.getQueryById(qid);
+            List<String> filters = new ArrayList<String>();
+            List<String> results = new ArrayList<String>();
+            String sql = "";
+            try {
+                sql = query.getString("sql");
+                JSONArray jFilters = query.getJSONArray("filters");
+                for (int i = 0; i < jFilters.length(); i++) {
+                    JSONObject jFilter = jFilters.getJSONObject(i);
+                    filters.add(jFilter.getString("dataIndex"));
                 }
-                builder.and(mstBuilder);
+                JSONArray jResults = query.getJSONArray("result");
+                for (int i = 0; i < jResults.length(); i++) {
+                    JSONObject jResult = jResults.getJSONObject(i);
+                    results.add(jResult.getString("dataIndex"));
+                }
             }
-            QueryBuilder<LProbeInfo> umwBuilder = builder.getEmptyBuilder();
-            if (params.keySet().contains("umwId")) {
-                String[] paramValues = params.getFirst("umwId").split(",");
-                for (String pv: paramValues) {
-                    umwBuilder.or("umwId", pv);
-                }
-                builder.and(umwBuilder);
+            catch (JSONException e) {
+                return new Response(false, 603, new ArrayList<LProbeInfo>());
             }
-            QueryBuilder<LProbeInfo> beginBuilder = builder.getEmptyBuilder();
-            if (params.keySet().contains("bedin")) {
-                String[] paramValues = params.getFirst("begin").split(",");
-                for (String pv: paramValues) {
-                    beginBuilder.or("probeentnahmeBegin", pv);
+
+            if (sql == null || sql.length() == 0) {
+                return new Response(false, 603, new ArrayList<LProbeInfo>());
+            }
+            LProbeRepository lpr = (LProbeRepository)repository;
+            String subselect = "(select * from l_probe_info where ";
+            List<String> netzbetreiberIds = auth.getNetzbetreiber();
+            boolean first = true;
+            for (String netzbetreiberId: netzbetreiberIds) {
+                if (first) {
+                    subselect += "netzbetreiber_id = '" + netzbetreiberId + "' ";
+                    first = false;
                 }
-                builder.and(beginBuilder);
+                else {
+                    subselect += "or netzbetreiber_id = '" + netzbetreiberId + "' ";
+                }
             }
-            builder.distinct();
-            return repository.filter(builder.getQuery());
+            subselect += "or fertig = true) as lp";
+            sql = sql.replace("l_probe", subselect);
+            return lpr.filterFree(sql, filters, results, params);
         }
         catch(AuthenticationException ae) {
             return new Response(false, 699, new ArrayList<LProbe>());
--- a/src/main/java/de/intevation/lada/utils/QueryTools.java	Fri Aug 16 12:27:43 2013 +0200
+++ b/src/main/java/de/intevation/lada/utils/QueryTools.java	Fri Aug 16 14:42:37 2013 +0200
@@ -65,8 +65,8 @@
                     ResultConfig config = new ResultConfig();
                     config.setDataIndex(result.getString("dataIndex"));
                     config.setHeader(result.getString("header"));
-                    config.setWidth(result.optInt("width"));
-                    config.setFlex(result.optInt("flex"));
+                    config.setWidth(result.optInt("width", 100));
+                    config.setFlex(result.optInt("flex", 0));
                     sResults.add(config);
                 }
                 qConf.setResults(sResults);
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)