RED-8702: Explore document databases to store entityLog
This commit is contained in:
parent
c4591654de
commit
351207e5f1
@ -1,238 +0,0 @@
|
||||
package com.iqser.red.service.persistence.management.v1.processor.service.persistence;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.iqser.red.service.persistence.management.v1.processor.document.EntityLogDocument;
|
||||
import com.iqser.red.service.persistence.management.v1.processor.document.EntityLogEntryDocument;
|
||||
import com.iqser.red.service.persistence.management.v1.processor.exception.EntityLogDocumentNotFoundException;
|
||||
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.repository.EntityLogDocumentRepository;
|
||||
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.repository.EntityLogEntryDocumentRepository;
|
||||
import com.iqser.red.service.persistence.service.v1.api.shared.model.analysislog.entitylog.EntityLog;
|
||||
import com.iqser.red.service.persistence.service.v1.api.shared.model.analysislog.entitylog.EntityLogEntry;
|
||||
|
||||
@Service
|
||||
public class EntityLogMongoService {
|
||||
|
||||
private final EntityLogDocumentRepository entityLogDocumentRepository;
|
||||
private final EntityLogEntryDocumentRepository entityLogEntryDocumentRepository;
|
||||
|
||||
|
||||
public EntityLogMongoService(EntityLogDocumentRepository entityLogDocumentRepository, EntityLogEntryDocumentRepository entityLogEntryDocumentRepository) {
|
||||
|
||||
this.entityLogDocumentRepository = entityLogDocumentRepository;
|
||||
this.entityLogEntryDocumentRepository = entityLogEntryDocumentRepository;
|
||||
}
|
||||
|
||||
|
||||
public void insertEntityLog(String dossierId, String fileId, EntityLog entityLog) {
|
||||
|
||||
EntityLogDocument entityLogDocument = entityLogDocumentRepository.insert(new EntityLogDocument(dossierId, fileId, entityLog));
|
||||
|
||||
entityLogEntryDocumentRepository.insert(entityLog.getEntityLogEntry()
|
||||
.stream()
|
||||
.map(entityLogEntry -> new EntityLogEntryDocument(entityLogDocument.getId(), entityLogEntry))
|
||||
.toList());
|
||||
}
|
||||
|
||||
|
||||
// this does everything : insert when not found and update if found
|
||||
// todo: remove and replace when services use insert,update,delete correctly
|
||||
public void saveEntityLog(String dossierId, String fileId, EntityLog entityLog) {
|
||||
|
||||
Optional<EntityLogDocument> optionalEntityLogDocument = entityLogDocumentRepository.findById(EntityLogDocument.getDocumentId(dossierId, fileId));
|
||||
if (optionalEntityLogDocument.isEmpty()) {
|
||||
// throw new EntityLogDocumentNotFoundException(String.format("Entity log for dossier %s and file %s not found.", dossierId, fileId));
|
||||
insertEntityLog(dossierId, fileId, entityLog);
|
||||
return;
|
||||
}
|
||||
|
||||
EntityLogDocument oldEntityLogDocument = optionalEntityLogDocument.get();
|
||||
List<EntityLogEntryDocument> oldEntityLogEntryDocuments = oldEntityLogDocument.getEntityLogEntryDocument();
|
||||
|
||||
EntityLogDocument newEntityLogDocument = new EntityLogDocument(dossierId, fileId, entityLog);
|
||||
List<EntityLogEntryDocument> newEntityLogEntryDocuments = newEntityLogDocument.getEntityLogEntryDocument();
|
||||
|
||||
List<EntityLogEntryDocument> toUpdate = new ArrayList<>(oldEntityLogEntryDocuments);
|
||||
toUpdate.retainAll(newEntityLogEntryDocuments);
|
||||
|
||||
List<EntityLogEntryDocument> toRemove = new ArrayList<>(oldEntityLogEntryDocuments);
|
||||
toRemove.removeAll(toUpdate);
|
||||
|
||||
List<EntityLogEntryDocument> toInsert = new ArrayList<>(newEntityLogEntryDocuments);
|
||||
toInsert.removeAll(toUpdate);
|
||||
|
||||
entityLogEntryDocumentRepository.saveAll(toUpdate);
|
||||
entityLogEntryDocumentRepository.deleteAll(toRemove);
|
||||
entityLogEntryDocumentRepository.insert(toInsert);
|
||||
|
||||
entityLogDocumentRepository.save(newEntityLogDocument);
|
||||
}
|
||||
|
||||
|
||||
public void deleteEntityLog(String dossierId, String fileId) {
|
||||
|
||||
String entityLogId = EntityLogDocument.getDocumentId(dossierId, fileId);
|
||||
|
||||
entityLogDocumentRepository.deleteById(entityLogId);
|
||||
|
||||
entityLogEntryDocumentRepository.deleteByEntityLogId(entityLogId);
|
||||
}
|
||||
|
||||
|
||||
public void insertEntityLogEntries(String dossierId, String fileId, List<EntityLogEntry> entityLogEntries) {
|
||||
|
||||
String entityLogId = EntityLogDocument.getDocumentId(dossierId, fileId);
|
||||
|
||||
EntityLogDocument entityLogDocument = getEntityLogDocument(entityLogId);
|
||||
|
||||
List<EntityLogEntryDocument> entityLogEntryDocuments = entityLogEntries.stream()
|
||||
.map(entityLogEntry -> new EntityLogEntryDocument(entityLogId, entityLogEntry))
|
||||
.toList();
|
||||
|
||||
entityLogDocument.getEntityLogEntryDocument().addAll(entityLogEntryDocuments);
|
||||
|
||||
entityLogEntryDocumentRepository.insert(entityLogEntryDocuments);
|
||||
entityLogDocumentRepository.save(entityLogDocument);
|
||||
}
|
||||
|
||||
|
||||
public void updateEntityLogEntries(String dossierId, String fileId, List<EntityLogEntry> entityLogEntries) {
|
||||
|
||||
String entityLogId = EntityLogDocument.getDocumentId(dossierId, fileId);
|
||||
|
||||
entityLogEntryDocumentRepository.saveAll(entityLogEntries.stream()
|
||||
.map(entityLogEntry -> new EntityLogEntryDocument(entityLogId, entityLogEntry))
|
||||
.toList());
|
||||
}
|
||||
|
||||
public void deleteEntityLogEntries(String dossierId, String fileId, List<EntityLogEntry> entityLogEntries) {
|
||||
|
||||
String entityLogId = EntityLogDocument.getDocumentId(dossierId, fileId);
|
||||
|
||||
EntityLogDocument entityLogDocument = getEntityLogDocument(entityLogId);
|
||||
|
||||
List<EntityLogEntryDocument> entityLogEntryDocuments = entityLogEntries.stream()
|
||||
.map(entityLogEntry -> new EntityLogEntryDocument(entityLogId, entityLogEntry))
|
||||
.toList();
|
||||
|
||||
entityLogDocument.getEntityLogEntryDocument().removeAll(entityLogEntryDocuments);
|
||||
|
||||
entityLogEntryDocumentRepository.deleteAll(entityLogEntryDocuments);
|
||||
entityLogDocumentRepository.save(entityLogDocument);
|
||||
}
|
||||
|
||||
|
||||
private EntityLogDocument getEntityLogDocument(String entityLogId) {
|
||||
|
||||
Optional<EntityLogDocument> optionalEntityLogDocument = entityLogDocumentRepository.findById(entityLogId);
|
||||
|
||||
if (optionalEntityLogDocument.isEmpty()) {
|
||||
throw new EntityLogDocumentNotFoundException(String.format("Entity log not found for %s", entityLogId));
|
||||
}
|
||||
|
||||
return optionalEntityLogDocument.get();
|
||||
}
|
||||
|
||||
|
||||
public Optional<EntityLog> findEntityLogByDossierIdAndFileId(String dossierId, String fileId) {
|
||||
|
||||
return entityLogDocumentRepository.findById(EntityLogDocument.getDocumentId(dossierId, fileId))
|
||||
.map(EntityLogMongoService::fromDocument);
|
||||
}
|
||||
|
||||
|
||||
public boolean entityLogDocumentExists(String dossierId, String fileId) {
|
||||
|
||||
return entityLogDocumentRepository.existsById(EntityLogDocument.getDocumentId(dossierId, fileId));
|
||||
}
|
||||
|
||||
|
||||
public Optional<Integer> findLatestAnalysisNumber(String dossierId, String fileId) {
|
||||
|
||||
return entityLogDocumentRepository.findAnalysisNumberById(EntityLogDocument.getDocumentId(dossierId, fileId))
|
||||
.map(EntityLogDocument::getAnalysisNumber);
|
||||
}
|
||||
|
||||
|
||||
public List<EntityLogEntry> findAllEntityLogEntriesWithManualChanges(String dossierId, String fileId) {
|
||||
|
||||
return entityLogEntryDocumentRepository.findByEntityLogIdAndManualChangesNotEmpty(EntityLogDocument.getDocumentId(dossierId, fileId))
|
||||
.stream()
|
||||
.map(EntityLogMongoService::fromDocument)
|
||||
.toList();
|
||||
}
|
||||
|
||||
|
||||
public List<EntityLogEntry> findAllEntityLogEntriesByAnalysisNumber(String dossierId, String fileId, int analysisNumber) {
|
||||
|
||||
return entityLogEntryDocumentRepository.findByEntityLogIdAndChangesAnalysisNumber(EntityLogDocument.getDocumentId(dossierId, fileId), analysisNumber)
|
||||
.stream()
|
||||
.map(EntityLogMongoService::fromDocument)
|
||||
.toList();
|
||||
}
|
||||
|
||||
|
||||
public List<EntityLogEntry> findAllEntriesByDossierIdAndFileId(String dossierId, String fileId) {
|
||||
|
||||
return entityLogEntryDocumentRepository.findByEntityLogId(EntityLogDocument.getDocumentId(dossierId, fileId))
|
||||
.stream()
|
||||
.map(EntityLogMongoService::fromDocument)
|
||||
.toList();
|
||||
}
|
||||
|
||||
|
||||
private static EntityLog fromDocument(EntityLogDocument entityLogDocument) {
|
||||
|
||||
EntityLog entityLog = new EntityLog();
|
||||
entityLog.setAnalysisVersion(entityLogDocument.getAnalysisVersion());
|
||||
entityLog.setAnalysisNumber(entityLogDocument.getAnalysisNumber());
|
||||
entityLog.setEntityLogEntry(entityLogDocument.getEntityLogEntryDocument()
|
||||
.stream()
|
||||
.map(EntityLogMongoService::fromDocument)
|
||||
.collect(Collectors.toList()));
|
||||
entityLog.setLegalBasis(entityLogDocument.getLegalBasis());
|
||||
entityLog.setDictionaryVersion(entityLogDocument.getDictionaryVersion());
|
||||
entityLog.setDossierDictionaryVersion(entityLogDocument.getDossierDictionaryVersion());
|
||||
entityLog.setRulesVersion(entityLogDocument.getRulesVersion());
|
||||
entityLog.setLegalBasisVersion(entityLogDocument.getLegalBasisVersion());
|
||||
return entityLog;
|
||||
}
|
||||
|
||||
|
||||
private static EntityLogEntry fromDocument(EntityLogEntryDocument entityLogEntryDocument) {
|
||||
|
||||
EntityLogEntry entityLogEntry = new EntityLogEntry();
|
||||
entityLogEntry.setId(entityLogEntryDocument.getEntryId());
|
||||
entityLogEntry.setState(entityLogEntryDocument.getState());
|
||||
entityLogEntry.setValue(entityLogEntryDocument.getValue());
|
||||
entityLogEntry.setReason(entityLogEntryDocument.getReason());
|
||||
entityLogEntry.setMatchedRule(entityLogEntryDocument.getMatchedRule());
|
||||
entityLogEntry.setLegalBasis(entityLogEntryDocument.getLegalBasis());
|
||||
entityLogEntry.setContainingNodeId(new ArrayList<>(entityLogEntryDocument.getContainingNodeId()));
|
||||
entityLogEntry.setClosestHeadline(entityLogEntryDocument.getClosestHeadline());
|
||||
entityLogEntry.setSection(entityLogEntryDocument.getSection());
|
||||
entityLogEntry.setPositions(new ArrayList<>(entityLogEntryDocument.getPositions()));
|
||||
entityLogEntry.setTextBefore(entityLogEntryDocument.getTextBefore());
|
||||
entityLogEntry.setTextAfter(entityLogEntryDocument.getTextAfter());
|
||||
entityLogEntry.setStartOffset(entityLogEntryDocument.getStartOffset());
|
||||
entityLogEntry.setEndOffset(entityLogEntryDocument.getEndOffset());
|
||||
entityLogEntry.setImageHasTransparency(entityLogEntryDocument.isImageHasTransparency());
|
||||
entityLogEntry.setDictionaryEntry(entityLogEntryDocument.isDictionaryEntry());
|
||||
entityLogEntry.setDossierDictionaryEntry(entityLogEntryDocument.isDossierDictionaryEntry());
|
||||
entityLogEntry.setExcluded(entityLogEntryDocument.isExcluded());
|
||||
entityLogEntry.setChanges(new ArrayList<>(entityLogEntryDocument.getChanges()));
|
||||
entityLogEntry.setManualChanges(new ArrayList<>(entityLogEntryDocument.getManualChanges()));
|
||||
entityLogEntry.setEngines(new HashSet<>(entityLogEntryDocument.getEngines()));
|
||||
entityLogEntry.setReference(new HashSet<>(entityLogEntryDocument.getReference()));
|
||||
entityLogEntry.setImportedRedactionIntersections(new HashSet<>(entityLogEntryDocument.getImportedRedactionIntersections()));
|
||||
entityLogEntry.setNumberOfComments(entityLogEntryDocument.getNumberOfComments());
|
||||
return entityLogEntry;
|
||||
}
|
||||
|
||||
}
|
||||
@ -5,9 +5,11 @@ import java.util.Optional;
|
||||
import org.springframework.data.mongodb.repository.MongoRepository;
|
||||
import org.springframework.data.mongodb.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import com.iqser.red.service.persistence.management.v1.processor.document.EntityLogDocument;
|
||||
|
||||
@Repository
|
||||
public interface EntityLogDocumentRepository extends MongoRepository<EntityLogDocument, String> {
|
||||
|
||||
@Query(value = "{ '_id' : ?0 }", fields = "{ 'analysisNumber' : 1 }")
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user