diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/mapper/LibraryPathMapper.java b/booklore-api/src/main/java/com/adityachandel/booklore/mapper/LibraryPathMapper.java index 2440591e1..c09c13941 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/mapper/LibraryPathMapper.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/mapper/LibraryPathMapper.java @@ -13,6 +13,4 @@ import java.util.List; public interface LibraryPathMapper { LibraryPath toLibraryPath(LibraryPathEntity libraryPathEntity); - - List toLibraryPathList(List libraryPathEntities); } diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/model/entity/LibraryEntity.java b/booklore-api/src/main/java/com/adityachandel/booklore/model/entity/LibraryEntity.java index 228e6c10d..329b5ddc4 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/model/entity/LibraryEntity.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/model/entity/LibraryEntity.java @@ -28,7 +28,7 @@ public class LibraryEntity { @OneToMany(mappedBy = "library", cascade = CascadeType.ALL, orphanRemoval = true) private List bookEntities; - @OneToMany(mappedBy = "library", cascade = CascadeType.ALL, orphanRemoval = true) + @OneToMany(mappedBy = "library", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER) private List libraryPaths; private boolean watch; diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/LibraryService.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/LibraryService.java index 0472555c1..d84d918f9 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/LibraryService.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/LibraryService.java @@ -17,6 +17,7 @@ import com.adityachandel.booklore.repository.LibraryPathRepository; import com.adityachandel.booklore.repository.LibraryRepository; import com.adityachandel.booklore.service.fileprocessor.FileProcessingUtils; import com.adityachandel.booklore.service.monitoring.MonitoringService; +import jakarta.annotation.PostConstruct; import jakarta.transaction.Transactional; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -45,59 +46,90 @@ public class LibraryService { private final LibraryMapper libraryMapper; private final NotificationService notificationService; private final FileProcessingUtils fileProcessingUtils; + private final MonitoringService monitoringService; + + @Transactional + @PostConstruct + public void initializeMonitoring() { + List libraries = libraryRepository.findAll().stream().map(libraryMapper::toLibrary).collect(Collectors.toList()); + monitoringService.registerLibrariesForMonitoring(libraries); + log.info("Monitoring initialized with {} libraries", libraries.size()); + } public Library updateLibrary(CreateLibraryRequest request, Long libraryId) { - LibraryEntity library = libraryRepository.findById(libraryId).orElseThrow(() -> ApiError.LIBRARY_NOT_FOUND.createException(libraryId)); + LibraryEntity library = libraryRepository.findById(libraryId) + .orElseThrow(() -> ApiError.LIBRARY_NOT_FOUND.createException(libraryId)); + library.setName(request.getName()); library.setIcon(request.getIcon()); library.setWatch(request.isWatch()); - Set currentPaths = library.getLibraryPaths().stream().map(LibraryPathEntity::getPath).collect(Collectors.toSet()); - Set updatedPaths = request.getPaths().stream().map(LibraryPath::getPath).collect(Collectors.toSet()); + Set currentPaths = library.getLibraryPaths().stream() + .map(LibraryPathEntity::getPath) + .collect(Collectors.toSet()); + Set updatedPaths = request.getPaths().stream() + .map(LibraryPath::getPath) + .collect(Collectors.toSet()); - Set deletedPaths = currentPaths.stream().filter(path -> !updatedPaths.contains(path)).collect(Collectors.toSet()); - Set newPaths = updatedPaths.stream().filter(path -> !currentPaths.contains(path)).collect(Collectors.toSet()); + Set deletedPaths = currentPaths.stream() + .filter(path -> !updatedPaths.contains(path)) + .collect(Collectors.toSet()); + Set newPaths = updatedPaths.stream() + .filter(path -> !currentPaths.contains(path)) + .collect(Collectors.toSet()); - if (newPaths.isEmpty() && deletedPaths.isEmpty()) { - return libraryMapper.toLibrary(libraryRepository.save(library)); - } else { - if (!deletedPaths.isEmpty()) { - Set pathsToRemove = library.getLibraryPaths().stream() - .filter(pathEntity -> deletedPaths.contains(pathEntity.getPath())) - .collect(Collectors.toSet()); - library.getLibraryPaths().removeAll(pathsToRemove); + if (!deletedPaths.isEmpty()) { + Set pathsToRemove = library.getLibraryPaths().stream() + .filter(pathEntity -> deletedPaths.contains(pathEntity.getPath())) + .collect(Collectors.toSet()); - List books = bookRepository.findAllBookIdsByLibraryPathIdIn(pathsToRemove.stream().map(LibraryPathEntity::getId).collect(Collectors.toSet())); - if (!books.isEmpty()) { - notificationService.sendMessage(Topic.BOOKS_REMOVE, books); - } + library.getLibraryPaths().removeAll(pathsToRemove); + List books = bookRepository.findAllBookIdsByLibraryPathIdIn( + pathsToRemove.stream().map(LibraryPathEntity::getId).collect(Collectors.toSet())); - libraryPathRepository.deleteAll(pathsToRemove); - libraryPathRepository.saveAll(library.getLibraryPaths()); - libraryRepository.save(library); + if (!books.isEmpty()) { + notificationService.sendMessage(Topic.BOOKS_REMOVE, books); } - if (!newPaths.isEmpty()) { - Set newPathEntities = newPaths.stream() - .map(path -> LibraryPathEntity.builder().path(path).library(library).build()) - .collect(Collectors.toSet()); - library.getLibraryPaths().addAll(newPathEntities); - libraryPathRepository.saveAll(library.getLibraryPaths()); - libraryRepository.save(library); - - Thread.startVirtualThread(() -> { - try { - libraryProcessingService.processLibrary(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!"); - }); - } - return libraryMapper.toLibrary(libraryRepository.save(library)); + libraryPathRepository.deleteAll(pathsToRemove); } + + if (!newPaths.isEmpty()) { + Set newPathEntities = newPaths.stream() + .map(path -> LibraryPathEntity.builder().path(path).library(library).build()) + .collect(Collectors.toSet()); + + library.getLibraryPaths().addAll(newPathEntities); + libraryPathRepository.saveAll(library.getLibraryPaths()); + } + + LibraryEntity savedLibrary = libraryRepository.save(library); + + if (request.isWatch()) { + for (LibraryPathEntity pathEntity : savedLibrary.getLibraryPaths()) { + Path path = Paths.get(pathEntity.getPath()); + monitoringService.registerPathWithLibraryId(path, libraryId); + } + } else { + for (LibraryPathEntity pathEntity : savedLibrary.getLibraryPaths()) { + monitoringService.unregisterPath(pathEntity.getPath()); + } + } + + if (!newPaths.isEmpty()) { + Thread.startVirtualThread(() -> { + try { + libraryProcessingService.processLibrary(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!"); + }); + } + + return libraryMapper.toLibrary(savedLibrary); } public Library createLibrary(CreateLibraryRequest request) { @@ -117,10 +149,12 @@ public class LibraryService { libraryEntity = libraryRepository.save(libraryEntity); Long libraryId = libraryEntity.getId(); - /*for (LibraryPathEntity pathEntity : libraryEntity.getLibraryPaths()) { - Path path = Paths.get(pathEntity.getPath()); - monitoringService.registerPathWithLibraryId(path, libraryId); - }*/ + if (request.isWatch()) { + for (LibraryPathEntity pathEntity : libraryEntity.getLibraryPaths()) { + Path path = Paths.get(pathEntity.getPath()); + monitoringService.registerPathWithLibraryId(path, libraryId); + } + } Thread.startVirtualThread(() -> { try { @@ -183,10 +217,4 @@ public class LibraryService { List bookEntities = bookRepository.findBooksByLibraryId(libraryId); return bookEntities.stream().map(bookMapper::toBook).toList(); } - - @Transactional - public List getAllLibraries() { - return libraryRepository.findAll().stream().map(libraryMapper::toLibrary).collect(Collectors.toList()); - } - } \ No newline at end of file diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/monitoring/MonitoringService.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/monitoring/MonitoringService.java index c8e684022..301d03a4f 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/monitoring/MonitoringService.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/monitoring/MonitoringService.java @@ -2,18 +2,17 @@ package com.adityachandel.booklore.service.monitoring; import com.adityachandel.booklore.model.dto.Library; import com.adityachandel.booklore.service.LibraryProcessingService; -import com.adityachandel.booklore.service.LibraryService; import jakarta.annotation.PostConstruct; import jakarta.annotation.PreDestroy; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.springframework.context.annotation.Lazy; import org.springframework.context.event.EventListener; import org.springframework.dao.InvalidDataAccessApiUsageException; import org.springframework.stereotype.Service; import java.io.IOException; import java.nio.file.*; +import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.*; @@ -25,7 +24,6 @@ public class MonitoringService { private final LibraryProcessingService libraryProcessingService; private final WatchService watchService; - private final LibraryService libraryService; private final MonitoringTask monitoringTask; private final Set monitoredPaths = ConcurrentHashMap.newKeySet(); @@ -36,15 +34,12 @@ public class MonitoringService { @PostConstruct public void initializeMonitoring() { - log.info("Initializing monitoring service..."); - loadInitialPaths(); monitoringTask.monitor(); startProcessingThread(); } - private void loadInitialPaths() { - libraryService.getAllLibraries() - .stream() + public void registerLibrariesForMonitoring(List libraries) { + libraries.stream() .filter(Library::isWatch) .forEach(library -> { library.getPaths().forEach(libraryPath -> { @@ -54,7 +49,7 @@ public class MonitoringService { } }); }); - log.info("Loaded initial monitored folders: {}", pathToLibraryIdMap); + log.info("Registered libraries for monitoring: {}", libraries.size()); } public synchronized void registerPathWithLibraryId(Path path, Long libraryId) { diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/monitoring/MonitoringTask.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/monitoring/MonitoringTask.java index f954c3047..3332118b9 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/monitoring/MonitoringTask.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/monitoring/MonitoringTask.java @@ -21,7 +21,6 @@ public class MonitoringTask { @Async public void monitor() { - log.info("START_MONITORING"); try { while (!Thread.currentThread().isInterrupted()) { WatchKey key;