changeset 299:a529909cffeb

merged.
author Raimund Renkert <rrenkert@intevation.de>
date Thu, 15 Aug 2013 15:20:41 +0200
parents 93b12b077edf (diff) 422e9ea0276f (current diff)
children dd0dec2609ad c96fcc4dd5fd
files pom.xml
diffstat 6 files changed, 344 insertions(+), 227 deletions(-) [+]
line wrap: on
line diff
--- a/pom.xml	Thu Aug 15 14:59:25 2013 +0200
+++ b/pom.xml	Thu Aug 15 15:20:41 2013 +0200
@@ -191,6 +191,11 @@
             <artifactId>arquillian-protocol-servlet</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+        	<groupId>org.json</groupId>
+        	<artifactId>json</artifactId>
+        	<version>20090211</version>
+        </dependency>
     </dependencies>
 
     <build>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/de/intevation/lada/model/query/QueryConfig.java	Thu Aug 15 15:20:41 2013 +0200
@@ -0,0 +1,102 @@
+package de.intevation.lada.model.query;
+
+import java.util.List;
+
+
+public class QueryConfig
+{
+    int id;
+    String name;
+    String description;
+    String sql;
+    List<QueryFilter> filters;
+    List<ResultConfig> results;
+
+    public QueryConfig()
+    {
+    }
+
+    /**
+     * @return the id
+     */
+    public int getId() {
+        return id;
+    }
+
+    /**
+     * @param id the id to set
+     */
+    public void setId(int id) {
+        this.id = id;
+    }
+
+    /**
+     * @return the name
+     */
+    public String getName() {
+        return name;
+    }
+
+    /**
+     * @param name the name to set
+     */
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    /**
+     * @return the description
+     */
+    public String getDescription() {
+        return description;
+    }
+
+    /**
+     * @param description the description to set
+     */
+    public void setDescription(String description) {
+        this.description = description;
+    }
+
+    /**
+     * @return the sql
+     */
+    public String getSql() {
+        return sql;
+    }
+
+    /**
+     * @param sql the sql to set
+     */
+    public void setSql(String sql) {
+        this.sql = sql;
+    }
+
+    /**
+     * @return the filter
+     */
+    public List<QueryFilter> getFilters() {
+        return filters;
+    }
+
+    /**
+     * @param filter the filter to set
+     */
+    public void setFilters(List<QueryFilter> filters) {
+        this.filters = filters;
+    }
+
+    /**
+     * @return the results
+     */
+    public List<ResultConfig> getResults() {
+        return results;
+    }
+
+    /**
+     * @param results the results to set
+     */
+    public void setResults(List<ResultConfig> results) {
+        this.results = results;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/de/intevation/lada/model/query/QueryFilter.java	Thu Aug 15 15:20:41 2013 +0200
@@ -0,0 +1,32 @@
+package de.intevation.lada.model.query;
+
+
+public class QueryFilter
+{
+    private String dataIndex;
+    private String type;
+
+    public QueryFilter() {
+    }
+
+    public QueryFilter(String dataIndex, String type) {
+        this.dataIndex = dataIndex;
+        this.type = type;
+    }
+
+    public String getDataIndex() {
+        return dataIndex;
+    }
+
+    public void setDataIndex(String dataIndex) {
+        this.dataIndex = dataIndex;
+    }
+
+    public String getType() {
+        return type;
+    }
+
+    public void setType(String type) {
+        this.type = type;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/de/intevation/lada/model/query/ResultConfig.java	Thu Aug 15 15:20:41 2013 +0200
@@ -0,0 +1,90 @@
+package de.intevation.lada.model.query;
+
+
+public class ResultConfig
+{
+    String dataIndex;
+    String header;
+    Integer flex;
+    Integer width;
+
+    public ResultConfig() {
+    }
+
+    public ResultConfig(String dataIndex, String header, Integer flex, Integer width) {
+        this.dataIndex= dataIndex;
+        this.header= header;
+        this.flex = flex;
+        this.width = width;
+    }
+
+    public ResultConfig(String dataIndex, String header, Integer flex) {
+        this.dataIndex= dataIndex;
+        this.header= header;
+        this.flex = flex;
+        this.width = null;
+    }
+
+    public ResultConfig(String dataIndex, String header) {
+        this.dataIndex= dataIndex;
+        this.header= header;
+        this.flex = 0;
+        this.width = null;
+    }
+
+    /**
+     * @return the dataIndex
+     */
+    public String getDataIndex() {
+        return dataIndex;
+    }
+
+    /**
+     * @param dataIndex the dataIndex to set
+     */
+    public void setDataIndex(String dataIndex) {
+        this.dataIndex = dataIndex;
+    }
+
+    /**
+     * @return the header
+     */
+    public String getHeader() {
+        return header;
+    }
+
+    /**
+     * @param header the header to set
+     */
+    public void setHeader(String header) {
+        this.header = header;
+    }
+
+    /**
+     * @return the width
+     */
+    public Integer getWidth() {
+        return width;
+    }
+
+    /**
+     * @param width the width to set
+     */
+    public void setWidth(Integer width) {
+        this.width = width;
+    }
+
+    /**
+     * @return the flex
+     */
+    public Integer getFlex() {
+        return flex;
+    }
+
+    /**
+     * @param flex the flex to set
+     */
+    public void setFlex(Integer flex) {
+        this.flex = flex;
+    }
+}
--- a/src/main/java/de/intevation/lada/rest/QueryService.java	Thu Aug 15 14:59:25 2013 +0200
+++ b/src/main/java/de/intevation/lada/rest/QueryService.java	Thu Aug 15 15:20:41 2013 +0200
@@ -1,5 +1,10 @@
 package de.intevation.lada.rest;
 
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.charset.Charset;
+import java.nio.file.Files;
+import java.nio.file.Paths;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -13,190 +18,17 @@
 import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.UriInfo;
 
+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.model.LOrt;
-
-class ResultConfig {
-    String dataIndex;
-    String header;
-    Integer flex;
-    Integer width;
-
-    public ResultConfig(String dataIndex, String header, Integer flex, Integer width) {
-        this.dataIndex= dataIndex;
-        this.header= header;
-        this.flex = flex;
-        this.width = width;
-    }
-
-    public ResultConfig(String dataIndex, String header, Integer flex) {
-        this.dataIndex= dataIndex;
-        this.header= header;
-        this.flex = flex;
-        this.width = null;
-    }
-
-    public ResultConfig(String dataIndex, String header) {
-        this.dataIndex= dataIndex;
-        this.header= header;
-        this.flex = 0;
-        this.width = null;
-    }
-
-    /**
-     * @return the dataIndex
-     */
-    public String getDataIndex() {
-        return dataIndex;
-    }
-
-    /**
-     * @param dataIndex the dataIndex to set
-     */
-    public void setDataIndex(String dataIndex) {
-        this.dataIndex = dataIndex;
-    }
-
-    /**
-     * @return the header
-     */
-    public String getHeader() {
-        return header;
-    }
-
-    /**
-     * @param header the header to set
-     */
-    public void setHeader(String header) {
-        this.header = header;
-    }
-
-    /**
-     * @return the width
-     */
-    public Integer getWidth() {
-        return width;
-    }
-
-    /**
-     * @param width the width to set
-     */
-    public void setWidth(Integer width) {
-        this.width = width;
-    }
-
-    /**
-     * @return the flex
-     */
-    public Integer getFlex() {
-        return flex;
-    }
-
-    /**
-     * @param flex the flex to set
-     */
-    public void setFlex(Integer flex) {
-        this.flex = flex;
-    }
-}
-
-class QueryConfig {
-    int id;
-    String name;
-    String description;
-    String sql;
-    List<String> filters;
-    List<ResultConfig> results;
-
-    public QueryConfig()
-    {
-    }
-
-    /**
-     * @return the id
-     */
-    public int getId() {
-        return id;
-    }
-
-    /**
-     * @param id the id to set
-     */
-    public void setId(int id) {
-        this.id = id;
-    }
-
-    /**
-     * @return the name
-     */
-    public String getName() {
-        return name;
-    }
-
-    /**
-     * @param name the name to set
-     */
-    public void setName(String name) {
-        this.name = name;
-    }
-
-    /**
-     * @return the description
-     */
-    public String getDescription() {
-        return description;
-    }
-
-    /**
-     * @param description the description to set
-     */
-    public void setDescription(String description) {
-        this.description = description;
-    }
-
-    /**
-     * @return the sql
-     */
-    public String getSql() {
-        return sql;
-    }
-
-    /**
-     * @param sql the sql to set
-     */
-    public void setSql(String sql) {
-        this.sql = sql;
-    }
-
-    /**
-     * @return the filter
-     */
-    public List<String> getFilters() {
-        return filters;
-    }
-
-    /**
-     * @param filter the filter to set
-     */
-    public void setFilters(List<String> filters) {
-        this.filters = filters;
-    }
-
-    /**
-     * @return the results
-     */
-    public List<ResultConfig> getResults() {
-        return results;
-    }
-
-    /**
-     * @param results the results to set
-     */
-    public void setResults(List<ResultConfig> results) {
-        this.results = results;
-    }
-}
+import de.intevation.lada.model.query.QueryConfig;
+import de.intevation.lada.model.query.QueryFilter;
+import de.intevation.lada.model.query.ResultConfig;
+import de.intevation.lada.utils.QueryTools;
 
 /**
  * This class produces a RESTful service to read, write and update
@@ -226,7 +58,7 @@
      */
     @GET
     @Produces("text/json")
-    public Response filter(
+    public Response get(
         @Context UriInfo info,
         @Context HttpHeaders headers
     ) {
@@ -234,8 +66,7 @@
             if (!authentication.isAuthorizedUser(headers)) {
                 return new Response(false, 699, new ArrayList<LOrt>());
             }
-            List<QueryConfig> queries = this.loadQueryConfig();
-            Response response = new Response(true, 200, queries);
+            Response response = new Response(true, 200, this.loadQueryConfig());
             return response;
         }
         catch(AuthenticationException ae) {
@@ -244,8 +75,6 @@
     }
 
     private List<QueryConfig> loadQueryConfig() {
-        List<QueryConfig> configs = new ArrayList<QueryConfig>();
-
         /* Typicall available fields
         {header: 'Datenbasis',  dataIndex: 'datenbasisId', width: 70},
         {header: 'MPL',  dataIndex: 'mplId', width: 50},
@@ -258,46 +87,6 @@
         {header: 'ProbeID', dataIndex: 'probeId'},
         {header: 'MST', dataIndex: 'mstId', width: 50}
         */
-
-        /* Query 1 */
-        QueryConfig qc1 = new QueryConfig();
-        qc1.setId(1);
-        qc1.setName("MST, UWB");
-        qc1.setDescription("Das ist die Beschreibung von Abfrage 1");
-        qc1.setSql("Select * from l_probe");
-        List<String> filters = new ArrayList<String>();
-        filters.add("mstId");
-        filters.add("umwId");
-        qc1.setFilters(filters);
-        List<ResultConfig> results = new ArrayList<ResultConfig>();
-        results.add(new ResultConfig("datenbasisId","Datenbasis"));
-        results.add(new ResultConfig("mplId","MPL"));
-        results.add(new ResultConfig("umwId","UWB"));
-        results.add(new ResultConfig("messmethode","MMT"));
-        results.add(new ResultConfig("hauptprobenNr","HPNR"));
-        results.add(new ResultConfig("nebenprobenNr","NPNR"));
-        results.add(new ResultConfig("bezeichnung","E.Gemeinde"));
-        results.add(new ResultConfig("kreis","Ursprungsgemeinde"));
-        results.add(new ResultConfig("probeId","ProbeID",1));
-        results.add(new ResultConfig("mstId","MS"));
-        qc1.setResults(results);
-        configs.add(qc1);
-        /* Query 2 */
-        QueryConfig qc2 = new QueryConfig();
-        qc2.setId(2);
-        qc2.setName("Einfach");
-        qc2.setDescription("Einfache Abfrage aller Proben ohne weitere Filterung.");
-        qc2.setSql("Select * from l_probe");
-        List<String> qcf2= new ArrayList<String>();
-        qc2.setFilters(qcf2);
-        List<ResultConfig> qcr2= new ArrayList<ResultConfig>();
-        qcr2.add(new ResultConfig("datenbasisId","Datenbasis"));
-        qcr2.add(new ResultConfig("nebenprobenNr","NPNR"));
-        qcr2.add(new ResultConfig("hauptprobenNr","HPNR"));
-        qcr2.add(new ResultConfig("mstId","MS"));
-        qcr2.add(new ResultConfig("probeId","ProbeID",1));
-        qc2.setResults(qcr2);
-        configs.add(qc2);
-        return configs;
+        return QueryTools.getConfig();
     }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/de/intevation/lada/utils/QueryTools.java	Thu Aug 15 15:20:41 2013 +0200
@@ -0,0 +1,99 @@
+package de.intevation.lada.utils;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.charset.Charset;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+import de.intevation.lada.model.query.QueryConfig;
+import de.intevation.lada.model.query.QueryFilter;
+import de.intevation.lada.model.query.ResultConfig;
+
+
+public class QueryTools
+{
+    public static String readConfigFile() {
+        String file = System.getProperty("de.intevation.lada.sqlconfig");
+        try {
+            byte[] encoded = Files.readAllBytes(Paths.get(file));
+            Charset encoding = Charset.defaultCharset();
+            return encoding.decode(ByteBuffer.wrap(encoded)).toString();
+        }
+        catch (IOException ioe) {
+            return null;
+        }
+    }
+
+    public static List<QueryConfig> getConfig() {
+        String content = readConfigFile();
+        if (content == null) {
+            return null;
+        }
+        List<QueryConfig> configs = new ArrayList<QueryConfig>();
+        JSONArray queries;
+        try {
+            queries = new JSONArray(content);
+            for (int i = 0; i < queries.length(); i++) {
+                JSONObject query = queries.getJSONObject(i);
+                QueryConfig qConf = new QueryConfig();
+                qConf.setId(query.getInt("id"));
+                qConf.setName(query.getString("name"));
+                qConf.setDescription(query.getString("description"));
+                qConf.setSql(query.getString("sql"));
+                JSONArray filters = query.getJSONArray("filters");
+                List<QueryFilter> qFilters = new ArrayList<QueryFilter>();
+                for (int j = 0; j < filters.length(); j++) {
+                    JSONObject filter = filters.getJSONObject(j);
+                    QueryFilter qFilter = new QueryFilter();
+                    qFilter.setDataIndex(filter.getString("dataIndex"));
+                    qFilter.setType(filter.getString("type"));
+                    qFilters.add(qFilter);
+                }
+                qConf.setFilters(qFilters);
+                JSONArray results = query.getJSONArray("result");
+                List<ResultConfig> sResults = new ArrayList<ResultConfig>();
+                for (int k = 0; k < results.length(); k++) {
+                    JSONObject result = results.getJSONObject(k);
+                    ResultConfig config = new ResultConfig();
+                    config.setDataIndex(result.getString("dataIndex"));
+                    config.setHeader(result.getString("header"));
+                    config.setWidth(result.optInt("width"));
+                    config.setFlex(result.optInt("flex"));
+                    sResults.add(config);
+                }
+                qConf.setResults(sResults);
+                configs.add(qConf);
+            }
+        }
+        catch (JSONException e) {
+            return null;
+        }
+        return configs;
+    }
+
+    public static JSONObject getQueryById(String id) {
+        try {
+            String content = readConfigFile();
+            if (content != null) {
+                JSONArray queries = new JSONArray(content);
+                for (int i = 0; i < queries.length(); i++) {
+                    JSONObject query = queries.getJSONObject(i);
+                    if (query.getString("id").equals(id)) {
+                        return query;
+                    }
+                }
+            }
+            return null;
+        }
+        catch (JSONException e) {
+            return null;
+        }
+    }
+}
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)