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 java.util.ArrayList;
37  import java.util.List;
38  import java.util.Optional;
39  import java.util.stream.Stream;
40  
41  import javax.jcr.Item;
42  import javax.jcr.Node;
43  
44  import com.vaadin.data.PropertyDefinition;
45  import com.vaadin.data.PropertySet;
46  import com.vaadin.data.ValueProvider;
47  import com.vaadin.server.Setter;
48  
49  import lombok.Builder;
50  import lombok.Data;
51  
52  
53  /**
54   * Describes a set of JCR properties that can be used for configuration based on
55   * {@link JcrPropertyDescriptor}. Supports both JCR nodes and properties.
56   *
57   * In order to not clutter this class logic, the actual interaction with JCR items is
58   * delegated to {@link JcrItemInteractionStrategy} (provides separate implementation
59   * for nodes and properties).
60   */
61  public class JcrItemPropertySet implements PropertySet<Item> {
62  
63      /**
64       * Jcr property descriptor.
65       * @param <T>
66       */
67      @Builder
68      @Data
69      public static class JcrPropertyDescriptor<T> {
70          private final Class<T> type;
71          private final String name;
72      }
73  
74      private List<PropertyDefinition<Item, ?>> knownProperties = new ArrayList<>();
75  
76      public JcrItemPropertySet(List<JcrPropertyDescriptor> knownProperties) {
77          //noinspection unchecked
78          knownProperties.forEach(property -> this.knownProperties.add(new JcrItemPropertyDefinition<>(this, property)));
79      }
80  
81      @Override
82      public Stream<PropertyDefinition<Item, ?>> getProperties() {
83          return knownProperties.stream();
84      }
85  
86      @Override
87      public Optional<PropertyDefinition<Item, ?>> getProperty(String name) {
88          return knownProperties.stream()
89                  .filter(def -> def.getName().equals(name))
90                  .findFirst();
91      }
92  
93      private final class JcrItemPropertyDefinition<V> implements PropertyDefinition<Item, V> {
94  
95          private final PropertySet<Item> propertySet;
96          private final JcrItemPropertySet.JcrPropertyDescriptor<V> descriptor;
97  
98          private JcrItemPropertyDefinition(PropertySet<Item> propertySet, JcrPropertyDescriptor<V> descriptor) {
99              this.propertySet = propertySet;
100             this.descriptor = descriptor;
101         }
102 
103         @Override
104         public ValueProvider<Item, V> getGetter() {
105             return item -> JcrItemInteractionStrategy.get(item).get(item, descriptor);
106         }
107 
108         @Override
109         public Optional<Setter<Item, V>> getSetter() {
110             return Optional.of((Setter<Item, V>) (Item item, V value) -> JcrItemInteractionStrategy.get(item).set(item, value, descriptor));
111         }
112 
113         @Override
114         public Class<V> getType() {
115             return descriptor.getType();
116         }
117 
118         @Override
119         public Class<?> getPropertyHolderType() {
120             return Node.class;
121         }
122 
123         @Override
124         public String getName() {
125             return descriptor.getName();
126         }
127 
128         @Override
129         public String getCaption() {
130             return descriptor.getName();
131         }
132 
133         @Override
134         public PropertySet<Item> getPropertySet() {
135             return propertySet;
136         }
137 
138 
139     }
140 }