From ae3488e5226cf3a5a2660161c0b31109fc836b7c Mon Sep 17 00:00:00 2001 From: guygrewal77 Date: Tue, 31 Mar 2026 10:24:15 +0100 Subject: [PATCH 1/2] HDPI-4238 - Added new description column in Document table --- .../pcs/ccd/domain/AdditionalDocument.java | 3 +- .../reform/pcs/ccd/entity/DocumentEntity.java | 2 + .../pcs/ccd/service/DocumentService.java | 31 +++++----- ...075__add_description_to_Document_table.sql | 1 + .../pcs/ccd/service/DocumentServiceTest.java | 62 +++++++++++++++++++ 5 files changed, 84 insertions(+), 15 deletions(-) create mode 100644 src/main/resources/db/migration/V075__add_description_to_Document_table.sql diff --git a/src/main/java/uk/gov/hmcts/reform/pcs/ccd/domain/AdditionalDocument.java b/src/main/java/uk/gov/hmcts/reform/pcs/ccd/domain/AdditionalDocument.java index 907d77bba8..201ba7ff60 100644 --- a/src/main/java/uk/gov/hmcts/reform/pcs/ccd/domain/AdditionalDocument.java +++ b/src/main/java/uk/gov/hmcts/reform/pcs/ccd/domain/AdditionalDocument.java @@ -28,7 +28,8 @@ public class AdditionalDocument { @CCD(label = "Short description", typeOverride = FieldType.TextArea, - access = {CitizenAccess.class} + access = {CitizenAccess.class}, + max = 60 ) private String description; } diff --git a/src/main/java/uk/gov/hmcts/reform/pcs/ccd/entity/DocumentEntity.java b/src/main/java/uk/gov/hmcts/reform/pcs/ccd/entity/DocumentEntity.java index 558fb82a10..450a10d0cb 100644 --- a/src/main/java/uk/gov/hmcts/reform/pcs/ccd/entity/DocumentEntity.java +++ b/src/main/java/uk/gov/hmcts/reform/pcs/ccd/entity/DocumentEntity.java @@ -54,6 +54,8 @@ public class DocumentEntity { @Enumerated(EnumType.STRING) private DocumentType type; + private String description; + @OneToMany(fetch = LAZY, cascade = ALL, mappedBy = "document") @Builder.Default @JsonManagedReference diff --git a/src/main/java/uk/gov/hmcts/reform/pcs/ccd/service/DocumentService.java b/src/main/java/uk/gov/hmcts/reform/pcs/ccd/service/DocumentService.java index 707fcfea1c..33b36ad158 100644 --- a/src/main/java/uk/gov/hmcts/reform/pcs/ccd/service/DocumentService.java +++ b/src/main/java/uk/gov/hmcts/reform/pcs/ccd/service/DocumentService.java @@ -1,7 +1,8 @@ package uk.gov.hmcts.reform.pcs.ccd.service; import lombok.AllArgsConstructor; -import org.apache.commons.lang3.tuple.Pair; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.tuple.Triple; import org.springframework.stereotype.Service; import uk.gov.hmcts.ccd.sdk.type.Document; import uk.gov.hmcts.ccd.sdk.type.ListValue; @@ -31,7 +32,7 @@ public class DocumentService { public List createAllDocuments(PCSCase pcsCase) { - List> allDocuments = new ArrayList<>(); + List> allDocuments = new ArrayList<>(); allDocuments.addAll(mapAdditionalDocumentsWithType(pcsCase.getAdditionalDocuments())); @@ -58,7 +59,7 @@ public List createAllDocuments(PCSCase pcsCase) { return documentRepository.saveAll(createDocumentEntities(allDocuments)); } - private List> mapDocumentsWithType( + private List> mapDocumentsWithType( List> docs, DocumentType type) { if (docs == null || docs.isEmpty()) { @@ -68,11 +69,11 @@ private List> mapDocumentsWithType( return docs.stream() .map(ListValue::getValue) .filter(Objects::nonNull) - .map(doc -> Pair.of(doc, type)) + .map(doc -> Triple.of(doc, type, "")) .toList(); } - private List> mapAdditionalDocumentsWithType( + private List> mapAdditionalDocumentsWithType( List> documents) { if (documents == null || documents.isEmpty()) { @@ -80,27 +81,29 @@ private List> mapAdditionalDocumentsWithType( } return ListValueUtils.unwrapListItems(documents).stream() - .map(doc -> Pair.of( + .map(doc -> Triple.of( doc.getDocument(), - mapAdditionalDocumentTypeToDocumentType(doc.getDocumentType()) + mapAdditionalDocumentTypeToDocumentType(doc.getDocumentType()), + doc.getDescription() )) .toList(); } private List createDocumentEntities( - List> documents) { + List> documents) { if (documents == null || documents.isEmpty()) { return List.of(); } return documents.stream() - .map(pair -> DocumentEntity.builder() - .url(pair.getKey().getUrl()) - .fileName(pair.getKey().getFilename()) - .binaryUrl(pair.getKey().getBinaryUrl()) - .categoryId(pair.getKey().getCategoryId()) - .type(pair.getValue()) + .map(triplet -> DocumentEntity.builder() + .url(triplet.getLeft().getUrl()) + .fileName(triplet.getLeft().getFilename()) + .binaryUrl(triplet.getLeft().getBinaryUrl()) + .categoryId(triplet.getLeft().getCategoryId()) + .type(triplet.getMiddle()) + .description(StringUtils.isEmpty(triplet.getRight()) ? null : triplet.getRight()) .build()) .toList(); } diff --git a/src/main/resources/db/migration/V075__add_description_to_Document_table.sql b/src/main/resources/db/migration/V075__add_description_to_Document_table.sql new file mode 100644 index 0000000000..b8dd433dc8 --- /dev/null +++ b/src/main/resources/db/migration/V075__add_description_to_Document_table.sql @@ -0,0 +1 @@ +ALTER TABLE Document ADD COLUMN description VARCHAR(60); \ No newline at end of file diff --git a/src/test/java/uk/gov/hmcts/reform/pcs/ccd/service/DocumentServiceTest.java b/src/test/java/uk/gov/hmcts/reform/pcs/ccd/service/DocumentServiceTest.java index 88e3d3d149..3e71306b9e 100644 --- a/src/test/java/uk/gov/hmcts/reform/pcs/ccd/service/DocumentServiceTest.java +++ b/src/test/java/uk/gov/hmcts/reform/pcs/ccd/service/DocumentServiceTest.java @@ -260,4 +260,66 @@ void shouldReturnEmptyListIfNoDocuments() { assertThat(entities).isEmpty(); verify(documentRepository).saveAll(anyList()); } + + @Test + void shouldSaveDescriptionForAdditionalDocuments() { + // Given + PCSCase pcsCase = mock(PCSCase.class); + + AdditionalDocumentType additionalDocumentType = AdditionalDocumentType.WITNESS_STATEMENT; + String description = "A short description"; + + AdditionalDocument additionalDocument1 = AdditionalDocument.builder() + .document(Document.builder().build()) + .documentType(additionalDocumentType) + .description(description) + .build(); + + List> additionalDocuments = List.of( + ListValue.builder().value(additionalDocument1).build() + ); + + when(pcsCase.getAdditionalDocuments()).thenReturn(additionalDocuments); + + // When + underTest.createAllDocuments(pcsCase); + + // Then + DocumentType expectedDocumentType = DocumentType.valueOf(additionalDocumentType.name()); + + verify(documentRepository).saveAll(documentEntityListCaptor.capture()); + List capturedEntities = documentEntityListCaptor.getValue(); + + assertThat(capturedEntities) + .extracting(DocumentEntity::getType) + .containsExactly(expectedDocumentType); + + assertThat(capturedEntities) + .extracting(DocumentEntity::getDescription) + .containsExactly(description); + } + + @Test + void shouldAllowNullDescriptionForDocumentsOtherThanAdditionalDocuments() { + // Given + PCSCase pcsCase = mock(PCSCase.class); + + Document doc = Document.builder() + .build(); + + NoticeServedDetails noticeServedDetails = NoticeServedDetails.builder() + .noticeDocuments(List.of(ListValue.builder().id("1").value(doc).build())) + .build(); + + when(pcsCase.getNoticeServedDetails()).thenReturn(noticeServedDetails); + + // When + underTest.createAllDocuments(pcsCase); + + // Then + verify(documentRepository).saveAll(documentEntityListCaptor.capture()); + List entities = documentEntityListCaptor.getValue(); + DocumentEntity entity = entities.getFirst(); + assertThat(entity.getDescription()).isNull(); + } } From b3bdb281cda8fd4e9af2927d922eef6246ae133a Mon Sep 17 00:00:00 2001 From: guygrewal77 Date: Tue, 31 Mar 2026 15:12:09 +0100 Subject: [PATCH 2/2] HDPI-4238 - Refactored to use a Document holder inner class to hold Document details --- .../pcs/ccd/service/DocumentService.java | 57 ++++++++++++------- 1 file changed, 35 insertions(+), 22 deletions(-) diff --git a/src/main/java/uk/gov/hmcts/reform/pcs/ccd/service/DocumentService.java b/src/main/java/uk/gov/hmcts/reform/pcs/ccd/service/DocumentService.java index 33b36ad158..7618dd2655 100644 --- a/src/main/java/uk/gov/hmcts/reform/pcs/ccd/service/DocumentService.java +++ b/src/main/java/uk/gov/hmcts/reform/pcs/ccd/service/DocumentService.java @@ -1,9 +1,11 @@ package uk.gov.hmcts.reform.pcs.ccd.service; import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; import org.apache.commons.lang3.StringUtils; -import org.apache.commons.lang3.tuple.Triple; import org.springframework.stereotype.Service; +import org.springframework.util.CollectionUtils; import uk.gov.hmcts.ccd.sdk.type.Document; import uk.gov.hmcts.ccd.sdk.type.ListValue; import uk.gov.hmcts.reform.pcs.ccd.domain.AdditionalDocument; @@ -32,8 +34,7 @@ public class DocumentService { public List createAllDocuments(PCSCase pcsCase) { - List> allDocuments = new ArrayList<>(); - + List allDocuments = new ArrayList<>(); allDocuments.addAll(mapAdditionalDocumentsWithType(pcsCase.getAdditionalDocuments())); allDocuments.addAll(mapDocumentsWithType( @@ -59,51 +60,55 @@ public List createAllDocuments(PCSCase pcsCase) { return documentRepository.saveAll(createDocumentEntities(allDocuments)); } - private List> mapDocumentsWithType( + private List mapDocumentsWithType( List> docs, DocumentType type) { - if (docs == null || docs.isEmpty()) { + if (CollectionUtils.isEmpty(docs)) { return Collections.emptyList(); } return docs.stream() .map(ListValue::getValue) .filter(Objects::nonNull) - .map(doc -> Triple.of(doc, type, "")) + .map(doc -> DocumentHolder.builder() + .document(doc) + .type(type) + .description("") + .build()) .toList(); } - private List> mapAdditionalDocumentsWithType( + private List mapAdditionalDocumentsWithType( List> documents) { - if (documents == null || documents.isEmpty()) { + if (CollectionUtils.isEmpty(documents)) { return Collections.emptyList(); } return ListValueUtils.unwrapListItems(documents).stream() - .map(doc -> Triple.of( - doc.getDocument(), - mapAdditionalDocumentTypeToDocumentType(doc.getDocumentType()), - doc.getDescription() - )) + .map(doc -> DocumentHolder.builder() + .document(doc.getDocument()) + .type(mapAdditionalDocumentTypeToDocumentType(doc.getDocumentType())) + .description(doc.getDescription()) + .build()) .toList(); } private List createDocumentEntities( - List> documents) { + List documents) { - if (documents == null || documents.isEmpty()) { + if (CollectionUtils.isEmpty(documents)) { return List.of(); } return documents.stream() - .map(triplet -> DocumentEntity.builder() - .url(triplet.getLeft().getUrl()) - .fileName(triplet.getLeft().getFilename()) - .binaryUrl(triplet.getLeft().getBinaryUrl()) - .categoryId(triplet.getLeft().getCategoryId()) - .type(triplet.getMiddle()) - .description(StringUtils.isEmpty(triplet.getRight()) ? null : triplet.getRight()) + .map(holder -> DocumentEntity.builder() + .url(holder.getDocument().getUrl()) + .fileName(holder.getDocument().getFilename()) + .binaryUrl(holder.getDocument().getBinaryUrl()) + .categoryId(holder.getDocument().getCategoryId()) + .type(holder.getType()) + .description(StringUtils.isEmpty(holder.getDescription()) ? null : holder.getDescription()) .build()) .toList(); } @@ -125,4 +130,12 @@ private DocumentType mapAdditionalDocumentTypeToDocumentType(AdditionalDocumentT case OTHER -> DocumentType.OTHER; }; } + + @Builder + @Data + private static class DocumentHolder { + private Document document; + private DocumentType type; + private String description; + } }