Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only fetch parents if node is fetched as standalone and not as child #278

Merged
merged 2 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/no/ndla/taxonomy/domain/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ private Collection<Node> getAllParentsRecursive() {
return all;
}

public Collection<TaxonomyContext> getAllParentContexts() {
public Set<TaxonomyContext> getAllParentContexts() {
return getAllParentsRecursive().stream()
.map(Node::getContexts)
.flatMap(Collection::stream)
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/no/ndla/taxonomy/rest/v1/Nodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ public List<NodeDTO> getAllNodes(
metadataFilters,
includeContexts,
filterProgrammes,
true,
rootId,
parentId);
}
Expand Down Expand Up @@ -237,7 +238,8 @@ public SearchResultDTO<NodeDTO> getNodePage(
Optional.empty(),
includeContexts,
filterProgrammes,
isVisible))
isVisible,
false))
.collect(Collectors.toList());
return new SearchResultDTO<>(ids.getTotalElements(), page.get(), pageSize.get(), contents);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ public record TaxonomyContextDTO(
@JsonProperty @Schema(description = "The rank of the parent connection object") int rank,
@JsonProperty @Schema(description = "The id of the parent connection object") String connectionId,
@JsonProperty @Schema(description = "Pretty-url of this particular context") String url,
@JsonProperty @Schema(description = "List of all parents to this context") List<TaxonomyCrumbDTO> parents) {
@JsonProperty @Schema(description = "List of all parents to this context. Empty if node is fetched as child") List<TaxonomyCrumbDTO> parents) {
// spotless:on
}
7 changes: 5 additions & 2 deletions src/main/java/no/ndla/taxonomy/service/NodeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public List<NodeDTO> getNodesByType(
MetadataFilters metadataFilters,
Optional<Boolean> includeContexts,
boolean filterProgrammes,
boolean includeParents,
Optional<URI> rootId,
Optional<URI> parentId) {
final List<NodeDTO> listToReturn = new ArrayList<>();
Expand Down Expand Up @@ -121,7 +122,8 @@ public List<NodeDTO> getNodesByType(
contextId,
includeContexts,
filterProgrammes,
metadataFilters.getVisible().orElse(false)))
metadataFilters.getVisible().orElse(false),
includeParents))
.toList();
listToReturn.addAll(dtos);
});
Expand Down Expand Up @@ -174,7 +176,8 @@ public NodeDTO getNode(
Optional.empty(),
includeContexts,
filterProgrammes,
isVisible);
isVisible,
true);
}

public Optional<Node> getMaybeNode(URI publicId) {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/no/ndla/taxonomy/service/SearchService.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ public SearchResultDTO<NodeDTO> search(
Optional.empty(),
includeContexts,
filterProgrammes,
false,
false))
.collect(Collectors.toList());

Expand Down
6 changes: 4 additions & 2 deletions src/main/java/no/ndla/taxonomy/service/dtos/NodeChildDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ public NodeChildDTO(
Optional.empty(),
includeContexts,
filterProgrammes,
isVisible);
isVisible,
false);

// This must be enabled when ed is updated to update metadata for connections.
// this.metadata = new MetadataDto(nodeConnection.getMetadata());
Expand Down Expand Up @@ -84,7 +85,8 @@ public NodeChildDTO(Node parent, NodeConnection nodeConnection, String language)
Optional.empty(),
Optional.of(false),
false,
true);
true,
false);

this.rank = nodeConnection.getRank();
this.connectionId = nodeConnection.getPublicId();
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/no/ndla/taxonomy/service/dtos/NodeDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@ public NodeDTO(
Optional<String> contextId,
Optional<Boolean> includeContexts,
boolean filterProgrammes,
boolean isVisible) {
boolean isVisible,
boolean includeParents) {

var contexts = entity.getContexts();
var parentContexts = entity.getAllParentContexts();
var visibleContexts =
isVisible ? contexts.stream().filter(TaxonomyContext::isVisible).collect(Collectors.toSet()) : contexts;
var filteredContexts = visibleContexts.stream()
Expand Down Expand Up @@ -151,6 +151,7 @@ public NodeDTO(
this.nodeType = entity.getNodeType();
this.contextids = entity.getContextIds();

Set<TaxonomyContext> parentContexts = includeParents ? entity.getAllParentContexts() : Set.of();
Optional<TaxonomyContext> selected = entity.pickContext(contextId, parent, root, filteredContexts);
selected.ifPresent(ctx -> {
var contextDto = getTaxonomyContextDTO(entity, ctx, parentContexts);
Expand Down Expand Up @@ -201,6 +202,7 @@ private TaxonomyContextDTO getTaxonomyContextDTO(
return null;
}
})
.filter(Objects::nonNull)
.toList();
var relevance = Relevance.unsafeGetRelevance(URI.create(ctx.relevanceId()));
return new TaxonomyContextDTO(
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/no/ndla/taxonomy/service/dtos/NodeWithParents.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,16 @@ public class NodeWithParents extends NodeDTO {
public NodeWithParents() {}

public NodeWithParents(Node node, String languageCode, Optional<Boolean> includeContexts) {
super(Optional.empty(), Optional.empty(), node, languageCode, Optional.empty(), includeContexts, false, true);
super(
Optional.empty(),
Optional.empty(),
node,
languageCode,
Optional.empty(),
includeContexts,
false,
true,
false);

node.getParentConnections().stream()
.map(nodeResource -> {
Expand Down
Loading