QueryUtils class to incorporate cypher query serialization; fixed pubdate string serialization issue; refactoring
diff --git a/src/main/java/de/ids_mannheim/korap/query/serialize/MetaQuery.java b/src/main/java/de/ids_mannheim/korap/query/serialize/MetaQuery.java
index 42665ea..41ebd01 100644
--- a/src/main/java/de/ids_mannheim/korap/query/serialize/MetaQuery.java
+++ b/src/main/java/de/ids_mannheim/korap/query/serialize/MetaQuery.java
@@ -33,37 +33,6 @@
this.types = new MetaTypes();
}
- /**
- * temporary solution, since there is no other place for this right now!
- *
- * @param request
- * @param page
- * @param num
- * @param cli
- * @param cri
- * @param cls
- * @param crs
- * @return
- */
- public static Map addParameters(Map request, int page, int num, String cli, String cri,
- int cls, int crs) {
- Map ctx = new LinkedHashMap();
- List left = new ArrayList();
- left.add(cli);
- left.add(cls);
- List right = new ArrayList();
- right.add(cri);
- right.add(crs);
- ctx.put("left", left);
- ctx.put("right", right);
-
- request.put("startPage", page);
- request.put("count", num);
- request.put("context", ctx);
-
- return request;
- }
-
public MetaQuery addResource(String query) {
try {
JsonParser jp = factory.createParser(query);
@@ -196,12 +165,12 @@
for (String value : dates) {
if (value.contains("<")) {
String[] sp = value.split("<");
- el[1] = types.formatDate(Long.valueOf(sp[1]), MetaTypes.YMD);
+ el[1] = sp[1];
} else if (value.contains(">")) {
String[] sp = value.split(">");
- el[2] = types.formatDate(Long.valueOf(sp[1]), MetaTypes.YMD);
+ el[2] = sp[1];
} else {
- el[idx] = types.formatDate(Long.valueOf(value), MetaTypes.YMD);
+ el[idx] = value;
idx++;
}
}
diff --git a/src/main/java/de/ids_mannheim/korap/query/serialize/MetaTypes.java b/src/main/java/de/ids_mannheim/korap/query/serialize/MetaTypes.java
index 59a9376..cee079a 100644
--- a/src/main/java/de/ids_mannheim/korap/query/serialize/MetaTypes.java
+++ b/src/main/java/de/ids_mannheim/korap/query/serialize/MetaTypes.java
@@ -93,30 +93,6 @@
return meta;
}
- public String formatDate(long date, String format) {
- DateTime time = new DateTime(date);
- String month, day;
-
- if (time.getDayOfMonth() < 10)
- day = "0" + time.getDayOfMonth();
- else
- day = String.valueOf(time.getDayOfMonth());
-
- if (time.getMonthOfYear() < 10)
- month = "0" + time.getMonthOfYear();
- else
- month = String.valueOf(time.getMonthOfYear());
-
- switch (format) {
- case YM:
- return time.getYear() + "-" + month;
- case YMD:
- return time.getYear() + "-" + month + "-" + day;
- default:
- return String.valueOf(time.getYear());
- }
- }
-
public Map mapify(String s) throws IOException {
return mapper.readValue(s, Map.class);
}
diff --git a/src/main/java/de/ids_mannheim/korap/query/serialize/JsonGenerator.java b/src/main/java/de/ids_mannheim/korap/query/serialize/QuerySerializer.java
similarity index 92%
rename from src/main/java/de/ids_mannheim/korap/query/serialize/JsonGenerator.java
rename to src/main/java/de/ids_mannheim/korap/query/serialize/QuerySerializer.java
index 8cd4d48..0f68298 100644
--- a/src/main/java/de/ids_mannheim/korap/query/serialize/JsonGenerator.java
+++ b/src/main/java/de/ids_mannheim/korap/query/serialize/QuerySerializer.java
@@ -13,14 +13,14 @@
/**
* @author bingel, hanl
*/
-public class JsonGenerator {
+public class QuerySerializer {
private ObjectMapper mapper;
private AbstractSyntaxTree ast;
private org.slf4j.Logger log = LoggerFactory
- .getLogger(JsonGenerator.class);
+ .getLogger(QuerySerializer.class);
- public JsonGenerator() {
+ public QuerySerializer() {
mapper = new ObjectMapper();
}
@@ -31,7 +31,7 @@
/*
* just for testing...
*/
- JsonGenerator jg = new JsonGenerator();
+ QuerySerializer jg = new QuerySerializer();
int i = 0;
String[] queries;
if (args.length == 0) {
@@ -86,7 +86,7 @@
}
/**
- * Runs the JsonGenerator by initializing the relevant AbstractSyntaxTree implementation (depending on specified query language)
+ * Runs the QuerySerializer by initializing the relevant AbstractSyntaxTree implementation (depending on specified query language)
* and transforms and writes the tree's requestMap to the specified output file.
*
* @param outFile The file to which the serialization is written
@@ -129,7 +129,7 @@
try {
requestMap.put("meta", metaQuery.raw());
- requestMap = MetaQuery.addParameters(requestMap, page, num,
+ requestMap = QueryUtils.addParameters(requestMap, page, num,
cli, cri, cls, crs);
String res = mapper.writeValueAsString(requestMap);
return res;
diff --git a/src/main/java/de/ids_mannheim/korap/query/serialize/QueryUtils.java b/src/main/java/de/ids_mannheim/korap/query/serialize/QueryUtils.java
new file mode 100644
index 0000000..4491abe
--- /dev/null
+++ b/src/main/java/de/ids_mannheim/korap/query/serialize/QueryUtils.java
@@ -0,0 +1,140 @@
+package de.ids_mannheim.korap.query.serialize;
+
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @author hanl
+ * @date 10/12/2013
+ */
+public class QueryUtils {
+
+
+
+
+ public static String buildCypherQuery(String cypher, String ctypel, String ctyper,
+ int cl, int cr, int page, int limit) {
+ //todo: implies that there is only one type allowed!
+ String sctypel = "", sctyper = "";
+ switch (ctypel) {
+ case "C":
+ sctypel = "chars";
+ break;
+ case "T":
+ sctypel = "tokens";
+ break;
+ }
+ switch (ctyper) {
+ case "C":
+ sctyper = "chars";
+ break;
+ case "T":
+ sctyper = "tokens";
+ break;
+ }
+
+ StringBuffer buffer = new StringBuffer();
+ buffer.append("<query><cypher><![CDATA[");
+ buffer.append(cypher);
+ buffer.append("]]></cypher>");
+ buffer.append("<wordAliasPrefix>wtok_</wordAliasPrefix>");
+ buffer.append("<contextColumn>sent</contextColumn>");
+ buffer.append("<contextIdColumn>sid</contextIdColumn>");
+ buffer.append("<textColumn>txt</textColumn>");
+ buffer.append("<startIndex>");
+ buffer.append(page);
+ buffer.append("</startIndex>");
+ buffer.append("<itemsPerPage>");
+ buffer.append(limit);
+ buffer.append("</itemsPerPage>");
+ buffer.append("<context>");
+ buffer.append("<left>");
+ buffer.append("<" + sctypel + ">");
+ buffer.append(cl);
+ buffer.append("</" + sctypel + ">");
+ buffer.append("</left>");
+ buffer.append("<right>");
+ buffer.append("<" + sctyper + ">");
+ buffer.append(cr);
+ buffer.append("</" + sctyper + ">");
+ buffer.append("</right>");
+ buffer.append("</context>");
+ buffer.append("</query>");
+ return buffer.toString();
+ }
+
+ public static String buildDotQuery(long sid, String graphdb_id) {
+ StringBuffer b = new StringBuffer();
+ b.append("<query>");
+ b.append("<sentenceId>");
+ b.append(sid);
+ b.append("</sentenceId>");
+ b.append("<gdbId>");
+ b.append(graphdb_id);
+ b.append("</gdbId>");
+ b.append("<hls>");
+ b.append("<hl>");
+ b.append(40857);
+ b.append("</hl>");
+ b.append("<hl>");
+ b.append(40856);
+ b.append("</hl>");
+ b.append("</hls>");
+ b.append("</query>");
+
+ return b.toString();
+ }
+
+ public String buildaggreQuery(String query) {
+ StringBuffer b = new StringBuffer();
+ b.append("<query><cypher><![CDATA[");
+ b.append(query);
+ b.append("]]></cypher>");
+ b.append("<columns>");
+ b.append("<column agg='true' sum='false'>");
+ b.append("<cypherAlias>");
+ b.append("aggBy");
+ b.append("</cypherAlias>");
+ b.append("<displayName>");
+ b.append("Aggregate");
+ b.append("</displayName>");
+ b.append("</column>");
+
+ b.append("<column agg='fals' sum='true'>");
+ b.append("<cypherAlias>");
+ b.append("cnt");
+ b.append("</cypherAlias>");
+ b.append("<displayName>");
+ b.append("Count");
+ b.append("</displayName>");
+ b.append("</column>");
+ b.append("</columns>");
+
+ b.append("</query>");
+ return b.toString();
+ }
+
+ public static Map addParameters(Map request, int page, int num, String cli, String cri,
+ int cls, int crs) {
+ Map ctx = new LinkedHashMap();
+ List left = new ArrayList();
+ left.add(cli);
+ left.add(cls);
+ List right = new ArrayList();
+ right.add(cri);
+ right.add(crs);
+ ctx.put("left", left);
+ ctx.put("right", right);
+
+ request.put("startPage", page);
+ request.put("count", num);
+ request.put("context", ctx);
+
+ return request;
+ }
+
+
+
+}
diff --git a/src/test/java/MetaQuerySerializationTest.java b/src/test/java/MetaQuerySerializationTest.java
index fc389b8..580d2be 100644
--- a/src/test/java/MetaQuerySerializationTest.java
+++ b/src/test/java/MetaQuerySerializationTest.java
@@ -1,7 +1,7 @@
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
-import de.ids_mannheim.korap.query.serialize.JsonGenerator;
+import de.ids_mannheim.korap.query.serialize.QuerySerializer;
import de.ids_mannheim.korap.query.serialize.MetaQuery;
import org.joda.time.DateTime;
import org.junit.Assert;
@@ -85,7 +85,7 @@
/*
* just for testing...
*/
- JsonGenerator jg = new JsonGenerator();
+ QuerySerializer jg = new QuerySerializer();
int i = 0;
String[] queries;
queries = new String[]{