-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
35 additions
and
46 deletions.
There are no files selected for viewing
55 changes: 22 additions & 33 deletions
55
src/main/java/org/folio/linked/data/service/resource/impl/ResourceEdgeServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,41 @@ | ||
package org.folio.linked.data.service.resource.impl; | ||
|
||
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 java.util.Map; | ||
import java.util.function.Predicate; | ||
import org.folio.ld.dictionary.PredicateDictionary; | ||
import org.folio.ld.dictionary.ResourceTypeDictionary; | ||
import org.folio.linked.data.model.entity.Resource; | ||
import org.folio.linked.data.model.entity.ResourceEdge; | ||
import org.folio.linked.data.service.resource.ResourceEdgeService; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class ResourceEdgeServiceImpl implements ResourceEdgeService { | ||
|
||
private final SingleResourceMapper singleResourceMapper; | ||
private static final Predicate<ResourceEdge> IS_ADMIN_METADATA = edge -> edge.getPredicate().getUri() | ||
.equals(PredicateDictionary.ADMIN_METADATA.getUri()); | ||
|
||
@Override | ||
public void copyOutgoingEdges(Resource from, Resource to) { | ||
if (shouldNotCopyOutgoingEdges(from, to)) { | ||
return; | ||
} | ||
private Map<ResourceTypeDictionary, Predicate<ResourceEdge>> edgesTobeCopied; | ||
|
||
from.getOutgoingEdges() | ||
.stream() | ||
.filter(edge -> !isExcludedEdge(edge)) | ||
.filter(this::hasNoMapper) | ||
.map(edge -> new ResourceEdge(to, edge.getTarget(), edge.getPredicate())) | ||
.forEach(to::addOutgoingEdge); | ||
public ResourceEdgeServiceImpl() { | ||
initializeEdgesTobeCopied(); | ||
} | ||
|
||
private boolean hasNoMapper(ResourceEdge edge) { | ||
return edge.getTarget() | ||
.getTypes() | ||
public void copyOutgoingEdges(Resource from, Resource to) { | ||
this.edgesTobeCopied.entrySet() | ||
.stream() | ||
.allMatch(type -> | ||
singleResourceMapper | ||
.getMapperUnit(type.getUri(), edge.getPredicate(), null, null) | ||
.isEmpty() | ||
); | ||
} | ||
|
||
private boolean shouldNotCopyOutgoingEdges(Resource from, Resource to) { | ||
return !(from.isOfType(INSTANCE) && to.isOfType(INSTANCE) | ||
|| from.isOfType(WORK) && to.isOfType(WORK)); | ||
.filter(entry -> from.isOfType(entry.getKey())) | ||
.filter(entry -> to.isOfType(entry.getKey())) | ||
.map(Map.Entry::getValue) | ||
.flatMap(condition -> from.getOutgoingEdges().stream().filter(condition)) | ||
.map(edge -> new ResourceEdge(to, edge.getTarget(), edge.getPredicate())) | ||
.forEach(to::addOutgoingEdge); | ||
} | ||
|
||
private boolean isExcludedEdge(ResourceEdge edge) { | ||
return edge.getPredicate().getUri().startsWith("http://bibfra.me/vocab/relation/"); | ||
private void initializeEdgesTobeCopied() { | ||
this.edgesTobeCopied = Map.of( | ||
ResourceTypeDictionary.WORK, IS_ADMIN_METADATA, | ||
ResourceTypeDictionary.INSTANCE, IS_ADMIN_METADATA | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters