| Joachim Bingel | 4b405f5 | 2013-11-15 15:29:30 +0000 | [diff] [blame] | 1 | package de.ids_mannheim.korap.query.serialize; |
| 2 | |
| Michael Hanl | 26a826e | 2015-01-29 16:26:09 +0000 | [diff] [blame] | 3 | import com.fasterxml.jackson.core.JsonProcessingException; |
| Michael Hanl | 26a826e | 2015-01-29 16:26:09 +0000 | [diff] [blame] | 4 | import com.fasterxml.jackson.databind.ObjectMapper; |
| Joachim Bingel | 1f8f378 | 2015-01-19 17:58:41 +0000 | [diff] [blame] | 5 | import de.ids_mannheim.korap.query.serialize.util.KoralObjectGenerator; |
| Joachim Bingel | e3fe367 | 2014-12-17 18:35:58 +0000 | [diff] [blame] | 6 | import de.ids_mannheim.korap.query.serialize.util.StatusCodes; |
| Joachim Bingel | 6e4e9f3 | 2015-01-30 18:59:31 +0000 | [diff] [blame] | 7 | import org.apache.log4j.BasicConfigurator; |
| Michael Hanl | 85d4b5d | 2014-07-25 12:07:20 +0000 | [diff] [blame] | 8 | import org.slf4j.Logger; |
| Michael Hanl | 9ca5edd | 2013-12-06 05:13:24 +0000 | [diff] [blame] | 9 | import org.slf4j.LoggerFactory; |
| Joachim Bingel | 4b405f5 | 2013-11-15 15:29:30 +0000 | [diff] [blame] | 10 | |
| Michael Hanl | 44c73f0 | 2013-12-06 03:12:52 +0000 | [diff] [blame] | 11 | import java.io.IOException; |
| Michael Hanl | 26a826e | 2015-01-29 16:26:09 +0000 | [diff] [blame] | 12 | import java.util.*; |
| Joachim Bingel | b5ada90 | 2013-11-19 14:46:04 +0000 | [diff] [blame] | 13 | |
| Michael Hanl | 44c73f0 | 2013-12-06 03:12:52 +0000 | [diff] [blame] | 14 | /** |
| margaretha | 2ffa955 | 2016-05-27 17:20:00 +0200 | [diff] [blame] | 15 | * Main class for Koral, serializes queries from concrete QLs to KoralQuery |
| Akron | 3ac85c9 | 2016-06-06 16:14:40 +0200 | [diff] [blame] | 16 | * |
| margaretha | 2ffa955 | 2016-05-27 17:20:00 +0200 | [diff] [blame] | 17 | * @author Joachim Bingel (bingel@ids-mannheim.de), Michael Hanl |
| 18 | * (hanl@ids-mannheim.de), Eliza Margaretha (margaretha@ids-mannheim.de) |
| Joachim Bingel | 7cb346e | 2015-03-09 10:56:20 +0100 | [diff] [blame] | 19 | * @version 0.3.0 |
| 20 | * @since 0.1.0 |
| Michael Hanl | 44c73f0 | 2013-12-06 03:12:52 +0000 | [diff] [blame] | 21 | */ |
| Michael Hanl | 4fe41cc | 2013-12-10 17:59:51 +0000 | [diff] [blame] | 22 | public class QuerySerializer { |
| Joachim Bingel | 4b405f5 | 2013-11-15 15:29:30 +0000 | [diff] [blame] | 23 | |
| Michael Hanl | 3f0c4c4 | 2015-07-02 20:21:23 +0200 | [diff] [blame] | 24 | // fixme: not used in any way! |
| 25 | @Deprecated |
| Joachim Bingel | 20e06ac | 2015-01-15 10:31:33 +0000 | [diff] [blame] | 26 | static HashMap<String, Class<? extends AbstractQueryProcessor>> qlProcessorAssignment; |
| 27 | |
| Michael Hanl | 3f0c4c4 | 2015-07-02 20:21:23 +0200 | [diff] [blame] | 28 | |
| 29 | |
| Joachim Bingel | 20e06ac | 2015-01-15 10:31:33 +0000 | [diff] [blame] | 30 | static { |
| margaretha | 7d980e0 | 2017-11-12 21:49:36 +0100 | [diff] [blame] | 31 | qlProcessorAssignment = |
| 32 | new HashMap<String, Class<? extends AbstractQueryProcessor>>(); |
| 33 | qlProcessorAssignment.put("poliqarpplus", |
| 34 | PoliqarpPlusQueryProcessor.class); |
| Joachim Bingel | 20e06ac | 2015-01-15 10:31:33 +0000 | [diff] [blame] | 35 | qlProcessorAssignment.put("cosmas2", Cosmas2QueryProcessor.class); |
| 36 | qlProcessorAssignment.put("annis", AnnisQueryProcessor.class); |
| 37 | qlProcessorAssignment.put("cql", CqlQueryProcessor.class); |
| 38 | } |
| 39 | |
| Michael Hanl | 26a826e | 2015-01-29 16:26:09 +0000 | [diff] [blame] | 40 | private static ObjectMapper mapper = new ObjectMapper(); |
| margaretha | 7d980e0 | 2017-11-12 21:49:36 +0100 | [diff] [blame] | 41 | private static Logger qllogger = |
| 42 | LoggerFactory.getLogger(QuerySerializer.class); |
| Michael Hanl | 2a30f42 | 2014-04-01 16:41:44 +0000 | [diff] [blame] | 43 | public static String queryLanguageVersion; |
| 44 | |
| Joachim Bingel | 1faf8a5 | 2015-01-09 13:17:34 +0000 | [diff] [blame] | 45 | private AbstractQueryProcessor ast; |
| margaretha | d7e75b5 | 2017-01-20 13:52:28 +0100 | [diff] [blame] | 46 | private Map<String, Object> collection = new HashMap<>(); |
| Joachim Bingel | 1f8f378 | 2015-01-19 17:58:41 +0000 | [diff] [blame] | 47 | private Map<String, Object> meta; |
| 48 | private List<Object> errors; |
| 49 | private List<Object> warnings; |
| 50 | private List<Object> messages; |
| Joachim Bingel | 4b405f5 | 2013-11-15 15:29:30 +0000 | [diff] [blame] | 51 | |
| margaretha | 7d980e0 | 2017-11-12 21:49:36 +0100 | [diff] [blame] | 52 | public QuerySerializer () { |
| margaretha | 7fd3292 | 2017-04-27 14:15:06 +0200 | [diff] [blame] | 53 | this.errors = new ArrayList<>(); |
| 54 | this.warnings = new ArrayList<>(); |
| 55 | this.messages = new ArrayList<>(); |
| Michael Hanl | f33f706 | 2015-06-24 21:14:26 +0200 | [diff] [blame] | 56 | } |
| Joachim Bingel | a6954de | 2015-03-20 16:37:37 +0100 | [diff] [blame] | 57 | |
| Joachim Bingel | 009623f | 2014-12-16 10:46:43 +0000 | [diff] [blame] | 58 | /** |
| 59 | * @param args |
| Joachim Bingel | 009623f | 2014-12-16 10:46:43 +0000 | [diff] [blame] | 60 | */ |
| margaretha | 7d980e0 | 2017-11-12 21:49:36 +0100 | [diff] [blame] | 61 | public static void main (String[] args) { |
| Joachim Bingel | 009623f | 2014-12-16 10:46:43 +0000 | [diff] [blame] | 62 | /* |
| 63 | * just for testing... |
| Joachim Bingel | 20e06ac | 2015-01-15 10:31:33 +0000 | [diff] [blame] | 64 | */ |
| Joachim Bingel | 6e4e9f3 | 2015-01-30 18:59:31 +0000 | [diff] [blame] | 65 | BasicConfigurator.configure(); |
| Joachim Bingel | 009623f | 2014-12-16 10:46:43 +0000 | [diff] [blame] | 66 | QuerySerializer jg = new QuerySerializer(); |
| 67 | int i = 0; |
| Joachim Bingel | 2b00f49 | 2015-01-31 18:25:49 +0000 | [diff] [blame] | 68 | String[] queries = null; |
| Joachim Bingel | 6e4e9f3 | 2015-01-30 18:59:31 +0000 | [diff] [blame] | 69 | String ql = "poliqarpplus"; |
| 70 | if (args.length < 2) { |
| Joachim Bingel | a6954de | 2015-03-20 16:37:37 +0100 | [diff] [blame] | 71 | System.err |
| 72 | .println("Usage: QuerySerializer \"query\" queryLanguage"); |
| Joachim Bingel | 2b00f49 | 2015-01-31 18:25:49 +0000 | [diff] [blame] | 73 | System.exit(1); |
| margaretha | 7d980e0 | 2017-11-12 21:49:36 +0100 | [diff] [blame] | 74 | } |
| 75 | else { |
| Joachim Bingel | 20e06ac | 2015-01-15 10:31:33 +0000 | [diff] [blame] | 76 | queries = new String[] { args[0] }; |
| Joachim Bingel | a6954de | 2015-03-20 16:37:37 +0100 | [diff] [blame] | 77 | ql = args[1]; |
| Joachim Bingel | 6e4e9f3 | 2015-01-30 18:59:31 +0000 | [diff] [blame] | 78 | } |
| Joachim Bingel | 009623f | 2014-12-16 10:46:43 +0000 | [diff] [blame] | 79 | for (String q : queries) { |
| 80 | i++; |
| 81 | try { |
| Joachim Bingel | 1f8f378 | 2015-01-19 17:58:41 +0000 | [diff] [blame] | 82 | jg.run(q, ql); |
| Joachim Bingel | 009623f | 2014-12-16 10:46:43 +0000 | [diff] [blame] | 83 | System.out.println(); |
| margaretha | 7d980e0 | 2017-11-12 21:49:36 +0100 | [diff] [blame] | 84 | } |
| 85 | catch (NullPointerException npe) { |
| Joachim Bingel | 009623f | 2014-12-16 10:46:43 +0000 | [diff] [blame] | 86 | npe.printStackTrace(); |
| 87 | System.out.println("null\n"); |
| margaretha | 7d980e0 | 2017-11-12 21:49:36 +0100 | [diff] [blame] | 88 | } |
| 89 | catch (IOException e) { |
| Joachim Bingel | 009623f | 2014-12-16 10:46:43 +0000 | [diff] [blame] | 90 | e.printStackTrace(); |
| Joachim Bingel | 009623f | 2014-12-16 10:46:43 +0000 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | } |
| Michael Hanl | 44c73f0 | 2013-12-06 03:12:52 +0000 | [diff] [blame] | 94 | |
| 95 | /** |
| margaretha | 7d980e0 | 2017-11-12 21:49:36 +0100 | [diff] [blame] | 96 | * Runs the QuerySerializer by initializing the relevant AbstractSyntaxTree |
| 97 | * implementation (depending on specified query language) and transforms and |
| 98 | * writes the tree's requestMap to the specified output file. |
| 99 | * |
| 100 | * @param query |
| 101 | * The query string |
| 102 | * @param queryLanguage |
| 103 | * The query language. As of 17 Dec 2014, this must be one of |
| 104 | * 'poliqarpplus', 'cosmas2', 'annis' or 'cql'. |
| 105 | * @throws IOException |
| 106 | */ |
| 107 | public void run (String query, String queryLanguage) throws IOException { |
| Joachim Bingel | 3d5b69b | 2015-01-14 10:46:44 +0000 | [diff] [blame] | 108 | if (queryLanguage.equalsIgnoreCase("poliqarp")) { |
| Joachim Bingel | 1faf8a5 | 2015-01-09 13:17:34 +0000 | [diff] [blame] | 109 | ast = new PoliqarpPlusQueryProcessor(query); |
| margaretha | 7d980e0 | 2017-11-12 21:49:36 +0100 | [diff] [blame] | 110 | } |
| 111 | else if (queryLanguage.equalsIgnoreCase("cosmas2")) { |
| Joachim Bingel | 1faf8a5 | 2015-01-09 13:17:34 +0000 | [diff] [blame] | 112 | ast = new Cosmas2QueryProcessor(query); |
| margaretha | 7d980e0 | 2017-11-12 21:49:36 +0100 | [diff] [blame] | 113 | } |
| 114 | else if (queryLanguage.equalsIgnoreCase("poliqarpplus")) { |
| Joachim Bingel | 1faf8a5 | 2015-01-09 13:17:34 +0000 | [diff] [blame] | 115 | ast = new PoliqarpPlusQueryProcessor(query); |
| margaretha | 7d980e0 | 2017-11-12 21:49:36 +0100 | [diff] [blame] | 116 | } |
| 117 | else if (queryLanguage.equalsIgnoreCase("cql")) { |
| Joachim Bingel | 1faf8a5 | 2015-01-09 13:17:34 +0000 | [diff] [blame] | 118 | ast = new CqlQueryProcessor(query); |
| margaretha | 7d980e0 | 2017-11-12 21:49:36 +0100 | [diff] [blame] | 119 | } |
| 120 | else if (queryLanguage.equalsIgnoreCase("fcsql")) { |
| 121 | ast = new FCSQLQueryProcessor(query, "2.0"); |
| 122 | } |
| 123 | else if (queryLanguage.equalsIgnoreCase("annis")) { |
| Joachim Bingel | 1faf8a5 | 2015-01-09 13:17:34 +0000 | [diff] [blame] | 124 | ast = new AnnisQueryProcessor(query); |
| margaretha | 7d980e0 | 2017-11-12 21:49:36 +0100 | [diff] [blame] | 125 | } |
| 126 | else { |
| 127 | throw new IllegalArgumentException( |
| 128 | queryLanguage + " is not a supported query language!"); |
| Joachim Bingel | 009623f | 2014-12-16 10:46:43 +0000 | [diff] [blame] | 129 | } |
| Akron | cf886dd | 2017-08-09 12:18:31 +0200 | [diff] [blame] | 130 | System.out.println(this.toJSON()); |
| Joachim Bingel | 009623f | 2014-12-16 10:46:43 +0000 | [diff] [blame] | 131 | } |
| Michael Hanl | 44c73f0 | 2013-12-06 03:12:52 +0000 | [diff] [blame] | 132 | |
| margaretha | 7d980e0 | 2017-11-12 21:49:36 +0100 | [diff] [blame] | 133 | public QuerySerializer setQuery (String query, String ql, String version) { |
| Joachim Bingel | 20e06ac | 2015-01-15 10:31:33 +0000 | [diff] [blame] | 134 | ast = new DummyQueryProcessor(); |
| 135 | if (query == null || query.isEmpty()) { |
| 136 | ast.addError(StatusCodes.NO_QUERY, "You did not specify a query!"); |
| margaretha | 7d980e0 | 2017-11-12 21:49:36 +0100 | [diff] [blame] | 137 | } |
| 138 | else if (ql == null || ql.isEmpty()) { |
| Joachim Bingel | 20e06ac | 2015-01-15 10:31:33 +0000 | [diff] [blame] | 139 | ast.addError(StatusCodes.NO_QUERY, |
| 140 | "You did not specify any query language!"); |
| margaretha | 7d980e0 | 2017-11-12 21:49:36 +0100 | [diff] [blame] | 141 | } |
| 142 | else if (ql.equalsIgnoreCase("poliqarp")) { |
| Joachim Bingel | 3d5b69b | 2015-01-14 10:46:44 +0000 | [diff] [blame] | 143 | ast = new PoliqarpPlusQueryProcessor(query); |
| margaretha | 7d980e0 | 2017-11-12 21:49:36 +0100 | [diff] [blame] | 144 | } |
| 145 | else if (ql.equalsIgnoreCase("cosmas2")) { |
| Joachim Bingel | 3d5b69b | 2015-01-14 10:46:44 +0000 | [diff] [blame] | 146 | ast = new Cosmas2QueryProcessor(query); |
| margaretha | 7d980e0 | 2017-11-12 21:49:36 +0100 | [diff] [blame] | 147 | } |
| 148 | else if (ql.equalsIgnoreCase("poliqarpplus")) { |
| Joachim Bingel | 3d5b69b | 2015-01-14 10:46:44 +0000 | [diff] [blame] | 149 | ast = new PoliqarpPlusQueryProcessor(query); |
| margaretha | 7d980e0 | 2017-11-12 21:49:36 +0100 | [diff] [blame] | 150 | } |
| 151 | else if (ql.equalsIgnoreCase("cql")) { |
| 152 | if (version == null) { |
| Joachim Bingel | 3d5b69b | 2015-01-14 10:46:44 +0000 | [diff] [blame] | 153 | ast = new CqlQueryProcessor(query); |
| margaretha | 7d980e0 | 2017-11-12 21:49:36 +0100 | [diff] [blame] | 154 | } |
| 155 | else { |
| Joachim Bingel | 3d5b69b | 2015-01-14 10:46:44 +0000 | [diff] [blame] | 156 | ast = new CqlQueryProcessor(query, version); |
| margaretha | 7d980e0 | 2017-11-12 21:49:36 +0100 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | else if (ql.equalsIgnoreCase("fcsql")) { |
| 160 | if (version == null) { |
| margaretha | 9c3a5e7 | 2017-11-21 19:42:28 +0100 | [diff] [blame^] | 161 | ast = new FCSQLQueryProcessor(query); |
| 162 | // ast.addError(StatusCodes.MISSING_VERSION, |
| 163 | // "SRU Version is missing!"); |
| margaretha | 7d980e0 | 2017-11-12 21:49:36 +0100 | [diff] [blame] | 164 | } |
| 165 | else { |
| 166 | ast = new FCSQLQueryProcessor(query, version); |
| 167 | } |
| 168 | } |
| 169 | else if (ql.equalsIgnoreCase("annis")) { |
| Joachim Bingel | 3d5b69b | 2015-01-14 10:46:44 +0000 | [diff] [blame] | 170 | ast = new AnnisQueryProcessor(query); |
| margaretha | 7d980e0 | 2017-11-12 21:49:36 +0100 | [diff] [blame] | 171 | } |
| 172 | else { |
| 173 | ast.addError(StatusCodes.UNKNOWN_QUERY_LANGUAGE, |
| Michael Hanl | f33f706 | 2015-06-24 21:14:26 +0200 | [diff] [blame] | 174 | ql + " is not a supported query language!"); |
| Michael Hanl | 44c73f0 | 2013-12-06 03:12:52 +0000 | [diff] [blame] | 175 | } |
| Michael Hanl | 0b70bb3 | 2014-05-06 16:09:41 +0000 | [diff] [blame] | 176 | return this; |
| 177 | } |
| Michael Hanl | 034be0d | 2014-02-14 10:17:34 +0000 | [diff] [blame] | 178 | |
| margaretha | 7d980e0 | 2017-11-12 21:49:36 +0100 | [diff] [blame] | 179 | public QuerySerializer setQuery (String query, String ql) { |
| Michael Hanl | baf1a5e | 2014-05-15 19:51:40 +0000 | [diff] [blame] | 180 | return setQuery(query, ql, ""); |
| 181 | } |
| 182 | |
| margaretha | 7d980e0 | 2017-11-12 21:49:36 +0100 | [diff] [blame] | 183 | public void setVerbose (boolean verbose) { |
| Joachim Bingel | a6465ef | 2015-01-16 14:24:34 +0000 | [diff] [blame] | 184 | AbstractQueryProcessor.verbose = verbose; |
| 185 | } |
| Michael Hanl | 26a826e | 2015-01-29 16:26:09 +0000 | [diff] [blame] | 186 | |
| margaretha | 7d980e0 | 2017-11-12 21:49:36 +0100 | [diff] [blame] | 187 | public final String toJSON () { |
| Michael Hanl | 26a826e | 2015-01-29 16:26:09 +0000 | [diff] [blame] | 188 | String ser; |
| 189 | try { |
| 190 | ser = mapper.writeValueAsString(raw()); |
| margaretha | 7d980e0 | 2017-11-12 21:49:36 +0100 | [diff] [blame] | 191 | // System.out.println(ser); |
| 192 | } |
| 193 | catch (JsonProcessingException e) { |
| 194 | ast.addError(StatusCodes.SERIALIZATION_FAILED, |
| 195 | "Serialization failed."); |
| Michael Hanl | 26a826e | 2015-01-29 16:26:09 +0000 | [diff] [blame] | 196 | return ""; |
| 197 | } |
| Michael Hanl | 85d4b5d | 2014-07-25 12:07:20 +0000 | [diff] [blame] | 198 | return ser; |
| Michael Hanl | 44c73f0 | 2013-12-06 03:12:52 +0000 | [diff] [blame] | 199 | } |
| Michael Hanl | 0b70bb3 | 2014-05-06 16:09:41 +0000 | [diff] [blame] | 200 | |
| margaretha | 7d980e0 | 2017-11-12 21:49:36 +0100 | [diff] [blame] | 201 | public final Map<String, Object> build () { |
| Michael Hanl | 91917f2 | 2014-06-04 14:38:48 +0000 | [diff] [blame] | 202 | return raw(); |
| 203 | } |
| Michael Hanl | 0b70bb3 | 2014-05-06 16:09:41 +0000 | [diff] [blame] | 204 | |
| margaretha | 7fd3292 | 2017-04-27 14:15:06 +0200 | [diff] [blame] | 205 | @SuppressWarnings("unchecked") |
| 206 | private Map<String, Object> raw () { |
| Michael Hanl | 2bd00c6 | 2014-11-04 16:26:42 +0000 | [diff] [blame] | 207 | if (ast != null) { |
| Michael Hanl | 15311e0 | 2016-06-02 17:20:34 +0200 | [diff] [blame] | 208 | Map<String, Object> requestMap = new HashMap<>(ast.getRequestMap()); |
| margaretha | 7d980e0 | 2017-11-12 21:49:36 +0100 | [diff] [blame] | 209 | Map<String, Object> meta = |
| 210 | (Map<String, Object>) requestMap.get("meta"); |
| 211 | Map<String, Object> collection = |
| 212 | (Map<String, Object>) requestMap.get("collection"); |
| margaretha | 7fd3292 | 2017-04-27 14:15:06 +0200 | [diff] [blame] | 213 | List<Object> errors = (List<Object>) requestMap.get("errors"); |
| 214 | List<Object> warnings = (List<Object>) requestMap.get("warnings"); |
| 215 | List<Object> messages = (List<Object>) requestMap.get("messages"); |
| Michael Hanl | 78208a8 | 2016-05-23 16:25:02 +0200 | [diff] [blame] | 216 | collection = mergeCollection(collection, this.collection); |
| 217 | requestMap.put("collection", collection); |
| Michael Hanl | 12aea4d | 2016-06-16 15:18:16 +0200 | [diff] [blame] | 218 | |
| margaretha | 7d980e0 | 2017-11-12 21:49:36 +0100 | [diff] [blame] | 219 | if (meta == null) meta = new HashMap<String, Object>(); |
| 220 | if (errors == null) errors = new ArrayList<Object>(); |
| 221 | if (warnings == null) warnings = new ArrayList<Object>(); |
| 222 | if (messages == null) messages = new ArrayList<Object>(); |
| Michael Hanl | c61b392 | 2016-06-06 20:02:53 +0200 | [diff] [blame] | 223 | |
| Michael Hanl | 2bd00c6 | 2014-11-04 16:26:42 +0000 | [diff] [blame] | 224 | if (this.meta != null) { |
| Michael Hanl | 15311e0 | 2016-06-02 17:20:34 +0200 | [diff] [blame] | 225 | meta.putAll(this.meta); |
| 226 | requestMap.put("meta", meta); |
| Michael Hanl | 2bd00c6 | 2014-11-04 16:26:42 +0000 | [diff] [blame] | 227 | } |
| Michael Hanl | f3553c9 | 2015-06-25 18:17:43 +0200 | [diff] [blame] | 228 | if (this.errors != null && !this.errors.isEmpty()) { |
| Joachim Bingel | 017915d | 2014-12-16 13:03:04 +0000 | [diff] [blame] | 229 | errors.addAll(this.errors); |
| 230 | requestMap.put("errors", errors); |
| 231 | } |
| Michael Hanl | f3553c9 | 2015-06-25 18:17:43 +0200 | [diff] [blame] | 232 | if (this.warnings != null && !this.warnings.isEmpty()) { |
| Joachim Bingel | 017915d | 2014-12-16 13:03:04 +0000 | [diff] [blame] | 233 | warnings.addAll(this.warnings); |
| 234 | requestMap.put("warnings", warnings); |
| 235 | } |
| Michael Hanl | f3553c9 | 2015-06-25 18:17:43 +0200 | [diff] [blame] | 236 | if (this.messages != null && !this.messages.isEmpty()) { |
| Joachim Bingel | 20e06ac | 2015-01-15 10:31:33 +0000 | [diff] [blame] | 237 | messages.addAll(this.messages); |
| Joachim Bingel | 017915d | 2014-12-16 13:03:04 +0000 | [diff] [blame] | 238 | requestMap.put("messages", messages); |
| 239 | } |
| Michael Hanl | 3f0c4c4 | 2015-07-02 20:21:23 +0200 | [diff] [blame] | 240 | return cleanup(requestMap); |
| Michael Hanl | 2bd00c6 | 2014-11-04 16:26:42 +0000 | [diff] [blame] | 241 | } |
| margaretha | 7fd3292 | 2017-04-27 14:15:06 +0200 | [diff] [blame] | 242 | return new HashMap<String, Object>(); |
| Michael Hanl | 0b70bb3 | 2014-05-06 16:09:41 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| margaretha | f7dbe33 | 2016-06-16 20:14:26 +0200 | [diff] [blame] | 245 | private Map<String, Object> cleanup (Map<String, Object> requestMap) { |
| margaretha | 7d980e0 | 2017-11-12 21:49:36 +0100 | [diff] [blame] | 246 | Iterator<Map.Entry<String, Object>> set = |
| 247 | requestMap.entrySet().iterator(); |
| Michael Hanl | 3f0c4c4 | 2015-07-02 20:21:23 +0200 | [diff] [blame] | 248 | while (set.hasNext()) { |
| 249 | Map.Entry<String, Object> entry = set.next(); |
| margaretha | f7dbe33 | 2016-06-16 20:14:26 +0200 | [diff] [blame] | 250 | if (entry.getValue() instanceof List |
| 251 | && ((List) entry.getValue()).isEmpty()) |
| Michael Hanl | 3f0c4c4 | 2015-07-02 20:21:23 +0200 | [diff] [blame] | 252 | set.remove(); |
| margaretha | f7dbe33 | 2016-06-16 20:14:26 +0200 | [diff] [blame] | 253 | else if (entry.getValue() instanceof Map |
| 254 | && ((Map) entry.getValue()).isEmpty()) |
| Michael Hanl | 3f0c4c4 | 2015-07-02 20:21:23 +0200 | [diff] [blame] | 255 | set.remove(); |
| margaretha | f7dbe33 | 2016-06-16 20:14:26 +0200 | [diff] [blame] | 256 | else if (entry.getValue() instanceof String |
| 257 | && ((String) entry.getValue()).isEmpty()) |
| Michael Hanl | 3f0c4c4 | 2015-07-02 20:21:23 +0200 | [diff] [blame] | 258 | set.remove(); |
| 259 | } |
| 260 | return requestMap; |
| 261 | } |
| 262 | |
| margaretha | 7d980e0 | 2017-11-12 21:49:36 +0100 | [diff] [blame] | 263 | private Map<String, Object> mergeCollection ( |
| margaretha | f7dbe33 | 2016-06-16 20:14:26 +0200 | [diff] [blame] | 264 | Map<String, Object> collection1, Map<String, Object> collection2) { |
| Joachim Bingel | 1f8f378 | 2015-01-19 17:58:41 +0000 | [diff] [blame] | 265 | if (collection1 == null || collection1.isEmpty()) { |
| 266 | return collection2; |
| margaretha | f7dbe33 | 2016-06-16 20:14:26 +0200 | [diff] [blame] | 267 | } |
| 268 | else if (collection2 == null || collection2.isEmpty()) { |
| Joachim Bingel | 1f8f378 | 2015-01-19 17:58:41 +0000 | [diff] [blame] | 269 | return collection1; |
| margaretha | f7dbe33 | 2016-06-16 20:14:26 +0200 | [diff] [blame] | 270 | } |
| 271 | else if (collection1.equals(collection2)) { |
| Michael Hanl | 78208a8 | 2016-05-23 16:25:02 +0200 | [diff] [blame] | 272 | return collection1; |
| margaretha | f7dbe33 | 2016-06-16 20:14:26 +0200 | [diff] [blame] | 273 | } |
| 274 | else { |
| margaretha | 7d980e0 | 2017-11-12 21:49:36 +0100 | [diff] [blame] | 275 | Map<String, Object> docGroup = |
| 276 | KoralObjectGenerator.makeDocGroup("and"); |
| 277 | ArrayList<Object> operands = |
| 278 | (ArrayList<Object>) docGroup.get("operands"); |
| Joachim Bingel | 1f8f378 | 2015-01-19 17:58:41 +0000 | [diff] [blame] | 279 | operands.add(collection1); |
| 280 | operands.add(collection2); |
| Michael Hanl | 26a826e | 2015-01-29 16:26:09 +0000 | [diff] [blame] | 281 | return docGroup; |
| Joachim Bingel | 1f8f378 | 2015-01-19 17:58:41 +0000 | [diff] [blame] | 282 | } |
| 283 | } |
| Michael Hanl | 26a826e | 2015-01-29 16:26:09 +0000 | [diff] [blame] | 284 | |
| Michael Hanl | f33f706 | 2015-06-24 21:14:26 +0200 | [diff] [blame] | 285 | @Deprecated |
| margaretha | 7d980e0 | 2017-11-12 21:49:36 +0100 | [diff] [blame] | 286 | public QuerySerializer addMeta (String cli, String cri, int cls, int crs, |
| Michael Hanl | 0b70bb3 | 2014-05-06 16:09:41 +0000 | [diff] [blame] | 287 | int num, int pageIndex) { |
| Michael Hanl | df206ab | 2014-05-13 10:22:27 +0000 | [diff] [blame] | 288 | MetaQueryBuilder meta = new MetaQueryBuilder(); |
| Michael Hanl | dd5c965 | 2014-09-02 18:51:08 +0000 | [diff] [blame] | 289 | meta.setSpanContext(cls, cli, crs, cri); |
| Michael Hanl | 0b70bb3 | 2014-05-06 16:09:41 +0000 | [diff] [blame] | 290 | meta.addEntry("startIndex", pageIndex); |
| 291 | meta.addEntry("count", num); |
| 292 | this.meta = meta.raw(); |
| 293 | return this; |
| 294 | } |
| 295 | |
| margaretha | 7d980e0 | 2017-11-12 21:49:36 +0100 | [diff] [blame] | 296 | public QuerySerializer setMeta (Map<String, Object> meta) { |
| Michael Hanl | f33f706 | 2015-06-24 21:14:26 +0200 | [diff] [blame] | 297 | this.meta = meta; |
| 298 | return this; |
| 299 | } |
| Joachim Bingel | a6954de | 2015-03-20 16:37:37 +0100 | [diff] [blame] | 300 | |
| margaretha | 7d980e0 | 2017-11-12 21:49:36 +0100 | [diff] [blame] | 301 | public QuerySerializer setMeta (MetaQueryBuilder meta) { |
| 302 | this.meta = meta.raw(); |
| 303 | return this; |
| 304 | } |
| Akron | 3ac85c9 | 2016-06-06 16:14:40 +0200 | [diff] [blame] | 305 | |
| margaretha | 7d980e0 | 2017-11-12 21:49:36 +0100 | [diff] [blame] | 306 | @SuppressWarnings("unchecked") |
| 307 | public QuerySerializer setCollection (String collection) { |
| Joachim Bingel | 1faf8a5 | 2015-01-09 13:17:34 +0000 | [diff] [blame] | 308 | CollectionQueryProcessor tree = new CollectionQueryProcessor(); |
| Michael Hanl | f1fead4 | 2014-05-14 15:13:33 +0000 | [diff] [blame] | 309 | tree.process(collection); |
| Michael Hanl | f3553c9 | 2015-06-25 18:17:43 +0200 | [diff] [blame] | 310 | Map<String, Object> collectionRequest = tree.getRequestMap(); |
| Michael Hanl | f33f706 | 2015-06-24 21:14:26 +0200 | [diff] [blame] | 311 | if (collectionRequest.get("errors") != null) |
| margaretha | 7fd3292 | 2017-04-27 14:15:06 +0200 | [diff] [blame] | 312 | this.errors.addAll((List<Object>) collectionRequest.get("errors")); |
| margaretha | 7d980e0 | 2017-11-12 21:49:36 +0100 | [diff] [blame] | 313 | if (collectionRequest.get("warnings") != null) this.warnings |
| 314 | .addAll((List<Object>) collectionRequest.get("warnings")); |
| 315 | if (collectionRequest.get("messages") != null) this.messages |
| 316 | .addAll((List<Object>) collectionRequest.get("messages")); |
| 317 | this.collection = |
| 318 | (Map<String, Object>) collectionRequest.get("collection"); |
| Michael Hanl | f1fead4 | 2014-05-14 15:13:33 +0000 | [diff] [blame] | 319 | return this; |
| 320 | } |
| margaretha | 7d980e0 | 2017-11-12 21:49:36 +0100 | [diff] [blame] | 321 | |
| 322 | public String convertCollectionToJson () |
| 323 | throws JsonProcessingException { |
| 324 | Map<String, Object> map = new HashMap<>(); |
| 325 | map.put("collection", collection); |
| 326 | return mapper.writeValueAsString(map); |
| 327 | } |
| Joachim Bingel | 4b405f5 | 2013-11-15 15:29:30 +0000 | [diff] [blame] | 328 | } |