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.definition;
35  
36  import info.magnolia.ui.api.app.AppDescriptor;
37  import info.magnolia.ui.editor.i18n.EditorPropertyDefinitionKeyGenerator;
38  import info.magnolia.ui.field.EditorPropertyDefinition;
39  import info.magnolia.ui.editor.AbstractFormKeyGenerator;
40  import info.magnolia.ui.form.definition.FormDefinition;
41  import info.magnolia.ui.form.definition.TabDefinition;
42  
43  import java.lang.reflect.AnnotatedElement;
44  import java.lang.reflect.Method;
45  import java.util.Deque;
46  import java.util.LinkedList;
47  import java.util.List;
48  import java.util.Optional;
49  
50  import org.apache.commons.lang3.StringUtils;
51  import org.slf4j.Logger;
52  import org.slf4j.LoggerFactory;
53  
54  /**
55   * An {@link info.magnolia.i18nsystem.I18nKeyGenerator} for {@link FieldDefinition}.
56   */
57  public class FieldDefinitionKeyGenerator extends EditorPropertyDefinitionKeyGenerator {
58  
59      protected static final String CHOOSE_DIALOG = "chooseDialog";
60  
61      private static final Logger log = LoggerFactory.getLogger(FieldDefinitionKeyGenerator.class);
62  
63      private static final String CHOOSE_DIALOG_DEFINITION = "ChooseDialogDefinition";
64  
65      @Override
66      protected void keysFor(List<String> list, EditorPropertyDefinition field, AnnotatedElement el) {
67          Object parent = getParentViaCast(field);
68          final String fieldName = replaceColons(field.getName());
69          final String fieldOrGetterName = fieldOrGetterName(el);
70  
71          if (parent != null && isChooseDialog(parent.getClass())) {
72              // handle choose dialog
73              final AppDescriptor app = (AppDescriptor) getRoot(field);
74              addKey(list, app.getName(), CHOOSE_DIALOG, FIELDS, fieldName, fieldOrGetterName);
75          } else {
76              final Deque<String> parentNames = new LinkedList<>();
77              while (parent != null && !(parent instanceof TabDefinition)) {
78                  getParentName(parent).ifPresent(parentNames::addFirst);
79                  parent = getParentViaCast(parent);
80              }
81              final String parentKeyPart = replaceColons(StringUtils.join(parentNames, '.'));
82  
83              if (parent instanceof TabDefinition) {
84  
85                  TabDefinition./../../../../info/magnolia/ui/form/definition/TabDefinition.html#TabDefinition">TabDefinition tab = (TabDefinition) parent;
86                  final String tabName = tab.getName();
87                  final FormDefinition formDef = getParentViaCast(tab);
88                  final String rawDialogID = getIdOrNameForUnknownRoot(formDef, false);
89                  final String dialogID = keyify(rawDialogID);
90  
91                  // in case of a field in field
92                  if (parentNames.size() > 0) {
93                      // <dialogId>.<tabName>.<parentFieldNames_separated_by_dots>.<fieldName>.<property>
94                      // <dialogId>.<tabName>.<parentFieldNames_separated_by_dots>.<fieldName> (in case of property==label)
95                      addKey(list, true, dialogID, tabName, parentKeyPart, fieldName, fieldOrGetterName);
96                  }
97                  // <dialogId>.<tabName>.<fieldName>.<property>
98                  // <dialogId>.<tabName>.<fieldName> (in case of property==label)
99                  addKey(list, dialogID, tabName, fieldName, fieldOrGetterName);
100                 // <tabName>.<fieldName> (in case of property==label)
101                 addKey(list, tabName, fieldName, fieldOrGetterName);
102                 // <dialogId>.<fieldName>.<property>
103                 // <dialogId>.<fieldName> (in case property==label)
104                 addKey(list, dialogID, fieldName, fieldOrGetterName);
105                 String[] parts = StringUtils.split(dialogID, ".");
106                 if (parts.length > 1) {
107                     String dialogIDNoModuleName = parts[parts.length - 1];
108                     addKey(list, dialogIDNoModuleName, fieldName, fieldOrGetterName);
109                     addKey(list, dialogIDNoModuleName, tabName, fieldName, fieldOrGetterName);
110                 }
111             } else {
112                 // In case we didn't encounter parent tab definition - we simply generated a key based on dot-separated parent names
113                 addKey(list, parentKeyPart, fieldName, fieldOrGetterName);
114             }
115         }
116         addKey(list, FIELDS, fieldName, fieldOrGetterName);
117     }
118 
119     /**
120      * Dirty hack, as the ChooseDialogDefinition is defined in dependent module.
121      */
122     private boolean isChooseDialog(Class<?> clazz) {
123         /**
124          * We can't really use something smarter than the current implementation due to following reasons:
125          * -  Fetching the ChooseDialogDefinition class with reflection and using Class#isAssignableFrom is practically impossible to test
126          * (test classes can't access ChooseDialogDefinition).
127          * - Using String#endsWith() is not feasible because enhancer appends its suffix to the class name.
128          */
129         return clazz.getSimpleName().contains(CHOOSE_DIALOG_DEFINITION);
130     }
131 
132     /**
133      * TODO - this method has to be considered for removal in favour of using the {@link AbstractFormKeyGenerator#getIdOrNameForUnknownRoot(Object)}.
134      *
135      * @see <a href="http://jira.magnolia-cms.com/browse/MGNLUI-4169">MGNLUI-4169</a>
136      */
137     private Optional<String> getParentName(Object parent) {
138         try {
139             Method getNameMethod = parent.getClass().getMethod("getName");
140             return Optional.of((String) getNameMethod.invoke(parent));
141         } catch (NoSuchMethodException e) {
142             // TODO - at this point the exception could be avoided if AbstractFormKeyGenerator#getIdOrNameForUnknownRoot(Object) was used
143             log.debug("Failed to obtain the name property value of an object [{}]: {}", parent, e.getMessage());
144         } catch (ReflectiveOperationException e) {
145             log.warn("Failed to obtain the name property value of an object [{}]: {}", parent, e.getMessage());
146         }
147         return Optional.empty();
148     }
149 }