Skip to content

Commit

Permalink
Merge pull request #243 from NDLANO/expand-context
Browse files Browse the repository at this point in the history
Expand taxonomycontext with parent contextids
  • Loading branch information
gunnarvelle authored Apr 22, 2024
2 parents 037245a + 33170ba commit 22a5ff1
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 133 deletions.
46 changes: 0 additions & 46 deletions src/main/java/no/ndla/taxonomy/domain/Context.java

This file was deleted.

15 changes: 8 additions & 7 deletions src/main/java/no/ndla/taxonomy/domain/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public class Node extends DomainObject implements EntityWithMetadata {

@Type(JsonBinaryType.class)
@Column(name = "contexts", columnDefinition = "jsonb")
private Set<Context> contexts = new HashSet<>();
private Set<TaxonomyContext> contexts = new HashSet<>();

// Needed for hibernate
public Node() {}
Expand Down Expand Up @@ -123,8 +123,8 @@ private void updatePublicID() {

public Optional<String> getPrimaryPath() {
return getContexts().stream()
.filter(Context::isPrimary)
.map(Context::path)
.filter(TaxonomyContext::isPrimary)
.map(TaxonomyContext::path)
.findFirst();
}

Expand All @@ -137,7 +137,8 @@ public Optional<String> getPrimaryPath() {
* publicId.
* @return Context
*/
public Optional<Context> pickContext(Optional<String> contextId, Optional<Node> parent, Optional<Node> root) {
public Optional<TaxonomyContext> pickContext(
Optional<String> contextId, Optional<Node> parent, Optional<Node> root) {
var contexts = getContexts();
var maybeContext = contextId.flatMap(
id -> contexts.stream().filter(c -> c.contextId().equals(id)).findFirst());
Expand Down Expand Up @@ -190,7 +191,7 @@ public Optional<Context> pickContext(Optional<String> contextId, Optional<Node>
}

public TreeSet<String> getAllPaths() {
return getContexts().stream().map(Context::path).collect(Collectors.toCollection(TreeSet::new));
return getContexts().stream().map(TaxonomyContext::path).collect(Collectors.toCollection(TreeSet::new));
}

/*
Expand Down Expand Up @@ -489,11 +490,11 @@ public Map<String, String> getCustomFields() {
return this.customfields;
}

public void setContexts(Set<Context> contexts) {
public void setContexts(Set<TaxonomyContext> contexts) {
this.contexts = contexts;
}

public Set<Context> getContexts() {
public Set<TaxonomyContext> getContexts() {
return this.contexts;
}

Expand Down
48 changes: 48 additions & 0 deletions src/main/java/no/ndla/taxonomy/domain/TaxonomyContext.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Part of NDLA taxonomy-api
* Copyright (C) 2023 NDLA
*
* See LICENSE
*/

package no.ndla.taxonomy.domain;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import java.util.List;
import java.util.Optional;

/**
* Identifies a unique context for any node. A context is a position for a node in the structure, identified by root
* node and parent-connection
*
* @param rootId The publicId of the node at the root of the context.
* @param rootName The name of the root.
* @param path The path for this connection.
* @param breadcrumbs Breadcrumbs corresponding with the path.
* @param contextType Type resource. Fetched from node.
* @param parentIds Parents ids.
* @param parentContextIds Parents context ids.
* @param isVisible Metadata from node.
* @param isActive Metadata from node. True if subjectCategory is active or otherResources.
* @param isPrimary Is this context marked as primary. From nodeConnection.
* @param relevanceId ID of relevance. From nodeConnection.
* @param contextId Hash of root publicId + nodeConnection publicId. Unique for this context.
* @param rank The rank of the context. From nodeConnection.
* @param connectionId The id of the connection. From nodeConnection.
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public record TaxonomyContext(
String rootId,
LanguageField<String> rootName,
String path,
LanguageField<List<String>> breadcrumbs,
Optional<String> contextType,
List<String> parentIds,
List<String> parentContextIds,
boolean isVisible,
boolean isActive,
boolean isPrimary,
String relevanceId,
String contextId,
int rank,
String connectionId) {}
Original file line number Diff line number Diff line change
Expand Up @@ -35,33 +35,4 @@ public record TaxonomyContextDTO(
@JsonProperty @Schema(description = "The id of the parent connection object") String connectionId,
@JsonProperty @Schema(description = "Pretty-url of this particular context") Optional<String> url) {
// spotless:on
@JsonProperty
@Deprecated
public Optional<URI> id() {
return Optional.of(publicId());
}

@JsonProperty
@Deprecated
public Optional<URI> subjectId() {
return Optional.of(rootId());
}

@JsonProperty
@Deprecated
public Optional<LanguageFieldDTO<String>> subject() {
return Optional.of(root());
}

@JsonProperty(access = JsonProperty.Access.READ_ONLY)
@Deprecated
public Optional<List<URI>> parentTopicIds() {
return Optional.of(parentIds());
}

@JsonProperty("isPrimaryConnection")
@Deprecated
public Optional<Boolean> isPrimaryConnection() {
return Optional.of(isPrimary());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

import java.util.*;
import no.ndla.taxonomy.config.Constants;
import no.ndla.taxonomy.domain.Context;
import no.ndla.taxonomy.domain.LanguageField;
import no.ndla.taxonomy.domain.Node;
import no.ndla.taxonomy.domain.TaxonomyContext;
import no.ndla.taxonomy.util.HashUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
Expand All @@ -22,21 +22,22 @@ public class ContextUpdaterServiceImpl implements ContextUpdaterService {

public ContextUpdaterServiceImpl() {}

private Set<Context> createContexts(Node node) {
final var returnedContexts = new HashSet<Context>();
private Set<TaxonomyContext> createContexts(Node node) {
final var returnedContexts = new HashSet<TaxonomyContext>();

boolean activeContext = node.getCustomFields()
.getOrDefault(Constants.SubjectCategory, Constants.Active)
.matches(String.format("%s|%s|%s", Constants.Active, Constants.Beta, Constants.OtherResources));
// This entity can be root path
if (node.isContext()) {
returnedContexts.add(new Context(
returnedContexts.add(new TaxonomyContext(
node.getPublicId().toString(),
LanguageField.fromNode(node),
node.getPathPart(),
new LanguageField<List<String>>(),
node.getContextType(),
new ArrayList<>(),
new ArrayList<>(),
node.isVisible(),
activeContext,
true,
Expand All @@ -56,13 +57,16 @@ private Set<Context> createContexts(Node node) {
parentContext.breadcrumbs(), LanguageField.fromNode(parent));
List<String> parentIds = parentContext.parentIds();
parentIds.add(parent.getPublicId().toString());
return new Context(
List<String> parentContextIds = parentContext.parentContextIds();
parentContextIds.add(parentContext.contextId());
return new TaxonomyContext(
parentContext.rootId(),
parentContext.rootName(),
parentContext.path() + node.getPathPart(),
breadcrumbs,
node.getContextType(),
parentIds,
parentContextIds,
parentContext.isVisible() && node.isVisible(),
parentContext.isActive() && activeContext,
parentConnection.isPrimary().orElse(false),
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/no/ndla/taxonomy/service/NodeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ List<TaxonomyContextDTO> nodesToContexts(List<Node> nodes, boolean filterVisible
.flatMap(node -> {
var contexts = filterVisibles
? node.getContexts().stream()
.filter(Context::isVisible)
.filter(TaxonomyContext::isVisible)
.collect(Collectors.toSet())
: node.getContexts();
return contexts.stream().map(context -> {
Expand Down
54 changes: 26 additions & 28 deletions src/main/java/no/ndla/taxonomy/service/dtos/NodeDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public NodeDTO(

this.nodeType = entity.getNodeType();

Optional<Context> context = entity.pickContext(contextId, parent, root);
Optional<TaxonomyContext> context = entity.pickContext(contextId, parent, root);
context.ifPresent(ctx -> {
this.path = ctx.path();
// TODO: this changes the content in context breadcrumbs
Expand All @@ -149,33 +149,31 @@ public NodeDTO(
LanguageField<String> finalRelevanceName = relevanceName;
this.contexts = entity.getContexts().stream()
.filter(ctx -> !filterProgrammes || !ctx.rootId().contains(NodeType.PROGRAMME.getName()))
.map(ctx -> {
return new TaxonomyContextDTO(
entity.getPublicId(),
URI.create(ctx.rootId()),
LanguageFieldDTO.fromLanguageField(ctx.rootName()),
ctx.path(),
LanguageFieldDTO.fromLanguageFieldList(ctx.breadcrumbs()),
entity.getContextType(),
URI.create(ctx.relevanceId()),
LanguageFieldDTO.fromLanguageField(finalRelevanceName),
entity.getResourceTypes().stream()
.sorted((o1, o2) -> {
if (o1.getParent().isEmpty()) return -1;
if (o2.getParent().isEmpty()) return 1;
return 0;
})
.map(SearchableTaxonomyResourceType::new)
.toList(),
ctx.parentIds().stream().map(URI::create).toList(),
ctx.isPrimary(),
ctx.isActive(),
ctx.isVisible(),
ctx.contextId(),
ctx.rank(),
ctx.connectionId(),
TitleUtil.createPrettyUrl(this.name, ctx.contextId()));
})
.map(ctx -> new TaxonomyContextDTO(
entity.getPublicId(),
URI.create(ctx.rootId()),
LanguageFieldDTO.fromLanguageField(ctx.rootName()),
ctx.path(),
LanguageFieldDTO.fromLanguageFieldList(ctx.breadcrumbs()),
entity.getContextType(),
URI.create(ctx.relevanceId()),
LanguageFieldDTO.fromLanguageField(finalRelevanceName),
entity.getResourceTypes().stream()
.sorted((o1, o2) -> {
if (o1.getParent().isEmpty()) return -1;
if (o2.getParent().isEmpty()) return 1;
return 0;
})
.map(SearchableTaxonomyResourceType::new)
.toList(),
ctx.parentIds().stream().map(URI::create).toList(),
ctx.isPrimary(),
ctx.isActive(),
ctx.isVisible(),
ctx.contextId(),
ctx.rank(),
ctx.connectionId(),
TitleUtil.createPrettyUrl(this.name, ctx.contextId())))
.toList();
});
}
Expand Down
10 changes: 10 additions & 0 deletions src/main/resources/db-master-changelog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1482,4 +1482,14 @@
</sql>
</changeSet>

<changeSet id="20240322 Add parent context ids to context" author="Gunnar Velle">
<sql>
UPDATE node
SET contexts = (
SELECT jsonb_agg(jsonb_set(obj, '{parentContextIds}', '[]'::jsonb, true))
FROM jsonb_array_elements(contexts) AS obj
)
</sql>
</changeSet>

</databaseChangeLog>
Loading

0 comments on commit 22a5ff1

Please sign in to comment.