Implement refresh library
This commit is contained in:
+8
@@ -11,6 +11,7 @@ import lombok.AllArgsConstructor;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
@@ -63,4 +64,11 @@ public class LibraryController {
|
||||
public ResponseEntity<LibraryDTO> updateLibrary(@PathVariable long libraryId, @RequestBody Sort sort) {
|
||||
return ResponseEntity.ok(libraryService.updateSort(libraryId, sort));
|
||||
}
|
||||
|
||||
@PutMapping("/{libraryId}/refresh")
|
||||
public ResponseEntity<?> refreshLibrary(@PathVariable long libraryId) {
|
||||
libraryService.refreshLibrary(libraryId);
|
||||
return ResponseEntity.noContent().build();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.adityachandel.booklore.model;
|
||||
|
||||
import com.adityachandel.booklore.model.entity.Library;
|
||||
import com.adityachandel.booklore.model.enums.BookFileType;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
@@ -11,5 +12,5 @@ import lombok.Data;
|
||||
public class LibraryFile {
|
||||
private Library library;
|
||||
private String filePath;
|
||||
private String fileType;
|
||||
private BookFileType bookFileType;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.adityachandel.booklore.model.enums;
|
||||
|
||||
public enum BookFileType {
|
||||
PDF, EPUB
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package com.adityachandel.booklore.model.stomp;
|
||||
|
||||
import com.adityachandel.booklore.model.dto.BookDTO;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
public class BookNotification {
|
||||
|
||||
private Action action;
|
||||
private BookDTO addedBook;
|
||||
private Set<Long> removedBookIds;
|
||||
|
||||
public enum Action {
|
||||
BOOK_ADDED, BOOKS_REMOVED
|
||||
}
|
||||
}
|
||||
@@ -5,12 +5,15 @@ import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||
import org.springframework.data.jpa.repository.Modifying;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
@Repository
|
||||
public interface BookRepository extends JpaRepository<Book, Long>, JpaSpecificationExecutor<Book> {
|
||||
@@ -36,5 +39,9 @@ public interface BookRepository extends JpaRepository<Book, Long>, JpaSpecificat
|
||||
|
||||
@Query("SELECT b FROM Book b JOIN b.shelves s WHERE s.id = :shelfId")
|
||||
List<Book> findByShelfId(@Param("shelfId") Long shelfId);
|
||||
|
||||
@Modifying
|
||||
@Query("DELETE FROM Book b WHERE b.id IN (:ids)")
|
||||
void deleteByIdIn(Collection<Long> ids);
|
||||
}
|
||||
|
||||
|
||||
+5
-4
@@ -3,6 +3,7 @@ package com.adityachandel.booklore.service;
|
||||
import com.adityachandel.booklore.exception.ApiError;
|
||||
import com.adityachandel.booklore.model.LibraryFile;
|
||||
import com.adityachandel.booklore.model.entity.Library;
|
||||
import com.adityachandel.booklore.model.enums.BookFileType;
|
||||
import com.adityachandel.booklore.repository.LibraryRepository;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -43,7 +44,7 @@ public class FileUploadService {
|
||||
file.transferTo(storageFile);
|
||||
LibraryFile libraryFile = LibraryFile.builder()
|
||||
.library(library)
|
||||
.fileType(getFileType(fileType))
|
||||
.bookFileType(getFileType(fileType))
|
||||
.filePath(storageFile.getAbsolutePath())
|
||||
.build();
|
||||
fileProcessor.processFile(libraryFile, false);
|
||||
@@ -54,11 +55,11 @@ public class FileUploadService {
|
||||
}
|
||||
}
|
||||
|
||||
private String getFileType(String f) {
|
||||
private BookFileType getFileType(String f) {
|
||||
if (f.equalsIgnoreCase("application/pdf")) {
|
||||
return "pdf";
|
||||
return BookFileType.PDF;
|
||||
} else if (f.equalsIgnoreCase("application/epub+zip")) {
|
||||
return "epub";
|
||||
return BookFileType.EPUB;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
+64
-6
@@ -3,12 +3,15 @@ package com.adityachandel.booklore.service;
|
||||
import com.adityachandel.booklore.exception.ApiError;
|
||||
import com.adityachandel.booklore.model.LibraryFile;
|
||||
import com.adityachandel.booklore.model.dto.BookDTO;
|
||||
import com.adityachandel.booklore.model.entity.Book;
|
||||
import com.adityachandel.booklore.model.entity.Library;
|
||||
import com.adityachandel.booklore.model.enums.BookFileType;
|
||||
import com.adityachandel.booklore.model.stomp.BookNotification;
|
||||
import com.adityachandel.booklore.repository.BookRepository;
|
||||
import com.adityachandel.booklore.repository.LibraryRepository;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -16,6 +19,11 @@ import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static com.adityachandel.booklore.model.stomp.BookNotification.Action.BOOKS_REMOVED;
|
||||
import static com.adityachandel.booklore.model.stomp.BookNotification.Action.BOOK_ADDED;
|
||||
|
||||
@Service
|
||||
@AllArgsConstructor
|
||||
@@ -25,29 +33,79 @@ public class LibraryProcessingService {
|
||||
private final LibraryRepository libraryRepository;
|
||||
private final NotificationService notificationService;
|
||||
private final PdfFileProcessor pdfFileProcessor;
|
||||
private final BookRepository bookRepository;
|
||||
|
||||
|
||||
@Transactional
|
||||
public void parseLibraryBooks(long libraryId) throws IOException {
|
||||
public void processLibrary(long libraryId) throws IOException {
|
||||
Library library = libraryRepository.findById(libraryId).orElseThrow(() -> ApiError.LIBRARY_NOT_FOUND.createException(libraryId));
|
||||
List<LibraryFile> libraryFiles = getLibraryFiles(library);
|
||||
processLibraryFiles(libraryFiles);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void refreshLibrary(long libraryId) throws IOException {
|
||||
Library library = libraryRepository.findById(libraryId).orElseThrow(() -> ApiError.LIBRARY_NOT_FOUND.createException(libraryId));
|
||||
processLibraryFiles(getUnProcessedFiles(library));
|
||||
deleteRemovedBooks(getRemovedBooks(library));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
protected void deleteRemovedBooks(List<Book> removedBooks) {
|
||||
if (!removedBooks.isEmpty()) {
|
||||
Set<Long> bookIds = removedBooks.stream().map(Book::getId).collect(Collectors.toSet());
|
||||
bookRepository.deleteByIdIn(bookIds);
|
||||
BookNotification notification = BookNotification.builder().action(BOOKS_REMOVED).removedBookIds(bookIds).build();
|
||||
notificationService.sendMessage("/topic/books", notification);
|
||||
log.info("Books removed: {}", bookIds);
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional
|
||||
protected void processLibraryFiles(List<LibraryFile> libraryFiles) {
|
||||
for (LibraryFile libraryFile : libraryFiles) {
|
||||
log.info("Processing file: {}", libraryFile.getFilePath());
|
||||
BookDTO bookDTO = processLibraryFile(libraryFile);
|
||||
if(bookDTO != null) {
|
||||
notificationService.sendMessage("/topic/books", bookDTO);
|
||||
if (bookDTO != null) {
|
||||
BookNotification notification = BookNotification.builder().action(BOOK_ADDED).addedBook(bookDTO).build();
|
||||
notificationService.sendMessage("/topic/books", notification);
|
||||
log.info("Processed file: {}", libraryFile.getFilePath());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional
|
||||
protected BookDTO processLibraryFile(LibraryFile libraryFile) {
|
||||
if (libraryFile.getFileType().equalsIgnoreCase("pdf")) {
|
||||
if (libraryFile.getBookFileType() == BookFileType.PDF) {
|
||||
return pdfFileProcessor.processFile(libraryFile, false);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
protected List<Book> getRemovedBooks(Library library) throws IOException {
|
||||
List<LibraryFile> libraryFiles = getLibraryFiles(library);
|
||||
List<Book> books = library.getBooks();
|
||||
Set<String> libraryFilePaths = libraryFiles.stream()
|
||||
.map(LibraryFile::getFilePath)
|
||||
.collect(Collectors.toSet());
|
||||
return books.stream()
|
||||
.filter(book -> !libraryFilePaths.contains(book.getPath()))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Transactional
|
||||
protected List<LibraryFile> getUnProcessedFiles(Library library) throws IOException {
|
||||
List<LibraryFile> libraryFiles = getLibraryFiles(library);
|
||||
List<Book> books = library.getBooks();
|
||||
Set<String> processedPaths = books.stream()
|
||||
.map(Book::getPath)
|
||||
.collect(Collectors.toSet());
|
||||
return libraryFiles.stream()
|
||||
.filter(libraryFile -> !processedPaths.contains(libraryFile.getFilePath()))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private List<LibraryFile> getLibraryFiles(Library library) throws IOException {
|
||||
List<LibraryFile> libraryFiles = new ArrayList<>();
|
||||
for (String libraryPath : library.getPaths()) {
|
||||
@@ -65,7 +123,7 @@ public class LibraryProcessingService {
|
||||
return fileName.endsWith(".pdf") || fileName.endsWith(".epub");
|
||||
})
|
||||
.forEach(file -> {
|
||||
String fileType = file.getFileName().toString().toLowerCase().endsWith(".pdf") ? "PDF" : "EPUB";
|
||||
BookFileType fileType = file.getFileName().toString().toLowerCase().endsWith(".pdf") ? BookFileType.PDF : BookFileType.EPUB;
|
||||
libraryFiles.add(new LibraryFile(library, file.toAbsolutePath().toString(), fileType));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -29,17 +29,12 @@ public class LibraryService {
|
||||
private final LibraryProcessingService libraryProcessingService;
|
||||
|
||||
public LibraryDTO createLibrary(CreateLibraryRequest request) {
|
||||
Library library = Library.builder()
|
||||
.name(request.getName())
|
||||
.paths(request.getPaths())
|
||||
.build();
|
||||
Library library = Library.builder().name(request.getName()).paths(request.getPaths()).build();
|
||||
library = libraryRepository.save(library);
|
||||
Long libraryId = library.getId();
|
||||
|
||||
Thread.startVirtualThread(() -> {
|
||||
log.info("Running in a virtual thread: {}", Thread.currentThread());
|
||||
try {
|
||||
libraryProcessingService.parseLibraryBooks(libraryId);
|
||||
libraryProcessingService.processLibrary(libraryId);
|
||||
} catch (InvalidDataAccessApiUsageException e) {
|
||||
log.warn("InvalidDataAccessApiUsageException - Library id: {}", libraryId);
|
||||
} catch (IOException e) {
|
||||
@@ -47,10 +42,23 @@ public class LibraryService {
|
||||
}
|
||||
log.info("Parsing task completed!");
|
||||
});
|
||||
|
||||
return LibraryTransformer.convertToLibraryDTO(library);
|
||||
}
|
||||
|
||||
public void refreshLibrary(long libraryId) {
|
||||
libraryRepository.findById(libraryId).orElseThrow(() -> ApiError.LIBRARY_NOT_FOUND.createException(libraryId));
|
||||
Thread.startVirtualThread(() -> {
|
||||
try {
|
||||
libraryProcessingService.refreshLibrary(libraryId);
|
||||
} catch (InvalidDataAccessApiUsageException e) {
|
||||
log.warn("InvalidDataAccessApiUsageException - Library id: {}", libraryId);
|
||||
} catch (IOException e) {
|
||||
log.error("Error while parsing library books", e);
|
||||
}
|
||||
log.info("Parsing task completed!");
|
||||
});
|
||||
}
|
||||
|
||||
public LibraryDTO getLibrary(long libraryId) {
|
||||
Library library = libraryRepository.findById(libraryId).orElseThrow(() -> ApiError.LIBRARY_NOT_FOUND.createException(libraryId));
|
||||
return LibraryTransformer.convertToLibraryDTO(library);
|
||||
@@ -83,4 +91,4 @@ public class LibraryService {
|
||||
library.setSort(sort);
|
||||
return LibraryTransformer.convertToLibraryDTO(libraryRepository.save(library));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -22,7 +22,7 @@ spring:
|
||||
hibernate:
|
||||
naming:
|
||||
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
|
||||
show-sql: false
|
||||
show-sql: true
|
||||
properties:
|
||||
hibernate:
|
||||
jdbc:
|
||||
|
||||
Reference in New Issue
Block a user