View Javadoc
1   /**
2    * This file Copyright (c) 2013-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.form.field.transformer.basic;
35  
36  import info.magnolia.objectfactory.Components;
37  import info.magnolia.ui.api.i18n.I18NAuthoringSupport;
38  import info.magnolia.ui.form.field.definition.ConfiguredFieldDefinition;
39  import info.magnolia.ui.form.field.definition.OptionGroupFieldDefinition;
40  
41  import java.util.Collection;
42  import java.util.HashSet;
43  import java.util.LinkedList;
44  import java.util.List;
45  import java.util.Set;
46  
47  import org.slf4j.Logger;
48  import org.slf4j.LoggerFactory;
49  
50  import com.vaadin.v7.data.Item;
51  import com.vaadin.v7.data.Property;
52  
53  /**
54   * Specific transformer for use with multi-select {@link OptionGroupFieldDefinition}.
55   * <p>
56   * Indeed, Vaadin's {@link com.vaadin.ui.OptionGroup OptionGroup} operates selection with sets, whereas we resolve
57   * JCR multi-value properties as lists.
58   * <p>
59   * Also mind that for single-select definitions, this transformer merely works like a plain {@link BasicTransformer}.
60   *
61   * @param <T>
62   */
63  public class ListToSetTransformer<T> extends BasicTransformer<T> {
64  
65      private static final Logger log = LoggerFactory.getLogger(ListToSetTransformer.class);
66  
67      private final boolean multiselect;
68  
69      /**
70       * @deprecated since 5.4.2 - use {@link #ListToSetTransformer(Item, ConfiguredFieldDefinition, Class, I18NAuthoringSupport)} instead.
71       */
72      @Deprecated
73      public ListToSetTransformer(Item relatedFormItem, ConfiguredFieldDefinition definition, Class<T> type) {
74          this(relatedFormItem, definition, type, Components.getComponent(I18NAuthoringSupport.class));
75      }
76  
77      public ListToSetTransformer(Item relatedFormItem, ConfiguredFieldDefinition definition, Class<T> type, I18NAuthoringSupport i18NAuthoringSupport) {
78          super(relatedFormItem, definition, type, i18NAuthoringSupport);
79          if (definition instanceof OptionGroupFieldDefinition) {
80              multiselect = ((OptionGroupFieldDefinition) definition).isMultiselect();
81          } else {
82              multiselect = false;
83              log.info("ListToSetTransformer is intended for multiselect OptionGroupFieldDefinition, but definition was of type {}. This may behave like a plain BasicTransformer.", definition);
84          }
85      }
86  
87      @Override
88      public void writeToItem(T newValue) {
89          if (!multiselect) {
90              super.writeToItem(newValue);
91              return;
92          }
93  
94          Property<T> p = getOrCreateProperty(type, false);
95          if (isCollectionConversionNeeded(newValue, p.getType())) {
96              newValue = (T) new LinkedList((Set) newValue);
97          }
98          if (newValue instanceof Collection && ((Collection) newValue).isEmpty()) {
99              newValue = null;
100         }
101         p.setValue(newValue);
102     }
103 
104     /**
105      * Check if the newValue has to be transformed from a {@link Set} to a {@link List}. {@link Set} is used by Vaadin multi fields and multi values are stored as {@link List} in Jcr.
106      */
107     protected boolean isCollectionConversionNeeded(T newValue, Class<?> propertyType) {
108         return List.class.isAssignableFrom(propertyType) && newValue instanceof Set;
109     }
110 
111     @Override
112     public T readFromItem() {
113         if (!multiselect) {
114             return super.readFromItem();
115         }
116 
117         Property<T> p = getOrCreateProperty(type, false);
118         if (definition.isReadOnly()) {
119             p.setReadOnly(true);
120         }
121         T value = p.getValue();
122         if (value != null && value instanceof List) {
123             return (T) new HashSet((List) value);
124         }
125         return null;
126     }
127 
128 }