Skip to content

Commit

Permalink
MODEXPW-537 - Add Statistical code column to instances CSV (#607)
Browse files Browse the repository at this point in the history
* MODEXPW-537 - statistical codes

* MODEXPW-537 - test mapper to map statistical codes

* MODEXPW-537 - test instance reference

* MODEXPW-537 - fix misprint

* MODEXPW-537 - rename Statistical codes to Statistical code

* MODEXPW-537 - update tests

* MODEXPW-537 - update shouldSaveErrorWhenInstanceNoteTypeNotFound

* MODEXPW-537 - use mapper for statistical codes tests

* MODEXPW-537 - fix sonar issues
  • Loading branch information
alekGbuz authored Dec 16, 2024
1 parent 6b23d3a commit ee9b9f1
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 26 deletions.
33 changes: 18 additions & 15 deletions src/main/java/org/folio/dew/domain/dto/InstanceFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import lombok.NoArgsConstructor;
import lombok.With;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.folio.dew.domain.dto.IdentifierType;

import java.lang.reflect.Field;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -59,60 +58,64 @@ public class InstanceFormat implements Formatable<org.folio.dew.domain.dto.Insta
@CsvBindByPosition(position = 8)
private String modeOfIssuanceId;

@CsvBindByName(column = "Administrative note")
@CsvBindByName(column = "Statistical code")
@CsvBindByPosition(position = 9)
private String statisticalCode;

@CsvBindByName(column = "Administrative note")
@CsvBindByPosition(position = 10)
private String administrativeNotes;

@CsvBindByName(column = "Resource title")
@CsvBindByPosition(position = 10)
@CsvBindByPosition(position = 11)
private String title;

@CsvBindByName(column = "Index title")
@CsvBindByPosition(position = 11)
@CsvBindByPosition(position = 12)
private String indexTitle;

@CsvBindByName(column = "Series statements")
@CsvBindByPosition(position = 12)
@CsvBindByPosition(position = 13)
private String series;

@CsvBindByName(column = "Contributors")
@CsvBindByPosition(position = 13)
@CsvBindByPosition(position = 14)
private String contributors;

@CsvBindByName(column = "Edition")
@CsvBindByPosition(position = 14)
@CsvBindByPosition(position = 15)
private String editions;

@CsvBindByName(column = "Physical description")
@CsvBindByPosition(position = 15)
@CsvBindByPosition(position = 16)
private String physicalDescriptions;

@CsvBindByName(column = "Resource type")
@CsvBindByPosition(position = 16)
@CsvBindByPosition(position = 17)
private String instanceTypeId;

@CsvBindByName(column = "Nature of content")
@CsvBindByPosition(position = 17)
@CsvBindByPosition(position = 18)
private String natureOfContentTermIds;

@CsvBindByName(column = "Formats")
@CsvBindByPosition(position = 18)
@CsvBindByPosition(position = 19)
private String instanceFormatIds;

@CsvBindByName(column = "Languages")
@CsvBindByPosition(position = 19)
@CsvBindByPosition(position = 20)
private String languages;

@CsvBindByName(column = "Publication frequency")
@CsvBindByPosition(position = 20)
@CsvBindByPosition(position = 21)
private String publicationFrequency;

@CsvBindByName(column = "Publication range")
@CsvBindByPosition(position = 21)
@CsvBindByPosition(position = 22)
private String publicationRange;

@CsvBindByName(column = "Notes")
@CsvBindByPosition(position = 22)
@CsvBindByPosition(position = 23)
private String notes;

private String isbn;
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/org/folio/dew/service/InstanceReferenceService.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package org.folio.dew.service;


import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.folio.dew.domain.dto.ErrorServiceArgs;
import org.folio.dew.error.BulkEditException;
import org.folio.dew.error.NotFoundException;
import org.springframework.stereotype.Service;


@Service
@Log4j2
@RequiredArgsConstructor
Expand Down Expand Up @@ -74,4 +72,15 @@ public String getInstanceNoteTypeNameById(String noteTypeId, ErrorServiceArgs ar
return noteTypeId;
}
}

