| Michael Hanl | 4fe41cc | 2013-12-10 17:59:51 +0000 | [diff] [blame] | 1 | package de.ids_mannheim.korap.query.serialize; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.LinkedHashMap; |
| 5 | import java.util.List; |
| 6 | import java.util.Map; |
| 7 | |
| 8 | /** |
| 9 | * @author hanl |
| 10 | * @date 10/12/2013 |
| 11 | */ |
| 12 | public class QueryUtils { |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | public static String buildCypherQuery(String cypher, String ctypel, String ctyper, |
| 18 | int cl, int cr, int page, int limit) { |
| 19 | //todo: implies that there is only one type allowed! |
| 20 | String sctypel = "", sctyper = ""; |
| 21 | switch (ctypel) { |
| 22 | case "C": |
| 23 | sctypel = "chars"; |
| 24 | break; |
| 25 | case "T": |
| 26 | sctypel = "tokens"; |
| 27 | break; |
| 28 | } |
| 29 | switch (ctyper) { |
| 30 | case "C": |
| 31 | sctyper = "chars"; |
| 32 | break; |
| 33 | case "T": |
| 34 | sctyper = "tokens"; |
| 35 | break; |
| 36 | } |
| 37 | |
| 38 | StringBuffer buffer = new StringBuffer(); |
| 39 | buffer.append("<query><cypher><![CDATA["); |
| 40 | buffer.append(cypher); |
| 41 | buffer.append("]]></cypher>"); |
| 42 | buffer.append("<wordAliasPrefix>wtok_</wordAliasPrefix>"); |
| 43 | buffer.append("<contextColumn>sent</contextColumn>"); |
| 44 | buffer.append("<contextIdColumn>sid</contextIdColumn>"); |
| 45 | buffer.append("<textColumn>txt</textColumn>"); |
| 46 | buffer.append("<startIndex>"); |
| 47 | buffer.append(page); |
| 48 | buffer.append("</startIndex>"); |
| 49 | buffer.append("<itemsPerPage>"); |
| 50 | buffer.append(limit); |
| 51 | buffer.append("</itemsPerPage>"); |
| 52 | buffer.append("<context>"); |
| 53 | buffer.append("<left>"); |
| 54 | buffer.append("<" + sctypel + ">"); |
| 55 | buffer.append(cl); |
| 56 | buffer.append("</" + sctypel + ">"); |
| 57 | buffer.append("</left>"); |
| 58 | buffer.append("<right>"); |
| 59 | buffer.append("<" + sctyper + ">"); |
| 60 | buffer.append(cr); |
| 61 | buffer.append("</" + sctyper + ">"); |
| 62 | buffer.append("</right>"); |
| 63 | buffer.append("</context>"); |
| 64 | buffer.append("</query>"); |
| 65 | return buffer.toString(); |
| 66 | } |
| 67 | |
| 68 | public static String buildDotQuery(long sid, String graphdb_id) { |
| 69 | StringBuffer b = new StringBuffer(); |
| 70 | b.append("<query>"); |
| 71 | b.append("<sentenceId>"); |
| 72 | b.append(sid); |
| 73 | b.append("</sentenceId>"); |
| 74 | b.append("<gdbId>"); |
| 75 | b.append(graphdb_id); |
| 76 | b.append("</gdbId>"); |
| 77 | b.append("<hls>"); |
| 78 | b.append("<hl>"); |
| 79 | b.append(40857); |
| 80 | b.append("</hl>"); |
| 81 | b.append("<hl>"); |
| 82 | b.append(40856); |
| 83 | b.append("</hl>"); |
| 84 | b.append("</hls>"); |
| 85 | b.append("</query>"); |
| 86 | |
| 87 | return b.toString(); |
| 88 | } |
| 89 | |
| 90 | public String buildaggreQuery(String query) { |
| 91 | StringBuffer b = new StringBuffer(); |
| 92 | b.append("<query><cypher><![CDATA["); |
| 93 | b.append(query); |
| 94 | b.append("]]></cypher>"); |
| 95 | b.append("<columns>"); |
| 96 | b.append("<column agg='true' sum='false'>"); |
| 97 | b.append("<cypherAlias>"); |
| 98 | b.append("aggBy"); |
| 99 | b.append("</cypherAlias>"); |
| 100 | b.append("<displayName>"); |
| 101 | b.append("Aggregate"); |
| 102 | b.append("</displayName>"); |
| 103 | b.append("</column>"); |
| 104 | |
| 105 | b.append("<column agg='fals' sum='true'>"); |
| 106 | b.append("<cypherAlias>"); |
| 107 | b.append("cnt"); |
| 108 | b.append("</cypherAlias>"); |
| 109 | b.append("<displayName>"); |
| 110 | b.append("Count"); |
| 111 | b.append("</displayName>"); |
| 112 | b.append("</column>"); |
| 113 | b.append("</columns>"); |
| 114 | |
| 115 | b.append("</query>"); |
| 116 | return b.toString(); |
| 117 | } |
| 118 | |
| 119 | public static Map addParameters(Map request, int page, int num, String cli, String cri, |
| 120 | int cls, int crs) { |
| 121 | Map ctx = new LinkedHashMap(); |
| 122 | List left = new ArrayList(); |
| 123 | left.add(cli); |
| 124 | left.add(cls); |
| 125 | List right = new ArrayList(); |
| 126 | right.add(cri); |
| 127 | right.add(crs); |
| 128 | ctx.put("left", left); |
| 129 | ctx.put("right", right); |
| 130 | |
| 131 | request.put("startPage", page); |
| 132 | request.put("count", num); |
| 133 | request.put("context", ctx); |
| 134 | |
| 135 | return request; |
| 136 | } |
| 137 | |
| 138 | |
| 139 | |
| 140 | } |