commit 4fd315dc5f09ddfe2f498f98ed42890e3939e8c8
Author: smoldas15 <jan.smolar@seznam.cz>
Date:   Sat Apr 18 17:10:26 2020 +0200

    Re #7813: Logování BE
    
    - vytvořeno logování BE

diff --git a/be/fulltextsearch/pom.xml b/be/fulltextsearch/pom.xml
index 19d82fb..8dabcfb 100644
--- a/be/fulltextsearch/pom.xml
+++ b/be/fulltextsearch/pom.xml
@@ -90,6 +90,12 @@
 			<version>1.10.0</version>
 		</dependency>
 
+		<dependency>
+			<groupId>org.slf4j</groupId>
+			<artifactId>slf4j-api</artifactId>
+			<version>1.7.30</version>
+		</dependency>
+
 	</dependencies>
 
 	<build>
diff --git a/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/FulltextSearchApplication.java b/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/FulltextSearchApplication.java
index 2d53463..6d33ba3 100644
--- a/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/FulltextSearchApplication.java
+++ b/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/FulltextSearchApplication.java
@@ -1,12 +1,18 @@
 package cz.zcu.kiv.aswi.fulltextsearch;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 
 @SpringBootApplication
 public class FulltextSearchApplication {
 
+	private static final Logger LOG = LoggerFactory.getLogger(FulltextSearchApplication.class);
+
 	public static void main(String[] args) {
+
+		LOG.info("Starting Spring BE");
 		SpringApplication.run(FulltextSearchApplication.class, args);
 	}
 
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 f3f422f..1f29c4e 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,8 @@ import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import java.util.ArrayList;
 import java.util.Date;
@@ -14,27 +16,34 @@ import java.util.List;
 @RestController
 public class IndexController {
 
+    private static final Logger LOG = LoggerFactory.getLogger(IndexController.class);
     private SolrService solrService = new SolrService();
 
     @PostMapping("/")
     public SearchResponse index(@RequestBody SearchRequest searchRequest) {
+        LOG.trace("Method index called");
         try {
+            LOG.debug("Calling SolrService and its Query method");
             return solrService.query(searchRequest.getExpression());
         } catch (Exception e) {
-            e.printStackTrace();
+            LOG.error("Cannot get method Query from SolrService returning NULL");
+            LOG.error(e.toString());
             return null;
         }
     }
 
     @PostMapping("/upload")
     public ResponseEntity<ResponseMessage> uploadFile(@RequestParam("file") List<MultipartFile> files) {
+        LOG.trace("Method uploadFile called");
         String message;
         try {
+            LOG.debug("Calling SolrService and its uploadFiles method");
             int uploaded = solrService.uploadFiles(files);
             message = "Successfully uploaded " + uploaded + " files";
             return ResponseEntity.status(HttpStatus.OK).body(new ResponseMessage(message));
         } catch (Exception e) {
-            e.printStackTrace();
+            LOG.error("Cannot get method uploadFiles from SolrService");
+            LOG.error(e.toString());
             message = "Could not upload the files! " + e.toString();
             return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body(new ResponseMessage(message));
         }
@@ -42,47 +51,53 @@ public class IndexController {
 
     @GetMapping("/documents")
     public List<DocumentResponse> listDocuments() {
+        LOG.trace("Method listDocuments called");
         List<DocumentResponse> response = new ArrayList<>();
 
         try {
+            LOG.debug("Calling SolrService and its listAllFiles method");
             List<String> filenames = solrService.listAllFiles();
             int id = 0; // todo
             for (String filename: filenames) {
                 response.add(new DocumentResponse(filename, id++, new Date(), new Date()));
             }
         } catch (Exception e) {
-            e.printStackTrace();
+            LOG.error("Cannot get method listAllFiles from SolrService");
+            LOG.error(e.toString());
         }
-
         return response;
     }
 
     @GetMapping("/delete")
     public String delete() {
+        LOG.trace("Method delete called");
         String response;
 
         try {
+            LOG.debug("Calling SolrService and its deleteAll method");
             solrService.deleteAll();
             response = "All documents have been deleted.";
         } catch (Exception e) {
+            LOG.error("Cannot get method deleteAll from SolrService");
+            LOG.error(e.toString());
             response = "Unknown exception";
-            e.printStackTrace();
         }
-
         return response;
     }
 
     @GetMapping("/info")
     public String info() {
+        LOG.trace("Method info called");
         String response;
 
         try {
+            LOG.debug("Calling SolrService and its info method");
             response = solrService.info();
         } catch (Exception e) {
+            LOG.error("Cannot get method info from SolrService");
+            LOG.error(e.toString());
             response = "Unknown exception";
-            e.printStackTrace();
         }
-
         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 e0b38ed..3a55a62 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
@@ -12,6 +12,8 @@ import org.apache.solr.client.solrj.response.schema.SchemaResponse;
 import org.apache.solr.common.SolrDocument;
 import org.apache.solr.common.SolrDocumentList;
 import org.apache.solr.common.SolrInputDocument;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.http.MediaType;
 import org.springframework.web.multipart.MultipartFile;
 
@@ -45,6 +47,7 @@ public class SolrService {
     private static final String FIELD_DOC_CONTENT = "doc_content";
     private static final String FIELD_IMG_CONTENT = "img_content";
     private static final float IMG_OUTPUT_QUALITY = 0.1f;
+    private static final Logger LOG = LoggerFactory.getLogger(IndexController.class);
 
     private static final int SEARCH_QUERY_ROWS = 100;
 
@@ -57,6 +60,7 @@ public class SolrService {
     }
 
     private void connect() {
+        LOG.info("Connecting to Solr");
         String urlString = URL + ":8983/solr/" + CORE_NAME_DOCUMENTS;
         solrClientDocuments = new HttpSolrClient.Builder(urlString).build();
 
@@ -65,18 +69,20 @@ public class SolrService {
     }
 
     public int uploadFiles(List<MultipartFile> files) throws IOException, SolrServerException, JAXBException {
+        LOG.trace("SolrService method uploadFiles called");
         Map<String, List<MultipartFile>> pairs = new HashMap<>();
 
+        LOG.debug("Preprocessing files for upload to Solr");
         for (MultipartFile file: files) {
             String filename = file.getOriginalFilename();
             if (filename == null) {
-                System.out.println("Skipping file with empty filename.");
+                LOG.error("Skipping file with empty filename");
                 continue;
             }
 
             String type = file.getContentType();
             if (type == null) {
-                System.out.println("Skipping file " + filename + " (unknown type).");
+                LOG.error("Skipping file " + filename + " (unknown type)");
                 continue;
             }
 
@@ -97,16 +103,17 @@ public class SolrService {
                 } else {
                     list.add(file);
                 }
-
+                LOG.debug("File " + filename + " added");
             } else {
-                System.out.println("Not supported type of file! File " + filename + " is type " + type + ".");
+                LOG.error("Not supported type of file! File " + filename + " is type " + type);
             }
         }
-
         return addFiles(pairs);
     }
 
     private int addFiles(Map<String, List<MultipartFile>> pairs) throws IOException, SolrServerException, JAXBException {
+        LOG.trace("SolrService method addFiles called");
+        LOG.debug("Uploading files to Solr");
         getFieldNamesFromDocumentCore();
         addBasicFieldsToDocumentsCore();
 
@@ -124,19 +131,22 @@ public class SolrService {
             } else {
                 addToFilesCore(pair.get(1), first);
             }
-
             count += 2;
         }
 
+        LOG.debug("Commiting Solr Documents and Files core changes");
         solrClientFiles.commit();
         solrClientDocuments.commit();
 
+        LOG.info("Files uploaded");
         return count;
     }
 
     private void addToFilesCore(MultipartFile xmlFile, MultipartFile imageFile) throws IOException, SolrServerException, JAXBException {
+        LOG.trace("SolrService method addToFilesCore called");
         SolrInputDocument solrInputDocument = new SolrInputDocument();
 
+        LOG.debug("Adding a file pair to File core");
         solrInputDocument.addField(FIELD_DOC_FILENAME, xmlFile.getOriginalFilename());
         solrInputDocument.addField(FIELD_IMG_FILENAME, imageFile.getOriginalFilename());
         solrInputDocument.addField(FIELD_DOC_CONTENT, xmlFile.getBytes());
@@ -144,12 +154,14 @@ public class SolrService {
 
         solrClientFiles.add(solrInputDocument);
 
+        LOG.debug("Loading XML file");
         XMLLoader xmlLoader = new XMLLoader();
         PcGts doc = xmlLoader.loadFile(xmlFile.getInputStream(), xmlFile.getOriginalFilename());
         addToDocumentsCore(doc, imageFile.getOriginalFilename());
     }
 
     private void addAllFieldsToFilesCore() throws IOException, SolrServerException {
+        LOG.trace("SolrService method addAllFieldsToFilesCore called");
         List<String> fieldNamesFilesCore = new ArrayList<>();
 
         SchemaRequest.Fields fieldRequest = new SchemaRequest.Fields();
@@ -163,6 +175,7 @@ public class SolrService {
         String[] filenames = new String[]{ FIELD_DOC_FILENAME, FIELD_IMG_FILENAME };
         String[] contents = new String[]{ FIELD_DOC_CONTENT, FIELD_IMG_CONTENT };
 
+        LOG.debug("Adding all fields to File core");
         for (String name: filenames) {
             if (fieldNamesFilesCore.contains(name)) continue;
 
@@ -170,8 +183,8 @@ public class SolrService {
                 SchemaRequest.Update request = getAddTextFieldRequest(name);
                 request.process(solrClientFiles);
             } catch (Exception e) {
-                System.out.println("Couldn't add field = " + name);
-                e.printStackTrace();
+                LOG.error("Couldn't add field = " + name);
+                LOG.error(e.toString());
             }
         }
 
@@ -188,13 +201,15 @@ public class SolrService {
                 SchemaRequest.Update request = new SchemaRequest.AddField(fieldAttributes);
                 request.process(solrClientFiles);
             } catch (Exception e) {
-                System.out.println("Couldn't add field = " + name);
-                e.printStackTrace();
+                LOG.error("Couldn't add field = " + name);
+                LOG.error(e.toString());
             }
         }
     }
 
     private void addBasicFieldsToDocumentsCore() {
+        LOG.trace("SolrService method addBasicFieldsToDocumentsCore called");
+        LOG.debug("Adding basic fields to Documents core");
         addFieldToDocumentCore(FIELD_DOC_FILENAME);
         addFieldToDocumentCore(FIELD_IMG_FILENAME);
         addFieldToDocumentCore(FIELD_TEXT_REGION);
@@ -202,6 +217,8 @@ public class SolrService {
     }
 
     private void getFieldNamesFromDocumentCore() throws IOException, SolrServerException {
+        LOG.trace("SolrService method getFieldNamesFromDocumentCore called");
+        LOG.debug("Getting field names from Documents core");
         SchemaRequest.Fields fieldRequest = new SchemaRequest.Fields();
         SchemaResponse.FieldsResponse fieldsResponse = fieldRequest.process(solrClientDocuments);
         List<Map<String, Object>> fields = fieldsResponse.getFields();
@@ -213,19 +230,22 @@ public class SolrService {
 
 
     private void addFieldToDocumentCore(String name) {
-        if (fieldNamesDocumentCore.contains(name)) return;
+        LOG.trace("SolrService method addFieldToDocumentCore called");
 
+        if (fieldNamesDocumentCore.contains(name)) return;
+        LOG.debug("Adding field to Documents core");
         try {
             SchemaRequest.Update request = getAddTextFieldRequest(name);
             request.process(solrClientDocuments);
             fieldNamesDocumentCore.add(name);
         } catch (Exception e) {
-            System.out.println("Couldn't add field = " + name);
-            e.printStackTrace();
+            LOG.error("Couldn't add field = " + name);
+            LOG.error(e.toString());
         }
     }
 
     private SchemaRequest.Update getAddTextFieldRequest(String name) {
+        LOG.trace("SolrService method getAddTextFieldRequest called");
         Map<String, Object> fieldAttributes = new LinkedHashMap<>();
         fieldAttributes.put("name", name);
         fieldAttributes.put("type", "text_general");
@@ -235,21 +255,26 @@ public class SolrService {
     }
 
     public void deleteAll() throws IOException, SolrServerException {
+        LOG.trace("SolrService method getAddTextFieldRequest called");
+        LOG.info("Deleting ALL from Documents core");
         solrClientDocuments.deleteByQuery("*:*");
         solrClientDocuments.commit();
 
+        LOG.info("Deleting ALL from Files core");
         solrClientFiles.deleteByQuery("*:*");
         solrClientFiles.commit();
     }
 
     public byte[] getImageFromFilesCore(String filename) throws IOException, SolrServerException {
+        LOG.trace("SolrService method getImageFromFilesCore called");
+        LOG.debug("Getting an image from Files core");
         SolrQuery solrQuery = new SolrQuery();
         solrQuery.set("q", FIELD_IMG_FILENAME + ":" + "\"" + filename + "\"");
         SolrDocumentList results = solrClientFiles.query(solrQuery).getResults();
         if (results.getNumFound() < 1) {
-            System.out.println("Image " + filename + " not found!");
+            LOG.error("Image " + filename + " not found!");
         } else if (results.getNumFound() > 1) {
-            System.out.println("Image " + filename + " has multiple files. Using first one...");
+            LOG.error("Image " + filename + " has multiple files. Using first one...");
         }
 
         SolrDocument result = results.get(0);
@@ -257,12 +282,15 @@ public class SolrService {
     }
 
     public SearchResponse query(String query) throws IOException, SolrServerException  {
+        LOG.trace("SolrService method query called");
+        LOG.debug("Processing query - creating Solr query");
         SolrQuery solrQuery = new SolrQuery();
         solrQuery.set("q", FIELD_TEXT_REGION + ":" + query);
         solrQuery.setHighlight(true);
         solrQuery.addHighlightField(PREFIX_TEXT_LINE +  "*");
         solrQuery.addHighlightField(PREFIX_TEXT_WORD +  "*");
         solrQuery.setRows(SEARCH_QUERY_ROWS);
+        LOG.debug("Processing query - sending Solr query");
         QueryResponse response = solrClientDocuments.query(solrQuery);
 
         SolrDocumentList docList = response.getResults();
@@ -271,6 +299,7 @@ public class SolrService {
         SearchResponse searchResponse = new SearchResponse();
         searchResponse.setExpression(query);
 
+        LOG.debug("Processing query - creating Search response");
         List<TextRegion> textRegions = new LinkedList<>();
         for (SolrDocument solrDocument: docList) {
             TextRegion textRegion = new TextRegion();
@@ -384,11 +413,14 @@ public class SolrService {
         }
 
         searchResponse.setTextRegions(textRegions.toArray(new TextRegion[0]));
-
+        LOG.debug("Processing query - sending Search response");
+        LOG.info("Query received and processed");
         return searchResponse;
     }
 
     private void addToDocumentsCore(PcGts document, String imageFilename) throws IOException, SolrServerException {
+        LOG.trace("SolrService method addToDocumentsCore called");
+        LOG.debug("Adding to Documents core");
         List<TextRegion> regions = document.getPage().getTextRegions();
         if (regions == null || regions.size() == 0) return;
 
@@ -426,12 +458,13 @@ public class SolrService {
                     solrInputDocument.addField(wordPrefix + SUFFIX_COORDS, textWord.getWordCoords().getPointsString());
                 }
             }
-
             solrClientDocuments.add(solrInputDocument);
         }
     }
 
     public String info() throws IOException, SolrServerException {
+        LOG.trace("SolrService method info called");
+        LOG.debug("Displaying information about files in Solr");
         SolrQuery q = new SolrQuery("*:*");
         q.setRows(0);  // don't actually request any data
         return "Number of documents in " + CORE_NAME_DOCUMENTS + " core: " + solrClientDocuments.query(q).getResults().getNumFound() +
@@ -439,6 +472,8 @@ public class SolrService {
     }
 
     public List<String> listAllFiles() throws IOException, SolrServerException {
+        LOG.trace("SolrService method listAllFiles called");
+        LOG.debug("Displaying ALL files in Solr");
         SolrQuery solrQuery = new SolrQuery();
         solrQuery.set("q", FIELD_DOC_FILENAME + ":*");
         solrQuery.setRows(500); // todo not a magic number!
