Skip to content

Commit

Permalink
MODLD-593 (Spike): Retaining admin metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
pkjacob committed Nov 25, 2024
1 parent 2fbcc74 commit 3549b71
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.folio.linked.data.service.resource;

import org.folio.linked.data.model.entity.Resource;

public interface ResourceEdgeService {
void copyOutgoingEdges(Resource from, Resource to);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.folio.linked.data.service.resource.impl;

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.springframework.stereotype.Service;

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

@Service
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();
}

public void copyOutgoingEdges(Resource from, Resource to) {
this.rules.entrySet()
.stream()
.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 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
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class ResourceServiceImpl implements ResourceService {
private final FolioMetadataRepository folioMetadataRepo;
private final RequestProcessingExceptionBuilder exceptionBuilder;
private final ApplicationEventPublisher applicationEventPublisher;
private final ResourceEdgeServiceImpl resourceEdgeService;

@Override
public ResourceResponseDto createResource(ResourceRequestDto resourceDto) {
Expand Down Expand Up @@ -101,6 +102,7 @@ private Resource getResource(Long id) {
private Resource saveNewResource(ResourceRequestDto resourceDto, Resource old) {
var mapped = resourceDtoMapper.toEntity(resourceDto);
metadataService.ensure(mapped, old.getFolioMetadata());
resourceEdgeService.copyOutgoingEdges(old, mapped);
return resourceGraphService.saveMergingGraph(mapped);
}

Expand Down

0 comments on commit 3549b71

Please sign in to comment.