Skip to content

Commit

Permalink
MODBULKOPS-172 - BE - Update Item record column names (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
siarhei-charniak authored Dec 8, 2023
1 parent 1ccc309 commit 247fb9b
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 30 deletions.
19 changes: 17 additions & 2 deletions src/main/java/org/folio/bulkops/service/BulkOperationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import static org.folio.bulkops.domain.dto.UpdateActionType.SET_TO_FALSE_INCLUDING_ITEMS;
import static org.folio.bulkops.domain.dto.UpdateActionType.SET_TO_TRUE_INCLUDING_ITEMS;
import static org.folio.bulkops.domain.dto.UpdateOptionType.SUPPRESS_FROM_DISCOVERY;
import static org.folio.bulkops.util.Constants.ADMINISTRATIVE_NOTE;
import static org.folio.bulkops.util.Constants.ADMINISTRATIVE_NOTES;
import static org.folio.bulkops.util.Constants.FIELD_ERROR_MESSAGE_PATTERN;
import static org.folio.bulkops.util.Constants.MSG_HOLDING_NO_CHANGE_REQUIRED_SUPPRESSED_ITEMS_UPDATED;
import static org.folio.bulkops.util.Constants.MSG_HOLDING_NO_CHANGE_REQUIRED_UNSUPPRESSED_ITEMS_UPDATED;
Expand Down Expand Up @@ -473,8 +475,7 @@ private UnifiedTable buildPreviewFromCsvFile(String pathToFile, Class<? extends
table.addRowsItem(row);
}
}
if (clazz == Item.class) noteTableUpdater.extendTableWithItemNotesTypes(table);
if (clazz == HoldingsRecord.class) noteTableUpdater.extendTableWithHoldingsNotesTypes(table);
processNoteFields(table, clazz);
table.getRows().forEach(row -> row.setRow(SpecialCharacterEscaper.restore(row.getRow())));
return table;
} catch (Exception e) {
Expand All @@ -483,6 +484,20 @@ private UnifiedTable buildPreviewFromCsvFile(String pathToFile, Class<? extends
return table;
}

private void processNoteFields(UnifiedTable table, Class<? extends BulkOperationsEntity> clazz) {
table.getHeader().forEach(cell -> {
if (ADMINISTRATIVE_NOTES.equalsIgnoreCase(cell.getValue())) {
cell.setValue(ADMINISTRATIVE_NOTE);
}
});
if (clazz == Item.class) {
noteTableUpdater.extendTableWithItemNotesTypes(table);
}
if (clazz == HoldingsRecord.class) {
noteTableUpdater.extendTableWithHoldingsNotesTypes(table);
}
}

public BulkOperation startBulkOperation(UUID bulkOperationId, UUID xOkapiUserId, BulkOperationStart bulkOperationStart) {
var step = bulkOperationStart.getStep();
var approach = bulkOperationStart.getApproach();
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/org/folio/bulkops/service/NoteTableUpdater.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import java.util.Arrays;
import java.util.List;
import java.util.Set;

@Component
@RequiredArgsConstructor
Expand All @@ -39,6 +40,7 @@ public class NoteTableUpdater {
public void extendTableWithHoldingsNotesTypes(UnifiedTable unifiedTable) {
var noteTypeNames = holdingsReferenceService.getAllHoldingsNoteTypes().stream()
.map(HoldingsNoteType::getName)
.map(this::concatNotePostfixIfRequired)
.sorted()
.toList();

Expand All @@ -49,13 +51,20 @@ public void extendTableWithHoldingsNotesTypes(UnifiedTable unifiedTable) {
public void extendTableWithItemNotesTypes(UnifiedTable unifiedTable) {
var noteTypeNames = itemReferenceService.getAllItemNoteTypes().stream()
.map(NoteType::getName)
.map(this::concatNotePostfixIfRequired)
.sorted()
.toList();

extendHeadersWithItemNoteTypeNames(ITEM_NOTE_POSITION, unifiedTable.getHeader(), noteTypeNames);
unifiedTable.getRows().forEach(row -> extendRowWithNotesData(ITEM_NOTE_POSITION, row, noteTypeNames));
}

private String concatNotePostfixIfRequired(String noteTypeName) {
return Set.of("Binding", "Electronic bookplate", "Provenance", "Reproduction").contains(noteTypeName) ?
noteTypeName + " note" :
noteTypeName;
}

private void extendHeadersWithItemNoteTypeNames(int notesInitialPosition, List<Cell> headers, List<String> noteTypeNames) {
var headerToReplace = headers.get(notesInitialPosition);
var cellsToInsert = noteTypeNames.stream()
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/folio/bulkops/util/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,6 @@ public class Constants {
public static final String MSG_HOLDING_NO_CHANGE_REQUIRED_UNSUPPRESSED_ITEMS_UPDATED = "No change in value for holdings record required, associated unsuppressed item(s) have been updated.";
public static final String MSG_HOLDING_NO_CHANGE_REQUIRED_SUPPRESSED_ITEMS_UPDATED = "No change in value for holdings record required, associated suppressed item(s) have been updated.";
public static final String STAFF_ONLY = "(staff only)";
public static final String ADMINISTRATIVE_NOTES = "Administrative Notes";
public static final String ADMINISTRATIVE_NOTE = "Administrative Note";
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import static java.util.Objects.nonNull;
import static org.folio.bulkops.domain.dto.EntityType.HOLDINGS_RECORD;
import static org.folio.bulkops.domain.dto.EntityType.ITEM;
import static org.folio.bulkops.util.Constants.ADMINISTRATIVE_NOTE;
import static org.folio.bulkops.util.Constants.ADMINISTRATIVE_NOTES;
import static org.folio.bulkops.util.Constants.MSG_NO_CHANGE_REQUIRED;
import static org.folio.bulkops.util.UnifiedTableHeaderBuilder.getHeaders;
import static org.folio.bulkops.domain.dto.BulkOperationStep.COMMIT;
Expand Down Expand Up @@ -71,6 +73,7 @@
import org.folio.bulkops.domain.dto.BulkOperationRuleRuleDetails;
import org.folio.bulkops.domain.dto.BulkOperationStart;
import org.folio.bulkops.domain.dto.BulkOperationStep;
import org.folio.bulkops.domain.dto.Cell;
import org.folio.bulkops.domain.dto.EntityType;
import org.folio.bulkops.domain.dto.IdentifierType;
import org.folio.bulkops.domain.dto.OperationStatusType;
Expand Down Expand Up @@ -897,9 +900,9 @@ void shouldReturnPreviewIfAvailable(String fileName, EntityType entityType, Bulk
if (USER.equals(entityType)) {
assertThat(table.getHeader(), equalTo(getHeaders(User.class)));
} else if (EntityType.ITEM.equals(entityType)) {
assertThat(table.getHeader(), equalTo(getHeaders(Item.class)));
assertThat(table.getHeader(), equalTo(renameAdministrativeNotesHeader(getHeaders(Item.class))));
} else if (EntityType.HOLDINGS_RECORD.equals(entityType)) {
assertThat(table.getHeader(), equalTo(getHeaders(HoldingsRecord.class)));
assertThat(table.getHeader(), equalTo(renameAdministrativeNotesHeader(getHeaders(HoldingsRecord.class))));
}
assertTrue(table.getRows().stream()
.map(Row::getRow)
Expand Down Expand Up @@ -1140,4 +1143,13 @@ private BulkOperation buildBulkOperation(String fileName, EntityType entityType,
.build();
};
}

private List<Cell> renameAdministrativeNotesHeader(List<Cell> headers) {
headers.forEach(cell -> {
if (ADMINISTRATIVE_NOTES.equalsIgnoreCase(cell.getValue())) {
cell.setValue(ADMINISTRATIVE_NOTE);
}
});
return headers;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import static org.mockito.Mockito.when;

import org.folio.bulkops.domain.bean.HoldingsNoteType;
import org.folio.bulkops.domain.bean.HoldingsRecord;
import org.folio.bulkops.domain.bean.Item;
import org.folio.bulkops.domain.bean.NoteType;
import org.folio.bulkops.domain.dto.Cell;
Expand All @@ -32,12 +33,12 @@
@ExtendWith(MockitoExtension.class)
class ItemNoteTableUpdaterTest {
private static final int ACTION_NOTE_POSITION = ITEM_NOTE_POSITION;
private static final int BINDING_POSITION = ITEM_NOTE_POSITION + 1;
private static final int NOTE_POSITION = ITEM_NOTE_POSITION + 2;
private static final int NOTE_POSITION = ITEM_NOTE_POSITION + 1;
private static final int OTHER_POSITION = ITEM_NOTE_POSITION + 2;

private static final int ACTION_HOLDINGS_NOTE_POSITION = HOLDINGS_NOTE_POSITION;
private static final int BINDING_HOLDINGS_POSITION = HOLDINGS_NOTE_POSITION + 1;
private static final int NOTE_HOLDINGS_POSITION = HOLDINGS_NOTE_POSITION + 2;
private static final int NOTE_HOLDINGS_POSITION = HOLDINGS_NOTE_POSITION + 1;
private static final int OTHER_HOLDINGS_POSITION = HOLDINGS_NOTE_POSITION + 2;

@Mock
private ItemReferenceService itemReferenceService;
Expand All @@ -51,29 +52,29 @@ class ItemNoteTableUpdaterTest {
void shouldExtendTableWithItemNoteTypes() {
var table = UnifiedTableHeaderBuilder.getEmptyTableWithHeaders(Item.class);
var row = Arrays.stream(new String[table.getHeader().size()]).collect(Collectors.toCollection(ArrayList::new));
row.set(ITEM_NOTE_POSITION, "Action note;Action note text;false|Note;Note text;false|Binding;Binding text;false");
row.set(ITEM_NOTE_POSITION, "Action note;Action note text;false|Note;Note text;false|Other;Other text;false");
table.setRows(List.of(new Row().row(row)));

var expectedTableSize = table.getHeader().size() + 2;

when(itemReferenceService.getAllItemNoteTypes())
.thenReturn(List.of(NoteType.builder().name("Action note").build(),
NoteType.builder().name("Note").build(),
NoteType.builder().name("Binding").build()));
NoteType.builder().name("Other").build()));

noteTableUpdater.extendTableWithItemNotesTypes(table);

assertThat(table.getHeader(), hasSize(expectedTableSize));
var headerNames = table.getHeader().stream().map(Cell::getValue).toList();
assertThat(headerNames.indexOf("Action note"), is(ACTION_NOTE_POSITION));
assertThat(headerNames.indexOf("Note"), is(NOTE_POSITION));
assertThat(headerNames.indexOf("Binding"), is(BINDING_POSITION));
assertThat(headerNames.indexOf("Other"), is(OTHER_POSITION));

var modifiedRow = table.getRows().get(0).getRow();
assertThat(modifiedRow, hasSize(expectedTableSize));
assertEquals("Action note text", modifiedRow.get(ACTION_NOTE_POSITION));
assertEquals("Note text", modifiedRow.get(NOTE_POSITION));
assertEquals("Binding text", modifiedRow.get(BINDING_POSITION));
assertEquals("Other text", modifiedRow.get(OTHER_POSITION));
}

@Test
Expand All @@ -87,79 +88,79 @@ void shouldExtendTableWithEmptyItemsNotes() {
when(itemReferenceService.getAllItemNoteTypes())
.thenReturn(List.of(NoteType.builder().name("Action note").build(),
NoteType.builder().name("Note").build(),
NoteType.builder().name("Binding").build()));
NoteType.builder().name("Other").build()));

noteTableUpdater.extendTableWithItemNotesTypes(table);

assertThat(table.getHeader(), hasSize(expectedTableSize));
var headerNames = table.getHeader().stream().map(Cell::getValue).toList();
assertThat(headerNames.indexOf("Action note"), is(ACTION_NOTE_POSITION));
assertThat(headerNames.indexOf("Note"), is(NOTE_POSITION));
assertThat(headerNames.indexOf("Binding"), is(BINDING_POSITION));
assertThat(headerNames.indexOf("Other"), is(OTHER_POSITION));

var modifiedRow = table.getRows().get(0).getRow();
assertThat(modifiedRow, hasSize(expectedTableSize));
assertNull(modifiedRow.get(ACTION_NOTE_POSITION));
assertNull(modifiedRow.get(NOTE_POSITION));
assertNull(modifiedRow.get(BINDING_POSITION));
assertNull(modifiedRow.get(OTHER_POSITION));
}

@Test
void shouldAddStaffOnlyPostfixWhenRequiredForItemNotes() {
var table = UnifiedTableHeaderBuilder.getEmptyTableWithHeaders(Item.class);
var row = Arrays.stream(new String[table.getHeader().size()]).collect(Collectors.toCollection(ArrayList::new));
row.set(ITEM_NOTE_POSITION, "Action note;Action note text;true|Note;Note text;false|Binding;Binding text;true");
row.set(ITEM_NOTE_POSITION, "Action note;Action note text;true|Note;Note text;false|Other;Other text;true");
table.setRows(List.of(new Row().row(row)));

var expectedTableSize = table.getHeader().size() + 2;

when(itemReferenceService.getAllItemNoteTypes())
.thenReturn(List.of(NoteType.builder().name("Action note").build(),
NoteType.builder().name("Note").build(),
NoteType.builder().name("Binding").build()));
NoteType.builder().name("Other").build()));

noteTableUpdater.extendTableWithItemNotesTypes(table);

assertThat(table.getHeader(), hasSize(expectedTableSize));
var headerNames = table.getHeader().stream().map(Cell::getValue).toList();
assertThat(headerNames.indexOf("Action note"), is(ACTION_NOTE_POSITION));
assertThat(headerNames.indexOf("Note"), is(NOTE_POSITION));
assertThat(headerNames.indexOf("Binding"), is(BINDING_POSITION));
assertThat(headerNames.indexOf("Other"), is(OTHER_POSITION));

var modifiedRow = table.getRows().get(0).getRow();
assertThat(modifiedRow, hasSize(expectedTableSize));
assertTrue(modifiedRow.get(ACTION_NOTE_POSITION).contains(STAFF_ONLY));
assertFalse(modifiedRow.get(NOTE_POSITION).contains(STAFF_ONLY));
assertTrue(modifiedRow.get(BINDING_POSITION).contains(STAFF_ONLY));
assertTrue(modifiedRow.get(OTHER_POSITION).contains(STAFF_ONLY));
}

@Test
void shouldExtendTableWithHoldingsNoteTypes() {
var table = UnifiedTableHeaderBuilder.getEmptyTableWithHeaders(Item.class);
var row = Arrays.stream(new String[table.getHeader().size()]).collect(Collectors.toCollection(ArrayList::new));
row.set(HOLDINGS_NOTE_POSITION, "Action note;Action note text;false|Note;Note text;false|Binding;Binding text;false");
row.set(HOLDINGS_NOTE_POSITION, "Action note;Action note text;false|Note;Note text;false|Other;Other text;false");
table.setRows(List.of(new Row().row(row)));

var expectedTableSize = table.getHeader().size() + 2;

when(holdingsReferenceService.getAllHoldingsNoteTypes())
.thenReturn(List.of(HoldingsNoteType.builder().name("Action note").build(),
HoldingsNoteType.builder().name("Note").build(),
HoldingsNoteType.builder().name("Binding").build()));
HoldingsNoteType.builder().name("Other").build()));

noteTableUpdater.extendTableWithHoldingsNotesTypes(table);

assertThat(table.getHeader(), hasSize(expectedTableSize));
var headerNames = table.getHeader().stream().map(Cell::getValue).toList();
assertThat(headerNames.indexOf("Action note"), is(ACTION_HOLDINGS_NOTE_POSITION));
assertThat(headerNames.indexOf("Note"), is(NOTE_HOLDINGS_POSITION));
assertThat(headerNames.indexOf("Binding"), is(BINDING_HOLDINGS_POSITION));
assertThat(headerNames.indexOf("Other"), is(OTHER_HOLDINGS_POSITION));

var modifiedRow = table.getRows().get(0).getRow();
assertThat(modifiedRow, hasSize(expectedTableSize));
assertEquals("Action note text", modifiedRow.get(ACTION_HOLDINGS_NOTE_POSITION));
assertEquals("Note text", modifiedRow.get(NOTE_HOLDINGS_POSITION));
assertEquals("Binding text", modifiedRow.get(BINDING_HOLDINGS_POSITION));
assertEquals("Other text", modifiedRow.get(OTHER_HOLDINGS_POSITION));
}


Expand All @@ -174,49 +175,87 @@ void shouldExtendTableWithEmptyHoldingsNotes() {
when(holdingsReferenceService.getAllHoldingsNoteTypes())
.thenReturn(List.of(HoldingsNoteType.builder().name("Action note").build(),
HoldingsNoteType.builder().name("Note").build(),
HoldingsNoteType.builder().name("Binding").build()));
HoldingsNoteType.builder().name("Other").build()));

noteTableUpdater.extendTableWithHoldingsNotesTypes(table);

assertThat(table.getHeader(), hasSize(expectedTableSize));
var headerNames = table.getHeader().stream().map(Cell::getValue).toList();
assertThat(headerNames.indexOf("Action note"), is(ACTION_HOLDINGS_NOTE_POSITION));
assertThat(headerNames.indexOf("Note"), is(NOTE_HOLDINGS_POSITION));
assertThat(headerNames.indexOf("Binding"), is(BINDING_HOLDINGS_POSITION));
assertThat(headerNames.indexOf("Other"), is(OTHER_HOLDINGS_POSITION));

var modifiedRow = table.getRows().get(0).getRow();
assertThat(modifiedRow, hasSize(expectedTableSize));
assertNull(modifiedRow.get(ACTION_HOLDINGS_NOTE_POSITION));
assertNull(modifiedRow.get(NOTE_HOLDINGS_POSITION));
assertNull(modifiedRow.get(BINDING_HOLDINGS_POSITION));
assertNull(modifiedRow.get(OTHER_HOLDINGS_POSITION));
}

@Test
void shouldAddStaffOnlyPostfixWhenRequiredForHoldingsNotes() {
var table = UnifiedTableHeaderBuilder.getEmptyTableWithHeaders(Item.class);
var row = Arrays.stream(new String[table.getHeader().size()]).collect(Collectors.toCollection(ArrayList::new));
row.set(HOLDINGS_NOTE_POSITION, "Action note;Action note text;true|Note;Note text;false|Binding;Binding text;true");
row.set(HOLDINGS_NOTE_POSITION, "Action note;Action note text;true|Note;Note text;false|Other;Other text;true");
table.setRows(List.of(new Row().row(row)));

var expectedTableSize = table.getHeader().size() + 2;

when(holdingsReferenceService.getAllHoldingsNoteTypes())
.thenReturn(List.of(HoldingsNoteType.builder().name("Action note").build(),
HoldingsNoteType.builder().name("Note").build(),
HoldingsNoteType.builder().name("Binding").build()));
HoldingsNoteType.builder().name("Other").build()));

