View Javadoc
1   /**
2    * This file Copyright (c) 2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.ui.contentapp;
35  
36  import static java.util.Spliterators.spliterator;
37  
38  import info.magnolia.context.Context;
39  import info.magnolia.jcr.RuntimeRepositoryException;
40  import info.magnolia.ui.datasource.jcr.JcrDatasourceDefinition;
41  import info.magnolia.ui.framework.datasource.components.HierarchySupport;
42  
43  import java.util.ArrayList;
44  import java.util.List;
45  import java.util.Optional;
46  import java.util.Spliterator;
47  import java.util.stream.Stream;
48  import java.util.stream.StreamSupport;
49  
50  import javax.inject.Inject;
51  import javax.inject.Provider;
52  import javax.jcr.Item;
53  import javax.jcr.Node;
54  import javax.jcr.NodeIterator;
55  import javax.jcr.RepositoryException;
56  import javax.jcr.Session;
57  
58  import org.slf4j.Logger;
59  import org.slf4j.LoggerFactory;
60  
61  import lombok.SneakyThrows;
62  
63  /**
64   * {@link HierarchySupport} implementation for the JCR nodes.
65   */
66  public class JcrWorkspaceHierarchySupport implements HierarchySupport<Item> {
67  
68      private static final Logger log = LoggerFactory.getLogger(JcrWorkspaceHierarchySupport.class);
69  
70      private final JcrDatasourceDefinition jcrDatasourceDefinition;
71      private final Provider<Context> contextProvider;
72  
73      @Inject
74      public JcrWorkspaceHierarchySupport(JcrDatasourceDefinition jcrDatasourceDefinition, Provider<Context> contextProvider) {
75          this.jcrDatasourceDefinition = jcrDatasourceDefinition;
76          this.contextProvider = contextProvider;
77      }
78  
79      @Override
80      @SneakyThrows
81      public Stream<Item> getRootItems() {
82          return nodeIteratorToStream(getSession().getRootNode().getNodes());
83      }
84  
85      @Override
86      @SneakyThrows
87      public Stream<Item> getChildren(Item parent) {
88          if (!parent.isNode()) {
89              return Stream.empty();
90          }
91          return nodeIteratorToStream(((Node) parent).getNodes());
92      }
93  
94      @Override
95      public Stream<Item> getParents(Item item) {
96          try {
97              if (!item.isNode()) {
98                  return Stream.of(item.getParent());
99              }
100             final List<Item> parents = new ArrayList<>();
101             Optional<Item> parent = getDirectParent(item);
102             while (parent.isPresent()) {
103                 parents.add(parent.get());
104                 parent = getDirectParent(parent.get());
105             }
106             return parents.stream();
107         } catch (RepositoryException e) {
108             log.debug("Failed to resolve Node parent");
109             return Stream.empty();
110         }
111     }
112 
113     @Override
114     @SneakyThrows
115     public Optional<Item> getDirectParent(Item item) {
116         if (item.getDepth() == 0) {
117             return Optional.empty();
118         }
119 
120         return Optional.of(item.getParent());
121     }
122 
123     @SuppressWarnings("unchecked")
124     private Stream<Item> nodeIteratorToStream(NodeIterator nodeIterator) {
125         return StreamSupport.stream(
126                 spliterator(
127                         nodeIterator,
128                         nodeIterator.getSize(),
129                         Spliterator.DISTINCT | Spliterator.NONNULL),
130                 false);
131     }
132 
133     private Session getSession() {
134         try {
135             return this.contextProvider.get().getJCRSession(this.jcrDatasourceDefinition.getWorkspace());
136         } catch (RepositoryException e) {
137             throw new RuntimeRepositoryException(e);
138         }
139     }
140 }