commit 3ef0789261672eed5a2a6de5a403ea5b2290af39
Author: jpoubova <jpoubova@students.zcu.cz>
Date:   Sat Apr 11 00:00:58 2020 +0200

    Re #7805: Vracení nalezených dokumentů na query
    - předěláno dle struktury na Wiki (stránka Architektura FE)

diff --git a/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/IndexController.java b/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/IndexController.java
index 19b0cb5..f0e0719 100644
--- a/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/IndexController.java
+++ b/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/IndexController.java
@@ -2,7 +2,7 @@ package cz.zcu.kiv.aswi.fulltextsearch;
 
 import cz.zcu.kiv.aswi.fulltextsearch.model.DocumentResponse;
 import cz.zcu.kiv.aswi.fulltextsearch.model.Query;
-import cz.zcu.kiv.aswi.fulltextsearch.model.QueryDocumentResponse;
+import cz.zcu.kiv.aswi.fulltextsearch.model.SearchResponse;
 import cz.zcu.kiv.aswi.fulltextsearch.model.ResponseMessage;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
@@ -20,7 +20,7 @@ public class IndexController {
     private SolrService solrService = new SolrService();
 
     @PostMapping("/")
-    public List<QueryDocumentResponse> index(@RequestBody Query query) {
+    public SearchResponse index(@RequestBody Query query) {
         try {
             return solrService.query(query.getQuery());
         } catch (Exception e) {
diff --git a/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/SolrService.java b/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/SolrService.java
index 2f3d253..c00652e 100644
--- a/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/SolrService.java
+++ b/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/SolrService.java
@@ -1,9 +1,7 @@
 package cz.zcu.kiv.aswi.fulltextsearch;
 
-import cz.zcu.kiv.aswi.fulltextsearch.document.PcGts;
-import cz.zcu.kiv.aswi.fulltextsearch.document.TextLine;
-import cz.zcu.kiv.aswi.fulltextsearch.document.TextRegion;
-import cz.zcu.kiv.aswi.fulltextsearch.model.QueryDocumentResponse;
+import cz.zcu.kiv.aswi.fulltextsearch.document.*;
+import cz.zcu.kiv.aswi.fulltextsearch.model.SearchResponse;
 import org.apache.solr.client.solrj.SolrQuery;
 import org.apache.solr.client.solrj.SolrServerException;
 import org.apache.solr.client.solrj.impl.HttpSolrClient;
@@ -119,7 +117,7 @@ public class SolrService {
         solrClient.commit();
     }
 
-    public List<QueryDocumentResponse> query(String query) throws IOException, SolrServerException  {
+    public SearchResponse query(String query) throws IOException, SolrServerException  {
         SolrQuery solrQuery = new SolrQuery();
         solrQuery.set("q", FIELD_TEXT_REGION + ":" + query);
         solrQuery.setHighlight(true);
@@ -129,37 +127,53 @@ public class SolrService {
         SolrDocumentList docList = response.getResults();
         Map<String, Map<String, List<String>>> highlight = response.getHighlighting();
 
-        List<QueryDocumentResponse> results = new ArrayList<>();
+        SearchResponse searchResponse = new SearchResponse();
+        searchResponse.setExpression(query);
+
+        List<TextRegion> textRegions = new LinkedList<>();
         for (SolrDocument solrDocument: docList) {
-            QueryDocumentResponse documentResponse = new QueryDocumentResponse();
+            TextRegion textRegion = new TextRegion();
+
+            // text, coords
+            textRegion.setRegionText((String) solrDocument.getFieldValue(FIELD_TEXT_REGION));
+            String pointsString = (String) solrDocument.getFieldValue(FIELD_TEXT_REGION_COORDS);
+            Point[] points = Coords.parsePointString(pointsString);
+            textRegion.setRegionCoords(new Coords(points));
 
-            // filename, text region
-            documentResponse.setDoc_filename((String) solrDocument.getFieldValue(FIELD_DOC_FILENAME));
-            documentResponse.setText_region((String) solrDocument.getFieldValue(FIELD_TEXT_REGION));
-            documentResponse.setText_region_coords((String) solrDocument.getFieldValue(FIELD_TEXT_REGION_COORDS));
+            // filename
+            String documentName = ((String) solrDocument.getFieldValue(FIELD_DOC_FILENAME)).replaceAll(".xml", "");
+            textRegion.setDocumentName(documentName);
 
             // text lines
             Map<String, List<String>> map = highlight.get(solrDocument.getFieldValue("id").toString());
             Set<String> linesNames = map.keySet();
-            Map<String, String> linesMap = new HashMap<>();
+            List<TextLine> textLines = new LinkedList<>();
             for (String lineName: linesNames) {
-                String value = (String) solrDocument.getFieldValue(lineName);
+                TextLine textLine = new TextLine();
+
+                textLine.setLineText((String) solrDocument.getFieldValue(lineName));
+
                 String coordsValue = (String) solrDocument.getFieldValue(lineName + SUFFIX_COORDS);
-                linesMap.put(lineName, value);
-                linesMap.put(lineName + SUFFIX_COORDS, coordsValue);
+                Point[] linePoints = Coords.parsePointString(coordsValue);
+                textLine.setLineCoords(new Coords(linePoints));
+
+                textLines.add(textLine);
             }
-            documentResponse.setText_lines(linesMap);
+
+            textRegion.setTextLines(textLines.toArray(new TextLine[0]));
 
             // image
             String imgFilename = (String) solrDocument.getFieldValue(FIELD_IMG_FILENAME);
             InputStream is = getClass().getClassLoader().getResourceAsStream(SolrService.DATA_DIR + imgFilename);
             String encodedImage = Base64.getEncoder().encodeToString(is.readAllBytes());
-            documentResponse.setImage(encodedImage);
+            textRegion.setImageCut(encodedImage); // todo - only temporary - setting imageCut to whole document image
 
-            results.add(documentResponse);
+            textRegions.add(textRegion);
         }
 
-        return results;
+        searchResponse.setTextRegions(textRegions.toArray(new TextRegion[0]));
+
+        return searchResponse;
     }
 
     private void addDocument(PcGts document) throws IOException, SolrServerException {
@@ -173,19 +187,19 @@ public class SolrService {
             solrInputDocument.addField(FIELD_DOC_FILENAME, document.getFilename());
             solrInputDocument.addField(FIELD_IMG_FILENAME, document.getFilename().replaceAll(".xml", ".png")); // todo
             solrInputDocument.addField(FIELD_TEXT_REGION, textRegion.getTextEquiv().getUnicode());
-            solrInputDocument.addField(FIELD_TEXT_REGION_COORDS, textRegion.getCoords().getPoints());
+            solrInputDocument.addField(FIELD_TEXT_REGION_COORDS, textRegion.getRegionCoords().getPointsString());
 
-            List<TextLine> textLines = textRegion.getTextLines();
-            if (textLines == null || textLines.size() == 0) continue;
-            for (int j = 0; j < textLines.size(); j++) {
-                TextLine textLine = textLines.get(j);
+            TextLine[] textLines = textRegion.getTextLines();
+            if (textLines == null || textLines.length == 0) continue;
+            for (int j = 0; j < textLines.length; j++) {
+                TextLine textLine = textLines[j];
                 String prefix = PREFIX_TEXT_LINE + j;
 
                 addFieldToSolr(prefix);
                 solrInputDocument.addField(prefix, textLine.getTextEquiv().getUnicode());
 
                 addFieldToSolr(prefix + SUFFIX_COORDS);
-                solrInputDocument.addField(prefix + SUFFIX_COORDS, textLine.getCoords().getPoints());
+                solrInputDocument.addField(prefix + SUFFIX_COORDS, textLine.getLineCoords().getPointsString());
             }
 
             solrClient.add(solrInputDocument);
diff --git a/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/document/Coords.java b/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/document/Coords.java
index 6abd590..82dd794 100644
--- a/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/document/Coords.java
+++ b/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/document/Coords.java
@@ -1,25 +1,67 @@
 package cz.zcu.kiv.aswi.fulltextsearch.document;
 
+import com.fasterxml.jackson.annotation.JsonIgnore;
+
 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;
 import javax.xml.bind.annotation.XmlAttribute;
+import java.util.LinkedList;
+import java.util.List;
 
 @XmlAccessorType(XmlAccessType.FIELD)
 public class Coords {
 
     @XmlAttribute(name = "points")
-    private String points;
+    @JsonIgnore
+    private String pointsString;
+
+    private Point[] points;
+
+    public Coords() {
+
+    }
+
+    public Coords(Point[] points) {
+        this.points = points;
+    }
+
+    public String getPointsString() {
+        return pointsString;
+    }
+
+    public void setPointsString(String pointsString) {
+        this.pointsString = pointsString;
+    }
 
-    public String getPoints() {
+    public Point[] getPoints() {
         return points;
     }
 
-    public void setPoints(String points) {
+    public void setPoints(Point[] points) {
         this.points = points;
     }
 
     @Override
     public String toString() {
-        return this.points;
+        return this.pointsString;
+    }
+
+    public static Point[] parsePointString(String pointsString) {
+        List<Point> pointList = new LinkedList<>();
+        if (pointsString == null || pointsString.isEmpty()) return pointList.toArray(new Point[0]);
+
+        String[] simplePoints = pointsString.split(" ");
+        for (String pointString: simplePoints) {
+            String[] numbers = pointString.split(",");
+
+            if (numbers.length < 2) continue;
+            int x = Integer.parseInt(numbers[0]);
+            int y = Integer.parseInt(numbers[1]);
+
+            Point p = new Point(x, y);
+            pointList.add(p);
+        }
+
+        return pointList.toArray(new Point[0]);
     }
 }
diff --git a/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/document/Page.java b/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/document/Page.java
index d362705..e955387 100644
--- a/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/document/Page.java
+++ b/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/document/Page.java
@@ -31,7 +31,7 @@ public class Page {
 
         for (int i = 0; i < textRegions.size(); i++) {
             TextRegion textRegion = textRegions.get(i);
-            textRegion.setFilename(filename);
+            textRegion.setDocumentName(filename);
         }
     }
 }
diff --git a/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/document/Point.java b/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/document/Point.java
new file mode 100644
index 0000000..4531e39
--- /dev/null
+++ b/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/document/Point.java
@@ -0,0 +1,28 @@
+package cz.zcu.kiv.aswi.fulltextsearch.document;
+
+public class Point {
+
+    private int x;
+    private int y;
+
+    public Point(int x, int y) {
+        this.x = x;
+        this.y = y;
+    }
+
+    public int getX() {
+        return x;
+    }
+
+    public void setX(int x) {
+        this.x = x;
+    }
+
+    public int getY() {
+        return y;
+    }
+
+    public void setY(int y) {
+        this.y = y;
+    }
+}
diff --git a/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/document/TextLine.java b/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/document/TextLine.java
index fa8d5e3..2bab03f 100644
--- a/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/document/TextLine.java
+++ b/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/document/TextLine.java
@@ -1,24 +1,28 @@
 package cz.zcu.kiv.aswi.fulltextsearch.document;
 
+import com.fasterxml.jackson.annotation.JsonIgnore;
+
 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;
 import javax.xml.bind.annotation.XmlElement;
-import java.util.List;
 
 @XmlAccessorType(XmlAccessType.PROPERTY)
 public class TextLine {
 
-    private Coords coords;
+    private Coords lineCoords;
+    private String lineText;
+    private TextWord[] textWords;
+
+    @JsonIgnore
     private TextEquiv textEquiv;
-    private List<Word> words;
 
-    public Coords getCoords() {
-        return coords;
+    public Coords getLineCoords() {
+        return lineCoords;
     }
 
     @XmlElement(name = "Coords")
-    public void setCoords(Coords coords) {
-        this.coords = coords;
+    public void setLineCoords(Coords lineCoords) {
+        this.lineCoords = lineCoords;
     }
 
     public TextEquiv getTextEquiv() {
@@ -30,13 +34,20 @@ public class TextLine {
         this.textEquiv = textEquiv;
     }
 
-    public List<Word> getWords() {
-        return words;
+    public TextWord[] getTextWords() {
+        return textWords;
     }
 
     @XmlElement(name = "Word")
-    public void setWords(List<Word> words) {
-        this.words = words;
+    public void setTextWords(TextWord[] textWords) {
+        this.textWords = textWords;
     }
 
+    public String getLineText() {
+        return lineText;
+    }
+
+    public void setLineText(String lineText) {
+        this.lineText = lineText;
+    }
 }
diff --git a/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/document/TextRegion.java b/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/document/TextRegion.java
index e65cabc..5f86b3d 100644
--- a/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/document/TextRegion.java
+++ b/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/document/TextRegion.java
@@ -1,26 +1,30 @@
 package cz.zcu.kiv.aswi.fulltextsearch.document;
 
+import com.fasterxml.jackson.annotation.JsonIgnore;
+
 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;
 import javax.xml.bind.annotation.XmlElement;
-import java.util.List;
 
 @XmlAccessorType(XmlAccessType.PROPERTY)
 public class TextRegion {
 
-    private Coords coords;
-    private TextEquiv textEquiv;
-    private List<TextLine> textLines;
+    private String imageCut;
+    private Coords regionCoords;
+    private String regionText;
+    private TextLine[] textLines;
+    private String documentName;
 
-    private String filename;
+    @JsonIgnore
+    private TextEquiv textEquiv;
 
-    public Coords getCoords() {
-        return coords;
+    public Coords getRegionCoords() {
+        return regionCoords;
     }
 
     @XmlElement(name = "Coords")
-    public void setCoords(Coords coords) {
-        this.coords = coords;
+    public void setRegionCoords(Coords regionCoords) {
+        this.regionCoords = regionCoords;
     }
 
     public TextEquiv getTextEquiv() {
@@ -32,21 +36,36 @@ public class TextRegion {
         this.textEquiv = textEquiv;
     }
 
-    public List<TextLine> getTextLines() {
+    public TextLine[] getTextLines() {
         return textLines;
     }
 
     @XmlElement(name = "TextLine")
-    public void setTextLines(List<TextLine> textLines) {
+    public void setTextLines(TextLine[] textLines) {
         this.textLines = textLines;
     }
 
-    public String getFilename() {
-        return filename;
+    public String getDocumentName() {
+        return documentName;
+    }
+
+    public void setDocumentName(String documentName) {
+        this.documentName = documentName;
     }
 
-    public void setFilename(String filename) {
-        this.filename = filename;
+    public String getImageCut() {
+        return imageCut;
     }
 
+    public void setImageCut(String imageCut) {
+        this.imageCut = imageCut;
+    }
+
+    public String getRegionText() {
+        return regionText;
+    }
+
+    public void setRegionText(String regionText) {
+        this.regionText = regionText;
+    }
 }
diff --git a/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/document/TextWord.java b/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/document/TextWord.java
new file mode 100644
index 0000000..04d7846
--- /dev/null
+++ b/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/document/TextWord.java
@@ -0,0 +1,31 @@
+package cz.zcu.kiv.aswi.fulltextsearch.document;
+
+import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlElement;
+
+@XmlAccessorType(XmlAccessType.PROPERTY)
+public class TextWord {
+
+    private Coords wordCoords;
+    private TextEquiv textEquiv;
+
+    public Coords getWordCoords() {
+        return wordCoords;
+    }
+
+    @XmlElement(name = "Coords")
+    public void setWordCoords(Coords wordCoords) {
+        this.wordCoords = wordCoords;
+    }
+
+    public TextEquiv getTextEquiv() {
+        return textEquiv;
+    }
+
+    @XmlElement(name = "TextEquiv")
+    public void setTextEquiv(TextEquiv textEquiv) {
+        this.textEquiv = textEquiv;
+    }
+
+}
diff --git a/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/document/Word.java b/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/document/Word.java
deleted file mode 100644
index 8346a4b..0000000
--- a/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/document/Word.java
+++ /dev/null
@@ -1,31 +0,0 @@
-package cz.zcu.kiv.aswi.fulltextsearch.document;
-
-import javax.xml.bind.annotation.XmlAccessType;
-import javax.xml.bind.annotation.XmlAccessorType;
-import javax.xml.bind.annotation.XmlElement;
-
-@XmlAccessorType(XmlAccessType.PROPERTY)
-public class Word {
-
-    private Coords coords;
-    private TextEquiv textEquiv;
-
-    public Coords getCoords() {
-        return coords;
-    }
-
-    @XmlElement(name = "Coords")
-    public void setCoords(Coords coords) {
-        this.coords = coords;
-    }
-
-    public TextEquiv getTextEquiv() {
-        return textEquiv;
-    }
-
-    @XmlElement(name = "TextEquiv")
-    public void setTextEquiv(TextEquiv textEquiv) {
-        this.textEquiv = textEquiv;
-    }
-
-}
diff --git a/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/model/QueryDocumentResponse.java b/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/model/QueryDocumentResponse.java
deleted file mode 100644
index 058a9f1..0000000
--- a/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/model/QueryDocumentResponse.java
+++ /dev/null
@@ -1,53 +0,0 @@
-package cz.zcu.kiv.aswi.fulltextsearch.model;
-
-import java.util.HashMap;
-import java.util.Map;
-
-public class QueryDocumentResponse {
-
-    private String doc_filename;
-    private String image; // PNG in Base64
-    private String text_region;
-    private String text_region_coords;
-    private Map<String, String> text_lines = new HashMap<>();
-
-    public String getDoc_filename() {
-        return doc_filename;
-    }
-
-    public void setDoc_filename(String doc_filename) {
-        this.doc_filename = doc_filename;
-    }
-
-    public String getText_region() {
-        return text_region;
-    }
-
-    public void setText_region(String text_region) {
-        this.text_region = text_region;
-    }
-
-    public String getText_region_coords() {
-        return text_region_coords;
-    }
-
-    public void setText_region_coords(String text_region_coords) {
-        this.text_region_coords = text_region_coords;
-    }
-
-    public Map<String, String> getText_lines() {
-        return text_lines;
-    }
-
-    public void setText_lines(Map<String, String> text_lines) {
-        this.text_lines = text_lines;
-    }
-
-    public String getImage() {
-        return image;
-    }
-
-    public void setImage(String image) {
-        this.image = image;
-    }
-}
diff --git a/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/model/SearchResponse.java b/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/model/SearchResponse.java
new file mode 100644
index 0000000..59d9ac1
--- /dev/null
+++ b/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/model/SearchResponse.java
@@ -0,0 +1,25 @@
+package cz.zcu.kiv.aswi.fulltextsearch.model;
+
+import cz.zcu.kiv.aswi.fulltextsearch.document.TextRegion;
+
+public class SearchResponse {
+
+    private String expression;
+    private TextRegion[] textRegions;
+
+    public String getExpression() {
+        return expression;
+    }
+
+    public void setExpression(String expression) {
+        this.expression = expression;
+    }
+
+    public TextRegion[] getTextRegions() {
+        return textRegions;
+    }
+
+    public void setTextRegions(TextRegion[] textRegions) {
+        this.textRegions = textRegions;
+    }
+}
