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.form.definition;
35  
36  import info.magnolia.ui.field.ConfiguredFieldDefinition;
37  import info.magnolia.ui.form.ConfiguredFormDefinition;
38  import info.magnolia.ui.field.ConfiguredNamedFieldDefinition;
39  import info.magnolia.ui.field.FieldDefinition;
40  import info.magnolia.ui.form.FormDefinition;
41  import info.magnolia.ui.field.NamedFieldDefinition;
42  import info.magnolia.ui.field.TextFieldDefinition;
43  import info.magnolia.ui.framework.databinding.layout.ConfiguredTabDefinition;
44  import info.magnolia.ui.framework.databinding.layout.TabDefinition;
45  import info.magnolia.ui.framework.databinding.layout.TabbedLayoutDefinition;
46  
47  import java.util.List;
48  import java.util.stream.Collectors;
49  
50  import org.slf4j.Logger;
51  import org.slf4j.LoggerFactory;
52  
53  import net.sf.cglib.proxy.Enhancer;
54  
55  /**
56   * Functional interface for definition converters.
57   *
58   * @param <OLD> - old definition class
59   * @param <NEW> - new definition class
60   */
61  @FunctionalInterface
62  public interface DefinitionConverter<OLD, NEW> {
63  
64      NEW convert(OLD definition);
65  
66      DefinitionConverter<info.magnolia.ui.form.field.definition.FieldDefinition, NamedFieldDefinition> NAMED = (oldDefinition) -> {
67          ConfiguredNamedFieldDefinition namedDefinition = new ConfiguredNamedFieldDefinition();
68          namedDefinition.setName(oldDefinition.getName());
69          return namedDefinition;
70      };
71  
72      DefinitionConverter<info.magnolia.ui.form.definition.TabDefinition, TabDefinition> TAB = (oldDefinition) -> {
73          info.magnolia.ui.framework.databinding.layout.ConfiguredTabDefinition newDefinition = new ConfiguredTabDefinition();
74          newDefinition.setLabel(oldDefinition.getLabel());
75          newDefinition.setName(oldDefinition.getName());
76          newDefinition.setFields(oldDefinition.getFields().stream().map(NAMED::convert).collect(Collectors.toList()));
77          return newDefinition;
78      };
79  
80      DefinitionConverter<info.magnolia.ui.form.field.definition.FieldDefinition, FieldDefinition<?>> FIELD = (oldDefinition) -> {
81          final Logger log = LoggerFactory.getLogger(DefinitionConverter.class);
82  
83          Class oldDefinitionClass = Enhancer.isEnhanced(oldDefinition.getClass()) ? oldDefinition.getClass().getSuperclass() : oldDefinition.getClass();
84          ConfiguredFieldDefinition newDefinition;
85          try {
86              newDefinition = (ConfiguredFieldDefinition) Class.forName("info.magnolia.ui.field." + oldDefinitionClass.getSimpleName()).newInstance();
87          } catch (ReflectiveOperationException e) {
88              log.error("No replacement for old {}. Falling back to {}", oldDefinitionClass, TextFieldDefinition.class, e);
89              newDefinition = new TextFieldDefinition();
90          }
91          newDefinition.setDefaultValue(oldDefinition.getDefaultValue());
92          newDefinition.setDescription(oldDefinition.getDescription());
93          newDefinition.setI18n(oldDefinition.isI18n());
94          newDefinition.setLabel(oldDefinition.getLabel());
95          newDefinition.setName(oldDefinition.getName());
96          newDefinition.setReadOnly(oldDefinition.isReadOnly());
97          newDefinition.setRequired(oldDefinition.isRequired());
98          newDefinition.setRequiredErrorMessage(oldDefinition.getRequiredErrorMessage());
99          newDefinition.setStyleName(oldDefinition.getStyleName());
100         newDefinition.setValidators(oldDefinition.getValidators());
101         if (oldDefinition.getConverterClass() != null) {
102             log.error("Converter are not supported for task detail view. No converter will be set for field {}", oldDefinition.getConverterClass(), oldDefinition.getName());
103         }
104         return newDefinition;
105     };
106 
107     DefinitionConverter<info.magnolia.ui.form.definition.FormDefinition, FormDefinition> FORM = (oldDefinition) -> {
108         TabbedLayoutDefinition tabbedLayoutDefinition = new TabbedLayoutDefinition();
109         tabbedLayoutDefinition.setTabs(oldDefinition.getTabs().stream().map(TAB::convert).collect(Collectors.toList()));
110         ConfiguredFormDefinition<?> newDefinition = new ConfiguredFormDefinition<>();
111         newDefinition.setName(oldDefinition.getLabel());
112         newDefinition.setLayout(tabbedLayoutDefinition);
113         //newDefinition.setImplementationClass();
114         newDefinition.setProperties(oldDefinition
115                 .getTabs()
116                 .stream()
117                 .map(info.magnolia.ui.form.definition.TabDefinition::getFields)
118                 .flatMap(List::stream)
119                 .map(FIELD::convert)
120                 .collect(Collectors.toList()));
121         return newDefinition;
122     };
123 }