Mercurial > lada > lada-server
comparison src/main/java/de/intevation/lada/utils/QueryTools.java @ 297:dc2e1668a179
New utility class for creating and reading query configs.
author | Raimund Renkert <rrenkert@intevation.de> |
---|---|
date | Thu, 15 Aug 2013 15:19:02 +0200 |
parents | |
children | 71284b42ba39 2d3f293899e8 |
comparison
equal
deleted
inserted
replaced
296:b196ef9d8645 | 297:dc2e1668a179 |
---|---|
1 package de.intevation.lada.utils; | |
2 | |
3 import java.io.IOException; | |
4 import java.nio.ByteBuffer; | |
5 import java.nio.charset.Charset; | |
6 import java.nio.file.Files; | |
7 import java.nio.file.Paths; | |
8 import java.util.ArrayList; | |
9 import java.util.List; | |
10 | |
11 import org.json.JSONArray; | |
12 import org.json.JSONException; | |
13 import org.json.JSONObject; | |
14 | |
15 import de.intevation.lada.model.query.QueryConfig; | |
16 import de.intevation.lada.model.query.QueryFilter; | |
17 import de.intevation.lada.model.query.ResultConfig; | |
18 | |
19 | |
20 public class QueryTools | |
21 { | |
22 public static String readConfigFile() { | |
23 String file = System.getProperty("de.intevation.lada.sqlconfig"); | |
24 try { | |
25 byte[] encoded = Files.readAllBytes(Paths.get(file)); | |
26 Charset encoding = Charset.defaultCharset(); | |
27 return encoding.decode(ByteBuffer.wrap(encoded)).toString(); | |
28 } | |
29 catch (IOException ioe) { | |
30 return null; | |
31 } | |
32 } | |
33 | |
34 public static List<QueryConfig> getConfig() { | |
35 String content = readConfigFile(); | |
36 if (content == null) { | |
37 return null; | |
38 } | |
39 List<QueryConfig> configs = new ArrayList<QueryConfig>(); | |
40 JSONArray queries; | |
41 try { | |
42 queries = new JSONArray(content); | |
43 for (int i = 0; i < queries.length(); i++) { | |
44 JSONObject query = queries.getJSONObject(i); | |
45 QueryConfig qConf = new QueryConfig(); | |
46 qConf.setId(query.getInt("id")); | |
47 qConf.setName(query.getString("name")); | |
48 qConf.setDescription(query.getString("description")); | |
49 qConf.setSql(query.getString("sql")); | |
50 JSONArray filters = query.getJSONArray("filters"); | |
51 List<QueryFilter> qFilters = new ArrayList<QueryFilter>(); | |
52 for (int j = 0; j < filters.length(); j++) { | |
53 JSONObject filter = filters.getJSONObject(j); | |
54 QueryFilter qFilter = new QueryFilter(); | |
55 qFilter.setDataIndex(filter.getString("dataIndex")); | |
56 qFilter.setType(filter.getString("type")); | |
57 qFilters.add(qFilter); | |
58 } | |
59 qConf.setFilters(qFilters); | |
60 JSONArray results = query.getJSONArray("result"); | |
61 List<ResultConfig> sResults = new ArrayList<ResultConfig>(); | |
62 for (int k = 0; k < results.length(); k++) { | |
63 JSONObject result = results.getJSONObject(k); | |
64 ResultConfig config = new ResultConfig(); | |
65 config.setDataIndex(result.getString("dataIndex")); | |
66 config.setHeader(result.getString("header")); | |
67 config.setWidth(result.optInt("width")); | |
68 config.setFlex(result.optInt("flex")); | |
69 sResults.add(config); | |
70 } | |
71 qConf.setResults(sResults); | |
72 configs.add(qConf); | |
73 } | |
74 } | |
75 catch (JSONException e) { | |
76 return null; | |
77 } | |
78 return configs; | |
79 } | |
80 | |
81 public static JSONObject getQueryById(String id) { | |
82 try { | |
83 String content = readConfigFile(); | |
84 if (content != null) { | |
85 JSONArray queries = new JSONArray(content); | |
86 for (int i = 0; i < queries.length(); i++) { | |
87 JSONObject query = queries.getJSONObject(i); | |
88 if (query.getString("id").equals(id)) { | |
89 return query; | |
90 } | |
91 } | |
92 } | |
93 return null; | |
94 } | |
95 catch (JSONException e) { | |
96 return null; | |
97 } | |
98 } | |
99 } |