Added document retrieval method to web api and fixed UID handling

Change-Id: Ibde91ac49cdabacf6a813864af2511d531730182
diff --git a/Changes b/Changes
index 3b39a83..4f91121 100644
--- a/Changes
+++ b/Changes
@@ -1,4 +1,4 @@
-0.55.6 2016-06-08
+0.55.6 2016-06-23
         - [bugfix] distance with key "t" uses default foundry (diewald)
 	- [cleanup] Renamed fromJson() to fromKoral() (diewald)
 	- [cleanup] Removed deprecated methods in Krill:
@@ -9,6 +9,8 @@
 	- [cleanup] Removed deprecated method in KrillCollection:
 	  getCount() (diewald)
 	- [feature] Added getDoc() method to KrillIndex (diewald)
+        - [bugfix] Fixed UID handling (diewald)
+	- [feature] Added document method to Web-API (diewald)
 
 0.55.5 2016-05-02
 	- [performance] Changed to a dynamic window for sorting in FocusSpans (margaretha)
diff --git a/src/main/java/de/ids_mannheim/korap/KrillIndex.java b/src/main/java/de/ids_mannheim/korap/KrillIndex.java
index 2706d8c..43b7928 100644
--- a/src/main/java/de/ids_mannheim/korap/KrillIndex.java
+++ b/src/main/java/de/ids_mannheim/korap/KrillIndex.java
@@ -534,7 +534,8 @@
             return fd;
         }
         catch (IOException e) {
-            log.error("File json not found");
+            log.error("File json not found or unmappable: {}",
+                      e.getLocalizedMessage());
         };
         return (FieldDocument) null;
     };
@@ -779,6 +780,9 @@
 
         Text text = new Text();
 
+        // Rewrite parse ID
+        uid = new Integer(Integer.parseInt(uid)).toString();
+
         Filter filter = (Filter) new QueryWrapperFilter(new TermQuery(new Term("UID", uid)));
 
         try {
diff --git a/src/main/java/de/ids_mannheim/korap/index/AbstractDocument.java b/src/main/java/de/ids_mannheim/korap/index/AbstractDocument.java
index e7712c8..4589bb4 100644
--- a/src/main/java/de/ids_mannheim/korap/index/AbstractDocument.java
+++ b/src/main/java/de/ids_mannheim/korap/index/AbstractDocument.java
@@ -25,9 +25,11 @@
  * Krill index.
  * 
  * This model is rather specific to DeReKo data and
- * should be considered experimental. It may be replaced
+ * should be considered experimental. It will be replaced
  * by a more agnostic model.
- * string fields, e.g. may be combined with a prefix.
+ * string fields, e.g. will be combined with a prefix.
+ * For example d:pubDate will mean: A field with the key "pubDate"
+ * of type date.
  * 
  * @author diewald
  */
@@ -45,6 +47,8 @@
     // newly added
             creationDate;
 
+    private HashMap<String, String> fieldMap;
+
     private String
 
     // No longer supported
@@ -104,7 +108,6 @@
     };
 
     public void populateFields (Document doc, Collection<String> fields) {
-
         // Remember - never serialize "tokens"
 
         // LEGACY
diff --git a/src/main/java/de/ids_mannheim/korap/index/FieldDocument.java b/src/main/java/de/ids_mannheim/korap/index/FieldDocument.java
index a0c1994..baed325 100644
--- a/src/main/java/de/ids_mannheim/korap/index/FieldDocument.java
+++ b/src/main/java/de/ids_mannheim/korap/index/FieldDocument.java
@@ -192,7 +192,6 @@
      * Deserialize token stream data (LEGACY).
      */
     public void setFields (ArrayList<Map<String, Object>> fields) {
-
         Map<String, Object> primary = fields.remove(0);
         this.setPrimaryData((String) primary.get("primaryData"));
 
@@ -307,13 +306,20 @@
         this.addString("ID", ID);
     };
 
-
     @Override
+    @JsonIgnore
     public void setUID (int ID) {
         super.setUID(ID);
         this.addString("UID", new Integer(ID).toString());
     };
 
+    @Override
+    public void setUID (String ID) {
+        if (ID != null) {
+            super.setUID(ID);
+            this.addString("UID", new Integer(this.UID).toString());
+        };
+    };
 
     // No longer supported
     @Override
diff --git a/src/main/java/de/ids_mannheim/korap/server/Resource.java b/src/main/java/de/ids_mannheim/korap/server/Resource.java
index 993fec3..a439480 100644
--- a/src/main/java/de/ids_mannheim/korap/server/Resource.java
+++ b/src/main/java/de/ids_mannheim/korap/server/Resource.java
@@ -144,6 +144,25 @@
     };
 
 
