Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reset document number count on each year #2464

Merged
merged 3 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,7 @@ public String generateDocumentNumber(@NotEmpty String documentationOfficeAbbrevi
if (recycledId != null) return recycledId;

DocumentNumberDTO documentNumberDTO =
repository
.findById(documentationOfficeAbbreviation)
.orElse(
DocumentNumberDTO.builder()
.documentationOfficeAbbreviation(documentationOfficeAbbreviation)
.lastNumber(0)
.build());
getOrCreateDocumentNumberDTO(documentationOfficeAbbreviation);

String documentNumber =
DocumentNumberFormatter.builder()
Expand All @@ -102,6 +96,26 @@ public String generateDocumentNumber(@NotEmpty String documentationOfficeAbbrevi
return documentNumber;
}

/**
* Retrieves an existing document number entry for the given documentation office abbreviation and
* year, or creates a new document number entry if none exists.
*
* @param documentationOfficeAbbreviation desired court abbreviation by office
* @return A {@link DocumentNumberDTO} containing the latest doc number count
*/
public DocumentNumberDTO getOrCreateDocumentNumberDTO(
@NotEmpty String documentationOfficeAbbreviation) {
return repository
.findByDocumentationOfficeAbbreviationAndYear(
documentationOfficeAbbreviation, DateUtil.getYear())
.orElse(
DocumentNumberDTO.builder()
.documentationOfficeAbbreviation(documentationOfficeAbbreviation)
.lastNumber(0)
.year(DateUtil.getYear())
.build());
}

/**
* Validate document number not exists in the database
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package de.bund.digitalservice.ris.caselaw.adapter.database.jpa;

import jakarta.persistence.LockModeType;
import jakarta.validation.constraints.NotBlank;
import java.time.Year;
import java.util.Optional;
import org.jetbrains.annotations.NotNull;
import org.springframework.data.jpa.repository.JpaRepository;
Expand All @@ -9,7 +11,9 @@

@Repository
public interface DatabaseDocumentNumberRepository extends JpaRepository<DocumentNumberDTO, String> {

@NotNull
@Lock(LockModeType.PESSIMISTIC_WRITE)
Nadav-B marked this conversation as resolved.
Show resolved Hide resolved
Optional<DocumentNumberDTO> findById(@NotNull String id);
Optional<DocumentNumberDTO> findByDocumentationOfficeAbbreviationAndYear(
@NotBlank String documentationOfficeAbbreviation, @NotNull Year year);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotEmpty;
import java.time.Year;
import java.util.UUID;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.jetbrains.annotations.NotNull;

@Data
@Builder
Expand All @@ -18,14 +22,19 @@
@Table(name = "document_number", schema = "public")
public class DocumentNumberDTO {

@Id
@Id @GeneratedValue private UUID id;

@Column(name = "documentation_office_abbreviation")
@NotEmpty
private String documentationOfficeAbbreviation;

@Column(name = "last_number")
private int lastNumber;

@Column(name = "year")
@NotNull
private Year year;

public Integer increaseLastNumber() {
this.lastNumber = lastNumber + 1;
return lastNumber;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- Adding unique id to document number table
ALTER TABLE document_number
ADD COLUMN id UUID NOT NULL DEFAULT gen_random_uuid();

-- Drop the existing primary key constraint directing to documentation_office_abbreviation
ALTER TABLE document_number
DROP CONSTRAINT document_number_pkey;

-- Add a new primary key constraint on id column
ALTER TABLE document_number
ADD CONSTRAINT document_number_pkey PRIMARY KEY (id);


Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- Adding year to document number count
ALTER TABLE IF EXISTS document_number
ADD year INTEGER;

-- Adding previous year to document number count
UPDATE document_number SET year = 2024 WHERE year IS NULL;

-- Setting required year for upcoming entries
ALTER TABLE document_number ALTER COLUMN year SET NOT NULL;

-- Create a combined index on 'documentation_office_abbreviation' and 'year'
CREATE INDEX idx_documentation_office_abbreviation_year
ON document_number (documentation_office_abbreviation, year);
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package de.bund.digitalservice.ris.caselaw.adapter;

import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import de.bund.digitalservice.ris.caselaw.adapter.database.jpa.DatabaseDocumentNumberRepository;
import de.bund.digitalservice.ris.caselaw.adapter.database.jpa.DatabaseDocumentationUnitRepository;
import de.bund.digitalservice.ris.caselaw.adapter.database.jpa.DocumentNumberDTO;
import de.bund.digitalservice.ris.caselaw.adapter.database.jpa.DocumentationUnitDTO;
import de.bund.digitalservice.ris.caselaw.domain.DateUtil;
import de.bund.digitalservice.ris.caselaw.domain.DocumentNumberRecyclingService;
Expand Down Expand Up @@ -101,4 +103,41 @@ void shouldStopTrying_ifPatternIsInvalid() {
.hasMessageContaining(
"Could not " + "find pattern for abbreviation " + docOfficeAbbreviation);
}

@Test
void getOrCreateDocumentNumberDTO_startsByZero_ifYearAndAbbreviationDoNotExist() {

var documentNumberDto = service.getOrCreateDocumentNumberDTO(DEFAULT_ABBREVIATION);

Assertions.assertEquals(0, documentNumberDto.getLastNumber());
Assertions.assertEquals(DateUtil.getYear(), documentNumberDto.getYear());
Assertions.assertEquals(
DEFAULT_ABBREVIATION, documentNumberDto.getDocumentationOfficeAbbreviation());

verify(databaseDocumentNumberRepository)
.findByDocumentationOfficeAbbreviationAndYear(DEFAULT_ABBREVIATION, DateUtil.getYear());
}

@Test
void getOrCreateDocumentNumberDTO_shouldContinuesFromLast_ifYearAndAbbreviationExist() {
when(databaseDocumentNumberRepository.findByDocumentationOfficeAbbreviationAndYear(
DEFAULT_ABBREVIATION, DateUtil.getYear()))
.thenReturn(
Optional.of(
DocumentNumberDTO.builder()
.lastNumber(4)
.year(DateUtil.getYear())
.documentationOfficeAbbreviation(DEFAULT_ABBREVIATION)
.build()));

var documentNumberDto = service.getOrCreateDocumentNumberDTO(DEFAULT_ABBREVIATION);

Assertions.assertEquals(4, documentNumberDto.getLastNumber());
Assertions.assertEquals(DateUtil.getYear(), documentNumberDto.getYear());
Assertions.assertEquals(
DEFAULT_ABBREVIATION, documentNumberDto.getDocumentationOfficeAbbreviation());

verify(databaseDocumentNumberRepository)
.findByDocumentationOfficeAbbreviationAndYear(DEFAULT_ABBREVIATION, DateUtil.getYear());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import de.bund.digitalservice.ris.caselaw.domain.AuthService;
import de.bund.digitalservice.ris.caselaw.domain.ContentRelatedIndexing;
import de.bund.digitalservice.ris.caselaw.domain.CoreData;
import de.bund.digitalservice.ris.caselaw.domain.DateUtil;
import de.bund.digitalservice.ris.caselaw.domain.DocumentationOffice;
import de.bund.digitalservice.ris.caselaw.domain.DocumentationUnit;
import de.bund.digitalservice.ris.caselaw.domain.DocumentationUnitCreationParameters;
Expand All @@ -77,8 +78,6 @@
import de.bund.digitalservice.ris.caselaw.webtestclient.RisWebTestClient;
import java.net.URI;
import java.time.LocalDate;
import java.time.Year;
import java.time.temporal.ChronoField;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -1036,18 +1035,20 @@ void testDeleteByUuid_withExistingReference_shouldNotRecycleDocumentNumberAfterF
DeletedDocumentationUnitDTO.builder()
.abbreviation("DS")
.documentNumber("ZZRE202400001")
.year(Year.of(LocalDate.now().get(ChronoField.YEAR)))
.year(DateUtil.getYear())
.build();
deletedDocumentationIdsRepository.save(deletedDocumentationUnitDTO);

when(documentNumberPatternConfig.getDocumentNumberPatterns())
.thenReturn(Map.of("DS", "ZZREYYYY*****"));
when(databaseDocumentNumberRepository.findById("DS"))
when(databaseDocumentNumberRepository.findByDocumentationOfficeAbbreviationAndYear(
"DS", DateUtil.getYear()))
.thenReturn(
Optional.of(
DocumentNumberDTO.builder()
.documentationOfficeAbbreviation("DS")
.lastNumber(1)
.year(DateUtil.getYear())
.build()));

risWebTestClient
Expand Down
Loading