Skip to content

Commit

Permalink
Merge pull request #235 from NDLANO/fix-deleting-orphans
Browse files Browse the repository at this point in the history
Remove orphans from node.
  • Loading branch information
gunnarvelle authored Jan 29, 2024
2 parents 1f274cf + 92e8bc0 commit 9355c0e
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 21 deletions.
6 changes: 2 additions & 4 deletions src/main/java/no/ndla/taxonomy/rest/v1/Nodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,7 @@ public SearchResultDTO<NodeDTO> searchNodes(
Optional<Boolean> includeContexts,
@Parameter(description = "Filter out programme contexts")
@RequestParam(value = "filterProgrammes", required = false, defaultValue = "false")
boolean filterProgrammes
) {
boolean filterProgrammes) {
return nodeService.searchByNodeType(
query,
ids,
Expand Down Expand Up @@ -172,8 +171,7 @@ public SearchResultDTO<NodeDTO> searchNodes(@RequestBody NodeSearchBody searchBo
searchBodyParams.pageSize,
searchBodyParams.page,
searchBodyParams.nodeType,
searchBodyParams.customFields
);
searchBodyParams.customFields);
}

@GetMapping("/page")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@

import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import no.ndla.taxonomy.config.Constants;
import no.ndla.taxonomy.domain.NodeType;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import no.ndla.taxonomy.config.Constants;
import no.ndla.taxonomy.domain.NodeType;

public class NodeSearchBody {
@JsonProperty
Expand All @@ -23,12 +22,10 @@ public class NodeSearchBody {
"If specified, the search result will be filtered by whether they include the key,value combination provided. If more than one provided only one will be required (OR)")
public Optional<Map<String, String>> customFields = Optional.empty();


@Schema(description = "ISO-639-1 language code", example = "nb")
@JsonProperty
public Optional<String> language = Optional.of(Constants.DefaultLanguage);


@Schema(description = "How many results to return per page")
@JsonProperty
public int pageSize = 10;
Expand Down Expand Up @@ -64,5 +61,4 @@ public class NodeSearchBody {
public Optional<Map<String, String>> getCustomFields() {
return customFields;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -203,18 +203,17 @@ private Optional<DomainEntity> updateNode(Node node, boolean cleanUp) {
}

result = nodeRepository.save(existing);

// delete orphans
List<URI> childIds = node.getChildConnections().stream()
.map(DomainEntity::getPublicId)
.toList();
result.getChildConnections().forEach(nodeConnection -> {
if (!childIds.contains(nodeConnection.getPublicId())) {
// Connection deleted
deleteEntityByPublicId(nodeConnection.getPublicId());
}
});
}
// delete any orphans
List<URI> childIds = node.getChildConnections().stream()
.map(DomainEntity::getPublicId)
.toList();
result.getChildConnections().forEach(nodeConnection -> {
if (!childIds.contains(nodeConnection.getPublicId())) {
// Connection deleted
deleteEntityByPublicId(nodeConnection.getPublicId());
}
});
if (cleanUp) {
buildPathsForEntity(result.getPublicId());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ public class NodePublishingIntegrationTest extends AbstractIntegrationTest {
@Autowired
NodeService nodeService;

@Autowired
NodeConnectionService nodeConnectionService;

@Autowired
VersionService versionService;

Expand Down Expand Up @@ -655,4 +658,63 @@ void can_publish_node_tree_to_schema_async() throws Exception {
assertEquals(1, customfields.size());
assertAnyTrue(customfields, customfield -> customfield.equals("to be kept"));
}

@Test
@Transactional(propagation = Propagation.REQUIRES_NEW)
void can_publish_node_tree_with_moved_resource_to_schema() throws Exception {
final var command = new VersionPostPut() {
{
name = "Beta";
}
};
Version target = versionService.createNewVersion(Optional.empty(), command);
assertTrue(checkSchemaExists(versionService.schemaFromHash(target.getHash())));
executor.getThreadPoolExecutor().awaitTermination(2, TimeUnit.SECONDS);

// a node to make sure testnode is not the first
builder.node(n -> n.publicId("urn:node:first").resource(r -> r.publicId("urn:resource:first")));
Node resource = builder.node(NodeType.RESOURCE, r -> r.publicId("urn:resource:1"));
Node topic1 =
builder.node(NodeType.TOPIC, t -> t.publicId("urn:topic:1").resource(resource));
Node topic2 = builder.node(NodeType.TOPIC, t -> t.publicId("urn:topic:2"));
Node node = builder.node(
NodeType.SUBJECT,
s -> s.isContext(true).publicId("urn:subject:1").child(topic1).child(topic2));

TestTransaction.flagForCommit();
TestTransaction.end();

nodeService.publishNode(node.getPublicId(), Optional.empty(), target.getPublicId(), true, false);

while (!changelogRepository.findAll().isEmpty()) {
executor.getThreadPoolExecutor().awaitTermination(2, TimeUnit.SECONDS);
}

TestTransaction.start();
TestTransaction.flagForCommit();
// Move resource to another node
nodeConnectionService.disconnectParentChild(topic1, resource);
nodeConnectionService.connectParentChild(topic2, resource, builder.core(), null, Optional.empty());
TestTransaction.end();

nodeService.publishNode(node.getPublicId(), Optional.empty(), target.getPublicId(), true, false);

while (!changelogRepository.findAll().isEmpty()) {
executor.getThreadPoolExecutor().awaitTermination(2, TimeUnit.SECONDS);
}

VersionContext.setCurrentVersion(versionService.schemaFromHash(target.getHash()));
Node updated = (Node) domainEntityHelperService
.getProcessedEntityByPublicId(URI.create("urn:resource:1"), false, false)
.get();
VersionContext.setCurrentVersion(versionService.schemaFromHash(null));

assertNotNull(updated);
assertNotNull(updated.getContexts());
assertEquals(1, updated.getParentConnections().size());
assertAnyTrue(
updated.getParentConnections(),
nodeResource -> nodeResource.getParent().get().getPublicId().equals(URI.create("urn:topic:2")));
assertAnyTrue(updated.getAllPaths(), path -> path.equals("/subject:1/topic:2/resource:1"));
}
}

0 comments on commit 9355c0e

Please sign in to comment.