+    @GET
+    @Path("/index/{textID}")
+    @Produces(MediaType.APPLICATION_JSON)
+    public String get (@PathParam("textID") String uid, @Context UriInfo uri) {
+
+        if (DEBUG)
+            log.trace("Get document with unique identifier {}", uid);
+
+        final Response kresp = _initResponse();
+        if (kresp.hasErrors())
+            return kresp.toJsonString();
+
+        // Get index
+        index = Node.getIndex();
+
+        return index.getDoc(uid).toJsonString();
+    };
+
+
     // TODO: Commit changes to the index before the server dies!
     /**
      * Commit data changes to the index
diff --git a/src/test/java/de/ids_mannheim/korap/index/TestFieldDocument.java b/src/test/java/de/ids_mannheim/korap/index/TestFieldDocument.java
index 91eab68..71532e0 100644
--- a/src/test/java/de/ids_mannheim/korap/index/TestFieldDocument.java
+++ b/src/test/java/de/ids_mannheim/korap/index/TestFieldDocument.java
@@ -198,14 +198,15 @@
         };
         ki.commit();
 
+        // {1:der} \w0:5 nicht
         SpanQueryWrapper sqwi = jsonQuery(getClass().getResource(
                 "/queries/bsp18.jsonld").getFile());
 
         Result kr = ki.search(sqwi.toQuery(), 0, (short) 5, true, (short) 2,
                 false, (short) 5);
 
-        // Bug:
-        // System.err.println(kr.toJSON());
+        assertEquals(1, kr.getTotalResults());
+        assertEquals("... bezeichnen, sofern [{1:der} schwedische Buchstabe „Å“ nicht] verfügbar ist im SI-Einheitensystem ist ...", kr.getMatch(0).getSnippetBrackets());
     };
 
 
diff --git a/src/test/java/de/ids_mannheim/korap/index/TestKrillIndex.java b/src/test/java/de/ids_mannheim/korap/index/TestKrillIndex.java
index 15079c2..2a892b5 100644
--- a/src/test/java/de/ids_mannheim/korap/index/TestKrillIndex.java
+++ b/src/test/java/de/ids_mannheim/korap/index/TestKrillIndex.java
@@ -21,6 +21,13 @@
 @RunWith(JUnit4.class)
 public class TestKrillIndex {
 
+
+    /*
+     * Todo: Currently fields can only be set if they are
+     * part of the general field set.
+     * this will change soon!
+     */
+
     @Test
     public void indexExample () throws IOException {
         KrillIndex ki = new KrillIndex();
@@ -35,6 +42,8 @@
         fd.addString("name", "Peter");
         fd.addInt("zahl1", 56);
         fd.addInt("zahl2", "58");
+        fd.addInt("zahl3", "059");
+        fd.addInt("UID", 1);
         fd.addText("teaser", "Das ist der Name der Rose");
         fd.addTV("base", "ich bau",
                 "[(0-3)s:ich|l:ich|p:PPER|-:sentences$<i>2]"
@@ -46,6 +55,7 @@
         fd.addString("name", "Hans");
         fd.addInt("zahl1", 14);
         fd.addText("teaser", "Das Sein");
+        fd.addInt("UID", 2);
 
         MultiTermTokenStream mtts = fd.newMultiTermTokenStream();
         mtts.addMultiTermToken("s:wir#0-3", "l:wir", "p:PPER");
@@ -66,6 +76,7 @@
         fd.addString("name", "Frank");
         fd.addInt("zahl1", 59);
         fd.addInt("zahl2", 65);
+        fd.addInt("UID", 3);
         fd.addText("teaser", "Noch ein Versuch");
         fd.addTV("base", "ich bau", "[(0-3)s:der|l:der|p:DET|-:sentences$<i>3]"
                 + "[(4-8)s:baum|l:baum|p:NN]");
@@ -79,6 +90,8 @@
 
         // KrillQuery kq = new KrillQuery("text");
         // ki.search();
+
+        ki.getDoc("1");
     };
 
 
@@ -115,11 +128,23 @@
         fd.setTitle("Peter");
         fd.setUID(22);
         ki.addDoc(fd);
+
+        fd = new FieldDocument();
+        fd.setTitle("Akron");
+        fd.setUID("05678");
+        ki.addDoc(fd);
+
         ki.commit();
 
-        assertEquals(1, ki.numberOf("base", "documents"));
+        assertEquals(2, ki.numberOf("base", "documents"));
 
         assertEquals("Peter", ki.getDoc("22").getTitle());
         assertEquals(22, ki.getDoc("22").getUID());
+
+        assertEquals("Akron", ki.getDoc("5678").getTitle());
+        assertEquals(5678, ki.getDoc("5678").getUID());
+
+        assertEquals("Akron", ki.getDoc("05678").getTitle());
+        assertEquals(5678, ki.getDoc("05678").getUID());
     };
 };
diff --git a/src/test/java/de/ids_mannheim/korap/server/TestResource.java b/src/test/java/de/ids_mannheim/korap/server/TestResource.java
index 3181245..d390de5 100644
--- a/src/test/java/de/ids_mannheim/korap/server/TestResource.java
+++ b/src/test/java/de/ids_mannheim/korap/server/TestResource.java
@@ -111,6 +111,8 @@
         for (String i : new String[] { "00001", "00002", "00003", "00004",
                 "00005", "00006" }) {
 
+            
+
             String json = StringfromFile(getClass().getResource(
                     "/wiki/" + i + ".json").getFile());
 
@@ -159,8 +161,31 @@
 
         // Staged data committed
         assertEquals(683, res.at("/messages/0/0").asInt());
+
+
+        // Get document by UID
+        resp = target.path("/index/00005").request().get(String.class);
+        res = mapper.readTree(resp);
+
+        assertEquals("freizeit-unterhaltung reisen schrott tabellen", res.at("/textClass").asText());
+        assertEquals("Å (Orte in Norwegen)", res.at("/title").asText());
+        assertEquals("WPD", res.at("/corpusID").asText());
+        assertEquals(5, res.at("/UID").asInt());
+        assertEquals("WPD_AAA.00005", res.at("/ID").asText());
+
+
+        // Get document by UID
+        resp = target.path("/index/5").request().get(String.class);
+        res = mapper.readTree(resp);
+
+        assertEquals("freizeit-unterhaltung reisen schrott tabellen", res.at("/textClass").asText());
+        assertEquals("Å (Orte in Norwegen)", res.at("/title").asText());
+        assertEquals("WPD", res.at("/corpusID").asText());
+        assertEquals(5, res.at("/UID").asInt());
+        assertEquals("WPD_AAA.00005", res.at("/ID").asText());
     };
 
+
     /*
     @Test
     public void testRemoving () throws IOException {