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 info.magnolia.objectfactory.ComponentProvider;
37  import info.magnolia.ui.api.ioc.ViewScoped;
38  import info.magnolia.ui.contentapp.browser.ActionExecutionService;
39  import info.magnolia.ui.contentapp.browser.TreePresenter;
40  import info.magnolia.ui.contentapp.browser.TreeView;
41  import info.magnolia.ui.contentapp.configuration.TreeViewDefinition;
42  import info.magnolia.ui.contentapp.observation.JcrDataSourceObservation;
43  import info.magnolia.ui.contentapp.observation.JcrObservation;
44  import info.magnolia.ui.databinding.JcrItemPropertySet;
45  import info.magnolia.ui.datasource.jcr.JcrDatasourceDefinition;
46  import info.magnolia.ui.datasource.jcr.JcrSessionManager;
47  
48  import java.util.ArrayList;
49  import java.util.List;
50  import java.util.Optional;
51  import java.util.stream.Stream;
52  
53  import javax.inject.Inject;
54  import javax.jcr.Item;
55  
56  import org.slf4j.Logger;
57  import org.slf4j.LoggerFactory;
58  
59  import com.vaadin.data.PropertySet;
60  
61  import lombok.SneakyThrows;
62  
63  /**
64   *
65   * JCR specific presenter implementation backing up {@link TreeView}.
66   */
67  @ViewScoped
68  public class JcrTreePresenter extends TreePresenter<Item> {
69  
70      private static final Logger log = LoggerFactory.getLogger(JcrTreePresenter.class);
71  
72      private final JcrDatasourceDefinition jcrDatasourceDefinition;
73      private final JcrDataSourceObservation jcrDataSourceObservation;
74      private final JcrItemInteractionAvailability interactionAvailability;
75  
76      @Inject
77      public JcrTreePresenter(
78              JcrSessionManager sessionManager,
79              JcrDatasourceDefinition jcrDatasourceDefinition,
80              TreeViewDefinition<Item> definition,
81              ComponentProvider componentProvider,
82              JcrObservation jcrObservation,
83              ActionExecutionService actionExecutionService) {
84          super(definition, componentProvider, actionExecutionService);
85          this.jcrDatasourceDefinition = jcrDatasourceDefinition;
86          this.jcrDataSourceObservation = new JcrDataSourceObservation(jcrObservation);
87          this.interactionAvailability  = new JcrItemInteractionAvailability(sessionManager);
88  
89          jcrDataSourceObservation.register(jcrDatasourceDefinition, this.dataProvider());
90      }
91  
92      @Override
93      public Stream<Item> getParents(Item item) {
94          final List<Item> parents = new ArrayList<>();
95          Optional<Item> parent = getDirectParent(item);
96          while (parent.isPresent()) {
97              parents.add(parent.get());
98              parent = getDirectParent(parent.get());
99          }
100         return parents.stream();
101     }
102 
103     @Override
104     public void destroy() {
105         this.jcrDataSourceObservation.destroy();
106     }
107 
108     @Override
109     public FilterableHierarchicalDataProvider<Item> createDataProvider() {
110         return FilterableHierarchicalDataProvider.wrap(componentProvider.newInstance(HierarchicalJcrDataProvider.class, jcrDatasourceDefinition));
111     }
112 
113     @SneakyThrows
114     private Optional<Item> getDirectParent(Item item) {
115         if (item.isNode() && item.getDepth() == 0) {
116             return Optional.empty();
117         }
118 
119         return Optional.of(item.getParent());
120     }
121 
122     @Override
123     protected PropertySet<Item> createPropertySet() {
124         return JcrItemPropertySet.fromColumns(definition.getColumns());
125     }
126 
127     @Override
128     public boolean isItemAvailable(Item item) {
129         return this.interactionAvailability.isItemAvailable(item);
130     }
131 }