1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
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
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
92 if (parentNames.size() > 0) {
93
94
95 addKey(list, true, dialogID, tabName, parentKeyPart, fieldName, fieldOrGetterName);
96 }
97
98
99 addKey(list, dialogID, tabName, fieldName, fieldOrGetterName);
100
101 addKey(list, tabName, fieldName, fieldOrGetterName);
102
103
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
113 addKey(list, parentKeyPart, fieldName, fieldOrGetterName);
114 }
115 }
116 addKey(list, FIELDS, fieldName, fieldOrGetterName);
117 }
118
119
120
121
122 private boolean isChooseDialog(Class<?> clazz) {
123
124
125
126
127
128
129 return clazz.getSimpleName().contains(CHOOSE_DIALOG_DEFINITION);
130 }
131
132
133
134
135
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
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 }