| Michael Hanl | ed5658f | 2014-02-07 22:24:46 +0000 | [diff] [blame] | 1 | package de.ids_mannheim.korap.query.serialize; |
| 2 | |
| Michael Hanl | c1c3b5d | 2014-08-04 16:57:46 +0000 | [diff] [blame] | 3 | import lombok.Data; |
| 4 | |
| Michael Hanl | 034be0d | 2014-02-14 10:17:34 +0000 | [diff] [blame] | 5 | import java.util.LinkedHashMap; |
| Michael Hanl | ed5658f | 2014-02-07 22:24:46 +0000 | [diff] [blame] | 6 | import java.util.LinkedList; |
| 7 | import java.util.List; |
| 8 | import java.util.Map; |
| 9 | |
| 10 | /** |
| 11 | * @author hanl |
| 12 | * @date 07/02/2014 |
| 13 | */ |
| Michael Hanl | df206ab | 2014-05-13 10:22:27 +0000 | [diff] [blame] | 14 | public class MetaQueryBuilder { |
| Michael Hanl | ed5658f | 2014-02-07 22:24:46 +0000 | [diff] [blame] | 15 | |
| Michael Hanl | ed5658f | 2014-02-07 22:24:46 +0000 | [diff] [blame] | 16 | private Map meta; |
| Michael Hanl | c1c3b5d | 2014-08-04 16:57:46 +0000 | [diff] [blame] | 17 | private SpanContext spanContext; |
| Michael Hanl | ed5658f | 2014-02-07 22:24:46 +0000 | [diff] [blame] | 18 | |
| Joachim Bingel | 20e06ac | 2015-01-15 10:31:33 +0000 | [diff] [blame] | 19 | public MetaQueryBuilder () { |
| Michael Hanl | 034be0d | 2014-02-14 10:17:34 +0000 | [diff] [blame] | 20 | this.meta = new LinkedHashMap(); |
| Michael Hanl | ed5658f | 2014-02-07 22:24:46 +0000 | [diff] [blame] | 21 | } |
| 22 | |
| Michael Hanl | b9f3fd1 | 2014-06-03 11:38:27 +0000 | [diff] [blame] | 23 | /** |
| 24 | * context segment if context is either of type char or token. |
| 25 | * size can differ for left and right span |
| Michael Hanl | 1b7f54a | 2014-07-25 17:35:55 +0000 | [diff] [blame] | 26 | * |
| Michael Hanl | b9f3fd1 | 2014-06-03 11:38:27 +0000 | [diff] [blame] | 27 | * @param left |
| 28 | * @param leftType |
| 29 | * @param right |
| 30 | * @param rightType |
| 31 | * @return |
| 32 | */ |
| Michael Hanl | dd5c965 | 2014-09-02 18:51:08 +0000 | [diff] [blame] | 33 | public MetaQueryBuilder setSpanContext(Integer left, String leftType, |
| Joachim Bingel | 20e06ac | 2015-01-15 10:31:33 +0000 | [diff] [blame] | 34 | Integer right, String rightType) { |
| Michael Hanl | c1c3b5d | 2014-08-04 16:57:46 +0000 | [diff] [blame] | 35 | this.spanContext = new SpanContext(left, leftType, right, rightType); |
| Michael Hanl | ed5658f | 2014-02-07 22:24:46 +0000 | [diff] [blame] | 36 | return this; |
| 37 | } |
| 38 | |
| Michael Hanl | c1c3b5d | 2014-08-04 16:57:46 +0000 | [diff] [blame] | 39 | public SpanContext getSpanContext() { |
| 40 | return this.spanContext; |
| 41 | } |
| 42 | |
| Michael Hanl | b9f3fd1 | 2014-06-03 11:38:27 +0000 | [diff] [blame] | 43 | /** |
| Joachim Bingel | 20e06ac | 2015-01-15 10:31:33 +0000 | [diff] [blame] | 44 | * context if of type paragraph or sentence where left and right |
| 45 | * size delimiters are irrelevant; or 2-token, 2-char p/paragraph, |
| 46 | * s/sentence or token, char |
| Michael Hanl | 1b7f54a | 2014-07-25 17:35:55 +0000 | [diff] [blame] | 47 | * |
| 48 | * @param context |
| Michael Hanl | b9f3fd1 | 2014-06-03 11:38:27 +0000 | [diff] [blame] | 49 | * @return |
| 50 | */ |
| Michael Hanl | dd5c965 | 2014-09-02 18:51:08 +0000 | [diff] [blame] | 51 | public MetaQueryBuilder setSpanContext(String context) { |
| Michael Hanl | 1b7f54a | 2014-07-25 17:35:55 +0000 | [diff] [blame] | 52 | if (context.startsWith("s") | context.startsWith("p")) |
| Michael Hanl | c1c3b5d | 2014-08-04 16:57:46 +0000 | [diff] [blame] | 53 | this.spanContext = new SpanContext(context); |
| Michael Hanl | 1b7f54a | 2014-07-25 17:35:55 +0000 | [diff] [blame] | 54 | else { |
| Joachim Bingel | 20e06ac | 2015-01-15 10:31:33 +0000 | [diff] [blame] | 55 | String[] ct = context.replaceAll("\\s+", "").split(","); |
| Michael Hanl | 1b7f54a | 2014-07-25 17:35:55 +0000 | [diff] [blame] | 56 | String[] lc = ct[0].split("-"); |
| 57 | String[] rc = ct[1].split("-"); |
| Joachim Bingel | 20e06ac | 2015-01-15 10:31:33 +0000 | [diff] [blame] | 58 | this.spanContext = new SpanContext(Integer.valueOf(lc[0]), lc[1], |
| 59 | Integer.valueOf(rc[0]), rc[1]); |
| Michael Hanl | 1b7f54a | 2014-07-25 17:35:55 +0000 | [diff] [blame] | 60 | } |
| Michael Hanl | b9f3fd1 | 2014-06-03 11:38:27 +0000 | [diff] [blame] | 61 | return this; |
| 62 | } |
| 63 | |
| Michael Hanl | c1c3b5d | 2014-08-04 16:57:46 +0000 | [diff] [blame] | 64 | public MetaQueryBuilder fillMeta(Integer pageIndex, Integer pageInteger, |
| Joachim Bingel | 20e06ac | 2015-01-15 10:31:33 +0000 | [diff] [blame] | 65 | Integer pageLength, String ctx, Boolean cutoff) { |
| Michael Hanl | c1c3b5d | 2014-08-04 16:57:46 +0000 | [diff] [blame] | 66 | if (pageIndex != null) |
| Michael Hanl | ecb9c1b | 2014-08-05 14:01:36 +0000 | [diff] [blame] | 67 | this.addEntry("startIndex", pageIndex); |
| Michael Hanl | c1c3b5d | 2014-08-04 16:57:46 +0000 | [diff] [blame] | 68 | if (pageIndex == null && pageInteger != null) |
| Michael Hanl | ecb9c1b | 2014-08-05 14:01:36 +0000 | [diff] [blame] | 69 | this.addEntry("startPage", pageInteger); |
| Michael Hanl | c1c3b5d | 2014-08-04 16:57:46 +0000 | [diff] [blame] | 70 | if (pageLength != null) |
| Michael Hanl | ecb9c1b | 2014-08-05 14:01:36 +0000 | [diff] [blame] | 71 | this.addEntry("count", pageLength); |
| Michael Hanl | c1c3b5d | 2014-08-04 16:57:46 +0000 | [diff] [blame] | 72 | if (ctx != null) |
| Michael Hanl | dd5c965 | 2014-09-02 18:51:08 +0000 | [diff] [blame] | 73 | this.setSpanContext(ctx); |
| Michael Hanl | c1c3b5d | 2014-08-04 16:57:46 +0000 | [diff] [blame] | 74 | if (cutoff != null) |
| Michael Hanl | ecb9c1b | 2014-08-05 14:01:36 +0000 | [diff] [blame] | 75 | this.addEntry("cutOff", cutoff); |
| 76 | return this; |
| Michael Hanl | c1c3b5d | 2014-08-04 16:57:46 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| Michael Hanl | df206ab | 2014-05-13 10:22:27 +0000 | [diff] [blame] | 79 | public MetaQueryBuilder addEntry(String name, Object value) { |
| Michael Hanl | ed5658f | 2014-02-07 22:24:46 +0000 | [diff] [blame] | 80 | meta.put(name, value); |
| 81 | return this; |
| 82 | } |
| 83 | |
| Michael Hanl | 034be0d | 2014-02-14 10:17:34 +0000 | [diff] [blame] | 84 | public Map raw() { |
| Michael Hanl | dd5c965 | 2014-09-02 18:51:08 +0000 | [diff] [blame] | 85 | if (this.spanContext != null) |
| 86 | meta.putAll(this.spanContext.raw()); |
| Michael Hanl | 034be0d | 2014-02-14 10:17:34 +0000 | [diff] [blame] | 87 | return meta; |
| 88 | } |
| Michael Hanl | c1c3b5d | 2014-08-04 16:57:46 +0000 | [diff] [blame] | 89 | |
| 90 | @Data |
| 91 | public class SpanContext { |
| 92 | private String left_type; |
| 93 | private String right_type; |
| 94 | private int left_size; |
| 95 | private int right_size; |
| 96 | private String context = null; |
| 97 | |
| Michael Hanl | c1c3b5d | 2014-08-04 16:57:46 +0000 | [diff] [blame] | 98 | /** |
| 99 | * context segment if context is either of type char or token. |
| 100 | * size can differ for left and right span |
| 101 | * |
| 102 | * @param ls |
| 103 | * @param lt |
| 104 | * @param rs |
| 105 | * @param rt |
| 106 | * @return |
| 107 | */ |
| Joachim Bingel | 20e06ac | 2015-01-15 10:31:33 +0000 | [diff] [blame] | 108 | public SpanContext (int ls, String lt, int rs, String rt) { |
| Michael Hanl | c1c3b5d | 2014-08-04 16:57:46 +0000 | [diff] [blame] | 109 | this.left_type = lt; |
| 110 | this.left_size = ls; |
| 111 | this.right_type = rt; |
| 112 | this.right_size = rs; |
| 113 | } |
| 114 | |
| Joachim Bingel | 20e06ac | 2015-01-15 10:31:33 +0000 | [diff] [blame] | 115 | public SpanContext (String context) { |
| Michael Hanl | c1c3b5d | 2014-08-04 16:57:46 +0000 | [diff] [blame] | 116 | this.context = context; |
| 117 | } |
| 118 | |
| 119 | public Map raw() { |
| 120 | Map meta = new LinkedHashMap(); |
| 121 | if (this.context == null) { |
| 122 | Map map = new LinkedHashMap(); |
| 123 | List l = new LinkedList(); |
| 124 | List r = new LinkedList(); |
| 125 | l.add(this.left_type); |
| 126 | l.add(this.left_size); |
| 127 | map.put("left", l); |
| 128 | r.add(this.right_type); |
| 129 | r.add(this.right_size); |
| 130 | map.put("right", r); |
| 131 | meta.put("context", map); |
| Joachim Bingel | 20e06ac | 2015-01-15 10:31:33 +0000 | [diff] [blame] | 132 | } |
| 133 | else |
| Michael Hanl | c1c3b5d | 2014-08-04 16:57:46 +0000 | [diff] [blame] | 134 | meta.put("context", this.context); |
| 135 | return meta; |
| 136 | } |
| 137 | } |
| Michael Hanl | ed5658f | 2014-02-07 22:24:46 +0000 | [diff] [blame] | 138 | } |