View Javadoc
1   /**
2    * This file Copyright (c) 2015-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.dam.app.contentview.tree;
35  
36  import info.magnolia.dam.api.AssetProviderRegistry;
37  import info.magnolia.dam.api.Item;
38  import info.magnolia.dam.app.data.AssetListDataProvider;
39  import info.magnolia.dam.app.data.HierarchicalAssetDataProvider;
40  import info.magnolia.objectfactory.ComponentProvider;
41  import info.magnolia.ui.availability.AvailabilityChecker;
42  import info.magnolia.ui.contentapp.Datasource;
43  import info.magnolia.ui.contentapp.FilterableHierarchicalDataProvider;
44  import info.magnolia.ui.contentapp.browser.TreePresenter;
45  import info.magnolia.ui.contentapp.configuration.GridViewDefinition;
46  import info.magnolia.ui.contentapp.configuration.column.ColumnDefinition;
47  import info.magnolia.ui.datasource.PropertySetFactory;
48  import info.magnolia.ui.observation.DatasourceObservation;
49  
50  import java.util.ArrayList;
51  import java.util.List;
52  import java.util.stream.Collectors;
53  import java.util.stream.Stream;
54  
55  import javax.inject.Inject;
56  
57  import com.vaadin.data.PropertySet;
58  
59  /**
60   * Extension of {@link TreePresenter} that uses {@link com.vaadin.data.provider.AbstractDataProvider} to work with assets.
61   */
62  public class AssetTreePresenter extends TreePresenter<Item> {
63  
64      private final AssetProviderRegistry assetProviderRegistry;
65      private final PropertySetFactory<Item> propertySetFactory;
66  
67      @Inject
68      AssetTreePresenter(GridViewDefinition<Item> definition,
69                         ComponentProvider componentProvider,
70                         AvailabilityChecker<Item> availabilityChecker,
71                         AssetProviderRegistry assetProviderRegistry,
72                         PropertySetFactory<Item> propertySetFactory,
73                         Datasource<Item> datasource,
74                         DatasourceObservation datasourceObservation
75      ) {
76          super(definition, componentProvider, availabilityChecker, datasource, datasourceObservation);
77          this.propertySetFactory = propertySetFactory;
78          this.assetProviderRegistry = assetProviderRegistry;
79      }
80  
81      /**
82       * @deprecated since 3.0.3. Use {@link #AssetTreePresenter(info.magnolia.ui.contentapp.configuration.GridViewDefinition, info.magnolia.objectfactory.ComponentProvider, info.magnolia.ui.availability.AvailabilityChecker, info.magnolia.dam.api.AssetProviderRegistry, info.magnolia.ui.datasource.PropertySetFactory, info.magnolia.ui.contentapp.Datasource, info.magnolia.ui.observation.DatasourceObservation)} instead.
83       */
84      @Deprecated
85      public AssetTreePresenter(GridViewDefinition<Item> definition, ComponentProvider componentProvider, AvailabilityChecker<Item> availabilityChecker, AssetProviderRegistry assetProviderRegistry, PropertySetFactory<Item> propertySetFactory, Datasource<Item> datasource) {
86          this(definition, componentProvider, availabilityChecker, assetProviderRegistry, propertySetFactory, datasource, componentProvider.getComponent(DatasourceObservation.class));
87      }
88  
89      @Override
90      public Stream<Item> getParents(Item item) {
91          final List<Item> parents = new ArrayList<>();
92          Item parent = item;
93          while (!"/".equals(parent.getPath())) {
94              parent = parent.getParent();
95              parents.add(parent);
96          }
97          return parents.stream();
98      }
99  
100     @Override
101     public FilterableHierarchicalDataProvider<Item> createDataProvider() {
102         return FilterableHierarchicalDataProvider.wrap(new HierarchicalAssetDataProvider(assetProviderRegistry), new AssetListDataProvider(assetProviderRegistry));
103     }
104 
105     @Override
106     protected PropertySet<Item> createPropertySet() {
107         return propertySetFactory.withProperties(definition.getColumns().stream().collect(Collectors.toMap(ColumnDefinition::getName, ColumnDefinition::getType)));
108     }
109 }