
br.com.jhonsapp.bootstrap.document.service.DocumentServiceImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bootstrap-document Show documentation
Show all versions of bootstrap-document Show documentation
A complete architecture for creating and managing documents.
The newest version!
package br.com.jhonsapp.bootstrap.document.service;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import br.com.jhonsapp.bootstrap.document.model.Document;
import br.com.jhonsapp.bootstrap.document.model.DocumentStatus;
import br.com.jhonsapp.bootstrap.document.repository.DocumentRepository;
import br.com.jhonsapp.bootstrap.document.repository.filter.DocumentFilter;
import br.com.jhonsapp.bootstrap.document.service.dto.DocumentDTO;
import br.com.jhonsapp.bootstrap.document.service.exception.DocumentServiceException;
import br.com.jhonsapp.bootstrap.document.service.message.DocumentMessage;
import br.com.jhonsapp.bootstrap.document.service.ocr.OpticalCharacterRecognitionService;
import br.com.jhonsapp.bootstrap.document.service.pdf.PDFService;
import br.com.jhonsapp.bootstrap.document.service.util.DocumentUtil;
import br.com.jhonsapp.repository.annotation.RepositoryType;
import br.com.jhonsapp.repository.annotation.RepositoryType.QualifierType;
import br.com.jhonsapp.repository.document.DocumentFile;
import br.com.jhonsapp.repository.document.DocumentFileImpl;
import br.com.jhonsapp.repository.implementation.Repository;
/**
* This class is the representation of the implementation of document service.
*
* @see DocumentService
*
* @author Jhonathan Camacho.
*/
@Service
public class DocumentServiceImpl implements DocumentService {
@Autowired
private DocumentRepository repository;
@Autowired
private PDFService pdfService;
@Autowired
private OpticalCharacterRecognitionService ocrService;
@Autowired @RepositoryType(QualifierType.AMAZON_S3)
private Repository amazonS3;
@Override
public DocumentDTO putIdentification(DocumentDTO documentDTO) {
Document document = obtainDocumentToPutIdentification(documentDTO);
if(hasStateArchived(document))
throw new DocumentServiceException(DocumentMessage.document_is_already_archived);
byte[] bytes = pdfService.putBarcodeInPDF(documentDTO.getDocumentByte(), document.getCode());
Document documentSaved = save(document);
documentSaved.setFolder(documentDTO.getFolder());
return DocumentUtil.buildDTO(documentSaved, bytes);
}
@Override
public void archivedDocument(DocumentDTO documentDTO) {
if(hasInvalidFolder(documentDTO))
throw new DocumentServiceException(DocumentMessage.invalid_folder);
Document document = obtainDocumentToArchived(documentDTO);
if(hasStateArchived(document))
throw new DocumentServiceException(DocumentMessage.document_is_already_archived);
ocrService.inspectDocument(documentDTO.getDocumentByte(), document);
DocumentFile dc = DocumentFileImpl.createDocument(documentDTO.getFolder(),
documentDTO.getName(),
documentDTO.getDocumentByte());
boolean result = amazonS3.upload(dc);
if(result) {
document.setFolder(dc.getFolder());
save(document);
}else {
throw new DocumentServiceException(DocumentMessage.update_document_problem);
}
}
@Override
public void archivedDocumentDirectly(DocumentDTO documentDTO) {
documentDTO = putIdentification(documentDTO);
archivedDocument(documentDTO);
}
@Override
public DocumentDTO findByCode(String code) {
Optional document = this.repository.findByCode(code);
if(document.isPresent()) {
if(document.get().getStatus() == DocumentStatus.ARCHIVED) {
DocumentFile dc = amazonS3.download(document.get().getFolder(), document.get().getName());
return DocumentUtil.buildDTO(document.get(), dc.getDocumentInByte());
} else {
return DocumentUtil.buildDTO(document.get(), null);
}
}
return null;
}
@Override
public Page findByFilter(DocumentFilter filter, Pageable pageable){
List list = repository.filter(filter, pageable);
List dtos = new ArrayList();
for(Document d: list) {
if(d.getStatus() == DocumentStatus.ARCHIVED) {
DocumentFile dc = amazonS3.download(d.getFolder(), d.getName());
dtos.add(DocumentUtil.buildDTO(d, dc.getDocumentInByte()));
} else {
dtos.add(DocumentUtil.buildDTO(d, null));
}
}
return new PageImpl(dtos, pageable, repository.total());
}
private boolean hasInvalidFolder(DocumentDTO documentDTO) {
return !StringUtils.hasText(documentDTO.getFolder());
}
private boolean hasStateArchived(Document document) {
return document.getStatus() == DocumentStatus.ARCHIVED;
}
private Document obtainDocumentToPutIdentification(DocumentDTO documentDTO) {
int pageSize = pdfService.getTotalPages(documentDTO.getDocumentByte());
if(!StringUtils.hasText(documentDTO.getCode())) {
return DocumentUtil.buildDocument(documentDTO, pageSize);
} else {
Optional documentOp = this.repository.findByCode(documentDTO.getCode());
documentOp.orElseThrow( () -> new DocumentServiceException(DocumentMessage.invalid_code));
documentOp.get().setPages(pageSize);
return documentOp.get();
}
}
private Document obtainDocumentToArchived(DocumentDTO documentDTO) {
Optional document = this.repository.findByCode(documentDTO.getCode());
document.orElseThrow( () -> new DocumentServiceException(DocumentMessage.invalid_code));
return document.get();
}
private Document save(Document document) {
return this.repository.save(document);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy