Skip to content

Commit

Permalink
MODLD-353: Update API to support language (#285)
Browse files Browse the repository at this point in the history
* MODLD-353: Update API to support language

* MODLD-353: fix review remark
  • Loading branch information
askhat-abishev authored Jul 2, 2024
1 parent 1d8bdaa commit cb69650
Show file tree
Hide file tree
Showing 18 changed files with 165 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static java.util.Objects.nonNull;
import static java.util.Optional.ofNullable;
import static org.apache.commons.collections4.CollectionUtils.isNotEmpty;
import static org.folio.ld.dictionary.PropertyDictionary.LANGUAGE;
import static org.folio.ld.dictionary.PropertyDictionary.TARGET_AUDIENCE;

import com.fasterxml.jackson.core.JsonProcessingException;
Expand Down Expand Up @@ -84,9 +85,10 @@ private <T> T readDoc(JsonNode node, Class<T> dtoClass) {
try {
if (nonNull(node)) {
if (dtoClass == WorkResponse.class) {
// Temp fix - targetAudience loaded through the Python ETL have targetAudience in text format
// causing the deserialization to fail. Here remove the targetAudience from node
// Temp fix - below properties loaded through the Python ETL have values in text format
// causing the deserialization to fail. Here remove such properties from node
((ObjectNode) node).remove(TARGET_AUDIENCE.getValue());
((ObjectNode) node).remove(LANGUAGE.getValue());
}
return jsonMapper.treeToValue(node, dtoClass);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public abstract class CategoryMapperUnit implements SingleResourceMapperUnit, Ma
WorkResponse.class
);

protected final HashService hashService;
private final CoreMapper coreMapper;
private final HashService hashService;

protected abstract void addToParent(CategoryResponse category, Object parentDto);

Expand Down Expand Up @@ -73,6 +73,15 @@ public Set<Class<?>> supportedParents() {

protected abstract String getCategorySetLink();

protected JsonNode getDoc(Category dto) {
var map = new HashMap<String, List<String>>();
putProperty(map, CODE, getMarcCodes(dto.getLink()));
putProperty(map, TERM, dto.getTerm());
putProperty(map, LINK, dto.getLink());
putProperty(map, SOURCE, dto.getSource());
return map.isEmpty() ? null : coreMapper.toJson(map);
}

private Resource getCategorySet() {
var label = getCategorySetLabel();
var link = getCategorySetLink();
Expand All @@ -86,13 +95,4 @@ private Resource getCategorySet() {
categorySet.setId(hashService.hash(categorySet));
return categorySet;
}

private JsonNode getDoc(Category dto) {
var map = new HashMap<String, List<String>>();
putProperty(map, CODE, getMarcCodes(dto.getLink()));
putProperty(map, TERM, dto.getTerm());
putProperty(map, LINK, dto.getLink());
putProperty(map, SOURCE, dto.getSource());
return map.isEmpty() ? null : coreMapper.toJson(map);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class CarrierMapperUnit extends CategoryMapperUnit {
private static final String CARRIER_TYPE_LINK_PREFIX = "http://id.loc.gov/vocabulary/carriers/";

public CarrierMapperUnit(CoreMapper coreMapper, HashService hashService) {
super(coreMapper, hashService);
super(hashService, coreMapper);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class MediaMapperUnit extends CategoryMapperUnit {
private static final String MEDIA_TYPE_LINK_PREFIX = "http://id.loc.gov/vocabulary/mediaTypes/";

public MediaMapperUnit(CoreMapper coreMapper, HashService hashService) {
super(coreMapper, hashService);
super(hashService, coreMapper);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
import static org.folio.ld.dictionary.PredicateDictionary.GEOGRAPHIC_COVERAGE;
import static org.folio.ld.dictionary.PredicateDictionary.GOVERNMENT_PUBLICATION;
import static org.folio.ld.dictionary.PredicateDictionary.INSTANTIATES;
import static org.folio.ld.dictionary.PredicateDictionary.LANGUAGE;
import static org.folio.ld.dictionary.PredicateDictionary.ORIGIN_PLACE;
import static org.folio.ld.dictionary.PredicateDictionary.SUBJECT;
import static org.folio.ld.dictionary.PredicateDictionary.TARGET_AUDIENCE;
import static org.folio.ld.dictionary.PredicateDictionary.TITLE;
import static org.folio.ld.dictionary.PropertyDictionary.BIBLIOGRAPHY_NOTE;
import static org.folio.ld.dictionary.PropertyDictionary.DATE_END;
import static org.folio.ld.dictionary.PropertyDictionary.DATE_START;
import static org.folio.ld.dictionary.PropertyDictionary.LANGUAGE;
import static org.folio.ld.dictionary.PropertyDictionary.LANGUAGE_NOTE;
import static org.folio.ld.dictionary.PropertyDictionary.NOTE;
import static org.folio.ld.dictionary.PropertyDictionary.SUMMARY;
Expand Down Expand Up @@ -86,14 +86,14 @@ public Resource toEntity(Object dto, Resource parentEntity) {
coreMapper.addOutgoingEdges(work, WorkRequest.class, workDto.getOriginPlace(), ORIGIN_PLACE);
coreMapper.addOutgoingEdges(work, WorkRequest.class, workDto.getDissertation(), DISSERTATION);
coreMapper.addOutgoingEdges(work, WorkRequest.class, workDto.getTargetAudience(), TARGET_AUDIENCE);
coreMapper.addOutgoingEdges(work, WorkRequest.class, workDto.getLanguage(), LANGUAGE);
coreMapper.addIncomingEdges(work, WorkRequest.class, workDto.getInstanceReference(), INSTANTIATES);
work.setId(hashService.hash(work));
return work;
}

private JsonNode getDoc(WorkRequest dto) {
var map = new HashMap<String, List<String>>();
putProperty(map, LANGUAGE, dto.getLanguage());
putProperty(map, SUMMARY, dto.getSummary());
putProperty(map, TABLE_OF_CONTENTS, dto.getTableOfContents());
putProperty(map, DATE_START, dto.getDateStart());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class ContentMapperUnit extends CategoryMapperUnit {
private static final String CONTENT_TYPE_LINK_PREFIX = "http://id.loc.gov/vocabulary/contentTypes/";

public ContentMapperUnit(CoreMapper coreMapper, HashService hashService) {
super(coreMapper, hashService);
super(hashService, coreMapper);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class GovernmentPublicationMapperUnit extends CategoryMapperUnit {
private static final String MARC_CODE_O = "o";

public GovernmentPublicationMapperUnit(CoreMapper coreMapper, HashService hashService) {
super(coreMapper, hashService);
super(hashService, coreMapper);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.folio.linked.data.mapper.dto.monograph.work.sub;

import static org.apache.commons.lang3.StringUtils.EMPTY;
import static org.folio.ld.dictionary.PredicateDictionary.LANGUAGE;
import static org.folio.ld.dictionary.ResourceTypeDictionary.LANGUAGE_CATEGORY;
import static org.folio.linked.data.util.BibframeUtils.getFirstValue;

import org.folio.linked.data.domain.dto.Category;
import org.folio.linked.data.domain.dto.CategoryResponse;
import org.folio.linked.data.domain.dto.WorkResponse;
import org.folio.linked.data.mapper.dto.common.CoreMapper;
import org.folio.linked.data.mapper.dto.common.MapperUnit;
import org.folio.linked.data.mapper.dto.monograph.common.CategoryMapperUnit;
import org.folio.linked.data.model.entity.Resource;
import org.folio.linked.data.service.HashService;
import org.springframework.stereotype.Component;

@Component
@MapperUnit(type = LANGUAGE_CATEGORY, predicate = LANGUAGE, requestDto = Category.class)
public class LanguageMapperUnit extends CategoryMapperUnit {

private static final String LANGUAGE_LINK_PREFIX = "http://id.loc.gov/vocabulary/languages";

public LanguageMapperUnit(CoreMapper coreMapper, HashService hashService) {
super(hashService, coreMapper);
}

@Override
protected void addToParent(CategoryResponse category, Object parentDto) {
if (parentDto instanceof WorkResponse work) {
work.addLanguageItem(category);
}
}

@Override
protected String getCategorySetLabel() {
return EMPTY;
}

@Override
protected String getCategorySetLink() {
return EMPTY;
}

@Override
public String getLinkPrefix() {
return LANGUAGE_LINK_PREFIX;
}

@Override
public Resource toEntity(Object dto, Resource parentEntity) {
var languageCategory = (Category) dto;
var resource = new Resource();
resource.setLabel(getFirstValue(() -> getMarcCodes(languageCategory.getLink())));
resource.addTypes(LANGUAGE_CATEGORY);
resource.setDoc(getDoc(languageCategory));
resource.setId(hashService.hash(resource));
return resource;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class TargetAudienceMapperUnit extends CategoryMapperUnit {


public TargetAudienceMapperUnit(CoreMapper coreMapper, HashService hashService) {
super(coreMapper, hashService);
super(hashService, coreMapper);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import static org.folio.ld.dictionary.PredicateDictionary.CONTRIBUTOR;
import static org.folio.ld.dictionary.PredicateDictionary.CREATOR;
import static org.folio.ld.dictionary.PredicateDictionary.INSTANTIATES;
import static org.folio.ld.dictionary.PredicateDictionary.LANGUAGE;
import static org.folio.ld.dictionary.PredicateDictionary.MAP;
import static org.folio.ld.dictionary.PredicateDictionary.PE_PUBLICATION;
import static org.folio.ld.dictionary.PredicateDictionary.SUBJECT;
Expand All @@ -20,7 +21,6 @@
import static org.folio.ld.dictionary.PropertyDictionary.DATE;
import static org.folio.ld.dictionary.PropertyDictionary.EAN_VALUE;
import static org.folio.ld.dictionary.PropertyDictionary.EDITION_STATEMENT;
import static org.folio.ld.dictionary.PropertyDictionary.LANGUAGE;
import static org.folio.ld.dictionary.PropertyDictionary.LOCAL_ID_VALUE;
import static org.folio.ld.dictionary.PropertyDictionary.MAIN_TITLE;
import static org.folio.ld.dictionary.PropertyDictionary.NAME;
Expand Down Expand Up @@ -195,8 +195,13 @@ private List<BibframeContributorsInner> extractContributors(Resource resource) {
}

private List<BibframeLanguagesInner> extractLanguages(Resource work) {
return getPropertyValues(work.getDoc(), LANGUAGE.getValue())
.map(p -> new BibframeLanguagesInner().value(p))
return work.getOutgoingEdges()
.stream()
.filter(re -> LANGUAGE.getUri().equals(re.getPredicate().getUri()))
.map(ResourceEdge::getTarget)
.map(Resource::getDoc)
.flatMap(d -> getPropertyValues(d, CODE.getValue()))
.map(pv -> new BibframeLanguagesInner().value(pv))
.toList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"language": {
"type": "array",
"items": {
"type": "string"
"type": "object",
"$ref": "../common/Category.json"
},
"x-json-property": "http://bibfra.me/vocab/lite/language"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"language": {
"type": "array",
"items": {
"type": "string"
"type": "object",
"$ref": "CategoryResponse.json"
},
"x-json-property": "http://bibfra.me/vocab/lite/language"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import lombok.SneakyThrows;
import org.folio.linked.data.e2e.base.IntegrationTest;
import org.folio.linked.data.model.entity.Resource;
import org.folio.linked.data.repo.ResourceEdgeRepository;
import org.folio.linked.data.repo.ResourceRepository;
import org.folio.linked.data.test.kafka.KafkaSearchIndexTopicListener;
import org.folio.spring.tools.kafka.KafkaAdminService;
Expand All @@ -41,6 +42,8 @@ class ReIndexControllerFolioIT {
@Autowired
private ResourceRepository resourceRepo;
@Autowired
private ResourceEdgeRepository resourceEdgeRepository;
@Autowired
private Environment env;
@Autowired
private KafkaSearchIndexTopicListener consumer;
Expand All @@ -58,6 +61,9 @@ void indexResourceWithNoIndexDate_andNotFullIndexRequest() throws Exception {
var anotherWork = getSampleWork(instance);
anotherWork.setId(randomLong());
resourceRepo.save(anotherWork);
var resourceEdge = work.getIncomingEdges().iterator().next();
resourceEdge.computeId();
resourceEdgeRepository.save(resourceEdge);

var requestBuilder = put(INDEX_URL)
.contentType(APPLICATION_JSON)
Expand Down Expand Up @@ -94,6 +100,9 @@ void indexResourceWithNoIndexDate_andFullIndexRequest() throws Exception {
// given
var work = resourceRepo.save(getSampleWork(null));
resourceRepo.save(getSampleInstanceResource(null, work));
var resourceEdge = work.getIncomingEdges().iterator().next();
resourceEdge.computeId();
resourceEdgeRepository.save(resourceEdge);

var requestBuilder = put(INDEX_URL)
.param("full", "true")
Expand All @@ -113,6 +122,9 @@ void indexResourceWithIndexDate_andFullIndexRequest() throws Exception {
// given
var work = resourceRepo.save(getSampleWork(null).setIndexDate(new Date()));
resourceRepo.save(getSampleInstanceResource(null, work));
var resourceEdge = work.getIncomingEdges().iterator().next();
resourceEdge.computeId();
resourceEdgeRepository.save(resourceEdge);

var requestBuilder = put(INDEX_URL)
.param("full", "true")
Expand Down
Loading

0 comments on commit cb69650

Please sign in to comment.