Skip to content

Commit

Permalink
Merge pull request #258 from NDLANO/add-root-and-parent-params
Browse files Browse the repository at this point in the history
Adds rootId and parentId when fetching nodes to pick correct context
  • Loading branch information
gunnarvelle authored Aug 13, 2024
2 parents e217922 + 9816ec5 commit 7dc0b77
Show file tree
Hide file tree
Showing 12 changed files with 185 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,19 @@
public class WarningSuppressionLogFilter extends Filter<ILoggingEvent> {

final String[] warningsToSuppress = {
// https://github.com/javamelody/javamelody/issues/1222
"Bean 'net.bull.javamelody.JavaMelodyAutoConfiguration' of type [net.bull.javamelody.JavaMelodyAutoConfiguration$$SpringCGLIB$$0] is not eligible for getting processed by all BeanPostProcessors",
"Bean 'monitoringSpringAdvisor' of type [net.bull.javamelody.MonitoringSpringAdvisor] is not eligible for getting processed by all BeanPostProcessors",
"Bean 'monitoringSpringServiceAdvisor' of type [net.bull.javamelody.MonitoringSpringAdvisor] is not eligible for getting processed by all BeanPostProcessors",
"Bean 'monitoringSpringControllerAdvisor' of type [net.bull.javamelody.MonitoringSpringAdvisor] is not eligible for getting processed by all BeanPostProcessors",
"Bean 'monitoringSpringRestControllerAdvisor' of type [net.bull.javamelody.MonitoringSpringAdvisor] is not eligible for getting processed by all BeanPostProcessors",
"Bean 'monitoringSpringAsyncAdvisor' of type [net.bull.javamelody.MonitoringSpringAdvisor] is not eligible for getting processed by all BeanPostProcessors",
"Bean 'monitoringSpringScheduledAdvisor' of type [net.bull.javamelody.MonitoringSpringAdvisor] is not eligible for getting processed by all BeanPostProcessors",
// NOTE: These are logged because we use `runAlways=true` in the liquibase configuration
"schema \"extensions\" already exists, skipping",
"extension \"btree_gist\" already exists, skipping",
// https://github.com/javamelody/javamelody/issues/1222
"Bean 'net.bull.javamelody.JavaMelodyAutoConfiguration' of type [net.bull.javamelody.JavaMelodyAutoConfiguration$$SpringCGLIB$$0] is not eligible for getting processed by all BeanPostProcessors",
"Bean 'monitoringSpringAdvisor' of type [net.bull.javamelody.MonitoringSpringAdvisor] is not eligible for getting processed by all BeanPostProcessors",
"Bean 'monitoringSpringServiceAdvisor' of type [net.bull.javamelody.MonitoringSpringAdvisor] is not eligible for getting processed by all BeanPostProcessors",
"Bean 'monitoringSpringControllerAdvisor' of type [net.bull.javamelody.MonitoringSpringAdvisor] is not eligible for getting processed by all BeanPostProcessors",
"Bean 'monitoringSpringRestControllerAdvisor' of type [net.bull.javamelody.MonitoringSpringAdvisor] is not eligible for getting processed by all BeanPostProcessors",
"Bean 'monitoringSpringAsyncAdvisor' of type [net.bull.javamelody.MonitoringSpringAdvisor] is not eligible for getting processed by all BeanPostProcessors",
"Bean 'monitoringSpringScheduledAdvisor' of type [net.bull.javamelody.MonitoringSpringAdvisor] is not eligible for getting processed by all BeanPostProcessors",
// NOTE: These are logged because we use `runAlways=true` in the liquibase configuration
"schema \"extensions\" already exists, skipping",
"extension \"btree_gist\" already exists, skipping",
};


@Override
public FilterReply decide(ILoggingEvent event) {
if (event.getLevel() == Level.WARN) {
Expand Down
14 changes: 6 additions & 8 deletions src/main/java/no/ndla/taxonomy/domain/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -254,17 +254,15 @@ public Optional<TaxonomyContext> pickContext(
if (maybeContext.isPresent()) {
return maybeContext;
}
var withParent = parent.map(p -> contexts.stream()
var containsParent = parent.map(p -> contexts.stream()
.filter(c -> c.parentIds().contains(p.getPublicId().toString()))
.collect(Collectors.toSet()))
.orElse(contexts);
var withRoot = root.flatMap(r -> withParent.stream()
.filter(c -> c.rootId().equals(r.getPublicId().toString()))
.findFirst());
if (withRoot.isPresent()) {
return withRoot;
}
return contexts.stream().min((context1, context2) -> {
var containsRoot = root.map(p -> containsParent.stream()
.filter(c -> c.parentIds().contains(p.getPublicId().toString()))
.collect(Collectors.toSet()))
.orElse(containsParent);
return containsRoot.stream().min((context1, context2) -> {
final var inPath1 =
context1.path().contains(root.map(Node::getPathPart).orElse("other"));
final var inPath2 =
Expand Down
24 changes: 20 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 @@ -110,21 +110,27 @@ public List<NodeDTO> getAllNodes(
Optional<Boolean> includeContexts,
@Parameter(description = "Filter out programme contexts")
@RequestParam(value = "filterProgrammes", required = false, defaultValue = "false")
boolean filterProgrammes) {
boolean filterProgrammes,
@Parameter(description = "Id to root id in context.") @RequestParam(value = "rootId", required = false)
Optional<URI> rootId,
@Parameter(description = "Id to parent id in context.") @RequestParam(value = "parentId", required = false)
Optional<URI> parentId) {
MetadataFilters metadataFilters = new MetadataFilters(key, value, isVisible);
var isRootOrContext = isRoot.isPresent() ? isRoot : isContext;
var defaultNodeTypes = getDefaultNodeTypes(nodeType, contentUri, contextId, isRootOrContext, metadataFilters);
return nodeService.getNodesByType(
Optional.of(defaultNodeTypes),
language,
language.orElse(Constants.DefaultLanguage),
publicIds,
contentUri,
contextId,
isRoot,
isContext,
metadataFilters,
includeContexts,
filterProgrammes);
filterProgrammes,
rootId,
parentId);
}

@GetMapping("/search")
Expand Down Expand Up @@ -222,10 +228,20 @@ public SearchResultDTO<NodeDTO> getNodePage(
@Transactional(readOnly = true)
public NodeDTO getNode(
@PathVariable("id") URI id,
@Parameter(description = "Id to root id in context.") @RequestParam(value = "rootId", required = false)
Optional<URI> rootId,
@Parameter(description = "Id to parent id in context.") @RequestParam(value = "parentId", required = false)
Optional<URI> parentId,
@Parameter(name = "includeContexts", description = "Include all contexts")
@RequestParam(value = "includeContexts", required = false, defaultValue = "true")
Optional<Boolean> includeContexts,
@Parameter(description = "Filter out programme contexts")
@RequestParam(value = "filterProgrammes", required = false, defaultValue = "false")
boolean filterProgrammes,
@Parameter(description = "ISO-639-1 language code", example = "nb")
@RequestParam(value = "language", required = false, defaultValue = Constants.DefaultLanguage)
Optional<String> language) {
return nodeService.getNode(id, language, Optional.of(true));
return nodeService.getNode(id, language, rootId, parentId, includeContexts, filterProgrammes);
}

@PostMapping
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/no/ndla/taxonomy/rest/v1/Resources.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,17 @@ public List<NodeDTO> getAllResources(
MetadataFilters metadataFilters = new MetadataFilters(key, value, isVisible);
return nodeService.getNodesByType(
Optional.of(List.of(NodeType.RESOURCE)),
language,
language.orElse(Constants.DefaultLanguage),
Optional.empty(),
contentUri,
Optional.empty(),
Optional.empty(),
Optional.empty(),
metadataFilters,
Optional.of(false),
false);
false,
Optional.empty(),
Optional.empty());
}

@Deprecated
Expand Down Expand Up @@ -167,7 +169,7 @@ public NodeDTO getResource(
@Parameter(description = "ISO-639-1 language code", example = "nb")
@RequestParam(value = "language", required = false, defaultValue = Constants.DefaultLanguage)
Optional<String> language) {
return nodeService.getNode(id, language, Optional.of(false));
return nodeService.getNode(id, language, Optional.empty(), Optional.empty(), Optional.of(false), false);
}

@Deprecated
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/no/ndla/taxonomy/rest/v1/Subjects.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,17 @@ public List<NodeDTO> getAllSubjects(
MetadataFilters metadataFilters = new MetadataFilters(key, value, isVisible);
return nodeService.getNodesByType(
Optional.of(List.of(NodeType.SUBJECT)),
language,
language.orElse(Constants.DefaultLanguage),
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.empty(),
metadataFilters,
Optional.of(false),
false);
false,
Optional.empty(),
Optional.empty());
}

@Deprecated
Expand Down Expand Up @@ -167,7 +169,7 @@ public NodeDTO getSubject(
@Parameter(description = "ISO-639-1 language code", example = "nb")
@RequestParam(value = "language", required = false, defaultValue = Constants.DefaultLanguage)
Optional<String> language) {
return nodeService.getNode(id, language, Optional.of(false));
return nodeService.getNode(id, language, Optional.empty(), Optional.empty(), Optional.of(false), false);
}

@Deprecated
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/no/ndla/taxonomy/rest/v1/Topics.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,17 @@ public List<NodeDTO> getAllTopics(
MetadataFilters metadataFilters = new MetadataFilters(key, value, isVisible);
return nodeService.getNodesByType(
Optional.of(List.of(NodeType.TOPIC)),
language,
language.orElse(Constants.DefaultLanguage),
Optional.empty(),
contentUri,
Optional.empty(),
Optional.empty(),
Optional.empty(),
metadataFilters,
Optional.of(false),
false);
false,
Optional.empty(),
Optional.empty());
}

@Deprecated
Expand Down Expand Up @@ -151,7 +153,7 @@ public NodeDTO getTopic(
@Parameter(description = "ISO-639-1 language code", example = "nb")
@RequestParam(value = "language", required = false, defaultValue = Constants.DefaultLanguage)
Optional<String> language) {
return nodeService.getNode(id, language, Optional.of(false));
return nodeService.getNode(id, language, Optional.empty(), Optional.empty(), Optional.of(false), false);
}

@Deprecated
Expand Down
30 changes: 21 additions & 9 deletions src/main/java/no/ndla/taxonomy/service/NodeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,17 @@ public Specification<Node> nodeHasOneOfNodeType(List<NodeType> nodeType) {

public List<NodeDTO> getNodesByType(
Optional<List<NodeType>> nodeType,
Optional<String> language,
String language,
Optional<List<URI>> publicIds,
Optional<URI> contentUri,
Optional<String> contextId,
Optional<Boolean> isRoot,
Optional<Boolean> isContext,
MetadataFilters metadataFilters,
Optional<Boolean> includeContexts,
boolean filterProgrammes) {
boolean filterProgrammes,
Optional<URI> rootId,
Optional<URI> parentId) {
final List<NodeDTO> listToReturn = new ArrayList<>();
List<Integer> ids;
if (contextId.isPresent()) {
Expand All @@ -114,17 +116,19 @@ public List<NodeDTO> getNodesByType(
isContext);
}
final var counter = new AtomicInteger();
var root = rootId.map(this::getNode);
var parent = parentId.map(this::getNode);
ids.stream()
.collect(Collectors.groupingBy(i -> counter.getAndIncrement() / 1000))
.values()
.forEach(idChunk -> {
final var nodes = nodeRepository.findByIds(idChunk);
var dtos = nodes.stream()
.map(node -> new NodeDTO(
Optional.empty(),
Optional.empty(),
root,
parent,
node,
language.get(),
language,
contextId,
includeContexts,
filterProgrammes,
Expand Down Expand Up @@ -162,16 +166,24 @@ public List<NodeChildDTO> getFilteredChildConnections(URI nodePublicId, String l
return topicTreeSorter.sortList(wrappedList);
}

public NodeDTO getNode(URI publicId, Optional<String> language, Optional<Boolean> includeContexts) {
public NodeDTO getNode(
URI publicId,
Optional<String> language,
Optional<URI> rootId,
Optional<URI> parentId,
Optional<Boolean> includeContexts,
boolean filterProgrammes) {
var node = getNode(publicId);
var root = rootId.map(this::getNode);
var parent = parentId.map(this::getNode);
return new NodeDTO(
Optional.empty(),
Optional.empty(),
root,
parent,
node,
language.orElse(Constants.DefaultLanguage),
Optional.empty(),
includeContexts,
false,
filterProgrammes,
newUrlSeparator);
}

Expand Down
10 changes: 0 additions & 10 deletions src/main/java/no/ndla/taxonomy/service/dtos/NodeChildDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,6 @@ public boolean equals(Object o) {

public NodeChildDTO() {}

@Deprecated
public URI getParent() {
return parentId;
}

public URI getParentId() {
return parentId;
}
Expand All @@ -132,11 +127,6 @@ public boolean isPrimary() {
return isPrimary;
}

@Deprecated
public boolean getPrimary() {
return isPrimary;
}

public void setPrimary(boolean primary) {
isPrimary = primary;
}
Expand Down
Loading

0 comments on commit 7dc0b77

Please sign in to comment.