Allow physical and digital book states to coexist (#3238)

This commit is contained in:
ACX
2026-03-07 17:59:24 -07:00
committed by GitHub
parent 4965f1811c
commit c3ecf49984
14 changed files with 67 additions and 35 deletions
@@ -280,6 +280,20 @@ public class BookController {
return ResponseEntity.ok(duplicateDetectionService.findDuplicates(request));
}
@Operation(summary = "Toggle physical book flag", description = "Mark or unmark a book as a physical book.")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "Physical flag updated successfully"),
@ApiResponse(responseCode = "404", description = "Book not found")
})
@PatchMapping("/{bookId}/physical")
@CheckBookAccess(bookIdParam = "bookId")
@PreAuthorize("@securityUtil.canManageLibrary() or @securityUtil.isAdmin()")
public ResponseEntity<Book> togglePhysicalFlag(
@Parameter(description = "ID of the book") @PathVariable long bookId,
@Parameter(description = "Whether the book is physical") @RequestParam boolean physical) {
return ResponseEntity.ok(physicalBookService.togglePhysicalFlag(bookId, physical));
}
@Operation(summary = "Attach book files", description = "Attach book files from single-file source books to a target book as alternative formats.")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "Book files attached successfully"),
@@ -139,7 +139,7 @@ public class MobileAuthorService {
StringBuilder jpql = new StringBuilder(
"SELECT COUNT(DISTINCT bm.id) FROM AuthorEntity a JOIN a.bookMetadataEntityList bm JOIN bm.book b"
+ " WHERE a.id = :authorId AND (b.deleted IS NULL OR b.deleted = false)"
+ " AND (b.isPhysical IS NULL OR b.isPhysical = false)");
+ " AND b.bookFiles IS NOT EMPTY");
if (accessibleLibraryIds != null) {
jpql.append(" AND b.library.id IN :libraryIds");
}
@@ -174,9 +174,8 @@ public class MobileAuthorService {
} else if (accessibleLibraryIds != null) {
whereClause.append(" AND b.library.id IN :libraryIds");
}
// Always filter out deleted/physical books when library filtering
whereClause.append(" AND (b.deleted IS NULL OR b.deleted = false)");
whereClause.append(" AND (b.isPhysical IS NULL OR b.isPhysical = false)");
whereClause.append(" AND b.bookFiles IS NOT EMPTY");
}
private void buildSearchFilter(StringBuilder whereClause, String search) {
@@ -298,7 +298,7 @@ public class MobileBookService {
Map<Long, UserBookProgressEntity> progressMap = getProgressMapForBooks(userId, bookEntities);
List<MobileBookSummary> summaries = bookEntities.stream()
.filter(bookEntity -> bookEntity.getIsPhysical() == null || !bookEntity.getIsPhysical())
.filter(BookEntity::hasFiles)
.map(bookEntity -> mobileBookMapper.toSummary(bookEntity, progressMap.get(bookEntity.getId())))
.collect(Collectors.toList());
@@ -363,7 +363,7 @@ public class MobileBookService {
String authorQuery = "SELECT a.name, COUNT(DISTINCT b.id) FROM BookEntity b"
+ " JOIN b.metadata m JOIN m.authors a"
+ " WHERE (b.deleted IS NULL OR b.deleted = false)"
+ " AND (b.isPhysical IS NULL OR b.isPhysical = false)"
+ " AND b.bookFiles IS NOT EMPTY"
+ scopeClause
+ " GROUP BY a.name ORDER BY COUNT(DISTINCT b.id) DESC";
var authorQ = entityManager.createQuery(authorQuery, Tuple.class);
@@ -381,7 +381,7 @@ public class MobileBookService {
String langQuery = "SELECT m.language, COUNT(DISTINCT b.id) FROM BookEntity b"
+ " JOIN b.metadata m"
+ " WHERE (b.deleted IS NULL OR b.deleted = false)"
+ " AND (b.isPhysical IS NULL OR b.isPhysical = false)"
+ " AND b.bookFiles IS NOT EMPTY"
+ " AND m.language IS NOT NULL AND m.language <> ''"
+ scopeClause
+ " GROUP BY m.language ORDER BY COUNT(DISTINCT b.id) DESC";
@@ -400,7 +400,7 @@ public class MobileBookService {
String fileTypeQuery = "SELECT DISTINCT bf.bookType FROM BookEntity b"
+ " JOIN b.bookFiles bf"
+ " WHERE (b.deleted IS NULL OR b.deleted = false)"
+ " AND (b.isPhysical IS NULL OR b.isPhysical = false)"
+ " AND b.bookFiles IS NOT EMPTY"
+ " AND bf.isBookFormat = true"
+ scopeClause;
var ftQ = entityManager.createQuery(fileTypeQuery, BookFileType.class);
@@ -80,7 +80,7 @@ public class MobileSeriesService {
+ " FROM BookEntity b JOIN b.metadata m"
+ " LEFT JOIN b.userBookProgress p ON p.user.id = :userId"
+ " WHERE (b.deleted IS NULL OR b.deleted = false)"
+ " AND (b.isPhysical IS NULL OR b.isPhysical = false)"
+ " AND b.bookFiles IS NOT EMPTY"
+ " AND m.seriesName IS NOT NULL"
+ libraryClause
+ searchClause
@@ -103,7 +103,7 @@ public class MobileSeriesService {
String countQuery = "SELECT COUNT(DISTINCT m.seriesName) FROM BookEntity b JOIN b.metadata m"
+ (inProgressOnly ? " LEFT JOIN b.userBookProgress p ON p.user.id = :userId" : "")
+ " WHERE (b.deleted IS NULL OR b.deleted = false)"
+ " AND (b.isPhysical IS NULL OR b.isPhysical = false)"
+ " AND b.bookFiles IS NOT EMPTY"
+ " AND m.seriesName IS NOT NULL"
+ libraryClause
+ searchClause;
@@ -114,7 +114,7 @@ public class MobileSeriesService {
+ "SELECT m.seriesName FROM BookEntity b JOIN b.metadata m"
+ " LEFT JOIN b.userBookProgress p ON p.user.id = :userId"
+ " WHERE (b.deleted IS NULL OR b.deleted = false)"
+ " AND (b.isPhysical IS NULL OR b.isPhysical = false)"
+ " AND b.bookFiles IS NOT EMPTY"
+ " AND m.seriesName IS NOT NULL"
+ libraryClause
+ searchClause
@@ -125,7 +125,7 @@ public class MobileSeriesService {
String countAlt = "SELECT m.seriesName FROM BookEntity b JOIN b.metadata m"
+ " LEFT JOIN b.userBookProgress p ON p.user.id = :userId"
+ " WHERE (b.deleted IS NULL OR b.deleted = false)"
+ " AND (b.isPhysical IS NULL OR b.isPhysical = false)"
+ " AND b.bookFiles IS NOT EMPTY"
+ " AND m.seriesName IS NOT NULL"
+ libraryClause
+ searchClause
@@ -179,7 +179,7 @@ public class MobileSeriesService {
+ " LEFT JOIN FETCH b.bookFiles"
+ " WHERE m.seriesName IN :seriesNames"
+ " AND (b.deleted IS NULL OR b.deleted = false)"
+ " AND (b.isPhysical IS NULL OR b.isPhysical = false)"
+ " AND b.bookFiles IS NOT EMPTY"
+ libraryClause;
var booksQ = entityManager.createQuery(booksQuery, BookEntity.class);
@@ -113,10 +113,7 @@ public class MobileBookSpecification {
}
public static Specification<BookEntity> hasDigitalFile() {
return (root, query, cb) -> cb.or(
cb.isNull(root.get("isPhysical")),
cb.equal(root.get("isPhysical"), false)
);
return (root, query, cb) -> cb.isNotEmpty(root.get("bookFiles"));
}
public static Specification<BookEntity> hasAudiobookFile() {
@@ -1,5 +1,6 @@
package org.booklore.service.book;
import org.booklore.exception.ApiError;
import org.booklore.exception.APIException;
import org.booklore.mapper.BookMapper;
import org.booklore.model.dto.Book;
@@ -146,6 +147,16 @@ public class PhysicalBookService {
.forEach(catEntity -> bookEntity.getMetadata().getCategories().add(catEntity));
}
@Transactional
public Book togglePhysicalFlag(long bookId, boolean physical) {
BookEntity book = bookRepository.findById(bookId)
.orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
book.setIsPhysical(physical);
bookRepository.save(book);
log.info("Book {} physical flag set to {}", bookId, physical);
return bookMapper.toBook(book);
}
private String truncate(String input, int maxLength) {
if (input == null) return null;
return input.length() <= maxLength ? input : input.substring(0, maxLength);
@@ -83,13 +83,6 @@ public class AdditionalFileService {
if (file.isBook() && book != null) {
book.getBookFiles().remove(file);
boolean hasRemainingBookFiles = book.getBookFiles().stream()
.anyMatch(BookFileEntity::isBook);
if (!hasRemainingBookFiles) {
book.setIsPhysical(true);
bookRepository.save(book);
log.info("Book {} reverted to physical (last book file removed)", book.getId());
}
}
}
@@ -163,13 +163,6 @@ public class FileUploadService {
final BookFileEntity entity = createAdditionalFileEntityWithSubPath(book, finalFileName, fileSubPath, isBook, effectiveBookType, file.getSize(), fileHash, description);
final BookFileEntity savedEntity = additionalFileRepository.save(entity);
// Promote physical book to digital if this is a book file
if (wasPhysicalBook && isBook) {
book.setIsPhysical(false);
bookRepository.save(book);
log.info("Physical book {} promoted to digital book after file upload", bookId);
}
return additionalFileMapper.toAdditionalFile(savedEntity);
} catch (IOException e) {