generated from digitalservicebund/java-application-template
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add duplicate check as api (only for testing)
RISDEV-5883
- Loading branch information
Showing
5 changed files
with
318 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
backend/src/main/java/de/bund/digitalservice/ris/caselaw/adapter/DuplicateCheckService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package de.bund.digitalservice.ris.caselaw.adapter; | ||
|
||
import de.bund.digitalservice.ris.caselaw.domain.DocumentationUnit; | ||
import de.bund.digitalservice.ris.caselaw.domain.DocumentationUnitService; | ||
import de.bund.digitalservice.ris.caselaw.domain.DuplicateCheckRepository; | ||
import java.time.LocalDate; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.UUID; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@Slf4j | ||
public class DuplicateCheckService { | ||
private final DocumentationUnitService documentationUnitService; | ||
|
||
private final DuplicateCheckRepository repository; | ||
|
||
public DuplicateCheckService( | ||
DuplicateCheckRepository repository, DocumentationUnitService documentationUnitService) { | ||
this.repository = repository; | ||
this.documentationUnitService = documentationUnitService; | ||
} | ||
|
||
List<DocumentationUnit> getDuplicates(String docNumber) { | ||
try { | ||
var documentationUnit = documentationUnitService.getByDocumentNumber(docNumber); | ||
|
||
var fileNumbers = documentationUnit.coreData().fileNumbers(); | ||
var deviatingFileNumbers = documentationUnit.coreData().deviatingFileNumbers(); | ||
List<String> allFileNumbers = new ArrayList<>(); | ||
if (fileNumbers != null) { | ||
allFileNumbers.addAll(fileNumbers.stream().map(String::toUpperCase).toList()); | ||
} | ||
if (deviatingFileNumbers != null) { | ||
allFileNumbers.addAll(deviatingFileNumbers.stream().map(String::toUpperCase).toList()); | ||
} | ||
|
||
var decisionDate = documentationUnit.coreData().decisionDate(); | ||
var deviatingDecisionDates = documentationUnit.coreData().deviatingDecisionDates(); | ||
List<LocalDate> allDates = new ArrayList<>(); | ||
if (decisionDate != null) allDates.add(decisionDate); | ||
if (!deviatingDecisionDates.isEmpty()) allDates.addAll(deviatingDecisionDates); | ||
|
||
List<UUID> allCourtIds = new ArrayList<>(); | ||
var court = documentationUnit.coreData().court(); | ||
if (court != null) allCourtIds.add(court.id()); | ||
|
||
List<String> allDeviatingCourts = new ArrayList<>(); | ||
var deviatingCourts = documentationUnit.coreData().deviatingCourts(); | ||
if (deviatingCourts != null) | ||
allDeviatingCourts.addAll(deviatingCourts.stream().map(String::toUpperCase).toList()); | ||
|
||
var ecli = documentationUnit.coreData().ecli(); | ||
var deviatingEclis = documentationUnit.coreData().deviatingEclis(); | ||
List<String> allEclis = new ArrayList<>(); | ||
if (ecli != null) allEclis.add(ecli.toUpperCase()); | ||
if (deviatingEclis != null) | ||
allEclis.addAll(deviatingEclis.stream().map(String::toUpperCase).toList()); | ||
|
||
var documentType = documentationUnit.coreData().documentType(); | ||
return repository.findDuplicates( | ||
allFileNumbers, allDates, allCourtIds, allDeviatingCourts, allEclis, documentType.uuid()); | ||
} catch (Exception e) { | ||
log.debug(e.getMessage()); | ||
return List.of(); | ||
} | ||
} | ||
} |
170 changes: 170 additions & 0 deletions
170
...und/digitalservice/ris/caselaw/adapter/database/jpa/DatabaseDuplicateCheckRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
package de.bund.digitalservice.ris.caselaw.adapter.database.jpa; | ||
|
||
import java.time.LocalDate; | ||
import java.util.List; | ||
import java.util.UUID; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.query.Param; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface DatabaseDuplicateCheckRepository | ||
extends JpaRepository<DocumentationUnitDTO, UUID> { | ||
|
||
@Query( | ||
nativeQuery = true, | ||
value = | ||
""" | ||
SELECT documentationUnit.* | ||
FROM incremental_migration.documentation_unit documentationUnit | ||
JOIN incremental_migration.file_number fileNumber | ||
ON documentationUnit.id = fileNumber.documentation_unit_id | ||
WHERE upper(fileNumber.value) IN (:allFileNumbers) | ||
AND documentationUnit.decision_date IN (:allDates) | ||
UNION | ||
SELECT documentationUnit.* | ||
FROM incremental_migration.documentation_unit documentationUnit | ||
JOIN incremental_migration.file_number fileNumber | ||
ON documentationUnit.id = fileNumber.documentation_unit_id | ||
JOIN incremental_migration.deviating_date deviatingDate | ||
ON documentationUnit.id = deviatingDate.documentation_unit_id | ||
WHERE upper(fileNumber.value) IN (:allFileNumbers) | ||
AND deviatingDate.value IN (:allDates) | ||
UNION | ||
SELECT documentationUnit.* | ||
FROM incremental_migration.documentation_unit documentationUnit | ||
JOIN incremental_migration.deviating_file_number deviatingFileNumber | ||
ON documentationUnit.id = deviatingFileNumber.documentation_unit_id | ||
WHERE upper(deviatingFileNumber.value) IN (:allFileNumbers) | ||
AND documentationUnit.decision_date IN (:allDates) | ||
UNION | ||
SELECT documentationUnit.* | ||
FROM incremental_migration.documentation_unit documentationUnit | ||
JOIN incremental_migration.deviating_file_number deviatingFileNumber | ||
ON documentationUnit.id = deviatingFileNumber.documentation_unit_id | ||
JOIN incremental_migration.deviating_date deviatingDate | ||
ON documentationUnit.id = deviatingDate.documentation_unit_id | ||
WHERE upper(deviatingFileNumber.value) IN (:allFileNumbers) | ||
AND deviatingDate.value IN (:allDates) | ||
UNION | ||
SELECT documentationUnit.* | ||
FROM incremental_migration.documentation_unit documentationUnit | ||
JOIN incremental_migration.file_number fileNumber | ||
ON documentationUnit.id = fileNumber.documentation_unit_id | ||
JOIN incremental_migration.court court | ||
ON documentationUnit.court_id = court.id | ||
WHERE upper(fileNumber.value) IN (:allFileNumbers) | ||
AND court.id IN (:allCourtIds) | ||
UNION | ||
SELECT documentationUnit.* | ||
FROM incremental_migration.documentation_unit documentationUnit | ||
JOIN incremental_migration.deviating_file_number deviatingFileNumber | ||
ON documentationUnit.id = deviatingFileNumber.documentation_unit_id | ||
JOIN incremental_migration.court court | ||
ON documentationUnit.court_id = court.id | ||
WHERE upper(deviatingFileNumber.value) IN (:allFileNumbers) | ||
AND court.id IN (:allCourtIds) | ||
UNION | ||
SELECT documentationUnit.* | ||
FROM incremental_migration.documentation_unit documentationUnit | ||
JOIN incremental_migration.file_number fileNumber | ||
ON documentationUnit.id = fileNumber.documentation_unit_id | ||
JOIN incremental_migration.deviating_court deviatingCourt | ||
ON documentationUnit.id = deviatingCourt.documentation_unit_id | ||
WHERE upper(fileNumber.value) IN (:allFileNumbers) | ||
AND upper(deviatingCourt.value) IN (:allDeviatingCourts) | ||
UNION | ||
SELECT documentationUnit.* | ||
FROM incremental_migration.documentation_unit documentationUnit | ||
JOIN incremental_migration.deviating_file_number deviatingFileNumber | ||
ON documentationUnit.id = deviatingFileNumber.documentation_unit_id | ||
JOIN incremental_migration.deviating_court deviatingCourt | ||
ON documentationUnit.id = deviatingCourt.documentation_unit_id | ||
WHERE upper(deviatingFileNumber.value) IN (:allFileNumbers) | ||
AND upper(deviatingCourt.value) IN (:allDeviatingCourts) | ||
UNION | ||
SELECT documentationUnit.* | ||
FROM incremental_migration.documentation_unit documentationUnit | ||
JOIN incremental_migration.file_number fileNumber | ||
ON documentationUnit.id = fileNumber.documentation_unit_id | ||
WHERE upper(fileNumber.value) IN (:allFileNumbers) | ||
AND upper(documentationUnit.ecli) IN (:allEclis) | ||
UNION | ||
SELECT documentationUnit.* | ||
FROM incremental_migration.documentation_unit documentationUnit | ||
JOIN incremental_migration.deviating_file_number deviatingFileNumber | ||
ON documentationUnit.id = deviatingFileNumber.documentation_unit_id | ||
WHERE upper(deviatingFileNumber.value) IN (:allFileNumbers) | ||
AND upper(documentationUnit.ecli) IN (:allEclis) | ||
UNION | ||
SELECT documentationUnit.* | ||
FROM incremental_migration.documentation_unit documentationUnit | ||
JOIN incremental_migration.file_number fileNumber | ||
ON documentationUnit.id = fileNumber.documentation_unit_id | ||
JOIN incremental_migration.deviating_ecli deviatingEcli | ||
ON documentationUnit.id = deviatingEcli.documentation_unit_id | ||
WHERE upper(fileNumber.value) IN (:allFileNumbers) | ||
AND upper(deviatingEcli.value) IN (:allEclis) | ||
UNION | ||
SELECT documentationUnit.* | ||
FROM incremental_migration.documentation_unit documentationUnit | ||
JOIN incremental_migration.deviating_file_number deviatingFileNumber | ||
ON documentationUnit.id = deviatingFileNumber.documentation_unit_id | ||
JOIN incremental_migration.deviating_ecli deviatingEcli | ||
ON documentationUnit.id = deviatingEcli.documentation_unit_id | ||
WHERE upper(deviatingFileNumber.value) IN (:allFileNumbers) | ||
AND upper(deviatingEcli.value) IN (:allEclis) | ||
UNION | ||
SELECT documentationUnit.* | ||
FROM incremental_migration.documentation_unit documentationUnit | ||
JOIN incremental_migration.file_number fileNumber | ||
ON documentationUnit.id = fileNumber.documentation_unit_id | ||
JOIN incremental_migration.document_type documentType | ||
ON documentationUnit.document_type_id = documentType.id | ||
WHERE upper(fileNumber.value) IN (:allFileNumbers) | ||
AND documentType.id = :documentTypeId | ||
UNION | ||
SELECT documentationUnit.* | ||
FROM incremental_migration.documentation_unit documentationUnit | ||
JOIN incremental_migration.deviating_file_number deviatingFileNumber | ||
ON documentationUnit.id = deviatingFileNumber.documentation_unit_id | ||
JOIN incremental_migration.document_type documentType | ||
ON documentationUnit.document_type_id = documentType.id | ||
WHERE upper(deviatingFileNumber.value) IN (:allFileNumbers) | ||
AND documentType.id = :documentTypeId | ||
""") | ||
List<DocumentationUnitDTO> findDuplicates( | ||
@Param("allFileNumbers") List<String> allFileNumbers, | ||
@Param("allDates") List<LocalDate> allDates, | ||
@Param("allCourtIds") List<UUID> allCourtIds, | ||
@Param("allDeviatingCourts") List<String> allDeviatingCourts, | ||
@Param("allEclis") List<String> allEclis, | ||
@Param("documentTypeId") UUID documentTypeId); | ||
} |
49 changes: 49 additions & 0 deletions
49
...digitalservice/ris/caselaw/adapter/database/jpa/PostgresDuplicateCheckRepositoryImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package de.bund.digitalservice.ris.caselaw.adapter.database.jpa; | ||
|
||
import de.bund.digitalservice.ris.caselaw.adapter.transformer.DocumentationUnitTransformer; | ||
import de.bund.digitalservice.ris.caselaw.domain.DocumentationUnit; | ||
import de.bund.digitalservice.ris.caselaw.domain.DuplicateCheckRepository; | ||
import java.time.LocalDate; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.UUID; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Repository; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Slf4j | ||
@Repository | ||
public class PostgresDuplicateCheckRepositoryImpl implements DuplicateCheckRepository { | ||
|
||
private final DatabaseDuplicateCheckRepository repository; | ||
|
||
public PostgresDuplicateCheckRepositoryImpl(DatabaseDuplicateCheckRepository repository) { | ||
this.repository = repository; | ||
} | ||
|
||
@Override | ||
@Transactional | ||
public List<DocumentationUnit> findDuplicates( | ||
List<String> allFileNumbers, | ||
List<LocalDate> allDates, | ||
List<UUID> allCourtIds, | ||
List<String> allDeviatingCourts, | ||
List<String> allEclis, | ||
UUID documentTypeId) { | ||
long startTime = System.currentTimeMillis(); | ||
|
||
List<DocumentationUnitDTO> result = | ||
repository.findDuplicates( | ||
allFileNumbers, allDates, allCourtIds, allDeviatingCourts, allEclis, documentTypeId); | ||
|
||
long endTime = System.currentTimeMillis(); | ||
long duration = endTime - startTime; | ||
log.info("Query executed in: " + duration + " ms"); | ||
|
||
return result.stream() | ||
.filter(Objects::nonNull) | ||
.distinct() | ||
.map(DocumentationUnitTransformer::transformToDomain) | ||
.toList(); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...end/src/main/java/de/bund/digitalservice/ris/caselaw/domain/DuplicateCheckRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package de.bund.digitalservice.ris.caselaw.domain; | ||
|
||
import java.time.LocalDate; | ||
import java.util.List; | ||
import java.util.UUID; | ||
import org.springframework.data.repository.NoRepositoryBean; | ||
|
||
@NoRepositoryBean | ||
public interface DuplicateCheckRepository { | ||
|
||
List<DocumentationUnit> findDuplicates( | ||
List<String> allFileNumbers, | ||
List<LocalDate> allDates, | ||
List<UUID> allCourtIds, | ||
List<String> allDeviatingCourts, | ||
List<String> allEclis, | ||
UUID documentTypeId); | ||
} |