Commit 762803b1 authored by George Novikov's avatar George Novikov

рефакторинг сохранения документа и проверки шаблона по templateId

parent 8dad440b
Pipeline #396 failed with stage
...@@ -14,18 +14,22 @@ import kz.project.printedFormsService.service.DocumentsService; ...@@ -14,18 +14,22 @@ import kz.project.printedFormsService.service.DocumentsService;
import kz.project.printedFormsService.service.TemplateService; import kz.project.printedFormsService.service.TemplateService;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@Service @Service
@RequiredArgsConstructor @RequiredArgsConstructor
public class DocumentsServiceImpl implements DocumentsService { public class DocumentsServiceImpl implements DocumentsService {
private static final Logger LOGGER = LoggerFactory.getLogger(DocumentsServiceImpl.class);
private static final Set<Long> INEXISTENT_TEMPLATES = new HashSet<>();
private final DocumentJdbcRepository documentJdbcRepository; private final DocumentJdbcRepository documentJdbcRepository;
private final TemplateService templateService; private final TemplateService templateService;
private final DocumentRepository documentRepository; private final DocumentRepository documentRepository;
...@@ -74,13 +78,24 @@ public class DocumentsServiceImpl implements DocumentsService { ...@@ -74,13 +78,24 @@ public class DocumentsServiceImpl implements DocumentsService {
@Transactional @Transactional
@Override @Override
public void saveDocument(Long templateId) throws ValidationException, JsonProcessingException { public void saveDocument(Long templateId) throws ValidationException, JsonProcessingException {
DocumentEntity documentEntity = new DocumentEntity();
TemplateEntity templateEntity = templateService.getTemplateEntity(templateId); TemplateEntity templateEntity = templateService.getTemplateEntity(templateId);
if (templateEntity == null){
printOrBypass(templateId);
return;
}
DocumentEntity documentEntity = new DocumentEntity();
documentEntity.setTemplate(templateEntity); documentEntity.setTemplate(templateEntity);
documentEntity.setProject(templateEntity.getProject()); documentEntity.setProject(templateEntity.getProject());
documentEntity.setCreatedAt(LocalDateTime.now()); documentEntity.setCreatedAt(LocalDateTime.now());
documentRepository.save(documentEntity);
documentRepository.save(documentEntity);
}
private void printOrBypass(Long templateId){
if (!INEXISTENT_TEMPLATES.contains(templateId)){
LOGGER.warn("Не найден шаблон с таким идентификатором {}", templateId);
}
INEXISTENT_TEMPLATES.add(templateId);
} }
} }
...@@ -175,7 +175,7 @@ public class TemplateServiceImpl implements TemplateService { ...@@ -175,7 +175,7 @@ public class TemplateServiceImpl implements TemplateService {
@Override @Override
public TemplateEntity getTemplateEntity(Long id) throws ValidationException { public TemplateEntity getTemplateEntity(Long id) throws ValidationException {
return repository.findById(id).orElseThrow(() -> new ValidationException("Не найден шаблон с таким идентификатором", 13)); return repository.findById(id).orElse(null);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment