Skip to content

Commit

Permalink
MODLD-593: Retain outgoing edges when no DTO mapper present for INSTA…
Browse files Browse the repository at this point in the history
…NCE AND WORK
  • Loading branch information
pkjacob committed Nov 26, 2024
1 parent 3549b71 commit 287038a
Showing 1 changed file with 26 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
package org.folio.linked.data.service.resource.impl;

import org.folio.ld.dictionary.PredicateDictionary;
import org.folio.ld.dictionary.ResourceTypeDictionary;
import static org.folio.ld.dictionary.ResourceTypeDictionary.INSTANCE;
import static org.folio.ld.dictionary.ResourceTypeDictionary.WORK;

import lombok.RequiredArgsConstructor;
import org.folio.linked.data.mapper.dto.common.SingleResourceMapper;
import org.folio.linked.data.model.entity.Resource;
import org.folio.linked.data.model.entity.ResourceEdge;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.function.Predicate;

@Service
@RequiredArgsConstructor
public class ResourceEdgeServiceImpl {

// If there are more conditions like this, move them to an inner class or enum
private static final Predicate<ResourceEdge> IS_ADMIN_METADATA = edge -> edge.getPredicate().getUri()
.equals(PredicateDictionary.ADMIN_METADATA.getUri());
private static final Predicate<ResourceEdge> SOME_OTHER_CONDITION = edge -> true; // Dummy condition

private Map<ResourceTypeDictionary, Predicate<ResourceEdge>> rules;

public ResourceEdgeServiceImpl() {
initializeRules();
}
private final SingleResourceMapper singleResourceMapper;

public void copyOutgoingEdges(Resource from, Resource to) {
this.rules.entrySet()
if (doNotCopyOutgoingEdges(from, to)) {
return;
}

from.getOutgoingEdges()
.stream()
.filter(entry -> from.isOfType(entry.getKey()))
.filter(entry -> to.isOfType(entry.getKey()))
.map(Map.Entry::getValue)
.flatMap(condition -> from.getOutgoingEdges().stream().filter(condition))
.filter(this::hasNoMapper)
.map(edge -> new ResourceEdge(to, edge.getTarget(), edge.getPredicate()))
.forEach(to::addOutgoingEdge);
}

private void initializeRules() {
this.rules = Map.of(
ResourceTypeDictionary.WORK, IS_ADMIN_METADATA.or(SOME_OTHER_CONDITION), // Chain more conditions here
ResourceTypeDictionary.INSTANCE, IS_ADMIN_METADATA // Chain more conditions here
);
private boolean hasNoMapper(ResourceEdge edge) {
return edge.getTarget().getTypes()
.stream()
.allMatch(type ->
singleResourceMapper
.getMapperUnit(type.getUri(), edge.getPredicate(), null, null)
.isEmpty()
);
}

private boolean doNotCopyOutgoingEdges(Resource from, Resource to) {
return from.isOfType(INSTANCE) && to.isOfType(INSTANCE)
|| from.isOfType(WORK) && to.isOfType(WORK);
}
}

0 comments on commit 287038a

Please sign in to comment.