# HG changeset patch # User Raimund Renkert # Date 1376572841 -7200 # Node ID a529909cffeb1d377050cff935e01096cc54ea40 # Parent 93b12b077edf0e28bb67f66219a14ffb72264d3a# Parent 422e9ea0276f5dce84f2f6793ed3d83cc5776714 merged. diff -r 422e9ea0276f -r a529909cffeb pom.xml --- 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 @@ arquillian-protocol-servlet test + + org.json + json + 20090211 + diff -r 422e9ea0276f -r a529909cffeb src/main/java/de/intevation/lada/model/query/QueryConfig.java --- /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 filters; + List 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 getFilters() { + return filters; + } + + /** + * @param filter the filter to set + */ + public void setFilters(List filters) { + this.filters = filters; + } + + /** + * @return the results + */ + public List getResults() { + return results; + } + + /** + * @param results the results to set + */ + public void setResults(List results) { + this.results = results; + } +} diff -r 422e9ea0276f -r a529909cffeb src/main/java/de/intevation/lada/model/query/QueryFilter.java --- /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; + } +} diff -r 422e9ea0276f -r a529909cffeb src/main/java/de/intevation/lada/model/query/ResultConfig.java --- /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; + } +} diff -r 422e9ea0276f -r a529909cffeb src/main/java/de/intevation/lada/rest/QueryService.java --- 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 filters; - List 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 getFilters() { - return filters; - } - - /** - * @param filter the filter to set - */ - public void setFilters(List filters) { - this.filters = filters; - } - - /** - * @return the results - */ - public List getResults() { - return results; - } - - /** - * @param results the results to set - */ - public void setResults(List 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()); } - List 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 loadQueryConfig() { - List configs = new ArrayList(); - /* 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 filters = new ArrayList(); - filters.add("mstId"); - filters.add("umwId"); - qc1.setFilters(filters); - List results = new ArrayList(); - 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 qcf2= new ArrayList(); - qc2.setFilters(qcf2); - List qcr2= new ArrayList(); - 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(); } } diff -r 422e9ea0276f -r a529909cffeb src/main/java/de/intevation/lada/utils/QueryTools.java --- /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 getConfig() { + String content = readConfigFile(); + if (content == null) { + return null; + } + List configs = new ArrayList(); + 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 qFilters = new ArrayList(); + 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 sResults = new ArrayList(); + 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; + } + } +}