Skip to content
Snippets Groups Projects
Commit 86f11116 authored by dre0059's avatar dre0059
Browse files

Validating and saving Authors into the database

parent bc07103b
Branches
No related merge requests found
......@@ -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);
}
......@@ -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);
}
}
  • Author Owner

    TODO :

    • validate author based on last name and initials (there are usually only initials of the first name in reference list)
    • process references from the list
    • upload abstractText (it's not working now. The text is too big for varchar(255))
    Edited by dre0059
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment