From 86f1111695d16e0def34abb3d97587491ac8618c Mon Sep 17 00:00:00 2001
From: dre0059 <eliska.dreveniakova@vsb.cz>
Date: Tue, 18 Feb 2025 14:04:16 +0100
Subject: [PATCH] Validating and saving Authors into the database

---
 .../repository/AuthorRepository.java          |  8 ++--
 .../service/HeaderService.java                | 48 ++++++++++---------
 2 files changed, 31 insertions(+), 25 deletions(-)

diff --git a/src/main/java/com/dre0059/articleprocessor/repository/AuthorRepository.java b/src/main/java/com/dre0059/articleprocessor/repository/AuthorRepository.java
index 42aca26..913ab5b 100644
--- a/src/main/java/com/dre0059/articleprocessor/repository/AuthorRepository.java
+++ b/src/main/java/com/dre0059/articleprocessor/repository/AuthorRepository.java
@@ -10,8 +10,10 @@ import java.util.Optional;
 
 @Repository
 public interface AuthorRepository extends JpaRepository<Author, Long> {
+    /*@Query("SELECT CASE WHEN COUNT(a) > 0 THEN true ELSE false END FROM Author a WHERE a.lastName = :lastName AND a.firstName = :firstName")
+    Optional<Author> findByLastNameAndInitial(@Param("lastName") String lastName, @Param("firstName") String firstName);
+    */
 
-    @Query("SELECT a FROM Author a WHERE a.lastName = :lastName AND SUBSTRING(a.firstName, 1, 1) = SUBSTRING(:firstName, 1, 1)")
-    Optional<Author> findByFullName(@Param("lastName") String lastName, @Param("firstName") String firstName);
-
+    Author findByLastNameAndFirstName(String lastName, String firstName);
 }
+
diff --git a/src/main/java/com/dre0059/articleprocessor/service/HeaderService.java b/src/main/java/com/dre0059/articleprocessor/service/HeaderService.java
index 352f6f7..a4e9ace 100644
--- a/src/main/java/com/dre0059/articleprocessor/service/HeaderService.java
+++ b/src/main/java/com/dre0059/articleprocessor/service/HeaderService.java
@@ -8,12 +8,15 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
 import javax.swing.text.html.Option;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Optional;
+import java.util.*;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
+// TODO :
+//  1. VALIDATE author based on surname and first INITIAL of the firstName.
+//      SOLUTION : change keys of the map on surname and first initial and compare it with surname and first initial of author
+//  2.
+
 @Service
 public class HeaderService {
 
@@ -77,13 +80,10 @@ public class HeaderService {
             return;
         }
 
-
-
-        authorRepository.saveAll(authorList);
-
+        List<Author> savedAuthors = authorRepository.saveAll(authorList);
         Dokument dokument = new Dokument(title, year, doi, pages, publisher);
-        dokument.setAuthors(authorList);
 
+        dokument.setAuthors(savedAuthors);
         this.documentRepository.save(dokument);
     }
 
@@ -100,37 +100,41 @@ public class HeaderService {
     }
 
     private List<Author> saveAuthorNameAndSurname(String author){
-        // "and" divide our authors
+        // "and" divides our authors
         String[] authorNames = author.split(" and ");
-
         List<Author> authors = new ArrayList<>();
+        List<Author> databaseAuthors = authorRepository.findAll();
 
-        System.out.println("Author is : \n" + author);
+        Map<String, Author> authorMap = new HashMap<>();
+        for(Author existingAuthor : databaseAuthors){
+            String key = existingAuthor.getLastname().toLowerCase() + "," + existingAuthor.getFirstname().toLowerCase();
+            authorMap.put(key, existingAuthor);
+        }
 
         for(String fullName : authorNames){
             String[] nameParts = fullName.split(",");
 
             String firstName;
-            String lastName = nameParts[1];
+            String lastName = nameParts[1].trim();
             if(nameParts.length > 2){
                 // have two names
-                firstName = nameParts[0] + " " + nameParts[2];
+                firstName = nameParts[0].trim() + " " + nameParts[2].trim();
             } else {
-                firstName = nameParts[0];
+                firstName = nameParts[0].trim();
             }
 
-            // check if author already exists
-            Optional<Author> existingAuthor = authorRepository.findByFullName(lastName, firstName);
-            if (existingAuthor.isPresent()) {
-                authors.add(existingAuthor.get());
+            String authorKey = lastName.toLowerCase() + "," + firstName.toLowerCase();
+
+            if(authorMap.containsKey(authorKey)){
+                authors.add(authorMap.get(authorKey));
+                System.out.println("This author already exists in the database : " + authorKey);
             } else {
-                Author newAuthor = new Author(lastName, firstName);
+                Author newAuthor = new Author(firstName, lastName);
                 authors.add(newAuthor);
+                authorMap.put(authorKey, newAuthor);
             }
-
-            //authors.add(new Author(lastName, firstName));
         }
 
-        return authors;
+        return authorRepository.saveAll(authors);
     }
 }
-- 
GitLab