fix(pdf): improve PDF blob handling and improve viewer destruction logic (#506)
* fix(pdf): improve PDF blob handling and improve viewer destruction logic * fix(pdf): PDF annotation saving logic and improve responsive design for mobile * fix(pdf): improve PDF fetching (once again) and caching logic to improve performance and reduce network requests * fix(pdf): hide footer and notification elements in PDF viewer * fix(pdf): improve loading behavior and improve annotation persistence logic * fix(pdf): improve PDF annotation persistence with retry mechanism and logging * fix(pdf): improve PDF annotation saving with raw fetch and improved error handling * fix(pdf): cap highlight opacity and adjust rendering for small viewports
This commit is contained in:
+63
@@ -0,0 +1,63 @@
|
||||
package org.booklore.service.book;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.booklore.model.entity.BookEntity;
|
||||
import org.booklore.model.entity.BookLoreUserEntity;
|
||||
import org.booklore.model.entity.PdfAnnotationEntity;
|
||||
import org.booklore.repository.BookRepository;
|
||||
import org.booklore.repository.PdfAnnotationRepository;
|
||||
import org.booklore.repository.UserRepository;
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Handles the actual database persistence for PDF annotations in isolated transactions.
|
||||
* This ensures that flush failures (optimistic locking or constraints) don't poison
|
||||
* the caller's transaction and allow for clean retries.
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class PdfAnnotationPersistenceHandler {
|
||||
|
||||
private final PdfAnnotationRepository pdfAnnotationRepository;
|
||||
private final BookRepository bookRepository;
|
||||
private final UserRepository userRepository;
|
||||
|
||||
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
||||
public void saveOrUpdate(Long bookId, Long userId, String data) {
|
||||
Optional<PdfAnnotationEntity> existing = pdfAnnotationRepository.findByBookIdAndUserId(bookId, userId);
|
||||
|
||||
if (existing.isPresent()) {
|
||||
PdfAnnotationEntity entity = existing.get();
|
||||
// Version check is performed by Hibernate on flush
|
||||
entity.setData(data);
|
||||
pdfAnnotationRepository.saveAndFlush(entity);
|
||||
log.info("Updated PDF annotations for book {} by user {}", bookId, userId);
|
||||
} else {
|
||||
PdfAnnotationEntity entity = PdfAnnotationEntity.builder()
|
||||
.book(findBook(bookId))
|
||||
.user(findUser(userId))
|
||||
.data(data)
|
||||
.build();
|
||||
// Unique constraint check happens on flush
|
||||
pdfAnnotationRepository.saveAndFlush(entity);
|
||||
log.info("Created PDF annotations for book {} by user {}", bookId, userId);
|
||||
}
|
||||
}
|
||||
|
||||
private BookEntity findBook(Long bookId) {
|
||||
return bookRepository.findById(bookId)
|
||||
.orElseThrow(() -> new EntityNotFoundException("Book not found: " + bookId));
|
||||
}
|
||||
|
||||
private BookLoreUserEntity findUser(Long userId) {
|
||||
return userRepository.findById(userId)
|
||||
.orElseThrow(() -> new EntityNotFoundException("User not found: " + userId));
|
||||
}
|
||||
}
|
||||
@@ -1,19 +1,16 @@
|
||||
package org.booklore.service.book;
|
||||
|
||||
import org.booklore.config.security.service.AuthenticationService;
|
||||
import org.booklore.model.entity.BookEntity;
|
||||
import org.booklore.model.entity.BookLoreUserEntity;
|
||||
import org.booklore.model.entity.PdfAnnotationEntity;
|
||||
import org.booklore.repository.BookRepository;
|
||||
import org.booklore.repository.PdfAnnotationRepository;
|
||||
import org.booklore.repository.UserRepository;
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.orm.ObjectOptimisticLockingFailureException;
|
||||
import org.springframework.dao.DataIntegrityViolationException;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.Optional;
|
||||
import org.booklore.model.entity.PdfAnnotationEntity;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@@ -21,9 +18,8 @@ import java.util.Optional;
|
||||
public class PdfAnnotationService {
|
||||
|
||||
private final PdfAnnotationRepository pdfAnnotationRepository;
|
||||
private final BookRepository bookRepository;
|
||||
private final UserRepository userRepository;
|
||||
private final AuthenticationService authenticationService;
|
||||
private final PdfAnnotationPersistenceHandler pdfAnnotationPersistenceHandler;
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public Optional<String> getAnnotations(Long bookId) {
|
||||
@@ -32,24 +28,38 @@ public class PdfAnnotationService {
|
||||
.map(PdfAnnotationEntity::getData);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
/**
|
||||
* Orchestrates the save/update of PDF annotations with a retry mechanism.
|
||||
* The actual database operation is handled by {@link PdfAnnotationPersistenceHandler}
|
||||
* in its own transaction (REQUIRES_NEW) to ensure that concurrency failures
|
||||
* don't poison the main request transaction.
|
||||
*/
|
||||
public void saveAnnotations(Long bookId, String data) {
|
||||
Long userId = getCurrentUserId();
|
||||
Optional<PdfAnnotationEntity> existing = pdfAnnotationRepository.findByBookIdAndUserId(bookId, userId);
|
||||
int maxRetries = 3;
|
||||
int retryCount = 0;
|
||||
|
||||
if (existing.isPresent()) {
|
||||
PdfAnnotationEntity entity = existing.get();
|
||||
entity.setData(data);
|
||||
pdfAnnotationRepository.save(entity);
|
||||
log.info("Updated PDF annotations for book {} by user {}", bookId, userId);
|
||||
} else {
|
||||
PdfAnnotationEntity entity = PdfAnnotationEntity.builder()
|
||||
.book(findBook(bookId))
|
||||
.user(findUser(userId))
|
||||
.data(data)
|
||||
.build();
|
||||
pdfAnnotationRepository.save(entity);
|
||||
log.info("Created PDF annotations for book {} by user {}", bookId, userId);
|
||||
while (retryCount < maxRetries) {
|
||||
try {
|
||||
pdfAnnotationPersistenceHandler.saveOrUpdate(bookId, userId, data);
|
||||
return;
|
||||
} catch (ObjectOptimisticLockingFailureException | DataIntegrityViolationException e) {
|
||||
retryCount++;
|
||||
if (retryCount >= maxRetries) {
|
||||
log.error("Failed to save PDF annotations for book {}/user {} after {} retries: {}",
|
||||
bookId, userId, maxRetries, e.getMessage());
|
||||
throw e; // Final failure, pass to exception handler
|
||||
}
|
||||
log.info("Concurrent update for PDF annotations (book {}, user {}), retrying (attempt {}/{}). Reason: {}",
|
||||
bookId, userId, retryCount, maxRetries, e.getMessage());
|
||||
|
||||
try {
|
||||
Thread.sleep(50); // Small backoff before retry
|
||||
} catch (InterruptedException ie) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new RuntimeException(ie);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,14 +73,4 @@ public class PdfAnnotationService {
|
||||
private Long getCurrentUserId() {
|
||||
return authenticationService.getAuthenticatedUser().getId();
|
||||
}
|
||||
|
||||
private BookEntity findBook(Long bookId) {
|
||||
return bookRepository.findById(bookId)
|
||||
.orElseThrow(() -> new EntityNotFoundException("Book not found: " + bookId));
|
||||
}
|
||||
|
||||
private BookLoreUserEntity findUser(Long userId) {
|
||||
return userRepository.findById(userId)
|
||||
.orElseThrow(() -> new EntityNotFoundException("User not found: " + userId));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user