/** * Extract the distinct nodes from the subscriptions. */ private Map<String, SubscribedNodeVo> toNodes(final Map<String, NodeVo> nodes, final Collection<SubscriptionLightVo> subscriptions) { final Map<String, SubscribedNodeVo> filteredNodes = new TreeMap<>(); // Add the related node of each subscription subscriptions.stream().map(SubscriptionLightVo::getNode).map(nodes::get) .forEach(n -> addNodeAsNeeded(filteredNodes, nodes, n)); return filteredNodes; }
/** * Add a node to the filtered nodes, and also add recursively the parent. */ private void addNodeAsNeeded(final Map<String, SubscribedNodeVo> filteredNodes, final Map<String, NodeVo> allNodes, final NodeVo node) { if (!filteredNodes.containsKey(node.getId())) { // Build the node wrapper final SubscribedNodeVo subscribedNode = new SubscribedNodeVo(); NamedBean.copy(node, subscribedNode); subscribedNode.setTag(node.getTag()); subscribedNode.setTagUiClasses(node.getTagUiClasses()); filteredNodes.put(node.getId(), subscribedNode); // Now check the parent exists or not and add it to the target // filtered nodes if (node.isRefining()) { // Completed the previous link subscribedNode.setRefined(node.getRefined().getId()); // Add the parent too (as needed addNodeAsNeeded(filteredNodes, allNodes, allNodes.get(subscribedNode.getRefined())); } } }