DomainTemplatesPrintSelectedService.java
package fr.tiogars.inception.domains.domaintemplate.services;
import java.io.IOException;
import java.util.List;
import java.util.Optional;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import fr.tiogars.inception.domains.domaintemplate.models.DomainTemplate;
import fr.tiogars.inception.domains.domaintemplate.repositories.DomainTemplateRepository;
/** Generates a printable PDF for selected active DomainTemplates. */
@Service
public class DomainTemplatesPrintSelectedService {
private final DomainTemplateRepository repository;
private final DomainTemplatePdfRenderer renderer;
public DomainTemplatesPrintSelectedService(DomainTemplateRepository repository, DomainTemplatePdfRenderer renderer) {
this.repository = repository;
this.renderer = renderer;
}
/**
* Prints a selection ordered by code without duplicating records.
*
* @param ids selected persistent identifiers
* @return PDF bytes, or empty if any selected record was not found
* @throws IOException when the PDF cannot be generated
*/
@Transactional(readOnly = true)
public Optional<byte[]> printSelected(List<Long> ids) throws IOException {
if (ids.isEmpty()) {
throw new IllegalArgumentException("Select at least one DomainTemplate to print.");
}
List<Long> uniqueIds = ids.stream().distinct().toList();
List<DomainTemplate> templates = repository.findAllByIdInOrderByCodeAsc(uniqueIds).stream()
.map(DomainTemplateFindAllService::toRecord)
.toList();
if (templates.size() != uniqueIds.size()) {
return Optional.empty();
}
return Optional.of(renderer.render("Selected DomainTemplates", templates));
}
}