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.databinding;
35  
36  import static java.util.stream.Collectors.toMap;
37  
38  import info.magnolia.ui.contentapp.configuration.column.ColumnDefinition;
39  import info.magnolia.ui.field.FieldDefinition;
40  
41  import java.util.ArrayList;
42  import java.util.Collection;
43  import java.util.List;
44  import java.util.Map;
45  import java.util.Optional;
46  import java.util.function.Function;
47  import java.util.stream.Collectors;
48  import java.util.stream.Stream;
49  
50  import javax.jcr.Item;
51  import javax.jcr.Node;
52  
53  import com.vaadin.data.PropertyDefinition;
54  import com.vaadin.data.PropertySet;
55  import com.vaadin.data.ValueProvider;
56  import com.vaadin.server.Setter;
57  
58  import lombok.Builder;
59  import lombok.Data;
60  
61  
62  /**
63   * Describes a set of JCR properties that can be used for configuration based on
64   * {@link JcrPropertyDescriptor}. Supports both JCR nodes and properties.
65   *
66   * In order to not clutter this class logic, the actual interaction with JCR items is
67   * delegated to {@link JcrItemInteractionStrategy} (provides separate implementation
68   * for nodes and properties).
69   */
70  public class JcrItemPropertySet implements PropertySet<Item> {
71  
72  
73      public static PropertySet<Item> withProperties(Map<String, Class> properties) {
74          final List<JcrItemPropertySet.JcrPropertyDescriptor> propertyDescriptors = properties.entrySet().stream()
75                  .map(entry -> JcrItemPropertySet.JcrPropertyDescriptor.builder()
76                          .type(entry.getValue())
77                          .name(entry.getKey())
78                          .build())
79                  .collect(Collectors.toList());
80  
81          return new JcrItemPropertySet(propertyDescriptors);
82      }
83  
84      public static <T> PropertySet<Item> fromColumns(List<ColumnDefinition<T>> columnDefinitions) {
85          return withProperties(columnDefinitions.stream().collect(toMap(ColumnDefinition::getName, ColumnDefinition::getType)));
86      }
87  
88      public static PropertySet<Item> fromFieldDefinitions(Collection<FieldDefinition> fieldDefinitions, Function<FieldDefinition, String> propertyNameTransformer) {
89          //noinspection unchecked
90          return new JcrItemPropertySet(fieldDefinitions.stream()
91                  .map(fieldDefinition -> JcrItemPropertySet.JcrPropertyDescriptor
92                          .builder()
93                          .name(propertyNameTransformer.apply(fieldDefinition))
94                          .type(fieldDefinition.getType())
95                          .build())
96                  .collect(Collectors.toList()));
97      }
98  
99      /**
100      * Jcr property descriptor.
101      * @param <T>
102      */
103     @Builder
104     @Data
105     public static class JcrPropertyDescriptor<T> {
106         private final Class<T> type;
107         private final String name;
108     }
109 
110     private List<PropertyDefinition<Item, ?>> knownProperties = new ArrayList<>();
111 
112     public JcrItemPropertySet(List<JcrPropertyDescriptor> knownProperties) {
113         //noinspection unchecked
114         knownProperties.forEach(property -> this.knownProperties.add(new JcrItemPropertyDefinition<>(this, property)));
115     }
116 
117     @Override
118     public Stream<PropertyDefinition<Item, ?>> getProperties() {
119         return knownProperties.stream();
120     }
121 
122     @Override
123     public Optional<PropertyDefinition<Item, ?>> getProperty(String name) {
124         return Optional.of(knownProperties.stream()
125                 .filter(def -> def.getName().equals(name))
126                 .findFirst()
127                 .orElse(new JcrItemPropertyDefinition<>(this, JcrItemPropertySet.JcrPropertyDescriptor.builder()
128                         .type(Object.class)
129                         .name(name)
130                         .build())
131                 )
132         );
133     }
134 
135     private final class JcrItemPropertyDefinition<V> implements PropertyDefinition<Item, V> {
136 
137         private final PropertySet<Item> propertySet;
138         private final JcrItemPropertySet.JcrPropertyDescriptor<V> descriptor;
139 
140         private JcrItemPropertyDefinition(PropertySet<Item> propertySet, JcrPropertyDescriptor<V> descriptor) {
141             this.propertySet = propertySet;
142             this.descriptor = descriptor;
143         }
144 
145         @Override
146         public ValueProvider<Item, V> getGetter() {
147             return item -> JcrItemInteractionStrategy.get(item).get(item, descriptor);
148         }
149 
150         @Override
151         public Optional<Setter<Item, V>> getSetter() {
152             return Optional.of((Setter<Item, V>) (Item item, V value) -> JcrItemInteractionStrategy.get(item).set(item, value, descriptor));
153         }
154 
155         @Override
156         public Class<V> getType() {
157             return descriptor.getType();
158         }
159 
160         @Override
161         public Class<?> getPropertyHolderType() {
162             return Node.class;
163         }
164 
165         @Override
166         public String getName() {
167             return descriptor.getName();
168         }
169 
170         @Override
171         public String getCaption() {
172             return descriptor.getName();
173         }
174 
175         @Override
176         public PropertySet<Item> getPropertySet() {
177             return propertySet;
178         }
179 
180 
181     }
182 }