RED-4501: Sonar issues: persistence-service

This commit is contained in:
aoezyetimoglu 2022-07-04 13:59:00 +02:00
parent cca7543699
commit 9f3f23ccc1
3 changed files with 97 additions and 83 deletions

View File

@ -115,7 +115,7 @@ public class JDBCWriteUtils {
} }
private String toSnakeCase(String name) { private String toSnakeCase(String name) {
String ret = name.replaceAll("([A-Z]+)([A-Z][a-z])", "$1_$2").replaceAll("([a-z])([A-Z])", "$1_$2"); String ret = name.replaceAll("([A-Z]{1,100})([A-Z][a-z])", "$1_$2").replaceAll("([a-z])([A-Z])", "$1_$2");
return ret.toLowerCase(); return ret.toLowerCase();
} }

View File

@ -240,74 +240,6 @@ public class FileStatusService {
} }
@Transactional
protected void addToAnalysisQueue(String dossierId, String fileId, boolean priority, Set<Integer> sectionsToReanalyse) {
var dossier = dossierPersistenceService.getAndValidateDossier(dossierId);
var fileEntity = fileStatusPersistenceService.getStatus(fileId);
if(!fileManagementStorageService.objectExists(dossierId, fileId, FileType.ORIGIN)){
addToPreprocessingQueue(dossierId, fileId, fileEntity.getFilename());
return;
}
if (fileEntity.isExcluded()) {
log.debug("File {} is excluded", fileEntity.getId());
return;
}
var fileModel = convert(fileEntity, FileModel.class, new FileModelMapper());
reanalysisRequiredStatusService.enhanceFileStatusWithAnalysisRequirements(fileModel, true);
boolean reanalyse = fileModel.isReanalysisRequired();
if (!reanalyse && settings.isImageServiceEnabled() && !fileManagementStorageService.objectExists(dossierId, fileId, FileType.IMAGE_INFO)) {
log.debug("Add file: {} from dossier {} to Image queue", fileId, dossierId);
addToImageQueue(dossierId, fileId);
return;
}
MessageType messageType = calculateMessageType(dossierId, fileId, reanalyse, fileModel.getProcessingStatus(), fileModel);
if (MessageType.ANALYSE.equals(messageType) && settings.isNerServiceEnabled() && !fileManagementStorageService.objectExists(dossierId, fileId, FileType.NER_ENTITIES)) {
log.debug("Add file: {} from dossier {} to NER queue", fileId, dossierId);
addToNerQueue(dossierId, fileId);
return;
}
var analyseRequest = AnalyzeRequest.builder()
.messageType(messageType)
.dossierId(dossierId)
.analysisNumber(fileModel.getNumberOfAnalyses() + 1)
.sectionsToReanalyse(sectionsToReanalyse)
.fileId(fileId)
.manualRedactions(manualRedactionProviderService.getManualRedactions(fileId))
.dossierTemplateId(dossier.getDossierTemplateId())
.lastProcessed(fileModel.getLastProcessed())
.fileAttributes(convertAttributes(fileEntity.getFileAttributes(), dossier.getDossierTemplateId()))
.excludedPages(fileModel.getExcludedPages())
.build();
log.info("Add file: {} from dossier {} to Analysis queue with MessageType {}", fileId, dossierId, messageType);
if (messageType.equals(MessageType.REANALYSE)) {
setStatusProcessing(fileId);
} else {
setStatusFullProcessing(fileId);
}
try {
if (priority) {
rabbitTemplate.convertAndSend(MessagingConfiguration.REDACTION_PRIORITY_QUEUE, objectMapper.writeValueAsString(analyseRequest));
} else {
rabbitTemplate.convertAndSend(MessagingConfiguration.REDACTION_QUEUE, objectMapper.writeValueAsString(analyseRequest));
}
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
@Transactional @Transactional
public void setStatusReprocess(String dossierId, String fileId, boolean priority) { public void setStatusReprocess(String dossierId, String fileId, boolean priority) {
@ -491,7 +423,6 @@ public class FileStatusService {
} }
@Transactional
public void deleteManualRedactions(String dossierId, String fileId) { public void deleteManualRedactions(String dossierId, String fileId) {
OffsetDateTime now = OffsetDateTime.now(); OffsetDateTime now = OffsetDateTime.now();
@ -553,6 +484,80 @@ public class FileStatusService {
} }
public boolean hasChangesSince(String dossierId, OffsetDateTime since) {
return fileStatusPersistenceService.hasChangesSince(dossierId, since);
}
@Transactional
protected void addToAnalysisQueue(String dossierId, String fileId, boolean priority, Set<Integer> sectionsToReanalyse) {
var dossier = dossierPersistenceService.getAndValidateDossier(dossierId);
var fileEntity = fileStatusPersistenceService.getStatus(fileId);
if(!fileManagementStorageService.objectExists(dossierId, fileId, FileType.ORIGIN)){
addToPreprocessingQueue(dossierId, fileId, fileEntity.getFilename());
return;
}
if (fileEntity.isExcluded()) {
log.debug("File {} is excluded", fileEntity.getId());
return;
}
var fileModel = convert(fileEntity, FileModel.class, new FileModelMapper());
reanalysisRequiredStatusService.enhanceFileStatusWithAnalysisRequirements(fileModel, true);
boolean reanalyse = fileModel.isReanalysisRequired();
if (!reanalyse && settings.isImageServiceEnabled() && !fileManagementStorageService.objectExists(dossierId, fileId, FileType.IMAGE_INFO)) {
log.debug("Add file: {} from dossier {} to Image queue", fileId, dossierId);
addToImageQueue(dossierId, fileId);
return;
}
MessageType messageType = calculateMessageType(dossierId, fileId, reanalyse, fileModel.getProcessingStatus(), fileModel);
if (MessageType.ANALYSE.equals(messageType) && settings.isNerServiceEnabled() && !fileManagementStorageService.objectExists(dossierId, fileId, FileType.NER_ENTITIES)) {
log.debug("Add file: {} from dossier {} to NER queue", fileId, dossierId);
addToNerQueue(dossierId, fileId);
return;
}
var analyseRequest = AnalyzeRequest.builder()
.messageType(messageType)
.dossierId(dossierId)
.analysisNumber(fileModel.getNumberOfAnalyses() + 1)
.sectionsToReanalyse(sectionsToReanalyse)
.fileId(fileId)
.manualRedactions(manualRedactionProviderService.getManualRedactions(fileId))
.dossierTemplateId(dossier.getDossierTemplateId())
.lastProcessed(fileModel.getLastProcessed())
.fileAttributes(convertAttributes(fileEntity.getFileAttributes(), dossier.getDossierTemplateId()))
.excludedPages(fileModel.getExcludedPages())
.build();
log.info("Add file: {} from dossier {} to Analysis queue with MessageType {}", fileId, dossierId, messageType);
if (messageType.equals(MessageType.REANALYSE)) {
setStatusProcessing(fileId);
} else {
setStatusFullProcessing(fileId);
}
try {
if (priority) {
rabbitTemplate.convertAndSend(MessagingConfiguration.REDACTION_PRIORITY_QUEUE, objectMapper.writeValueAsString(analyseRequest));
} else {
rabbitTemplate.convertAndSend(MessagingConfiguration.REDACTION_QUEUE, objectMapper.writeValueAsString(analyseRequest));
}
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
protected void addToNerQueue(String dossierId, String fileId) { protected void addToNerQueue(String dossierId, String fileId) {
setStatusNerAnalyzing(fileId); setStatusNerAnalyzing(fileId);
@ -588,9 +593,7 @@ public class FileStatusService {
} else if (manualRedactionProviderService.getManualRedactions(fileId) } else if (manualRedactionProviderService.getManualRedactions(fileId)
.getResizeRedactions() .getResizeRedactions()
.stream() .stream()
.filter(resize -> resize.getRequestDate().isAfter(fileModel.getLastProcessed())) .anyMatch(resize -> resize.getRequestDate().isAfter(fileModel.getLastProcessed()))) {
.findFirst()
.isPresent()) {
return MessageType.REANALYSE; return MessageType.REANALYSE;
} }
return MessageType.ANALYSE; return MessageType.ANALYSE;
@ -635,10 +638,4 @@ public class FileStatusService {
return fileAttributeList; return fileAttributeList;
} }
public boolean hasChangesSince(String dossierId, OffsetDateTime since) {
return fileStatusPersistenceService.hasChangesSince(dossierId, since);
}
} }

View File

@ -27,33 +27,44 @@ public class FileSystemBackedArchiver implements AutoCloseable {
private final File tempFile; private final File tempFile;
private final ZipOutputStream zipOutputStream; private final ZipOutputStream zipOutputStream;
@SneakyThrows @SneakyThrows
public FileSystemBackedArchiver() { public FileSystemBackedArchiver() {
if(SystemUtils.IS_OS_UNIX) {
if (SystemUtils.IS_OS_UNIX) {
FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rwx------")); FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rwx------"));
tempFile = Files.createTempFile("archive", ".zip", attr).toFile(); tempFile = Files.createTempFile("archive", ".zip", attr).toFile();
} else { } else {
tempFile = Files.createTempFile("archive", ".zip").toFile(); tempFile = Files.createTempFile("archive", ".zip").toFile();
tempFile.setReadable(true, true); boolean isReadable = tempFile.setReadable(true, true);
tempFile.setWritable(true, true); boolean isWritable = tempFile.setWritable(true, true);
tempFile.setExecutable(true, true); boolean isExecutable = tempFile.setExecutable(true, true);
if (!isReadable || !isWritable || !isExecutable) {
log.warn("Could not archive temporary zip file.");
}
} }
zipOutputStream = new ZipOutputStream(new FileOutputStream(tempFile)); zipOutputStream = new ZipOutputStream(new FileOutputStream(tempFile));
} }
public void addEntries(ArchiveModel... archives) { public void addEntries(ArchiveModel... archives) {
addEntries(Arrays.asList(archives)); addEntries(Arrays.asList(archives));
} }
public void addEntries(List<ArchiveModel> archives) { public void addEntries(List<ArchiveModel> archives) {
archives.forEach(this::addEntry); archives.forEach(this::addEntry);
} }
@SneakyThrows @SneakyThrows
public InputStream toInputStream() { public InputStream toInputStream() {
try { try {
zipOutputStream.close(); zipOutputStream.close();
} catch (IOException e) { } catch (IOException e) {
@ -62,6 +73,7 @@ public class FileSystemBackedArchiver implements AutoCloseable {
return new BufferedInputStream(new FileInputStream(tempFile)); return new BufferedInputStream(new FileInputStream(tempFile));
} }
@SneakyThrows @SneakyThrows
public void addEntry(ArchiveModel archiveModel) { public void addEntry(ArchiveModel archiveModel) {
// begin writing a new ZIP entry, positions the stream to the start of the entry entity // begin writing a new ZIP entry, positions the stream to the start of the entry entity
@ -82,6 +94,7 @@ public class FileSystemBackedArchiver implements AutoCloseable {
@Override @Override
public void close() { public void close() {
try { try {
boolean res = tempFile.delete(); boolean res = tempFile.delete();
if (!res) { if (!res) {
@ -93,7 +106,9 @@ public class FileSystemBackedArchiver implements AutoCloseable {
} }
} }
public long getContentLength() { public long getContentLength() {
try { try {
zipOutputStream.close(); zipOutputStream.close();
} catch (IOException e) { } catch (IOException e) {
@ -102,6 +117,7 @@ public class FileSystemBackedArchiver implements AutoCloseable {
return tempFile.length(); return tempFile.length();
} }
@Data @Data
@AllArgsConstructor @AllArgsConstructor
public static class ArchiveModel { public static class ArchiveModel {
@ -109,6 +125,7 @@ public class FileSystemBackedArchiver implements AutoCloseable {
private String folderName; private String folderName;
private String name; private String name;
private byte[] bytes; private byte[] bytes;
} }
} }