# HG changeset patch # User Raimund Renkert # Date 1423062535 -3600 # Node ID 7b3b9e5f040df4bf344c3e5fd306215427a1297a # Parent be0cd91abd0f3c6f32d00e82d29488ed82c5450a Added classes to handle serversite configured queries. diff -r be0cd91abd0f -r 7b3b9e5f040d src/main/java/de/intevation/lada/query/QueryConfig.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/de/intevation/lada/query/QueryConfig.java Wed Feb 04 16:08:55 2015 +0100 @@ -0,0 +1,118 @@ +/* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz + * Software engineering by Intevation GmbH + * + * This file is Free Software under the GNU GPL (v>=3) + * and comes with ABSOLUTELY NO WARRANTY! Check out + * the documentation coming with IMIS-Labordaten-Application for details. + */ +package de.intevation.lada.query; + +import java.util.List; + + +/** + * Container for SQL query configuration. + * + * The server can filter {@link LProbeInfo} objects by configurable SQL queries + * as described in the project wiki (https://bfs-intern.intevation.de/Server/Suche). + * This container is used to store the config at runtime. + * + * @author Raimund Renkert + */ +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 filters 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 be0cd91abd0f -r 7b3b9e5f040d src/main/java/de/intevation/lada/query/QueryFilter.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/de/intevation/lada/query/QueryFilter.java Wed Feb 04 16:08:55 2015 +0100 @@ -0,0 +1,98 @@ +/* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz + * Software engineering by Intevation GmbH + * + * This file is Free Software under the GNU GPL (v>=3) + * and comes with ABSOLUTELY NO WARRANTY! Check out + * the documentation coming with IMIS-Labordaten-Application for details. + */ +package de.intevation.lada.query; + + +/** + * Container for data filter. + * Stores filter defined in the SQL query configuration. + * + * @author Raimund Renkert + */ +public class QueryFilter +{ + private String dataIndex; + private String type; + private String label; + private boolean multiSelect; + + /** + * Default constructor. + */ + public QueryFilter() { + } + + /** + * Constructor to create a filled filter. + * + * @param dataIndex The dataIndex. + * @param type The filter type. + * @param label The label. + */ + public QueryFilter(String dataIndex, String type, String label) { + this.dataIndex = dataIndex; + this.type = type; + this.label = label; + } + + /** + * @return The dataIndex + */ + public String getDataIndex() { + return dataIndex; + } + + /** + * @param dataIndex THe dataIndex to set. + */ + public void setDataIndex(String dataIndex) { + this.dataIndex = dataIndex; + } + + /** + * @return The filter type. + */ + public String getType() { + return type; + } + + /** + * @param type The filter type to set. + */ + public void setType(String type) { + this.type = type; + } + + /** + * @return The label. + */ + public String getLabel() { + return label; + } + + /** + * @param label The label to set. + */ + public void setLabel(String label) { + this.label = label; + } + + /** + * @return the multiSelect + */ + public boolean isMultiSelect() { + return multiSelect; + } + + /** + * @param multiSelect the multiSelect to set + */ + public void setMultiSelect(boolean multiSelect) { + this.multiSelect = multiSelect; + } +} diff -r be0cd91abd0f -r 7b3b9e5f040d src/main/java/de/intevation/lada/query/QueryTools.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/de/intevation/lada/query/QueryTools.java Wed Feb 04 16:08:55 2015 +0100 @@ -0,0 +1,137 @@ +/* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz + * Software engineering by Intevation GmbH + * + * This file is Free Software under the GNU GPL (v>=3) + * and comes with ABSOLUTELY NO WARRANTY! Check out + * the documentation coming with IMIS-Labordaten-Application for details. + */ +package de.intevation.lada.query; + +import java.io.IOException; +import java.io.StringReader; +import java.net.URL; +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 javax.json.Json; +import javax.json.JsonArray; +import javax.json.JsonException; +import javax.json.JsonObject; +import javax.json.JsonReader; + + +/** + * Utility class to handle the SQL query configuration. + * + * @author Raimund Renkert + */ +public class QueryTools +{ + private static String FILE = "/META-INF/queryconf.json"; + /** + * Read the config file using the system property + * "de.intevation.lada.sqlconfig". + * + * @return The file content. + */ + public static String readConfigFile() { + try { + URL path = QueryConfig.class.getResource(FILE); + byte[] encoded = Files.readAllBytes(Paths.get(path.getPath())); + Charset encoding = Charset.defaultCharset(); + return encoding.decode(ByteBuffer.wrap(encoded)).toString(); + } + catch (IOException ioe) { + ioe.printStackTrace(); + return null; + } + } + + /** + * Get the configuration objects. + * First reads the config file and creates {@link QueryConfig} objects + * from JSON. + * + * @return List of {@link QueryConfig} objects. + */ + public static List getConfig() { + String content = readConfigFile(); + if (content == null) { + return null; + } + List configs = new ArrayList(); + try { + JsonReader reader = Json.createReader(new StringReader(content)); + JsonArray queries = reader.readArray(); + for (int i = 0; i < queries.size(); 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.size(); j++) { + JsonObject filter = filters.getJsonObject(j); + QueryFilter qFilter = new QueryFilter(); + qFilter.setDataIndex(filter.getString("dataIndex")); + qFilter.setType(filter.getString("type")); + qFilter.setLabel(filter.getString("label")); + qFilter.setMultiSelect(filter.getBoolean("multiselect", false)); + qFilters.add(qFilter); + } + qConf.setFilters(qFilters); + JsonArray results = query.getJsonArray("result"); + List sResults = new ArrayList(); + for (int k = 0; k < results.size(); k++) { + JsonObject result = results.getJsonObject(k); + ResultConfig config = new ResultConfig(); + config.setDataIndex(result.getString("dataIndex")); + config.setHeader(result.getString("header")); + config.setWidth(result.getInt("width", 100)); + config.setFlex(result.getInt("flex", 0)); + sResults.add(config); + } + qConf.setResults(sResults); + configs.add(qConf); + } + } + catch (JsonException e) { + return null; + } + return configs; + } + + /** + * Get a query by id. + * First reads the config file and returns the {@link QueryConfig} + * identified by the given id. + * + * @param id {@link QueryConfig} id. + * @return The query config as JSON object or null if no object was found. + */ + public static JsonObject getQueryById(String id) { + try { + String content = readConfigFile(); + if (content != null) { + JsonReader reader = Json.createReader(new StringReader(content)); + JsonArray queries = reader.readArray(); + for (int i = 0; i < queries.size(); i++) { + JsonObject query = queries.getJsonObject(i); + if (query.getString("id").equals(id)) { + return query; + } + } + } + return null; + } + catch (JsonException e) { + return null; + } + } +} diff -r be0cd91abd0f -r 7b3b9e5f040d src/main/java/de/intevation/lada/query/ResultConfig.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/de/intevation/lada/query/ResultConfig.java Wed Feb 04 16:08:55 2015 +0100 @@ -0,0 +1,121 @@ +/* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz + * Software engineering by Intevation GmbH + * + * This file is Free Software under the GNU GPL (v>=3) + * and comes with ABSOLUTELY NO WARRANTY! Check out + * the documentation coming with IMIS-Labordaten-Application for details. + */ +package de.intevation.lada.query; + + +/** + * Container for result configurations. + * Provides config for the client like column header, column with and data index. + * + * @author Raimund Renkert + */ +public class ResultConfig +{ + String dataIndex; + String header; + Integer flex; + Integer width; + + /** + * Default constructor. + */ + public ResultConfig() { + } + + /** + * @param dataIndex The dataIndex + * @param header The column header + * @param flex Flexible with + * @param width Width in px. + */ + public ResultConfig(String dataIndex, String header, Integer flex, Integer width) { + this.dataIndex= dataIndex; + this.header= header; + this.flex = flex; + this.width = width; + } + + /** + * @param dataIndex The dataIndex + * @param header The column header + * @param flex Flexible with + */ + public ResultConfig(String dataIndex, String header, Integer flex) { + this.dataIndex= dataIndex; + this.header= header; + this.flex = flex; + this.width = null; + } + + /** + * @param dataIndex The dataIndex + * @param header The column header + */ + 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; + } +}