json date format for pubdate (currently default is yyyy-mm-dd)
diff --git a/pom.xml b/pom.xml
index 27a2667..3017972 100644
--- a/pom.xml
+++ b/pom.xml
@@ -42,6 +42,11 @@
<version>4.11</version>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>joda-time</groupId>
+ <artifactId>joda-time</artifactId>
+ <version>2.2</version>
+ </dependency>
</dependencies>
<build>
<sourceDirectory>${basedir}/src/main/java</sourceDirectory>
diff --git a/src/main/java/de/ids_mannheim/korap/query/serialize/MetaCollectionSerializer.java b/src/main/java/de/ids_mannheim/korap/query/serialize/MetaCollectionSerializer.java
index f43ba9e..53c90ab 100644
--- a/src/main/java/de/ids_mannheim/korap/query/serialize/MetaCollectionSerializer.java
+++ b/src/main/java/de/ids_mannheim/korap/query/serialize/MetaCollectionSerializer.java
@@ -112,7 +112,6 @@
//todo: connection to database module!
public Map<String, String> getResource(String id) {
return tester.get(id);
-
}
private Map<String, Map> tester() {
diff --git a/src/main/java/de/ids_mannheim/korap/query/serialize/MetaQuerySerializer.java b/src/main/java/de/ids_mannheim/korap/query/serialize/MetaQuerySerializer.java
index 6db54f4..60bd4b8 100644
--- a/src/main/java/de/ids_mannheim/korap/query/serialize/MetaQuerySerializer.java
+++ b/src/main/java/de/ids_mannheim/korap/query/serialize/MetaQuerySerializer.java
@@ -18,6 +18,7 @@
*/
public class MetaQuerySerializer {
+
private ObjectMapper mapper;
private MetaTypes types;
@@ -30,77 +31,142 @@
//todo: how to handle regex types?
// only handles AND relation between query attributes and values!
// value pair : pubdate=<date>, pubPlace=<place>, etc.
- public List serializeQueries(Map<String, String> queries) {
- boolean single = true;
+ public List serializeQueries(Map<String, String> queries, TYPE type) {
+ boolean extend, single = true;
boolean multypes = queries.keySet().size() > 1;
List metavalue;
String def_key = null;
if (queries.size() > 1)
single = false;
+ switch (type) {
+ case EXTEND:
+ extend = true;
+ break;
+ default:
+ extend = false;
+ break;
+ }
List value = new ArrayList<>();
+ Map<String, String> dates = new LinkedHashMap<>();
for (String key : queries.keySet()) {
if (!multypes)
def_key = key;
- String[] dr;
- if (queries.get(key).contains("~")) {
- dr = queries.get(key).split("~");
- Map fd = types.createTerm(null, null, dr[0].trim(), "korap:date");
- Map td = types.createTerm(null, null, dr[1].trim(), "korap:date");
- Map dg = types.createGroup("between", key, Arrays.asList(fd, td));
- value.add(dg);
- continue;
- } else if (queries.get(key).contains(">")) {
- dr = queries.get(key).split(">");
- Map fd = types.createTerm(null, null, dr[0].trim(), "korap:date");
- Map td = types.createTerm(null, null, dr[1].trim(), "korap:date");
- Map dg = types.createGroup("between", key, Arrays.asList(fd, td));
- value.add(dg);
- continue;
- } else if (queries.get(key).contains("<")) {
- dr = queries.get(key).split("<");
- Map fd = types.createTerm(null, null, dr[0].trim(), "korap:date");
- Map td = types.createTerm(null, null, dr[1].trim(), "korap:date");
- Map dg = types.createGroup("between", key, Arrays.asList(fd, td));
- value.add(dg);
+ if (key.contains("~") | key.contains(">") |
+ key.contains("<")) {
+ System.out.println("values " + key);
+ dates.put(key, queries.get(key));
continue;
}
+// if (queries.get(key).contains("~")) {
+// dr = queries.get(key).split("~");
+// Map fd = types.createTerm(dr[0].trim(), "korap:date");
+// Map td = types.createTerm(dr[1].trim(), "korap:date");
+// Map dg = types.createGroup("between", key, Arrays.asList(fd, td));
+// value.add(dg);
+// continue;
+// } else if (queries.get(key).contains(">")) {
+// dr = queries.get(key).split(">");
+// Map fd = types.createTerm(dr[0].trim(), "korap:date");
+// Map td = types.createTerm(dr[1].trim(), "korap:date");
+// Map dg = types.createGroup("between", key, Arrays.asList(fd, td));
+// value.add(dg);
+// continue;
+// } else if (queries.get(key).contains("<")) {
+// dr = queries.get(key).split("<");
+// Map fd = types.createTerm(dr[0].trim(), "korap:date");
+// Map td = types.createTerm(dr[1].trim(), "korap:date");
+// Map dg = types.createGroup("between", key, Arrays.asList(fd, td));
+// value.add(dg);
+// continue;
+// }
+
Map term;
if (multypes)
term = types.createTerm(key, null, queries.get(key).trim(), null);
else
- term = types.createTerm(null, null, queries.get(key).trim(), null);
+ term = types.createTerm(queries.get(key).trim(), null);
value.add(term);
}
+ String[] proc = processDates(dates);
+ int idx = 0;
+ if (proc[0] != null && proc[0].equals("r")) {
+ Map term1 = types.createTerm(proc[1], "korap:date");
+ Map term2 = types.createTerm(proc[2], "korap:date");
+ Map group = types.createGroup("between", "pubDate", Arrays.asList(term1, term2));
+ value.add(group);
+ idx = 3;
+ }
+
+ for (int i = idx; i < proc.length; i++) {
+ Map term = types.createTerm("pubDate", null, proc[i], "korap:date");
+ value.add(term);
+ }
+
+
// todo: missing: - takes only one resource, but resources can be chained!
// only filters, no extension
-// metavalue.put("meta", Arrays.asList(types.createMetaFilter(resource, (Map) value.get(0))));
- if (single)
- metavalue = Arrays.asList(types.createMetaFilter((Map) value.get(0)));
- else {
+ if (single) {
+ if (extend)
+ metavalue = Arrays.asList(types.createMetaExtend((Map) value.get(0)));
+ else
+ metavalue = Arrays.asList(types.createMetaFilter((Map) value.get(0)));
+ } else {
Map group;
if (!multypes)
group = types.createGroup("and", def_key, value);
else
group = types.createGroup("and", null, value);
-// metavalue.put("meta", Arrays.asList(types.createMetaFilter(resource, group)));
- metavalue = Arrays.asList(types.createMetaFilter(group));
+ if (extend)
+ metavalue = Arrays.asList(types.createMetaExtend(group));
+ else
+ metavalue = Arrays.asList(types.createMetaFilter(group));
}
return metavalue;
}
- public String stringify(Map<String, String> queries) throws IOException {
+ //todo: resource id must be added!
+ public String stringify(Map<String, String> queries, TYPE type) throws IOException {
Map f = new HashMap();
- f.put("meta", serializeQueries(queries));
+ f.put("meta", serializeQueries(queries, type));
return mapper.writeValueAsString(f);
}
+ //fixme: only allows for one until and since entry!!
+ private String[] processDates(Map<String, String> dates) {
+ if (dates.isEmpty())
+ return new String[1];
+ boolean until = false, since = false;
+ String[] el = new String[dates.keySet().size() + 1];
+ int idx = 1;
+ for (String key : dates.keySet()) {
+ if (key.contains("<")) {
+ since = true;
+ el[1] = dates.get(key);
+ }
+ if (key.contains(">")) {
+ until = true;
+ el[2] = dates.get(key);
+ } else
+ el[idx] = dates.get(key);
+ idx++;
+ }
+ if (since && until)
+ el[0] = "r";
+ return el;
+ }
+
public JsonNode jsonify(Map<String, String> queries) {
- List s = serializeQueries(queries);
+ List s = serializeQueries(queries, TYPE.FILTER);
return mapper.valueToTree(s);
}
+ public enum TYPE {
+ EXTEND, FILTER
+ }
+
+
}
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 887f176..fcabf20 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
@@ -2,6 +2,7 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
+import org.joda.time.DateTime;
import java.io.IOException;
import java.util.LinkedHashMap;
@@ -14,6 +15,9 @@
*/
public class MetaTypes {
+ public static final String Y = "yyyy";
+ public static final String YM = "yyyy-mm";
+ public static final String YMD = "yyyy-mm-dd";
private ObjectMapper mapper;
public MetaTypes() {
@@ -39,13 +43,17 @@
type = "korap:term";
term.put("@type", type);
if (field != null)
- term.put("@field", "korap:field#" + field);
+ term.put("field", "korap:field#" + field);
if (subtype != null)
term.put("subtype", "korap:value#" + subtype);
term.put("@value", value);
return term;
}
+ public Map createTerm(String value, String type) {
+ return createTerm(null, null, value, type);
+ }
+
public Map createResourceFilter(String resource, Map value) {
Map meta = new LinkedHashMap();
meta.put("@type", "korap:meta-filter");
@@ -80,8 +88,18 @@
return meta;
}
- public String formatDate(String date) {
- return "";
+ public String formatDate(long date, String format) {
+ DateTime time = new DateTime(date);
+ switch (format) {
+ case YM:
+ String s = time.getYear() + "-" + time.getMonthOfYear();
+ return s;
+ case YMD:
+ String s1 = time.getYear() + "-" + time.getMonthOfYear() + "-" + time.getDayOfMonth();
+ return s1;
+ default:
+ return String.valueOf(time.getYear());
+ }
}
public Map mapify(String s) throws IOException {
diff --git a/src/test/java/MetaQuerySerializationTest.java b/src/test/java/MetaQuerySerializationTest.java
index 71ca546..c44d036 100644
--- a/src/test/java/MetaQuerySerializationTest.java
+++ b/src/test/java/MetaQuerySerializationTest.java
@@ -1,11 +1,13 @@
import de.ids_mannheim.korap.query.serialize.MetaCollectionSerializer;
import de.ids_mannheim.korap.query.serialize.MetaQuerySerializer;
+import org.joda.time.DateTime;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import java.io.IOException;
import java.util.HashMap;
+import java.util.LinkedHashMap;
import java.util.Map;
/**
@@ -16,19 +18,21 @@
@RunWith(JUnit4.class)
public class MetaQuerySerializationTest {
- private MetaQuerySerializer serializer;
+ private MetaQuerySerializer querySerializer;
+ private MetaCollectionSerializer collSerializer;
public MetaQuerySerializationTest() {
- serializer = new MetaQuerySerializer();
+ querySerializer = new MetaQuerySerializer();
+ collSerializer = new MetaCollectionSerializer();
}
@Test
public void test() throws IOException {
Map<String, String> j = new HashMap();
j.put("author", "Goethe");
- j.put("pubPLace", "Erfurt");
+ j.put("pubPlace", "Erfurt");
j.put("textClass", "wissenschaft");
- String s = serializer.stringify(j);
+ String s = querySerializer.stringify(j, MetaQuerySerializer.TYPE.FILTER);
// System.out.println("value reference " + s);
}
@@ -36,16 +40,23 @@
public void testSingle() throws IOException {
Map<String, String> j = new HashMap();
j.put("textClass", "wissenschaft");
- String s = serializer.stringify(j);
+ String s = querySerializer.stringify(j, MetaQuerySerializer.TYPE.FILTER);
// System.out.println("value reference test single " + s);
}
@Test
public void testResourceMeta() throws IOException {
- MetaCollectionSerializer ser = new MetaCollectionSerializer();
- String s = ser.serialize("25");
- System.out.println(" --- RESULT JSON " + s);
+ String s = collSerializer.serialize("25");
+// System.out.println(" --- RESULT JSON " + s);
+ }
-
+ @Test
+ public void testDates() throws IOException {
+ Map<String, String> queries = new LinkedHashMap<>();
+ queries.put("<pubDate", String.valueOf(new DateTime().getMillis()));
+ queries.put(">pubDate", String.valueOf(new DateTime().getMillis()+2));
+ queries.put("author", "Goethe");
+ String f = querySerializer.stringify(queries, MetaQuerySerializer.TYPE.FILTER);
+ System.out.println("value : "+ f);
}
}