diff --git a/src/main/java/ru/redrise/marinesco/BookController.java b/src/main/java/ru/redrise/marinesco/BookController.java index 0cca2eb..dc7ae69 100644 --- a/src/main/java/ru/redrise/marinesco/BookController.java +++ b/src/main/java/ru/redrise/marinesco/BookController.java @@ -6,21 +6,21 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; -import ru.redrise.marinesco.data.InpEntryRepository; -import ru.redrise.marinesco.library.InpEntry; +import ru.redrise.marinesco.data.BookRepository; +import ru.redrise.marinesco.library.Book; @Controller @RequestMapping("/book") public class BookController { - InpEntryRepository inpEntryRepository; + BookRepository bookRepository; - public BookController(InpEntryRepository inpEntryRepository){ - this.inpEntryRepository = inpEntryRepository; + public BookController(BookRepository bookRepository){ + this.bookRepository = bookRepository; } @GetMapping("/{bookId}") - public String getPage(@PathVariable("bookId") Long bookId, Model model) { - InpEntry book = inpEntryRepository.findById(bookId).orElse(null); + public String getPage(@PathVariable("bookId") Integer bookId, Model model) { + Book book = bookRepository.findById(bookId).orElse(null); if (book == null){ model.addAttribute("Error", "Not found"); return "book"; diff --git a/src/main/java/ru/redrise/marinesco/DownloadController.java b/src/main/java/ru/redrise/marinesco/DownloadController.java index 70be12d..f14b718 100644 --- a/src/main/java/ru/redrise/marinesco/DownloadController.java +++ b/src/main/java/ru/redrise/marinesco/DownloadController.java @@ -1,7 +1,6 @@ package ru.redrise.marinesco; import java.io.File; -import java.io.FileOutputStream; import java.nio.charset.StandardCharsets; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; diff --git a/src/main/java/ru/redrise/marinesco/SearchController.java b/src/main/java/ru/redrise/marinesco/SearchController.java new file mode 100644 index 0000000..0de70cb --- /dev/null +++ b/src/main/java/ru/redrise/marinesco/SearchController.java @@ -0,0 +1,47 @@ +package ru.redrise.marinesco; + +import java.util.List; + +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; + +import lombok.extern.slf4j.Slf4j; +import ru.redrise.marinesco.data.AuthorRepository; +import ru.redrise.marinesco.data.BookRepository; +import ru.redrise.marinesco.library.Author; +import ru.redrise.marinesco.library.Book; + +@Slf4j +@Controller +@RequestMapping("/search") +public class SearchController { + + private BookRepository inpEntryRepository; + private AuthorRepository authorRepository; + + public SearchController(BookRepository bookRepository, AuthorRepository authorRepository){ + this.inpEntryRepository = bookRepository; + this.authorRepository = authorRepository; + } + + @GetMapping + public String requestMethodName(@RequestParam String search, Model model) { + + if (search.trim().equals("")) + return "search"; + + List books = inpEntryRepository.findByTitleContainingIgnoreCase(search); + model.addAttribute("books", books); + + List bookSeries = inpEntryRepository.findByTitleContainingIgnoreCase(search); + model.addAttribute("series", bookSeries); + + List authors = authorRepository.findByAuthorNameContainingIgnoreCase(search); + model.addAttribute("authors", authors); + + return "search"; + } +} diff --git a/src/main/java/ru/redrise/marinesco/data/AuthorRepository.java b/src/main/java/ru/redrise/marinesco/data/AuthorRepository.java index 23f23a4..4e220b3 100644 --- a/src/main/java/ru/redrise/marinesco/data/AuthorRepository.java +++ b/src/main/java/ru/redrise/marinesco/data/AuthorRepository.java @@ -6,10 +6,14 @@ import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Repository; import ru.redrise.marinesco.library.Author; +import java.util.List; + + @Repository public interface AuthorRepository extends CrudRepository{ Optional findByAuthorName(String authorName); + List findByAuthorNameContainingIgnoreCase(String authorName); } diff --git a/src/main/java/ru/redrise/marinesco/data/BookRepository.java b/src/main/java/ru/redrise/marinesco/data/BookRepository.java new file mode 100644 index 0000000..137fabb --- /dev/null +++ b/src/main/java/ru/redrise/marinesco/data/BookRepository.java @@ -0,0 +1,16 @@ +package ru.redrise.marinesco.data; + +import java.util.List; + +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +import ru.redrise.marinesco.library.Book; + + + +@Repository +public interface BookRepository extends CrudRepository{ + List findBySeriesContainingIgnoreCase(String title); + List findByTitleContainingIgnoreCase(String title); +} \ No newline at end of file diff --git a/src/main/java/ru/redrise/marinesco/data/InpEntryRepository.java b/src/main/java/ru/redrise/marinesco/data/InpEntryRepository.java deleted file mode 100644 index b54001e..0000000 --- a/src/main/java/ru/redrise/marinesco/data/InpEntryRepository.java +++ /dev/null @@ -1,10 +0,0 @@ -package ru.redrise.marinesco.data; - -import org.springframework.data.repository.CrudRepository; -import org.springframework.stereotype.Repository; - -import ru.redrise.marinesco.library.InpEntry; - -@Repository -public interface InpEntryRepository extends CrudRepository{ -} \ No newline at end of file diff --git a/src/main/java/ru/redrise/marinesco/library/InpEntry.java b/src/main/java/ru/redrise/marinesco/library/Book.java similarity index 84% rename from src/main/java/ru/redrise/marinesco/library/InpEntry.java rename to src/main/java/ru/redrise/marinesco/library/Book.java index 306d6ba..e324b48 100644 --- a/src/main/java/ru/redrise/marinesco/library/InpEntry.java +++ b/src/main/java/ru/redrise/marinesco/library/Book.java @@ -21,12 +21,12 @@ import ru.redrise.marinesco.data.GenreRepository; @Entity @Data @NoArgsConstructor(access = AccessLevel.PRIVATE, force = true) -public class InpEntry { +public class Book { @Id private Integer id; private Long libraryId; private String libraryVersion; - + @ManyToMany private List authors; // Surname,name,by-father @ManyToMany @@ -36,22 +36,22 @@ public class InpEntry { private String serNo; private String fsFileName; // inside zip private Long fileSize; // extracted, in bytes + private String fileSizeForHumans; private String libId; // same to filename private String deleted; // is deleted flag private String fileExtension; // - concatenate to fsFileName private LocalDate addedDate; private String container; - @Transient private int position = 0; @Transient private byte[] line; - public InpEntry(byte[] line, + public Book(byte[] line, String container, AuthorRepository authorRepository, - GenreRepository genreRepository, + GenreRepository genreRepository, Long libraryId, String libraryVersion) throws Exception { // AUTHOR;GENRE;TITLE;SERIES;SERNO;FILE;SIZE;LIBID;DEL;EXT;DATE; @@ -69,6 +69,7 @@ public class InpEntry { this.serNo = parseNextString(); this.fsFileName = parseNextString(); this.fileSize = Long.valueOf(parseNextString()); + this.fileSizeForHumans = formatByteSize(fileSize); this.libId = parseNextString(); this.deleted = parseNextString(); this.fileExtension = parseNextString(); @@ -101,9 +102,11 @@ public class InpEntry { String allAuthors = new String(line, 0, position, StandardCharsets.UTF_8); for (String authorName : allAuthors.split(":")) { - Author author = authorRepository.findByAuthorName(authorName).orElse(new Author(authorName)); - - authors.add(authorRepository.save(author)); + authorName = authorName.replaceAll(",", " ").trim(); + if (!authorName.equals("")) { + Author author = authorRepository.findByAuthorName(authorName).orElse(new Author(authorName)); + authors.add(authorRepository.save(author)); + } } ++position; @@ -143,4 +146,12 @@ public class InpEntry { RainbowDump.hexDumpUTF8(line); throw new Exception("Invalid 'inp' file format (parse Title)"); } + + private String formatByteSize(double length) { + final String[] measureName = { "bytes", "KB", "MB", "GB", "TB" }; + int i; + for (i = 0; length > 1024 && i < measureName.length - 1; i++) + length = length / 1024; + return String.format("%,.2f %s", length, measureName[i]); + } } diff --git a/src/main/java/ru/redrise/marinesco/library/InpxScanner.java b/src/main/java/ru/redrise/marinesco/library/InpxScanner.java index f58a577..158a593 100644 --- a/src/main/java/ru/redrise/marinesco/library/InpxScanner.java +++ b/src/main/java/ru/redrise/marinesco/library/InpxScanner.java @@ -8,14 +8,13 @@ import java.util.stream.Stream; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; -import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.core.io.FileSystemResource; import org.springframework.stereotype.Component; import lombok.extern.slf4j.Slf4j; import ru.redrise.marinesco.data.AuthorRepository; +import ru.redrise.marinesco.data.BookRepository; import ru.redrise.marinesco.data.GenreRepository; -import ru.redrise.marinesco.data.InpEntryRepository; import ru.redrise.marinesco.data.LibraryMetadataRepository; import ru.redrise.marinesco.settings.ApplicationSettings; @@ -30,19 +29,19 @@ public class InpxScanner implements Runnable { private LibraryMetadataRepository libraryMetadataRepository; private AuthorRepository authorRepository; private GenreRepository genreRepository; - private InpEntryRepository inpEntryRepository; + private BookRepository bookRepository; private String filesLocation; public InpxScanner(ApplicationSettings applicationSettings, AuthorRepository authorRepository, GenreRepository genreRepository, - InpEntryRepository inpEntryRepository, + BookRepository bookRepository, LibraryMetadataRepository libraryMetadataRepository) { this.filesLocation = applicationSettings.getFilesLocation(); this.authorRepository = authorRepository; this.genreRepository = genreRepository; - this.inpEntryRepository = inpEntryRepository; + this.bookRepository = bookRepository; this.libraryMetadataRepository = libraryMetadataRepository; } @@ -170,14 +169,14 @@ public class InpxScanner implements Runnable { byte[] line = new byte[i - lastIndex]; System.arraycopy(content, lastIndex, line, 0, i - lastIndex - 1); - InpEntry book = new InpEntry(line, + Book book = new Book(line, name, authorRepository, genreRepository, libraryMetadata.getId(), libraryMetadata.getVersion()); - inpEntryRepository.save(book); + bookRepository.save(book); if (isNextCarriageReturn(i, content)) { i += 2; diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 9973be0..63d08f9 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -5,7 +5,8 @@ spring: driver-class-name: org.h2.Driver generate-unique-name: false name: marinesco - url: jdbc:h2:mem:marinesco +# url: jdbc:h2:mem:marinesco + url: jdbc:h2:file:/tmp/h2 username: sa password: jpa: diff --git a/src/main/resources/templates/book.html b/src/main/resources/templates/book.html index 54ec738..55bc7c1 100644 --- a/src/main/resources/templates/book.html +++ b/src/main/resources/templates/book.html @@ -27,7 +27,7 @@


-
+

diff --git a/src/main/resources/templates/fragments/header.html b/src/main/resources/templates/fragments/header.html index b075b7b..d49bc6b 100644 --- a/src/main/resources/templates/fragments/header.html +++ b/src/main/resources/templates/fragments/header.html @@ -31,13 +31,15 @@
Marinesco
- - - + +
+ +
+ \ No newline at end of file diff --git a/src/main/resources/templates/root.html b/src/main/resources/templates/root.html index 954dc0c..6db75fb 100644 --- a/src/main/resources/templates/root.html +++ b/src/main/resources/templates/root.html @@ -20,7 +20,7 @@
H2

-
+
diff --git a/src/main/resources/templates/search.html b/src/main/resources/templates/search.html new file mode 100644 index 0000000..ed2b154 --- /dev/null +++ b/src/main/resources/templates/search.html @@ -0,0 +1,83 @@ + + + + + Marinesco + + + + + + +
+
+
+
+ + +
+
+
+

Titles

+
+ + + +
+
+
+
+ + + + +
+ + + +

+
+
+ +
+
+

Series

+
+ + + +
+
+
+
+ + + + +
+ + + +

+
+
+ +
+
+

Authors

+
+ + + +

+
+
+
+
+ +
+ + + \ No newline at end of file