| Michael Hanl | 173ba4b | 2016-05-24 20:25:05 +0200 | [diff] [blame] | 1 | package de.ids_mannheim.korap.utils; |
| 2 | |
| 3 | import com.fasterxml.jackson.databind.JsonNode; |
| 4 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 5 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 6 | import de.ids_mannheim.korap.query.serialize.CollectionQueryProcessor; |
| 7 | import edu.emory.mathcs.backport.java.util.Arrays; |
| 8 | |
| Michael Hanl | 9a24216 | 2016-06-03 11:18:06 +0200 | [diff] [blame] | 9 | import java.io.IOError; |
| Michael Hanl | 173ba4b | 2016-05-24 20:25:05 +0200 | [diff] [blame] | 10 | import java.util.Map; |
| 11 | |
| 12 | /** |
| 13 | * convenience builder class for collection query |
| 14 | * |
| 15 | * @author hanl |
| 16 | * @date 16/09/2014 |
| 17 | */ |
| 18 | public class KoralCollectionQueryBuilder { |
| 19 | |
| 20 | public enum EQ { |
| 21 | EQUAL, UNEQUAL |
| 22 | } |
| 23 | |
| 24 | private boolean verbose; |
| 25 | private JsonNode base; |
| 26 | private StringBuilder builder; |
| Michael Hanl | cedf721 | 2016-05-28 10:43:09 +0200 | [diff] [blame] | 27 | private String mergeOperator; |
| Michael Hanl | 173ba4b | 2016-05-24 20:25:05 +0200 | [diff] [blame] | 28 | |
| 29 | |
| Michael Hanl | 33829ec | 2016-05-28 17:03:38 +0200 | [diff] [blame] | 30 | public KoralCollectionQueryBuilder () { |
| Michael Hanl | 173ba4b | 2016-05-24 20:25:05 +0200 | [diff] [blame] | 31 | this(false); |
| 32 | } |
| 33 | |
| 34 | |
| Michael Hanl | 33829ec | 2016-05-28 17:03:38 +0200 | [diff] [blame] | 35 | public KoralCollectionQueryBuilder (boolean verbose) { |
| Michael Hanl | 173ba4b | 2016-05-24 20:25:05 +0200 | [diff] [blame] | 36 | this.verbose = verbose; |
| 37 | this.builder = new StringBuilder(); |
| 38 | this.base = null; |
| Michael Hanl | cedf721 | 2016-05-28 10:43:09 +0200 | [diff] [blame] | 39 | this.mergeOperator = null; |
| Michael Hanl | 173ba4b | 2016-05-24 20:25:05 +0200 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | |
| 43 | /** |
| 44 | * raw method for field - value pair adding. Supports all |
| 45 | * operators (leq, geq, contains, etc.) |
| 46 | * |
| 47 | * @param field |
| 48 | * @param op |
| 49 | * @param value |
| 50 | * @return |
| 51 | */ |
| Michael Hanl | 9a24216 | 2016-06-03 11:18:06 +0200 | [diff] [blame] | 52 | public KoralCollectionQueryBuilder with (String field, String op, |
| Michael Hanl | 33829ec | 2016-05-28 17:03:38 +0200 | [diff] [blame] | 53 | String value) { |
| Michael Hanl | 9a24216 | 2016-06-03 11:18:06 +0200 | [diff] [blame] | 54 | //String end = this.builder.substring(this.builder.length() - 4, |
| 55 | // this.builder.length() - 1); |
| 56 | //if (this.builder.length() != 0 |
| 57 | // && (!end.contains("&") | !end.contains("|"))) |
| 58 | // throw new RuntimeException("no join operator given!"); |
| 59 | this.with(field + op + value); |
| Michael Hanl | 173ba4b | 2016-05-24 20:25:05 +0200 | [diff] [blame] | 60 | return this; |
| 61 | } |
| 62 | |
| 63 | |
| 64 | /** |
| 65 | * element can be a more complex sub query like |
| 66 | * (textClass=freizeit & Attributes.CORPUS_SIGLE=WPD) |
| 67 | * |
| 68 | * @param query |
| 69 | * will be enclosed by parenthesis in order to make sub |
| 70 | * query |
| 71 | * element |
| 72 | * @return |
| 73 | */ |
| Michael Hanl | 33829ec | 2016-05-28 17:03:38 +0200 | [diff] [blame] | 74 | public KoralCollectionQueryBuilder with (String query) { |
| Michael Hanl | 173ba4b | 2016-05-24 20:25:05 +0200 | [diff] [blame] | 75 | if (!query.startsWith("(") && !query.endsWith(")")) |
| 76 | query = "(" + query + ")"; |
| 77 | this.builder.append(query); |
| 78 | return this; |
| 79 | } |
| 80 | |
| 81 | |
| 82 | |
| Michael Hanl | 173ba4b | 2016-05-24 20:25:05 +0200 | [diff] [blame] | 83 | public KoralCollectionQueryBuilder and () { |
| Michael Hanl | cedf721 | 2016-05-28 10:43:09 +0200 | [diff] [blame] | 84 | if (this.builder.length() != 0) |
| 85 | this.builder.append(" & "); |
| 86 | if (this.base != null && this.mergeOperator == null) |
| 87 | this.mergeOperator = "AND"; |
| Michael Hanl | 173ba4b | 2016-05-24 20:25:05 +0200 | [diff] [blame] | 88 | return this; |
| 89 | } |
| 90 | |
| 91 | |
| 92 | public KoralCollectionQueryBuilder or () { |
| Michael Hanl | cedf721 | 2016-05-28 10:43:09 +0200 | [diff] [blame] | 93 | if (this.builder.length() != 0) |
| 94 | this.builder.append(" | "); |
| 95 | if (this.base != null && this.mergeOperator == null) |
| 96 | this.mergeOperator = "OR"; |
| Michael Hanl | 173ba4b | 2016-05-24 20:25:05 +0200 | [diff] [blame] | 97 | return this; |
| 98 | } |
| 99 | |
| 100 | |
| Michael Hanl | 9a24216 | 2016-06-03 11:18:06 +0200 | [diff] [blame] | 101 | public Object rebaseCollection () { |
| Michael Hanl | cedf721 | 2016-05-28 10:43:09 +0200 | [diff] [blame] | 102 | if (this.builder.length() == 0 && this.base == null) |
| Michael Hanl | 173ba4b | 2016-05-24 20:25:05 +0200 | [diff] [blame] | 103 | return null; |
| 104 | |
| 105 | JsonNode request = null; |
| Michael Hanl | cedf721 | 2016-05-28 10:43:09 +0200 | [diff] [blame] | 106 | if (this.builder.length() != 0) { |
| Michael Hanl | 173ba4b | 2016-05-24 20:25:05 +0200 | [diff] [blame] | 107 | CollectionQueryProcessor tree = new CollectionQueryProcessor( |
| 108 | this.verbose); |
| 109 | tree.process(this.builder.toString()); |
| 110 | request = JsonUtils.valueToTree(tree.getRequestMap()); |
| 111 | } |
| 112 | |
| Michael Hanl | cedf721 | 2016-05-28 10:43:09 +0200 | [diff] [blame] | 113 | if (this.base != null) { |
| Michael Hanl | 173ba4b | 2016-05-24 20:25:05 +0200 | [diff] [blame] | 114 | // check that collection non empty |
| Michael Hanl | cedf721 | 2016-05-28 10:43:09 +0200 | [diff] [blame] | 115 | JsonNode tmp = this.base.deepCopy(); |
| Michael Hanl | 9a24216 | 2016-06-03 11:18:06 +0200 | [diff] [blame] | 116 | if (request != null) |
| 117 | request = mergeWith(request); |
| Michael Hanl | 33829ec | 2016-05-28 17:03:38 +0200 | [diff] [blame] | 118 | else |
| Michael Hanl | cedf721 | 2016-05-28 10:43:09 +0200 | [diff] [blame] | 119 | request = tmp; |
| Michael Hanl | 173ba4b | 2016-05-24 20:25:05 +0200 | [diff] [blame] | 120 | } |
| 121 | return request; |
| 122 | } |
| 123 | |
| Michael Hanl | 33829ec | 2016-05-28 17:03:38 +0200 | [diff] [blame] | 124 | |
| 125 | public JsonNode mergeWith (JsonNode node) { |
| Michael Hanl | cedf721 | 2016-05-28 10:43:09 +0200 | [diff] [blame] | 126 | if (this.base != null) { |
| Michael Hanl | 173ba4b | 2016-05-24 20:25:05 +0200 | [diff] [blame] | 127 | if (node != null) { |
| 128 | JsonNode tobase = node.at("/collection"); |
| 129 | JsonNode base = this.base.deepCopy(); |
| Michael Hanl | cb2d3f9 | 2016-06-02 17:34:06 +0200 | [diff] [blame] | 130 | JsonNode result = base.at("/collection"); |
| 131 | |
| 132 | if (result.isMissingNode() && !tobase.isMissingNode()) |
| 133 | result = tobase; |
| 134 | else if (result.isMissingNode() && tobase.isMissingNode()) |
| 135 | return base; |
| 136 | else { |
| 137 | result = JsonBuilder.buildDocGroup( |
| 138 | this.mergeOperator != null ? this.mergeOperator |
| 139 | .toLowerCase() : "and", result, tobase); |
| 140 | } |
| Michael Hanl | 173ba4b | 2016-05-24 20:25:05 +0200 | [diff] [blame] | 141 | ((ObjectNode) base).put("collection", result); |
| 142 | return base; |
| 143 | } |
| Michael Hanl | cb2d3f9 | 2016-06-02 17:34:06 +0200 | [diff] [blame] | 144 | return this.base; |
| Michael Hanl | 173ba4b | 2016-05-24 20:25:05 +0200 | [diff] [blame] | 145 | } |
| Michael Hanl | cb2d3f9 | 2016-06-02 17:34:06 +0200 | [diff] [blame] | 146 | throw new RuntimeException("no query found to merge with!"); |
| Michael Hanl | 173ba4b | 2016-05-24 20:25:05 +0200 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | |
| 150 | /** |
| 151 | * sets base query. All consequent queries are added to the first |
| 152 | * koral:docGroup within the collection base query |
| 153 | * If no group in base query, consequent queries are skipped. |
| 154 | * |
| 155 | * @param query |
| 156 | */ |
| 157 | public KoralCollectionQueryBuilder setBaseQuery (String query) { |
| 158 | this.base = JsonUtils.readTree(query); |
| 159 | return this; |
| 160 | } |
| Michael Hanl | 33829ec | 2016-05-28 17:03:38 +0200 | [diff] [blame] | 161 | |
| 162 | |
| Michael Hanl | cedf721 | 2016-05-28 10:43:09 +0200 | [diff] [blame] | 163 | public KoralCollectionQueryBuilder setBaseQuery (JsonNode query) { |
| 164 | this.base = query; |
| 165 | return this; |
| 166 | } |
| Michael Hanl | 173ba4b | 2016-05-24 20:25:05 +0200 | [diff] [blame] | 167 | |
| 168 | |
| 169 | public String toJSON () { |
| Michael Hanl | 9a24216 | 2016-06-03 11:18:06 +0200 | [diff] [blame] | 170 | return JsonUtils.toJSON(rebaseCollection()); |
| Michael Hanl | 173ba4b | 2016-05-24 20:25:05 +0200 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | |
| 174 | @Override |
| 175 | public String toString () { |
| 176 | return this.builder.toString(); |
| 177 | } |
| 178 | |
| 179 | |
| Michael Hanl | 173ba4b | 2016-05-24 20:25:05 +0200 | [diff] [blame] | 180 | private static class JsonBuilder { |
| 181 | |
| 182 | public static ObjectNode buildDoc (String key, String value) { |
| 183 | ObjectNode node = JsonUtils.createObjectNode(); |
| 184 | node.put("@type", "koral:doc"); |
| 185 | // eq.equals(EQ.EQUAL) ? "match:eq" : "match:ne" |
| 186 | node.put("match", "match:eq"); |
| 187 | node.put("key", key); |
| 188 | node.put("value", value); |
| 189 | return node; |
| 190 | } |
| 191 | |
| 192 | |
| 193 | public static ObjectNode buildDocGroup (String op, JsonNode ... groups) { |
| Michael Hanl | 173ba4b | 2016-05-24 20:25:05 +0200 | [diff] [blame] | 194 | ObjectNode node = JsonUtils.createObjectNode(); |
| 195 | node.put("@type", "koral:docGroup"); |
| 196 | node.put("operation", "operation:" + op); |
| 197 | ArrayNode ops = JsonUtils.createArrayNode(); |
| 198 | ops.addAll(Arrays.asList(groups)); |
| 199 | node.put("operands", ops); |
| 200 | return node; |
| 201 | } |
| 202 | |
| 203 | } |
| 204 | |
| 205 | } |