commit c965c938268d68b1b88451f25d51a3ad90374e01
Author: jpoubova <jpoubova@students.zcu.cz>
Date:   Mon Mar 30 17:46:38 2020 +0200

    Re #7734: Implementace ukládání dokumentů
    - do Solru se nyní ukládají i jednotlivé řádky a slova
    - ukládání id dokumentu (= název souboru)

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 8eff9e2..4fa2617 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
@@ -5,6 +5,7 @@ import cz.zcu.kiv.aswi.fulltextsearch.model.QueryResponse;
 import org.apache.solr.client.solrj.SolrServerException;
 import org.springframework.web.bind.annotation.*;
 
+import javax.xml.bind.JAXBException;
 import java.io.IOException;
 
 @CrossOrigin(origins = "*", allowedHeaders = "*")
@@ -19,8 +20,12 @@ public class IndexController {
 
         try {
             response = "Found: " + solrService.query(query.getQuery());
-        } catch (IOException | SolrServerException e) {
-            response = "Error with Solr.";
+        } catch (IOException e) {
+            response = "IOException";
+        } catch (SolrServerException e) {
+            response = "SolrServerException";
+        } catch (Exception e) {
+            response = "Unknown exception";
         }
 
         return new QueryResponse(response);
@@ -28,15 +33,57 @@ public class IndexController {
 
     @GetMapping("/add")
     public String add() {
-        solrService.addAllDocuments();
-        return "Document created";
+        String response;
+
+        try {
+            solrService.addAllDocuments();
+            response = "All documents have been added.";
+        } catch (IOException e) {
+            response = "IOException";
+        } catch (SolrServerException e) {
+            response = "SolrServerException";
+        } catch (JAXBException e) {
+            response = "JAXBException";
+        } catch (Exception e) {
+            response = "Unknown exception";
+        }
+
+        return response;
     }
 
     @GetMapping("/delete")
     public String delete() {
-        solrService.deleteAll();
-        return "Documents deleted";
+        String response;
+
+        try {
+            solrService.deleteAll();
+            response = "All documents have been deleted.";
+        } catch (IOException e) {
+            response = "IOException";
+        } catch (SolrServerException e) {
+            response = "SolrServerException";
+        } catch (Exception e) {
+            response = "Unknown exception";
+        }
+
+        return response;
     }
 
+    @GetMapping("/info")
+    public String info() {
+        String response;
+
+        try {
+            response = solrService.info();
+        } catch (IOException e) {
+            response = "IOException";
+        } catch (SolrServerException e) {
+            response = "SolrServerException";
+        } catch (Exception e) {
+            response = "Unknown exception";
+        }
+
+        return response;
+    }
 }
 
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 367b107..3e7817d 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
@@ -9,8 +9,8 @@ import org.apache.solr.client.solrj.impl.XMLResponseParser;
 import org.apache.solr.client.solrj.response.QueryResponse;
 import org.apache.solr.common.SolrDocumentList;
 
+import javax.xml.bind.JAXBException;
 import java.io.IOException;
-import java.io.InputStream;
 
 public class SolrService {
 
@@ -27,46 +27,41 @@ public class SolrService {
         solrClient.setParser(new XMLResponseParser());
     }
 
-    public void addAllDocuments() { // todo all
+    public void addAllDocuments() throws IOException, SolrServerException, JAXBException { // todo all
         XMLLoader xmlLoader = new XMLLoader();
-        InputStream is = getClass().getClassLoader().getResourceAsStream("soap-ch_knihovna_ascher-zeitung-1866-06-09-n23_0815.xml");
-        PcGts doc = xmlLoader.loadFile(is);
+        String filename = "soap-ch_knihovna_ascher-zeitung-1866-06-09-n23_0815.xml";
+        PcGts doc = xmlLoader.loadFile(filename);
         addDocument(doc);
     }
 
-    public void deleteAll() {
-        try {
-            solrClient.deleteByQuery("*:*");
-            solrClient.commit();
-            System.out.println("all docs deleted");
-        } catch (SolrServerException | IOException e) {
-            e.printStackTrace();
-        }
+    public void deleteAll() throws IOException, SolrServerException {
+        solrClient.deleteByQuery("*:*");
+        solrClient.commit();
     }
 
     public String query(String query) throws IOException, SolrServerException  {
         SolrQuery solrQuery = new SolrQuery();
-        solrQuery.set("q", "TextEquiv:" + query);
+        solrQuery.set("q", "TextEquiv:" + query + " AND id:*_region");
         QueryResponse response = solrClient.query(solrQuery);
 
         SolrDocumentList docList = response.getResults();
         return Long.toString(docList.getNumFound());
     }
 
-    private void addDocument(PcGts document) {
+    private void addDocument(PcGts document) throws IOException, SolrServerException {
         for (TextRegion textRegion: document.getPage().getTextRegions()) {
             addTextRegion(textRegion);
         }
     }
 
-    private void addTextRegion(TextRegion textRegion) {
-        try {
-            solrClient.addBean(textRegion);
-            solrClient.commit();
-        } catch (SolrServerException | IOException e) {
-            e.printStackTrace();
-        }
+    private void addTextRegion(TextRegion textRegion) throws IOException, SolrServerException {
+        solrClient.addBean(textRegion);
+        solrClient.commit();
     }
 
-
+    public String info() throws IOException, SolrServerException {
+        SolrQuery q = new SolrQuery("*:*");
+        q.setRows(0);  // don't actually request any data
+        return "Number of documents in core: " + solrClient.query(q).getResults().getNumFound();
+    }
 }
diff --git a/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/XMLLoader.java b/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/XMLLoader.java
index b8151d2..92b95d3 100644
--- a/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/XMLLoader.java
+++ b/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/XMLLoader.java
@@ -10,27 +10,20 @@ import java.util.logging.Level;
 
 public class XMLLoader {
 
-    public PcGts loadFile(InputStream file) {
-        try {
-            JAXBContext jaxbContext = JAXBContext.newInstance(PcGts.class);
-            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
-
-            java.util.logging.Logger.getLogger("com.sun.xml.bind").setLevel(Level.FINEST);
-            jaxbUnmarshaller.setEventHandler(
-                    event -> {
-                        System.out.println("Event Info: "+event);
-                        return event.getMessage().toLowerCase().contains("unexpected element");
-                    });
-
-            PcGts document = (PcGts) jaxbUnmarshaller.unmarshal(file);
-
-            System.out.println("page=" + document.getPage());
-            return document;
-        } catch (JAXBException e) {
-            e.printStackTrace();
-        }
-
-        return null;
+    public PcGts loadFile(String filename) throws JAXBException {
+        InputStream file = getClass().getClassLoader().getResourceAsStream(filename);
+
+        JAXBContext jaxbContext = JAXBContext.newInstance(PcGts.class);
+        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
+
+        // ignoring unused elements
+        java.util.logging.Logger.getLogger("com.sun.xml.bind").setLevel(Level.FINEST);
+        jaxbUnmarshaller.setEventHandler(event -> event.getMessage().toLowerCase().contains("unexpected element"));
+
+        PcGts document = (PcGts) jaxbUnmarshaller.unmarshal(file);
+        document.setId(filename.replaceAll(".xml", ""));
+
+        return document;
     }
 
 }
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 780f070..ef2b1e0 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
@@ -11,6 +11,8 @@ public class Page {
     @XmlElement(name = "TextRegion")
     private List<TextRegion> textRegions;
 
+    private String id;
+
     public List<TextRegion> getTextRegions() {
         return textRegions;
     }
@@ -18,4 +20,17 @@ public class Page {
     public void setTextRegions(List<TextRegion> textRegions) {
         this.textRegions = textRegions;
     }
+
+    public String getId() {
+        return id;
+    }
+
+    public void setId(String id) {
+        this.id = id;
+        for (int i = 0; i < textRegions.size(); i++) {
+            TextRegion textRegion = textRegions.get(i);
+            textRegion.setId(id + "_" + i +"_region");
+            textRegion.setFile(id);
+        }
+    }
 }
diff --git a/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/document/PcGts.java b/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/document/PcGts.java
index 9078037..8887a9c 100644
--- a/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/document/PcGts.java
+++ b/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/document/PcGts.java
@@ -12,6 +12,8 @@ public class PcGts {
     @XmlElement(name = "Page")
     private Page page;
 
+    private String id;
+
     public Page getPage() {
         return page;
     }
@@ -19,4 +21,13 @@ public class PcGts {
     public void setPage(Page page) {
         this.page = page;
     }
+
+    public String getId() {
+        return id;
+    }
+
+    public void setId(String id) {
+        this.id = id;
+        this.page.setId(id);
+    }
 }
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
new file mode 100644
index 0000000..83a595b
--- /dev/null
+++ b/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/document/TextLine.java
@@ -0,0 +1,84 @@
+package cz.zcu.kiv.aswi.fulltextsearch.document;
+
+import org.apache.solr.client.solrj.beans.Field;
+
+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 TextEquiv textEquiv;
+
+    @Field(value = "Word", child = true)
+    private List<Word> words;
+
+    @Field("id")
+    private String id;
+
+    @Field("Coords")
+    private String coordsString;
+
+    @Field("TextEquiv")
+    private String textEquivString;
+
+    public Coords getCoords() {
+        return coords;
+    }
+
+    @XmlElement(name = "Coords")
+    public void setCoords(Coords coords) {
+        this.coords = coords;
+        this.coordsString = coords.getPoints();
+    }
+
+    public TextEquiv getTextEquiv() {
+        return textEquiv;
+    }
+
+    @XmlElement(name = "TextEquiv")
+    public void setTextEquiv(TextEquiv textEquiv) {
+        this.textEquiv = textEquiv;
+        this.textEquivString = textEquiv.getUnicode();
+    }
+
+    public String getCoordsString() {
+        return coordsString;
+    }
+
+    public String getTextEquivString() {
+        return textEquivString;
+    }
+
+    public void setCoordsString(String coordsString) {
+        this.coordsString = coordsString;
+    }
+
+    public void setTextEquivString(String textEquivString) {
+        this.textEquivString = textEquivString;
+    }
+
+    public List<Word> getWords() {
+        return words;
+    }
+
+    @XmlElement(name = "Word")
+    public void setWords(List<Word> words) {
+        this.words = words;
+    }
+
+    public String getId() {
+        return id;
+    }
+
+    public void setId(String id) {
+        this.id = id;
+        for (int i = 0; i < words.size(); i++) {
+            Word word = words.get(i);
+            word.setId(id + "_" + i + "_word");
+        }
+    }
+}
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 9909387..cdc9a8a 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
@@ -5,31 +5,91 @@ import org.apache.solr.client.solrj.beans.Field;
 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;
 import javax.xml.bind.annotation.XmlElement;
+import java.util.List;
 
-@XmlAccessorType(XmlAccessType.FIELD)
+@XmlAccessorType(XmlAccessType.PROPERTY)
 public class TextRegion {
 
-    @Field("Coords")
-    @XmlElement(name = "Coords")
     private Coords coords;
+    private TextEquiv textEquiv;
+
+    @Field("id")
+    private String id;
+
+    @Field("file")
+    private String file;
+
+    @Field(value = "TextLine", child = true)
+    private List<TextLine> textLines;
+
+    @Field("Coords")
+    private String coordsString;
 
     @Field("TextEquiv")
-    @XmlElement(name = "TextEquiv")
-    private TextEquiv textEquiv;
+    private String textEquivString;
 
     public Coords getCoords() {
         return coords;
     }
 
+    @XmlElement(name = "Coords")
     public void setCoords(Coords coords) {
         this.coords = coords;
+        this.coordsString = coords.getPoints();
     }
 
     public TextEquiv getTextEquiv() {
         return textEquiv;
     }
 
+    @XmlElement(name = "TextEquiv")
     public void setTextEquiv(TextEquiv textEquiv) {
         this.textEquiv = textEquiv;
+        this.textEquivString = textEquiv.getUnicode();
+    }
+
+    public List<TextLine> getTextLines() {
+        return textLines;
+    }
+
+    @XmlElement(name = "TextLine")
+    public void setTextLines(List<TextLine> textLines) {
+        this.textLines = textLines;
+    }
+
+    public String getCoordsString() {
+        return coordsString;
+    }
+
+    public String getTextEquivString() {
+        return textEquivString;
+    }
+
+    public void setCoordsString(String coordsString) {
+        this.coordsString = coordsString;
+    }
+
+    public void setTextEquivString(String textEquivString) {
+        this.textEquivString = textEquivString;
+    }
+
+    public String getId() {
+        return id;
+    }
+
+    public void setId(String id) {
+        this.id = id;
+        for (int i = 0; i < textLines.size(); i++) {
+            TextLine textLine = textLines.get(i);
+            textLine.setId(id + "_" + i + "_line");
+        }
+    }
+
+    public String getFile() {
+        return file;
+    }
+
+    public void setFile(String file) {
+        this.file = file;
     }
 }
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
new file mode 100644
index 0000000..ef80543
--- /dev/null
+++ b/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/document/Word.java
@@ -0,0 +1,67 @@
+package cz.zcu.kiv.aswi.fulltextsearch.document;
+
+import org.apache.solr.client.solrj.beans.Field;
+
+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;
+
+    @Field("id")
+    private String id;
+
+    @Field("Coords")
+    private String coordsString;
+
+    @Field("TextEquiv")
+    private String textEquivString;
+
+    public Coords getCoords() {
+        return coords;
+    }
+
+    @XmlElement(name = "Coords")
+    public void setCoords(Coords coords) {
+        this.coords = coords;
+        this.coordsString = coords.getPoints().toString();
+    }
+
+    public TextEquiv getTextEquiv() {
+        return textEquiv;
+    }
+
+    @XmlElement(name = "TextEquiv")
+    public void setTextEquiv(TextEquiv textEquiv) {
+        this.textEquiv = textEquiv;
+        this.textEquivString = textEquiv.getUnicode();
+    }
+
+    public String getCoordsString() {
+        return coordsString;
+    }
+
+    public String getTextEquivString() {
+        return textEquivString;
+    }
+
+    public void setCoordsString(String coordsString) {
+        this.coordsString = coordsString;
+    }
+
+    public void setTextEquivString(String textEquivString) {
+        this.textEquivString = textEquivString;
+    }
+
+    public String getId() {
+        return id;
+    }
+
+    public void setId(String id) {
+        this.id = id;
+    }
+}
