Skip to content

Commit

Permalink
MODBULKOPS-329 - Include tenantId in Item's and Holdings' Notes names…
Browse files Browse the repository at this point in the history
… in ECS (#259)

* MODBULKOPS-329 Added tenant
  • Loading branch information
obozhko-folio authored Sep 4, 2024
1 parent a183711 commit 6e84815
Show file tree
Hide file tree
Showing 28 changed files with 356 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public ResponseEntity<Resource> downloadFileByOperationId(
is.readAllBytes();
var entityType = bulkOperation.getEntityType().getValue();
if (isDownloadPreview(fileContentType) && SPLIT_NOTE_ENTITIES.contains(entityType)) {
content = noteProcessorFactory.getNoteProcessor(entityType).processNotes(content);
content = noteProcessorFactory.getNoteProcessor(entityType).processNotes(content, bulkOperation);
}
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,7 @@ default BulkOperationsEntity getRecordBulkOperationEntity() {
default String getTenant() {
return StringUtils.EMPTY;
}
@JsonIgnore
default void setTenantToNotes(){
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,9 @@ public String getTenant() {
public List<ElectronicAccess> getElectronicAccess() {
return entity.getElectronicAccess();
}

@Override
public void setTenantToNotes() {
entity.getNotes().forEach(note -> note.setTenantId(tenantId));
}
}
5 changes: 5 additions & 0 deletions src/main/java/org/folio/bulkops/domain/bean/ExtendedItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,9 @@ public BulkOperationsEntity getRecordBulkOperationEntity() {
public String getTenant() {
return tenantId;
}

@Override
public void setTenantToNotes() {
entity.getNotes().forEach(note -> note.setTenantId(tenantId));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ public class HoldingsNote {

@JsonProperty("staffOnly")
private Boolean staffOnly;
private String tenantId;
}

Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ public class HoldingsNoteType {

@JsonProperty("metadata")
private Metadata metadata;
private String tenantId;
}

1 change: 0 additions & 1 deletion src/main/java/org/folio/bulkops/domain/bean/Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.folio.bulkops.domain.converter.DamagedStatusConverter;
import org.folio.bulkops.domain.converter.DateWithoutTimeConverter;
import org.folio.bulkops.domain.converter.EffectiveCallNumberComponentsConverter;
import org.folio.bulkops.domain.converter.ElectronicAccessListConverter;
import org.folio.bulkops.domain.converter.ItemElectronicAccessListConverter;
import org.folio.bulkops.domain.converter.ItemLocationConverter;
import org.folio.bulkops.domain.converter.ItemNoteListConverter;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/folio/bulkops/domain/bean/ItemNote.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ public class ItemNote {

@JsonProperty("staffOnly")
private Boolean staffOnly = false;
private String tenantId;
}

1 change: 1 addition & 0 deletions src/main/java/org/folio/bulkops/domain/bean/NoteType.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ public class NoteType {

@JsonProperty("metadata")
private Metadata metadata;
private String tenantId;
}

Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ public String convertToString(List<HoldingsNote> object) {
.map(note -> String.join(ARRAY_DELIMITER,
escape(HoldingsReferenceHelper.service().getNoteTypeNameById(note.getHoldingsNoteTypeId())),
escape(note.getNote()),
booleanToStringNullSafe(note.getStaffOnly())))
booleanToStringNullSafe(note.getStaffOnly()),
note.getTenantId(),
note.getHoldingsNoteTypeId()))
.collect(Collectors.joining(ITEM_DELIMITER));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.folio.bulkops.domain.entity;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

import io.hypersistence.utils.hibernate.type.json.JsonBinaryType;
import jakarta.persistence.Column;
import org.folio.bulkops.domain.converter.PostgresUUIDConverter;
import org.folio.bulkops.domain.dto.ApproachType;
Expand All @@ -21,6 +23,8 @@
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.folio.bulkops.domain.dto.TenantNotePair;
import org.hibernate.annotations.Type;

@Data
@Builder
Expand Down Expand Up @@ -83,4 +87,9 @@ public class BulkOperation {
private UUID fqlQueryId;
private String fqlQuery;
private String userFriendlyQuery;

@Type(JsonBinaryType.class)
@Column(columnDefinition = "jsonb[]")
private List<TenantNotePair> tenantNotePairs;
private List<String> usedTenants;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import com.opencsv.CSVReaderBuilder;
import com.opencsv.RFC4180ParserBuilder;
import lombok.extern.log4j.Log4j2;
import org.folio.bulkops.domain.entity.BulkOperation;
import org.folio.bulkops.service.NoteTableUpdater;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;

import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
Expand All @@ -21,15 +23,21 @@ public abstract class AbstractNoteProcessor {

private static final int FIRST_LINE = 1;

private NoteTableUpdater noteTableUpdater;
protected NoteTableUpdater noteTableUpdater;
protected CacheManager cacheManager;

@Autowired
private void setNoteTableUpdater(NoteTableUpdater noteTableUpdater) {
this.noteTableUpdater = noteTableUpdater;
}

public byte[] processNotes(byte[] input) {
List<String> noteTypeNames = getNoteTypeNames();
@Autowired
private void setCacheManager(CacheManager cacheManager) {
this.cacheManager = cacheManager;
}

public byte[] processNotes(byte[] input, BulkOperation bulkOperation) {
List<String> noteTypeNames = getNoteTypeNames(bulkOperation);
var noteTypeHeaders = noteTypeNames.stream()
.map(noteTableUpdater::concatNotePostfixIfRequired)
.toList();
Expand All @@ -47,7 +55,7 @@ public byte[] processNotes(byte[] input) {
.map(this::processSpecialCharacters)
.toArray(String[]::new);
} else {
line = processNotesData(line, noteTypeNames);
line = processNotesData(line, noteTypeNames, bulkOperation);
}
stringWriter.write(String.join(",", line) + "\n");
}
Expand All @@ -59,12 +67,12 @@ public byte[] processNotes(byte[] input) {
}
}

protected abstract List<String> getNoteTypeNames();
protected abstract List<String> getNoteTypeNames(BulkOperation bulkOperation);

protected abstract int getNotePosition();

private String[] processNotesData(String[] line, List<String> noteTypeNames) {
return noteTableUpdater.enrichWithNotesByType(new ArrayList<>(Arrays.asList(line)), getNotePosition(), noteTypeNames).stream()
private String[] processNotesData(String[] line, List<String> noteTypeNames, BulkOperation bulkOperation) {
return noteTableUpdater.enrichWithNotesByType(new ArrayList<>(Arrays.asList(line)), getNotePosition(), noteTypeNames, bulkOperation.getTenantNotePairs()).stream()
.map(this::processSpecialCharacters)
.toArray(String[]::new);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,23 @@
import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.folio.bulkops.domain.bean.HoldingsNoteType;
import org.folio.bulkops.domain.bean.NoteType;
import org.folio.bulkops.domain.entity.BulkOperation;
import org.folio.bulkops.service.ConsortiaService;
import org.folio.bulkops.service.HoldingsReferenceService;
import org.folio.spring.FolioExecutionContext;
import org.folio.spring.FolioModuleMetadata;
import org.folio.spring.scope.FolioExecutionContextSetter;
import org.springframework.cache.Cache;
import org.springframework.stereotype.Component;

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

import static java.util.Optional.ofNullable;
import static org.folio.bulkops.util.Constants.HOLDINGS_NOTE_POSITION;
import static org.folio.bulkops.util.FolioExecutionContextUtil.prepareContextForTenant;

Expand All @@ -28,22 +34,26 @@ public class HoldingsNotesProcessor extends AbstractNoteProcessor {
private final ConsortiaService consortiaService;

@Override
public List<String> getNoteTypeNames() {
public List<String> getNoteTypeNames(BulkOperation bulkOperation) {
var noteTypeNamesSet = new HashSet<>(holdingsReferenceService.getAllHoldingsNoteTypes(folioExecutionContext.getTenantId()).stream()
.map(HoldingsNoteType::getName)
.filter(Objects::nonNull)
.toList());
if (consortiaService.isCurrentTenantCentralTenant(folioExecutionContext.getTenantId())) {
var userTenants = consortiaService.getAffiliatedTenants(folioExecutionContext.getTenantId(), folioExecutionContext.getUserId().toString());
for (var userTenant : userTenants) {
try (var ignored = new FolioExecutionContextSetter(prepareContextForTenant(userTenant, folioModuleMetadata, folioExecutionContext))) {
var noteTypesFromMember = holdingsReferenceService.getAllHoldingsNoteTypes(userTenant).stream()
.map(HoldingsNoteType::getName)
.filter(Objects::nonNull)
.toList();
noteTypeNamesSet.addAll(noteTypesFromMember);
noteTypeNamesSet.clear();
List<HoldingsNoteType> noteTypesFromUsedTenants = new ArrayList<>();
var usedTenants = bulkOperation.getUsedTenants();
for (var usedTenant : usedTenants) {
try (var ignored = new FolioExecutionContextSetter(prepareContextForTenant(usedTenant, folioModuleMetadata, folioExecutionContext))) {
var noteTypesFromUsedTenant = holdingsReferenceService.getAllHoldingsNoteTypes(usedTenant);
ofNullable(cacheManager.getCache("holdingsNoteTypes")).ifPresent(Cache::invalidate);
noteTypesFromUsedTenants.addAll(noteTypesFromUsedTenant);
}
}
var noteTypes = noteTypesFromUsedTenants.stream().map(note -> new NoteType().withName(note.getName())
.withTenantId(note.getTenantId()).withId(note.getId())).toList();
noteTableUpdater.updateNoteTypeNamesWithTenants(noteTypes);
noteTypeNamesSet.addAll(noteTypes.stream().map(NoteType::getName).collect(Collectors.toSet()));
}
return noteTypeNamesSet.stream().sorted().toList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@
import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.folio.bulkops.domain.bean.NoteType;
import org.folio.bulkops.domain.entity.BulkOperation;
import org.folio.bulkops.service.ConsortiaService;
import org.folio.bulkops.service.ItemReferenceService;
import org.folio.spring.FolioExecutionContext;
import org.folio.spring.FolioModuleMetadata;
import org.folio.spring.scope.FolioExecutionContextSetter;
import org.springframework.cache.Cache;
import org.springframework.stereotype.Component;

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

import static java.util.Optional.ofNullable;
import static org.folio.bulkops.util.Constants.ITEM_NOTE_POSITION;
import static org.folio.bulkops.util.FolioExecutionContextUtil.prepareContextForTenant;

Expand All @@ -28,22 +33,24 @@ public class ItemNoteProcessor extends AbstractNoteProcessor {
private final ConsortiaService consortiaService;

@Override
public List<String> getNoteTypeNames() {
public List<String> getNoteTypeNames(BulkOperation bulkOperation) {
var noteTypeNamesSet = new HashSet<>(itemReferenceService.getAllItemNoteTypes(folioExecutionContext.getTenantId()).stream()
.map(NoteType::getName)
.filter(Objects::nonNull)
.toList());
if (consortiaService.isCurrentTenantCentralTenant(folioExecutionContext.getTenantId())) {
var userTenants = consortiaService.getAffiliatedTenants(folioExecutionContext.getTenantId(), folioExecutionContext.getUserId().toString());
for (var userTenant : userTenants) {
try (var ignored = new FolioExecutionContextSetter(prepareContextForTenant(userTenant, folioModuleMetadata, folioExecutionContext))) {
var noteTypesFromMember = itemReferenceService.getAllItemNoteTypes(userTenant).stream()
.map(NoteType::getName)
.filter(Objects::nonNull)
.toList();
noteTypeNamesSet.addAll(noteTypesFromMember);
noteTypeNamesSet.clear();
List<NoteType> noteTypesFromUsedTenants = new ArrayList<>();
var usedTenants = bulkOperation.getUsedTenants();
for (var usedTenant : usedTenants) {
try (var ignored = new FolioExecutionContextSetter(prepareContextForTenant(usedTenant, folioModuleMetadata, folioExecutionContext))) {
var noteTypesFromUsedTenant = itemReferenceService.getAllItemNoteTypes(usedTenant);
ofNullable(cacheManager.getCache("itemNoteTypes")).ifPresent(Cache::invalidate);
noteTypesFromUsedTenants.addAll(noteTypesFromUsedTenant);
}
}
noteTableUpdater.updateNoteTypeNamesWithTenants(noteTypesFromUsedTenants);
noteTypeNamesSet.addAll(noteTypesFromUsedTenants.stream().map(NoteType::getName).collect(Collectors.toSet()));
}
return noteTypeNamesSet.stream().sorted().toList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ public void confirm(BulkOperation operation) {
} else {
var tenantIdOfEntity = modified.getPreview().getTenant();
try (var ignored = new FolioExecutionContextSetter(prepareContextForTenant(tenantIdOfEntity, folioModuleMetadata, folioExecutionContext))) {
modified.getPreview().setTenantToNotes();
writeToCsv(operation, csvWriter, modified.getPreview().getRecordBulkOperationEntity());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ public StatisticalCode getStatisticalCodeByName(String name, String tenantId) {

@Cacheable(cacheNames = "holdingsNoteTypes")
public List<HoldingsNoteType> getAllHoldingsNoteTypes(String tenantId) {
return holdingsNoteTypeClient.getNoteTypes(Integer.MAX_VALUE).getHoldingsNoteTypes();
var noteTypes = holdingsNoteTypeClient.getNoteTypes(Integer.MAX_VALUE).getHoldingsNoteTypes();
noteTypes.forEach(nt -> nt.setTenantId(tenantId));
return noteTypes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ public List<String> getAllowedStatuses(String statusName) {

@Cacheable(cacheNames = "itemNoteTypes")
public List<NoteType> getAllItemNoteTypes(String tenantId) {
return itemNoteTypeClient.getNoteTypes(Integer.MAX_VALUE).getItemNoteTypes();
var noteTypes = itemNoteTypeClient.getNoteTypes(Integer.MAX_VALUE).getItemNoteTypes();
noteTypes.forEach(noteType -> noteType.setTenantId(tenantId));
return noteTypes;
}
}
Loading

0 comments on commit 6e84815

Please sign in to comment.