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.resourceloader.Resource;
37  import info.magnolia.ui.datasource.PropertySetFactory;
38  import info.magnolia.ui.field.FieldDefinition;
39  
40  import java.util.Collection;
41  import java.util.List;
42  import java.util.Locale;
43  import java.util.Map;
44  import java.util.Optional;
45  import java.util.stream.Stream;
46  
47  import org.apache.commons.io.IOUtils;
48  
49  import com.google.common.collect.Lists;
50  import com.machinezoo.noexception.Exceptions;
51  import com.vaadin.data.PropertyDefinition;
52  import com.vaadin.data.PropertySet;
53  import com.vaadin.data.ValueProvider;
54  import com.vaadin.server.Setter;
55  
56  /**
57   * {@link PropertySetFactory} for resources.
58   */
59  public class ResourcePropertySetFactory implements PropertySetFactory<Resource> {
60  
61      public static final String CONTENT = "content";
62      public static final String NAME = "name";
63  
64      @Override
65      public PropertySet<Resource> withProperties(Map<String, Class> map) {
66          return new Set();
67      }
68  
69      @Override
70      public PropertySet<Resource> fromFieldDefinitions(Collection<FieldDefinition> collection, Locale locale) {
71          return new Set();
72      }
73  
74      private static class Set implements PropertySet<Resource> {
75          private List<PropertyDefinition<Resource, ?>> propertyDefinitions;
76  
77          public Set() {
78              Definition contentPropertyDefinition = new Definition(CONTENT, this);
79              NameDefinition namePropertyDefinition = new NameDefinition(this);
80  
81              propertyDefinitions = Lists.newArrayList(contentPropertyDefinition, namePropertyDefinition);
82          }
83  
84          @Override
85          public Stream<PropertyDefinition<Resource, ?>> getProperties() {
86              return propertyDefinitions.stream();
87          }
88  
89          @Override
90          public Optional<PropertyDefinition<Resource, ?>> getProperty(String name) {
91              Optional<PropertyDefinition<Resource, ?>> propertyDefinitionOptional = getProperties()
92                      .filter(resourcePropertyDefinition -> resourcePropertyDefinition.getName().equals(name))
93                      .findFirst();
94  
95              if (!propertyDefinitionOptional.isPresent()) {
96                  PropertyDefinition<Resource, ?> propertyDefinition = new Definition(name, this);
97                  propertyDefinitions.add(propertyDefinition);
98                  propertyDefinitionOptional = Optional.of(propertyDefinition);
99              }
100 
101             return propertyDefinitionOptional;
102         }
103     }
104 
105     private static class Definition implements PropertyDefinition<Resource, Object>, ResourceHelper {
106 
107         private final String name;
108         private final PropertySet<Resource> propertySet;
109 
110         private Definition(String name, PropertySet<Resource> propertySet) {
111             this.name = name;
112             this.propertySet = propertySet;
113         }
114 
115         @Override
116         public ValueProvider<Resource, Object> getGetter() {
117             return resource -> {
118                 String resourceName = resource.getName();
119                 boolean isTextFileContent = CONTENT.equals(name) && !resource.isDirectory() && isTextMediaType(resourceName);
120 
121                 return isTextFileContent ? Exceptions.wrap().get(() -> IOUtils.toString(resource.openReader())) : null;
122             };
123         }
124 
125         @Override
126         public Optional<Setter<Resource, Object>> getSetter() {
127             return Optional.of((Setter<Resource, Object>) (resource, content) -> {});
128         }
129 
130         @Override
131         public Class getType() {
132             return Object.class;
133         }
134 
135         @Override
136         public Class<?> getPropertyHolderType() {
137             return Resource.class;
138         }
139 
140         @Override
141         public String getName() {
142             return name;
143         }
144 
145         @Override
146         public String getCaption() {
147             return getName();
148         }
149 
150         @Override
151         public PropertySet<Resource> getPropertySet() {
152             return propertySet;
153         }
154     }
155 
156     private static class NameDefinition implements PropertyDefinition<Resource, String>, ResourceHelper {
157 
158         private final PropertySet<Resource> propertySet;
159 
160         public NameDefinition(PropertySet<Resource> propertySet) {
161             this.propertySet = propertySet;
162         }
163 
164         @Override
165         public ValueProvider<Resource, String> getGetter() {
166             return Resource::getName;
167         }
168 
169         @Override
170         public Optional<Setter<Resource, String>> getSetter() {
171             return Optional.of((Setter<Resource, String>) (resource, content) -> {});
172         }
173 
174         @Override
175         public Class<String> getType() {
176             return String.class;
177         }
178 
179         @Override
180         public Class<?> getPropertyHolderType() {
181             return Resource.class;
182         }
183 
184         @Override
185         public String getName() {
186             return NAME;
187         }
188 
189         @Override
190         public String getCaption() {
191             return NAME;
192         }
193 
194         @Override
195         public PropertySet<Resource> getPropertySet() {
196             return propertySet;
197         }
198     }
199 }