public String getStatisticalCodeNameById(String statisticalCodeId, ErrorServiceArgs args) {
try {
return instanceReferenceServiceCache.getStatisticalCodeNameById(statisticalCodeId);
} catch (NotFoundException e) {
var msg = "Statistical code not found by id=" + statisticalCodeId;
log.error(msg);
errorsService.saveErrorInCSV(args.getJobId(), args.getIdentifier(), new BulkEditException(msg), args.getFileName());
return statisticalCodeId;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.folio.dew.client.InstanceStatusesClient;
import org.folio.dew.client.InstanceTypesClient;
import org.folio.dew.client.NatureOfContentTermsClient;
import org.folio.dew.client.StatisticalCodeClient;
import org.folio.dew.domain.dto.IdentifierTypeReferenceCollection;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
Expand All @@ -32,6 +33,7 @@ public class InstanceReferenceServiceCache {
private final InstanceFormatsClient instanceFormatsClient;
private final IdentifierTypeClient identifierTypeClient;
private final InstanceNoteTypesClient instanceNoteTypesClient;
private final StatisticalCodeClient statisticalCodeClient;


@Cacheable(cacheNames = "instanceStatusNames")
Expand Down Expand Up @@ -76,4 +78,12 @@ public String getTypeOfIdentifiersIdByName(String identifierName) {
public String getInstanceNoteTypeNameById(String noteTypeId) {
return isEmpty(noteTypeId) ? EMPTY : instanceNoteTypesClient.getNoteTypeById(noteTypeId).getName();
}

@Cacheable(cacheNames = "instanceStatisticalCodeNames")
public String getStatisticalCodeNameById(String id) {
if (StringUtils.isEmpty(id)) {
return EMPTY;
}
return statisticalCodeClient.getById(id).getName();
}
}
10 changes: 10 additions & 0 deletions src/main/java/org/folio/dew/service/mapper/InstanceMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

Expand All @@ -44,6 +45,7 @@ public InstanceFormat mapToInstanceFormat(Instance instance, String identifier,
.catalogedDate(formatDate(instance.getCatalogedDate()))
.statusId(instanceReferenceService.getInstanceStatusNameById(instance.getStatusId(), errorServiceArgs))
.modeOfIssuanceId(instanceReferenceService.getModeOfIssuanceNameById(instance.getModeOfIssuanceId(), errorServiceArgs))
.statisticalCode(getStatisticalCodeNames(instance.getStatisticalCodeIds(), errorServiceArgs))
.notes(fetchNotes(instance.getNotes(), errorServiceArgs))
.administrativeNotes(isEmpty(instance.getAdministrativeNotes()) ? EMPTY : String.join(ITEM_DELIMITER_SPACED, instance.getAdministrativeNotes()))
.title(instance.getTitle())
Expand Down Expand Up @@ -102,4 +104,12 @@ private String noteToString(InstanceNotesInner note, ErrorServiceArgs errorServi
specialCharacterEscaper.escape(note.getNote()),
booleanToStringNullSafe(note.getStaffOnly())));
}

private String getStatisticalCodeNames(List<String> codeIds, ErrorServiceArgs args) {
return isEmpty(codeIds) ? EMPTY : codeIds.stream()
.filter(Objects::nonNull)
.map(id -> instanceReferenceService.getStatisticalCodeNameById(id, args))
.map(specialCharacterEscaper::escape)
.collect(Collectors.joining(ARRAY_DELIMITER));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

@ExtendWith(MockitoExtension.class)
class InstanceReferenceServiceTest extends BaseBatchTest {

@Autowired
private BulkEditProcessingErrorsService errorsService;
@Autowired
Expand All @@ -31,12 +32,13 @@ void shouldSaveErrorWhenInstanceNoteTypeNotFound() {
when(instanceNoteTypesClient.getNoteTypeById(anyString()))
.thenThrow(new NotFoundException("not found"));

var jobId = UUID.randomUUID().toString();
var id = UUID.randomUUID().toString();

instanceReferenceService.getInstanceNoteTypeNameById(id,
new ErrorServiceArgs("jobId", "identifier", "errorFile"));
new ErrorServiceArgs(jobId, "identifier", "errorFile"));

var errors = errorsService.readErrorsFromCSV("jobId", "errorFile", Integer.MAX_VALUE);
var errors = errorsService.readErrorsFromCSV(jobId, "errorFile", Integer.MAX_VALUE);

assertThat(errors.getErrors(), Matchers.hasSize(1));
assertThat(errors.getErrors().get(0).getMessage(), Matchers.equalTo(format("identifier,Instance note type was not found by id: [%s]", id)));
Expand All @@ -49,4 +51,16 @@ void shouldReturnEmptyNameWhenInstanceNoteTypeIsNullOrEmpty(String id) {
new ErrorServiceArgs("jobId", "identifier", "errorFile"));
assertThat(name, Matchers.emptyOrNullString());
}

@Test
void shouldSaveErrorWhenStatisticalCodeNotFound() {
var jobId = UUID.randomUUID().toString();

instanceReferenceService.getStatisticalCodeNameById(UUID.randomUUID().toString(),
new ErrorServiceArgs(jobId, "identifier", "errorFile"));

var errors = errorsService.readErrorsFromCSV(jobId, "errorFile", Integer.MAX_VALUE);

assertThat(errors.getErrors(), Matchers.hasSize(1));
}
}
15 changes: 15 additions & 0 deletions src/test/java/org/folio/dew/service/mapper/InstanceMapperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Collections;
import java.util.List;
import java.util.UUID;

class InstanceMapperTest extends BaseBatchTest {
Expand All @@ -37,4 +38,18 @@ void shouldMapInstanceNoteTypes() {

assertThat(instanceFormat.getNotes()).isEqualTo("note type;test note;true");
}

@Test
void shouldMapInstanceStatisticalCodes() {
var statisticalCodeId1 = "b5968c9e-cddc-4576-99e3-8e60aed8b0dd";
var statisticalCodeId2 = "b5968c9e-cddc-4576-99e3-8e60aed8b0dd";
var instance = new Instance()
.id(UUID.randomUUID().toString())
.statisticalCodeIds(List.of(statisticalCodeId1, statisticalCodeId2));
var mapper = new InstanceMapper(instanceReferenceService, new SpecialCharacterEscaper());

var instanceFormat = mapper.mapToInstanceFormat(instance, "identifier", UUID.randomUUID().toString(), "errorFile");

assertThat(instanceFormat.getStatisticalCode()).isEqualTo("Book, print (books);Book, print (books)");
}
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Instance UUID,Suppress from discovery,Staff suppress,Previously held,Instance HRID,Source,Cataloged date,Instance status term,Mode of issuance,Administrative note,Resource title,Index title,Series statements,Contributors,Edition,Physical description,Resource type,Nature of content,Formats,Languages,Publication frequency,Publication range,Notes
7fbd5d84-62d1-44c6-9c45-6cb173998bbd,false,false,true,inst000000000006,FOLIO,2024-01-26,,,Cataloging data,Bridget Jones's Baby: the diaries,,,"Fielding, Helen",First American Edition,219 pages ; 20 cm.,6312d172-f0cf-40f6-b27d-9fa8feaf332f,,,eng,A frequency description,A publication range,
Instance UUID,Suppress from discovery,Staff suppress,Previously held,Instance HRID,Source,Cataloged date,Instance status term,Mode of issuance,Statistical code,Administrative note,Resource title,Index title,Series statements,Contributors,Edition,Physical description,Resource type,Nature of content,Formats,Languages,Publication frequency,Publication range,Notes
7fbd5d84-62d1-44c6-9c45-6cb173998bbd,false,false,true,inst000000000006,FOLIO,2024-01-26,,,,Cataloging data,Bridget Jones's Baby: the diaries,,,"Fielding, Helen",First American Edition,219 pages ; 20 cm.,6312d172-f0cf-40f6-b27d-9fa8feaf332f,,,eng,A frequency description,A publication range,
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Instance UUID,Suppress from discovery,Staff suppress,Previously held,Instance HRID,Source,Cataloged date,Instance status term,Mode of issuance,Administrative note,Resource title,Index title,Series statements,Contributors,Edition,Physical description,Resource type,Nature of content,Formats,Languages,Publication frequency,Publication range,Notes
00f10ab9-d845-4334-92d2-ff55862bf4f9,false,,false,inst000000000002,FOLIO,,,,,American Bar Association journal.,American Bar Association journal.,,American Bar Association; American Bar Association. Journal,,69 v. : ill. ; 23-30 cm.,6312d172-f0cf-40f6-b27d-9fa8feaf332f,,5cb91d15-96b1-4b8a-bf60-ec310538da66,eng,"Monthly, 1921-83 | Quarterly, 1915-20","Began with vol. 1, no. 1 (Jan. 1915); ceased with v. 69, [no.12] (Dec. 1983)",
549fad9e-7f8e-4d8e-9a71-00d251817866,false,true,false,inst000000000028,FOLIO,,,,,"Agile Organisation, Risiko- und Change Management Harald Augustin (Hrsg.)",,Umsetzung der DIN EN ISO 9001:2015 / Harald Augustin (Hrsg.) ; Band 2 | Berichte aus der Betriebswirtschaft | Umsetzung der DIN EN ISO 9001:2015 Band 2,"Augustin, Harald; Shaker Verlag GmbH",,"x, 148 Seiten Illustrationen 21 cm, 188 g",6312d172-f0cf-40f6-b27d-9fa8feaf332f,,,ger,,,
Instance UUID,Suppress from discovery,Staff suppress,Previously held,Instance HRID,Source,Cataloged date,Instance status term,Mode of issuance,Statistical code,Administrative note,Resource title,Index title,Series statements,Contributors,Edition,Physical description,Resource type,Nature of content,Formats,Languages,Publication frequency,Publication range,Notes
00f10ab9-d845-4334-92d2-ff55862bf4f9,false,,false,inst000000000002,FOLIO,,,,,,American Bar Association journal.,American Bar Association journal.,,American Bar Association; American Bar Association. Journal,,69 v. : ill. ; 23-30 cm.,6312d172-f0cf-40f6-b27d-9fa8feaf332f,,5cb91d15-96b1-4b8a-bf60-ec310538da66,eng,"Monthly, 1921-83 | Quarterly, 1915-20","Began with vol. 1, no. 1 (Jan. 1915); ceased with v. 69, [no.12] (Dec. 1983)",
549fad9e-7f8e-4d8e-9a71-00d251817866,false,true,false,inst000000000028,FOLIO,,,,,,"Agile Organisation, Risiko- und Change Management Harald Augustin (Hrsg.)",,Umsetzung der DIN EN ISO 9001:2015 / Harald Augustin (Hrsg.) ; Band 2 | Berichte aus der Betriebswirtschaft | Umsetzung der DIN EN ISO 9001:2015 Band 2,"Augustin, Harald; Shaker Verlag GmbH",,"x, 148 Seiten Illustrationen 21 cm, 188 g",6312d172-f0cf-40f6-b27d-9fa8feaf332f,,,ger,,,
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Instance UUID,Suppress from discovery,Staff suppress,Previously held,Instance HRID,Source,Cataloged date,Instance status term,Mode of issuance,Administrative note,Resource title,Index title,Series statements,Contributors,Edition,Physical description,Resource type,Nature of content,Formats,Languages,Publication frequency,Publication range,Notes
00f10ab9-d845-4334-92d2-ff55862bf4f9,false,,false,inst000000000022,FOLIO,,,,,American Bar Association journal.,American Bar Association journal.,,American Bar Association; American Bar Association. Journal,,69 v. : ill. ; 23-30 cm.,6312d172-f0cf-40f6-b27d-9fa8feaf332f,921e6d93-bafb-4a02-b62f-dcd027c45406,5cb91d15-96b1-4b8a-bf60-ec310538da66,eng,"Monthly, 1921-83 | Quarterly, 1915-20","Began with vol. 1, no. 1 (Jan. 1915); ceased with v. 69, [no.12] (Dec. 1983)",
Instance UUID,Suppress from discovery,Staff suppress,Previously held,Instance HRID,Source,Cataloged date,Instance status term,Mode of issuance,Statistical code,Administrative note,Resource title,Index title,Series statements,Contributors,Edition,Physical description,Resource type,Nature of content,Formats,Languages,Publication frequency,Publication range,Notes
00f10ab9-d845-4334-92d2-ff55862bf4f9,false,,false,inst000000000022,FOLIO,,,,,,American Bar Association journal.,American Bar Association journal.,,American Bar Association; American Bar Association. Journal,,69 v. : ill. ; 23-30 cm.,6312d172-f0cf-40f6-b27d-9fa8feaf332f,921e6d93-bafb-4a02-b62f-dcd027c45406,5cb91d15-96b1-4b8a-bf60-ec310538da66,eng,"Monthly, 1921-83 | Quarterly, 1915-20","Began with vol. 1, no. 1 (Jan. 1915); ceased with v. 69, [no.12] (Dec. 1983)",

0 comments on commit ee9b9f1

Please sign in to comment.