noteTableUpdater.extendTableWithHoldingsNotesTypes(table);

assertThat(table.getHeader(), hasSize(expectedTableSize));
var headerNames = table.getHeader().stream().map(Cell::getValue).toList();
assertThat(headerNames.indexOf("Action note"), is(ACTION_HOLDINGS_NOTE_POSITION));
assertThat(headerNames.indexOf("Note"), is(NOTE_HOLDINGS_POSITION));
assertThat(headerNames.indexOf("Binding"), is(BINDING_HOLDINGS_POSITION));
assertThat(headerNames.indexOf("Other"), is(OTHER_HOLDINGS_POSITION));

var modifiedRow = table.getRows().get(0).getRow();
assertThat(modifiedRow, hasSize(expectedTableSize));
assertTrue(modifiedRow.get(ACTION_HOLDINGS_NOTE_POSITION).contains(STAFF_ONLY));
assertFalse(modifiedRow.get(NOTE_HOLDINGS_POSITION).contains(STAFF_ONLY));
assertTrue(modifiedRow.get(BINDING_HOLDINGS_POSITION).contains(STAFF_ONLY));
assertTrue(modifiedRow.get(OTHER_HOLDINGS_POSITION).contains(STAFF_ONLY));
}

@Test
void shouldAddNotePostfixWhenRequiredForHoldingsNoteTypeNames() {
var table = UnifiedTableHeaderBuilder.getEmptyTableWithHeaders(HoldingsRecord.class);
var row = Arrays.stream(new String[table.getHeader().size()]).collect(Collectors.toCollection(ArrayList::new));
table.setRows(List.of(new Row().row(row)));

when(holdingsReferenceService.getAllHoldingsNoteTypes())
.thenReturn(List.of(HoldingsNoteType.builder().name("Binding").build(),
HoldingsNoteType.builder().name("Electronic bookplate").build(),
HoldingsNoteType.builder().name("Provenance").build(),
HoldingsNoteType.builder().name("Reproduction").build()));

noteTableUpdater.extendTableWithHoldingsNotesTypes(table);

var headerNames = table.getHeader().stream().map(Cell::getValue).toList();
List.of("Binding", "Electronic bookplate", "Provenance", "Reproduction").forEach(name ->
assertTrue(headerNames.contains(name + " note")));
}

@Test
void shouldAddNotePostfixWhenRequiredForItemNoteTypeNames() {
var table = UnifiedTableHeaderBuilder.getEmptyTableWithHeaders(Item.class);
var row = Arrays.stream(new String[table.getHeader().size()]).collect(Collectors.toCollection(ArrayList::new));
table.setRows(List.of(new Row().row(row)));

when(itemReferenceService.getAllItemNoteTypes())
.thenReturn(List.of(NoteType.builder().name("Binding").build(),
NoteType.builder().name("Electronic bookplate").build(),
NoteType.builder().name("Provenance").build(),
NoteType.builder().name("Reproduction").build()));

noteTableUpdater.extendTableWithItemNotesTypes(table);

var headerNames = table.getHeader().stream().map(Cell::getValue).toList();
List.of("Binding", "Electronic bookplate", "Provenance", "Reproduction").forEach(name ->
assertTrue(headerNames.contains(name + " note")));
}
}

0 comments on commit 247fb9b

Please sign in to comment.