View Javadoc
1   /**
2    * This file Copyright (c) 2019 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.resources.app.data;
35  
36  import info.magnolia.config.registry.DefinitionMetadata;
37  import info.magnolia.config.registry.DefinitionProvider;
38  import info.magnolia.config.registry.RegistryFacade;
39  import info.magnolia.module.ModuleRegistry;
40  import info.magnolia.objectfactory.Components;
41  import info.magnolia.resourceloader.Resource;
42  import info.magnolia.resourceloader.ResourceOrigin;
43  import info.magnolia.resourceloader.classpath.ClasspathResource;
44  import info.magnolia.ui.filter.DataFilter;
45  
46  import java.util.Collection;
47  import java.util.HashSet;
48  import java.util.stream.Stream;
49  
50  import javax.inject.Inject;
51  
52  import com.vaadin.data.provider.AbstractBackEndHierarchicalDataProvider;
53  import com.vaadin.data.provider.HierarchicalQuery;
54  
55  /**
56   * Data provider used to show non filtered tree structure of resources.
57   */
58  public class ResourceDataProvider extends AbstractBackEndHierarchicalDataProvider<Resource, DataFilter> implements ResourceHelper {
59  
60      private final ResourceOrigin<?> resourceOrigin;
61      private final Collection<String> moduleFolders;
62  
63      @Inject
64      ResourceDataProvider(ResourceOrigin<?> resourceOrigin, ModuleRegistry moduleRegistry, RegistryFacade registryFacade) {
65          this.resourceOrigin = resourceOrigin;
66          this.moduleFolders = new HashSet<>(moduleRegistry.getModuleNames()); //include all java modules (including those without any definitions, but might have e.g. CSS files)
67          final Collection<DefinitionProvider<?>> definitionProviders = registryFacade.query().findMultiple(); //add all modules (including light modules)
68          definitionProviders
69                  .stream()
70                  .map(DefinitionProvider::getMetadata)
71                  .map(DefinitionMetadata::getModule)
72                  .forEach(moduleFolders::add);
73      }
74  
75      /**
76       * @deprecated since 3.0.3. Use {@link #ResourceDataProvider(info.magnolia.resourceloader.ResourceOrigin, info.magnolia.module.ModuleRegistry, info.magnolia.config.registry.RegistryFacade)} instead.
77       */
78      @Deprecated
79      public ResourceDataProvider(ResourceOrigin<?> resourceOrigin, ModuleRegistry moduleRegistry) {
80          this(resourceOrigin, moduleRegistry, Components.getComponent(RegistryFacade.class));
81      }
82  
83      @Override
84      public Object getId(Resource item) {
85          return item.getPath();
86      }
87  
88      @Override
89      protected Stream<Resource> fetchChildrenFromBackEnd(HierarchicalQuery<Resource, DataFilter> query) {
90          boolean isRoot = query.getParent() == null;
91          Stream<Resource> children = getChildren(isRoot ? resourceOrigin.getRoot() : query.getParent());
92          if (isRoot) { //if no column filter is set, filter resources outside of modules
93              children = children.filter(resource -> {
94                  boolean isClasspathOnlyResource = getFirstResource(resource)
95                          .map(ClasspathResource.class::isInstance)
96                          .orElse(false);
97  
98                  return !isClasspathOnlyResource || moduleFolders.contains(resource.getName());
99              });
100         }
101         return children
102                 .skip(query.getOffset())
103                 .limit(query.getLimit());
104     }
105 
106     @Override
107     public int getChildCount(HierarchicalQuery<Resource, DataFilter> query) {
108         return (int) fetchChildrenFromBackEnd(query).count();
109     }
110 
111     @Override
112     public boolean hasChildren(Resource item) {
113         return getChildren(item).findFirst().isPresent();
114     }
115 
116     private Stream<Resource> getChildren(Resource parent) {
117         return !parent.isDirectory() ? Stream.empty() : parent.listChildren().stream();
118     }
119 }
120