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.contentapp;
35  
36  import info.magnolia.objectfactory.ComponentProvider;
37  import info.magnolia.objectfactory.Components;
38  import info.magnolia.ui.api.i18n.I18NAuthoringSupport;
39  import info.magnolia.ui.datasource.PropertySetFactory;
40  import info.magnolia.ui.datasource.jcr.JcrDatasourceDefinition;
41  import info.magnolia.ui.editor.FormDefinition;
42  import info.magnolia.ui.editor.JcrItemPropertySet;
43  import info.magnolia.ui.editor.JcrItemPropertySet.JcrPropertyDescriptor;
44  import info.magnolia.ui.editor.JcrItemPropertySet.JcrPropertyDescriptor.JcrPropertyDescriptorBuilder;
45  import info.magnolia.ui.field.FieldDefinition;
46  import info.magnolia.ui.field.NoopNameDecorator;
47  import info.magnolia.ui.field.WithPropertyNameDecorator;
48  import info.magnolia.ui.field.WithPropertyNameDecorator.PropertyNameDecorator;
49  
50  import java.util.Collection;
51  import java.util.List;
52  import java.util.Locale;
53  import java.util.Map;
54  import java.util.stream.Collectors;
55  
56  import javax.inject.Inject;
57  import javax.jcr.Item;
58  
59  import com.vaadin.data.PropertySet;
60  
61  /**
62   * {@link PropertySet PropertySet} factory which yields JCR-specific implementations.
63   */
64  public class JcrPropertySetFactory implements PropertySetFactory<Item> {
65  
66      private final I18NAuthoringSupport<Item> localisationSupport;
67      private final ComponentProvider componentProvider;
68      private final JcrDatasourceDefinition datasourceDefinition;
69  
70      @Inject
71      JcrPropertySetFactory(I18NAuthoringSupport<Item> localisationSupport, ComponentProvider componentProvider, JcrDatasourceDefinition datasourceDefinition) {
72          this.localisationSupport = localisationSupport;
73          this.componentProvider = componentProvider;
74          this.datasourceDefinition = datasourceDefinition;
75      }
76  
77      /**
78       * @deprecated since 6.2.4. Use {@link #JcrPropertySetFactory(info.magnolia.ui.api.i18n.I18NAuthoringSupport, info.magnolia.objectfactory.ComponentProvider, info.magnolia.ui.datasource.jcr.JcrDatasourceDefinition)} instead.
79       */
80      @Deprecated
81      public JcrPropertySetFactory(I18NAuthoringSupport<Item> localisationSupport, ComponentProvider componentProvider) {
82          this(localisationSupport, componentProvider, Components.getComponent(JcrDatasourceDefinition.class));
83      }
84  
85          @Override
86      public PropertySet<Item> fromFormDefinition(FormDefinition<Item> formDefinition, Locale locale) {
87          final PropertyNameDecorator propertyNameDecorator;
88          if (formDefinition instanceof WithPropertyNameDecorator) {
89              Class<? extends PropertyNameDecorator> propertyNameDecoratorType =
90                      ((WithPropertyNameDecorator) formDefinition).getPropertyNameDecorator();
91  
92              propertyNameDecorator = componentProvider.newInstance(propertyNameDecoratorType);
93          } else {
94              propertyNameDecorator = new NoopNameDecorator();
95          }
96  
97          final List<JcrPropertyDescriptor> propertyDefinitions =
98                  formDefinition.getFieldDefinitions().stream()
99                          .map(fieldDefinition -> initPropertyDescriptor(locale, fieldDefinition))
100                         .map(propertyDescriptorBuilder -> propertyDescriptorBuilder.propertyNameDecorator(propertyNameDecorator))
101                         .map(JcrPropertyDescriptorBuilder::build)
102                         .collect(Collectors.toList());
103 
104         return new JcrItemPropertySet(propertyDefinitions, locale, localisationSupport, propertyNameDecorator);
105     }
106 
107     @Override
108     public PropertySet<Item> withProperties(Map<String, Class> properties) {
109         final List<JcrPropertyDescriptor> propertyDescriptors = properties.entrySet().stream()
110                 .map(entry -> JcrPropertyDescriptor.builder()
111                         .type(entry.getValue())
112                         .name(entry.getKey())
113                         .nodeNameProperty(datasourceDefinition.getNodeNameProperty())
114                         .build())
115                 .collect(Collectors.toList());
116 
117         return new JcrItemPropertySet(propertyDescriptors, localisationSupport);
118     }
119 
120     @Override
121     public PropertySet<Item> fromFieldDefinitions(Collection<FieldDefinition> fieldDefinitions, Locale locale) {
122         final List<JcrPropertyDescriptor> propertyDefinitions =
123                 fieldDefinitions.stream()
124                         .map(fieldDefinition -> initPropertyDescriptor(locale, fieldDefinition).build())
125                         .collect(Collectors.toList());
126 
127         return new JcrItemPropertySet(propertyDefinitions, locale, localisationSupport);
128     }
129 
130     public JcrPropertyDescriptorBuilder initPropertyDescriptor(Locale locale, FieldDefinition fieldDefinition) {
131         return JcrPropertyDescriptor
132                 .builder()
133                 .name(fieldDefinition.getName())
134                 .type(fieldDefinition.getType())
135                 .isI18n(fieldDefinition.isI18n())
136                 .locale(locale)
137                 .nodeNameProperty(datasourceDefinition.getNodeNameProperty())
138                 .isReadonly(fieldDefinition.isReadOnly());
139     }
140 }