Skip to content

Commit

Permalink
MODLD-540: API to support new Inventory to LDE workflow (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
askhat-abishev authored Oct 18, 2024
1 parent 88ee5a9 commit 49e786e
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 0 deletions.
11 changes: 11 additions & 0 deletions descriptors/ModuleDescriptor-template.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@
"pathPattern": "/resource/check/{inventoryId}/supported",
"permissionsRequired": [ "linked-data.resources.support-check.get" ],
"modulePermissions": ["source-storage.records.get"]
},
{
"methods": [ "GET" ],
"pathPattern": "/resource/preview/{inventoryId}",
"permissionsRequired": [ "linked-data.resources.preview.get" ],
"modulePermissions": ["source-storage.records.get"]
}
]
},
Expand Down Expand Up @@ -177,6 +183,11 @@
"displayName": "Linked Data: Check if marc to bib conversion is supported",
"description": "Check if marc to bib conversion is supported by mod-linked-data"
},
{
"permissionName": "linked-data.resources.preview.get",
"displayName": "Linked Data: Get the preview of a resource",
"description": "Get the preview of a linked-data resource"
},
{
"permissionName": "linked-data.profiles.get",
"displayName": "Linked Data: Get the profiles for performing CRUD operations on resources",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ public ResponseEntity<String> isSupportedByInventoryId(String inventoryId) {
return ResponseEntity.ok(resourceMarcService.isSupportedByInventoryId(inventoryId).toString());
}

@Override
public ResponseEntity<ResourceResponseDto> getResourcePreviewByInventoryId(String inventoryId) {
return ResponseEntity.ok(resourceMarcService.getResourcePreviewByInventoryId(inventoryId));
}

@Override
public ResponseEntity<ResourceResponseDto> updateResource(Long id, String okapiTenant,
@Valid ResourceRequestDto resourceDto) {
Expand All @@ -55,4 +60,5 @@ public ResponseEntity<Void> deleteResource(Long id, String okapiTenant) {
public ResponseEntity<ResourceMarcViewDto> getResourceMarcViewById(Long id, String okapiTenant) {
return ResponseEntity.ok(resourceMarcService.getResourceMarcView(id));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.folio.ld.dictionary.model.Resource;
import org.folio.linked.data.domain.dto.ResourceMarcViewDto;
import org.folio.linked.data.domain.dto.ResourceResponseDto;

public interface ResourceMarcService {

Expand All @@ -11,4 +12,6 @@ public interface ResourceMarcService {

Boolean isSupportedByInventoryId(String inventoryId);

ResourceResponseDto getResourcePreviewByInventoryId(String inventoryId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
import java.util.function.Function;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.log4j.Log4j2;
import org.folio.linked.data.client.SrsClient;
import org.folio.linked.data.domain.dto.ResourceMarcViewDto;
import org.folio.linked.data.domain.dto.ResourceResponseDto;
import org.folio.linked.data.exception.NotFoundException;
import org.folio.linked.data.exception.ValidationException;
import org.folio.linked.data.mapper.ResourceModelMapper;
Expand All @@ -35,6 +37,8 @@
import org.folio.linked.data.repo.ResourceEdgeRepository;
import org.folio.linked.data.repo.ResourceRepository;
import org.folio.marc4ld.service.ld2marc.Bibframe2MarcMapper;
import org.folio.marc4ld.service.marc2ld.bib.MarcBib2ldMapper;
import org.folio.rest.jaxrs.model.ParsedRecord;
import org.folio.rest.jaxrs.model.Record;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
Expand All @@ -52,6 +56,7 @@ public class ResourceMarcServiceImpl implements ResourceMarcService {
private final ResourceDtoMapper resourceDtoMapper;
private final ResourceModelMapper resourceModelMapper;
private final Bibframe2MarcMapper bibframe2MarcMapper;
private final MarcBib2ldMapper marcBib2ldMapper;
private final ResourceGraphService resourceGraphService;
private final FolioMetadataRepository folioMetadataRepository;
private final ApplicationEventPublisher applicationEventPublisher;
Expand Down Expand Up @@ -91,6 +96,19 @@ public Boolean isSupportedByInventoryId(String inventoryId) {
.orElse(false);
}

@Override
public ResourceResponseDto getResourcePreviewByInventoryId(String inventoryId) {
var response = srsClient.getFormattedSourceStorageInstanceRecordById(inventoryId);
return Optional.ofNullable(response.getBody())
.map(Record::getParsedRecord)
.map(ParsedRecord::getContent)
.map(this::toJsonString)
.flatMap(marcBib2ldMapper::fromMarcJson)
.map(resourceModelMapper::toEntity)
.map(resourceDtoMapper::toDto)
.orElse(null);
}

private void validateMarkViewSupportedType(Resource resource) {
if (resource.isOfType(INSTANCE)) {
return;
Expand Down Expand Up @@ -205,4 +223,9 @@ private void addOutgoingEdges(Resource resource) {
private boolean isMonograph(String leader) {
return isLanguageMaterial(leader.charAt(6)) && isMonographicComponentPartOrItem(leader.charAt(7));
}

@SneakyThrows
private String toJsonString(Object content) {
return objectMapper.writeValueAsString(content);
}
}
16 changes: 16 additions & 0 deletions src/main/resources/swagger.api/mod-linked-data.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,22 @@ paths:
'500':
$ref: '#/components/responses/internalServerErrorResponse'

/resource/preview/{inventoryId}:
get:
operationId: getResourcePreviewByInventoryId
description: Get the preview of a resource
parameters:
- $ref: '#/components/parameters/inventoryId'
responses:
'200':
description: Resource as json string
content:
application/json:
schema:
$ref: schema/resourceResponseDto.json
'500':
$ref: '#/components/responses/internalServerErrorResponse'

/profile:
get:
operationId: getProfile
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,22 @@
import static org.folio.linked.data.test.TestUtil.OBJECT_MAPPER;
import static org.folio.linked.data.test.TestUtil.random;
import static org.folio.linked.data.test.TestUtil.randomLong;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import org.folio.ld.dictionary.model.FolioMetadata;
import org.folio.linked.data.client.SrsClient;
import org.folio.linked.data.domain.dto.ResourceMarcViewDto;
import org.folio.linked.data.domain.dto.ResourceResponseDto;
import org.folio.linked.data.exception.NotFoundException;
import org.folio.linked.data.exception.ValidationException;
import org.folio.linked.data.mapper.ResourceModelMapper;
Expand All @@ -36,6 +39,7 @@
import org.folio.linked.data.repo.ResourceEdgeRepository;
import org.folio.linked.data.repo.ResourceRepository;
import org.folio.marc4ld.service.ld2marc.Bibframe2MarcMapper;
import org.folio.marc4ld.service.marc2ld.bib.MarcBib2ldMapper;
import org.folio.rest.jaxrs.model.ParsedRecord;
import org.folio.rest.jaxrs.model.Record;
import org.folio.spring.testing.type.UnitTest;
Expand Down Expand Up @@ -71,6 +75,8 @@ class ResourceMarcServiceTest {
@Mock
private Bibframe2MarcMapper bibframe2MarcMapper;
@Mock
private MarcBib2ldMapper marcBib2ldMapper;
@Mock
private ApplicationEventPublisher applicationEventPublisher;
@Mock
private ResourceGraphService resourceGraphService;
Expand Down Expand Up @@ -318,6 +324,29 @@ void isSupportedByInventoryId_shouldReturnFalse(char type, char level) {
assertFalse(resourceMarcService.isSupportedByInventoryId(inventoryId));
}

@Test
void getResourcePreviewByInventoryId_shouldReturn_resourceResponseDto() throws JsonProcessingException {
//given
var inventoryId = UUID.randomUUID().toString();
var marcRecord = createRecord('a', 'm');
var marcJson = "";
var resourceModel = new org.folio.ld.dictionary.model.Resource();
var resourceEntity = new Resource();
var resourceDto = new ResourceResponseDto();
when(srsClient.getFormattedSourceStorageInstanceRecordById(inventoryId))
.thenReturn(new ResponseEntity<>(marcRecord, HttpStatusCode.valueOf(200)));
when(objectMapper.writeValueAsString(marcRecord.getParsedRecord().getContent())).thenReturn(marcJson);
when(marcBib2ldMapper.fromMarcJson(marcJson)).thenReturn(Optional.of(resourceModel));
when(resourceModelMapper.toEntity(resourceModel)).thenReturn(resourceEntity);
when(resourceDtoMapper.toDto(resourceEntity)).thenReturn(resourceDto);

//when
var result = resourceMarcService.getResourcePreviewByInventoryId(inventoryId);

//then
assertEquals(resourceDto, result);
}

private org.folio.rest.jaxrs.model.Record createRecord(char type, char level) {
var leader = "04809n a2200865 i 4500";
leader = leader.substring(0, 6) + type + level + leader.substring(8);
Expand Down

0 comments on commit 49e786e

Please sign in to comment.