Skip to content

Commit

Permalink
Allow procedures multiple times in history
Browse files Browse the repository at this point in the history
RISDEV-2857
  • Loading branch information
rvp-c committed Dec 12, 2023
1 parent 95bf593 commit e67d57f
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public List<DocumentUnitListEntry> getDocumentUnits(UUID procedureId) {

return procedures.get(0).getProcedure().equals(procedureDTO);
})
.distinct()
.map(DocumentationUnitTransformer::transformToMetaDomain)
.toList())
.orElse(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import java.util.UUID;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

@Repository
public interface DatabaseDocumentationUnitProcedureRepository
extends JpaRepository<DocumentationUnitProcedureDTO, UUID> {

@Query("SELECT pl FROM DocumentationUnitProcedureDTO pl ORDER BY pl.primaryKey.rank ASC LIMIT 1")
DocumentationUnitProcedureDTO findFirstByDocumentationUnitOrderByRankDesc(
DocumentationUnitDTO documentationUnitDTO);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.UUID;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
Expand Down Expand Up @@ -42,6 +43,7 @@ public class DocumentationUnitFieldOfLawDTO {

@Embeddable
@NoArgsConstructor
@EqualsAndHashCode
class DocumentationUnitFieldOfLawId implements Serializable {
private UUID documentationUnitId;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.UUID;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
Expand Down Expand Up @@ -41,6 +42,7 @@ public class DocumentationUnitKeywordDTO {

@Embeddable
@NoArgsConstructor
@EqualsAndHashCode
class DocumentationUnitKeywordId implements Serializable {
private UUID documentationUnitId;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import jakarta.persistence.ManyToOne;
import jakarta.persistence.MapsId;
import jakarta.persistence.Table;
import jakarta.persistence.Transient;
import java.io.Serializable;
import java.util.UUID;
import lombok.AllArgsConstructor;
Expand Down Expand Up @@ -40,7 +41,15 @@ public class DocumentationUnitProcedureDTO {
@JoinColumn(name = "procedure_id")
private ProcedureDTO procedure;

private int rank;
@Transient
public int getRank() {
return primaryKey.getRank();
}

@Transient
public void setRank(int rank) {
primaryKey.setRank(rank);
}
}

@Embeddable
Expand All @@ -51,6 +60,7 @@ class DocumentationUnitProcedureId implements Serializable {
private UUID documentationUnitId;

private UUID procedureId;
@Setter private int rank;

public DocumentationUnitProcedureId(UUID documentationUnitId, UUID procedureId) {
this.documentationUnitId = documentationUnitId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,11 @@ public void saveProcedures(DocumentUnit documentUnit) {
Procedure procedure = documentUnit.coreData().procedure();

List<DocumentationUnitProcedureDTO> documentationUnitProcedureDTOs =
documentationUnitDTO.getProcedures();
new ArrayList<>();

ProcedureDTO procedureDTO = null;
if (procedure.id() == null) {
ProcedureDTO procedureDTO =
procedureDTO =
ProcedureDTO.builder()
.label(procedure.label())
.createdAt(Instant.now())
Expand All @@ -270,6 +271,23 @@ public void saveProcedures(DocumentUnit documentUnit) {

procedureDTO = procedureRepository.save(procedureDTO);

} else {
Optional<ProcedureDTO> optionalProcedureDTO =
procedureRepository.findById(procedure.id());
if (optionalProcedureDTO.isPresent()) {
procedureDTO = optionalProcedureDTO.get();
}
}

boolean sameAsLast =
!documentationUnitDTO.getProcedures().isEmpty()
&& documentationUnitDTO
.getProcedures()
.get(0)
.getProcedure()
.equals(procedureDTO);

if (procedureDTO != null && !sameAsLast) {
DocumentationUnitProcedureDTO documentationUnitProcedureDTO =
DocumentationUnitProcedureDTO.builder()
.primaryKey(
Expand All @@ -278,34 +296,30 @@ public void saveProcedures(DocumentUnit documentUnit) {
.documentationUnit(documentationUnitDTO)
.procedure(procedureDTO)
.build();
documentationUnitProcedureDTOs.add(0, documentationUnitProcedureDTO);

updateProcedureRank(documentationUnitProcedureDTOs);
} else {
procedureRepository
.findById(procedure.id())
.ifPresent(
procedureDTO -> {
DocumentationUnitProcedureDTO documentationUnitProcedureDTO =
DocumentationUnitProcedureDTO.builder()
.primaryKey(
new DocumentationUnitProcedureId(
documentationUnitDTO.getId(), procedureDTO.getId()))
.documentationUnit(documentationUnitDTO)
.procedure(procedureDTO)
.build();

if (documentationUnitProcedureDTOs.isEmpty()
|| !documentationUnitProcedureDTOs
.get(0)
.equals(documentationUnitProcedureDTO)) {
documentationUnitProcedureDTOs.add(0, documentationUnitProcedureDTO);

updateProcedureRank(documentationUnitProcedureDTOs);
}
});
documentationUnitProcedureDTOs.add(documentationUnitProcedureDTO);
}

documentationUnitDTO
.getProcedures()
.forEach(
documentationUnitProcedureDTO -> {
DocumentationUnitProcedureDTO newLink =
DocumentationUnitProcedureDTO.builder()
.primaryKey(
new DocumentationUnitProcedureId(
documentationUnitDTO.getId(),
documentationUnitProcedureDTO.getProcedure().getId()))
.documentationUnit(documentationUnitDTO)
.procedure(documentationUnitProcedureDTO.getProcedure())
.build();
documentationUnitProcedureDTOs.add(newLink);
});

updateProcedureRank(documentationUnitProcedureDTOs);

documentationUnitDTO.getProcedures().clear();
documentationUnitDTO.getProcedures().addAll(documentationUnitProcedureDTOs);

repository.save(documentationUnitDTO);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ private static Long getDocumentationUnitCount(ProcedureDTO procedureDTO) {
.filter(
documentationUnitDTO ->
documentationUnitDTO.getProcedures().get(0).getProcedure().equals(procedureDTO))
.distinct()
.count();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
import de.bund.digitalservice.ris.caselaw.domain.UserService;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicReference;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
Expand Down Expand Up @@ -317,6 +319,46 @@ void testAddingProcedureToPreviousProcedures() {
assertThat(repository.findAll().get(2).getLabel()).isEqualTo("baz");
}

@Test
void testAddProcedureWhichIsInHistoryAgain() {
DocumentationUnitDTO dto =
documentUnitRepository.save(
DocumentationUnitDTO.builder()
.documentNumber("1234567890123")
.documentationOffice(documentationOfficeDTO)
.build());

UUID procedureId = addProcedure(dto, "foo");
addProcedure(dto, "bar");

DocumentUnit documentUnitFromFrontend1 =
DocumentUnit.builder()
.uuid(dto.getId())
.documentNumber(dto.getDocumentNumber())
.coreData(
CoreData.builder()
.procedure(Procedure.builder().id(procedureId).build())
.documentationOffice(docOffice)
.build())
.build();

risWebTestClient
.withDefaultLogin()
.put()
.uri("/api/v1/caselaw/documentunits/" + dto.getId())
.bodyValue(documentUnitFromFrontend1)
.exchange()
.expectStatus()
.is2xxSuccessful()
.expectBody(DocumentUnit.class)
.consumeWith(
response -> {
CoreData coreData = response.getResponseBody().coreData();
assertThat(coreData.procedure().label()).isEqualTo("foo");
assertThat(coreData.previousProcedures()).containsExactly("bar", "foo");
});
}

@Test
void testAddProcedureWithSameNameToDifferentOffice() {
DocumentationOfficeDTO bghDocOfficeDTO =
Expand Down Expand Up @@ -493,6 +535,38 @@ private ProcedureDTO createProcedure(String label, DocumentationOfficeDTO docume
return createProcedures(List.of(label), documentationOffice).get(0);
}

private UUID addProcedure(DocumentationUnitDTO dto, String procedureValue) {
DocumentUnit documentUnitFromFrontend1 =
DocumentUnit.builder()
.uuid(dto.getId())
.documentNumber(dto.getDocumentNumber())
.coreData(
CoreData.builder()
.procedure(Procedure.builder().label(procedureValue).build())
.documentationOffice(docOffice)
.build())
.build();

AtomicReference<UUID> procedureId = new AtomicReference<>();
risWebTestClient
.withDefaultLogin()
.put()
.uri("/api/v1/caselaw/documentunits/" + dto.getId())
.bodyValue(documentUnitFromFrontend1)
.exchange()
.expectStatus()
.is2xxSuccessful()
.expectBody(DocumentUnit.class)
.consumeWith(
response -> {
CoreData coreData = response.getResponseBody().coreData();
assertThat(coreData.procedure().label()).isEqualTo(procedureValue);
procedureId.set(coreData.procedure().id());
});

return procedureId.get();
}

public static class RestPageImpl<T> extends PageImpl<T> {

@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
Expand Down

0 comments on commit e67d57f

Please sign in to comment.