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.module.blossom.dialog; |
35 |
|
|
36 |
|
import info.magnolia.cms.security.MgnlUser; |
37 |
|
import info.magnolia.cms.security.User; |
38 |
|
import info.magnolia.context.Context; |
39 |
|
import info.magnolia.context.MgnlContext; |
40 |
|
import info.magnolia.context.WebContext; |
41 |
|
import info.magnolia.init.MagnoliaConfigurationProperties; |
42 |
|
import info.magnolia.module.blossom.annotation.TabFactory; |
43 |
|
import info.magnolia.module.blossom.support.MethodInvocationUtils; |
44 |
|
import info.magnolia.module.blossom.support.ParameterResolver; |
45 |
|
import info.magnolia.objectfactory.Components; |
46 |
|
import info.magnolia.ui.dialog.config.DialogBuilder; |
47 |
|
import info.magnolia.ui.dialog.definition.ConfiguredFormDialogDefinition; |
48 |
|
import info.magnolia.ui.dialog.definition.FormDialogDefinition; |
49 |
|
import info.magnolia.ui.form.config.TabBuilder; |
50 |
|
import info.magnolia.ui.form.definition.ConfiguredFormDefinition; |
51 |
|
import info.magnolia.ui.form.definition.TabDefinition; |
52 |
|
import info.magnolia.ui.framework.config.UiConfig; |
53 |
|
|
54 |
|
import java.lang.reflect.Method; |
55 |
|
import java.util.Collections; |
56 |
|
import java.util.Comparator; |
57 |
|
|
58 |
|
import javax.jcr.Node; |
59 |
|
|
60 |
|
import org.apache.commons.lang.ArrayUtils; |
61 |
|
import org.apache.commons.lang.StringUtils; |
62 |
|
import org.springframework.beans.BeanUtils; |
63 |
|
import org.springframework.beans.factory.InitializingBean; |
64 |
|
import org.springframework.util.ClassUtils; |
65 |
|
|
66 |
|
import com.vaadin.data.Item; |
67 |
|
|
68 |
|
|
69 |
|
@link |
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
@since |
76 |
|
|
|
|
| 75% |
Uncovered Elements: 42 (168) |
Complexity: 50 |
Complexity Density: 0.52 |
|
77 |
|
public class DefaultDialogCreator implements DialogCreator, InitializingBean { |
78 |
|
|
79 |
|
public static final String SORT_TABS_BY_LABEL_PROPERTY_NAME = "magnolia.blossom.sortTabsByLabel"; |
80 |
|
public static final Boolean SORT_TABS_BY_LABEL_PROPERTY_DEFAULT = Boolean.TRUE; |
81 |
|
|
82 |
|
public static final String SET_TAB_LABELS_PROPERTY_NAME = "magnolia.blossom.setTabLabels"; |
83 |
|
public static final Boolean SET_TAB_LABELS_PROPERTY_DEFAULT = Boolean.TRUE; |
84 |
|
|
85 |
|
private static final String[] DAM_CONFIG_CLASS_NAMES = new String[]{ |
86 |
|
"info.magnolia.dam.app.ui.config.DamConfig", |
87 |
|
"info.magnolia.dam.asset.config.DamConfig" |
88 |
|
}; |
89 |
|
|
90 |
|
private Class damConfigClass; |
91 |
|
private Boolean setTabLabels; |
92 |
|
private Boolean sortTabsByLabel; |
93 |
|
|
|
|
| 57.1% |
Uncovered Elements: 3 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
94 |
41 |
public DefaultDialogCreator() {... |
95 |
41 |
ClassLoader classLoader = this.getClass().getClassLoader(); |
96 |
41 |
for (String damConfigClassName : DAM_CONFIG_CLASS_NAMES) { |
97 |
82 |
if (ClassUtils.isPresent(damConfigClassName, classLoader)) { |
98 |
0 |
damConfigClass = ClassUtils.resolveClassName(damConfigClassName, classLoader); |
99 |
0 |
break; |
100 |
|
} |
101 |
|
} |
102 |
|
} |
103 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
104 |
|
public Boolean getSetTabLabels() {... |
105 |
|
return setTabLabels; |
106 |
|
} |
107 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
108 |
|
public void setSetTabLabels(Boolean setTabLabels) {... |
109 |
|
this.setTabLabels = setTabLabels; |
110 |
|
} |
111 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
112 |
|
public Boolean getSortTabsByLabel() {... |
113 |
|
return sortTabsByLabel; |
114 |
|
} |
115 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
116 |
|
public void setSortTabsByLabel(Boolean sortTabsByLabel) {... |
117 |
|
this.sortTabsByLabel = sortTabsByLabel; |
118 |
|
} |
119 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (15) |
Complexity: 3 |
Complexity Density: 0.27 |
|
120 |
20 |
@Override... |
121 |
|
public void createDialog(DialogFactoryMetaData metaData, final DialogCreationContext context) throws Exception { |
122 |
|
|
123 |
20 |
DialogBuilder dialogBuilder = new DialogBuilder(context.getId()); |
124 |
20 |
context.setDialog(dialogBuilder.definition()); |
125 |
20 |
context.getDialog().setForm(new ConfiguredFormDefinition()); |
126 |
20 |
context.getDialog().setPresenterClass(BlossomFormDialogPresenter.class); |
127 |
|
|
128 |
20 |
if (StringUtils.isNotEmpty(metaData.getLabel())) { |
129 |
11 |
ConfiguredFormDefinition form = (ConfiguredFormDefinition) dialogBuilder.definition().getForm(); |
130 |
11 |
form.setLabel(metaData.getLabel()); |
131 |
11 |
dialogBuilder.label(metaData.getLabel()); |
132 |
|
} |
133 |
|
|
134 |
20 |
if (metaData.getFactoryMethod() != null) { |
135 |
4 |
invokeMethodDialogFactory(metaData, context, dialogBuilder); |
136 |
|
} else { |
137 |
16 |
invokeClassDialogFactory(metaData, context, dialogBuilder); |
138 |
|
} |
139 |
|
} |
140 |
|
|
|
|
| 60% |
Uncovered Elements: 4 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
141 |
4 |
protected void invokeMethodDialogFactory(DialogFactoryMetaData metaData, DialogCreationContext context, DialogBuilder dialogBuilder) {... |
142 |
|
|
143 |
4 |
if (StringUtils.isNotEmpty(metaData.getI18nBasename())) { |
144 |
0 |
dialogBuilder.i18nBasename(metaData.getI18nBasename()); |
145 |
|
} |
146 |
|
|
147 |
4 |
ParameterResolver parameters = getDialogFactoryParameters(metaData, context, dialogBuilder); |
148 |
4 |
MethodInvocationUtils.invoke(metaData.getFactoryMethod(), metaData.getFactoryObject(), parameters); |
149 |
|
|
150 |
4 |
if (metaData.getTabOrder() != null) { |
151 |
0 |
sortTabs(context.getDialog(), metaData.getTabOrder()); |
152 |
|
} |
153 |
|
} |
154 |
|
|
|
|
| 92.6% |
Uncovered Elements: 2 (27) |
Complexity: 5 |
Complexity Density: 0.24 |
|
155 |
16 |
protected void invokeClassDialogFactory(DialogFactoryMetaData metaData, DialogCreationContext context, DialogBuilder dialogBuilder) {... |
156 |
|
|
157 |
16 |
if (metaData.getI18nBasename() != null) { |
158 |
0 |
dialogBuilder.i18nBasename(metaData.getI18nBasename()); |
159 |
|
} |
160 |
|
|
161 |
16 |
Object factoryObject = metaData.getFactoryObject(); |
162 |
|
|
163 |
16 |
for (DialogFactoryClassMetaData classMetaData : metaData.getClassMetaData()) { |
164 |
|
|
165 |
18 |
int tabPosition = 0; |
166 |
|
|
167 |
18 |
for (Method tabFactory : classMetaData.getTabFactories()) { |
168 |
|
|
169 |
24 |
TabFactory annotation = tabFactory.getAnnotation(TabFactory.class); |
170 |
24 |
TabBuilder tabBuilder = new TabBuilder(annotation.value()); |
171 |
24 |
if (setTabLabels == null || setTabLabels) { |
172 |
22 |
tabBuilder.label(annotation.value()); |
173 |
|
} |
174 |
24 |
dialogBuilder.form().definition().getTabs().add(tabPosition++, tabBuilder.definition()); |
175 |
|
|
176 |
24 |
ParameterResolver parameters = getTabFactoryParameters(metaData, context, tabBuilder); |
177 |
24 |
MethodInvocationUtils.invoke(tabFactory, metaData.getFactoryObject(), parameters); |
178 |
|
} |
179 |
|
|
180 |
18 |
for (Method postCreateMethod : classMetaData.getPostCreateCallbacks()) { |
181 |
|
|
182 |
7 |
ParameterResolver parameters = getPostCreateCallbackParameters(metaData, context, dialogBuilder); |
183 |
7 |
MethodInvocationUtils.invoke(postCreateMethod, factoryObject, parameters); |
184 |
|
} |
185 |
|
} |
186 |
|
|
187 |
16 |
for (Method postCreateMethod : metaData.getPostCreateCallbacks()) { |
188 |
|
|
189 |
2 |
ParameterResolver parameters = getPostCreateCallbackParameters(metaData, context, dialogBuilder); |
190 |
2 |
MethodInvocationUtils.invoke(postCreateMethod, factoryObject, parameters); |
191 |
|
} |
192 |
|
|
193 |
16 |
if (metaData.getTabOrder() != null) { |
194 |
3 |
sortTabs(context.getDialog(), metaData.getTabOrder()); |
195 |
|
} |
196 |
|
} |
197 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
198 |
3 |
protected void sortTabs(FormDialogDefinition dialog, final String[] order) {... |
199 |
3 |
Collections.sort(dialog.getForm().getTabs(), new Comparator<Object>() { |
200 |
|
|
|
|
| 50% |
Uncovered Elements: 9 (18) |
Complexity: 9 |
Complexity Density: 0.75 |
|
201 |
8 |
@Override... |
202 |
|
public int compare(Object o1, Object o2) { |
203 |
8 |
switch (((o1 instanceof TabDefinition) ? 2 : 0) + ((o2 instanceof TabDefinition) ? 1 : 0)) { |
204 |
0 |
case 0: |
205 |
0 |
return 0; |
206 |
0 |
case 1: |
207 |
0 |
return -1; |
208 |
0 |
case 2: |
209 |
0 |
return 1; |
210 |
8 |
case 3: |
211 |
8 |
if (sortTabsByLabel == null || sortTabsByLabel) { |
212 |
6 |
return ArrayUtils.indexOf(order, ((TabDefinition) o1).getLabel()) - ArrayUtils.indexOf(order, ((TabDefinition) o2).getLabel()); |
213 |
|
} else { |
214 |
2 |
return ArrayUtils.indexOf(order, ((TabDefinition) o1).getName()) - ArrayUtils.indexOf(order, ((TabDefinition) o2).getName()); |
215 |
|
} |
216 |
|
} |
217 |
0 |
return 0; |
218 |
|
} |
219 |
|
}); |
220 |
|
} |
221 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
222 |
4 |
protected ParameterResolver getDialogFactoryParameters(DialogFactoryMetaData metaData, DialogCreationContext context, final DialogBuilder dialogBuilder) {... |
223 |
4 |
return new ParameterResolver(getStandardParameters(metaData, context)) { |
224 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
225 |
7 |
@Override... |
226 |
|
public Object resolveParameter(Class<?> parameterType) { |
227 |
7 |
if (parameterType.equals(DialogBuilder.class)) { |
228 |
4 |
return dialogBuilder; |
229 |
|
} |
230 |
3 |
return super.resolveParameter(parameterType); |
231 |
|
} |
232 |
|
}; |
233 |
|
} |
234 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
235 |
24 |
protected ParameterResolver getTabFactoryParameters(DialogFactoryMetaData metaData, DialogCreationContext context, final TabBuilder tabBuilder) {... |
236 |
24 |
return new ParameterResolver(getStandardParameters(metaData, context)) { |
237 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
238 |
14 |
@Override... |
239 |
|
public Object resolveParameter(Class<?> parameterType) { |
240 |
14 |
if (parameterType.equals(TabBuilder.class)) { |
241 |
10 |
return tabBuilder; |
242 |
|
} |
243 |
4 |
return super.resolveParameter(parameterType); |
244 |
|
} |
245 |
|
}; |
246 |
|
} |
247 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
248 |
9 |
protected ParameterResolver getPostCreateCallbackParameters(DialogFactoryMetaData metaData, DialogCreationContext context, final DialogBuilder dialogBuilder) {... |
249 |
9 |
return new ParameterResolver(getStandardParameters(metaData, context)) { |
250 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
251 |
6 |
@Override... |
252 |
|
public Object resolveParameter(Class<?> parameterType) { |
253 |
6 |
if (parameterType.isAssignableFrom(DialogBuilder.class)) { |
254 |
1 |
return dialogBuilder; |
255 |
|
} |
256 |
5 |
return super.resolveParameter(parameterType); |
257 |
|
} |
258 |
|
}; |
259 |
|
} |
260 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
261 |
37 |
protected ParameterResolver getStandardParameters(DialogFactoryMetaData metaData, final DialogCreationContext context) {... |
262 |
37 |
return new ParameterResolver() { |
263 |
|
|
|
|
| 46.3% |
Uncovered Elements: 22 (41) |
Complexity: 12 |
Complexity Density: 0.57 |
|
264 |
12 |
@Override... |
265 |
|
public Object resolveParameter(Class<?> parameterType) { |
266 |
12 |
if (parameterType.isAssignableFrom(ConfiguredFormDialogDefinition.class)) { |
267 |
3 |
return context.getDialog(); |
268 |
|
} |
269 |
9 |
if (parameterType.equals(DialogCreationContext.class)) { |
270 |
3 |
return context; |
271 |
|
} |
272 |
6 |
if (parameterType.equals(Node.class)) { |
273 |
1 |
return context.getContentNode(); |
274 |
|
} |
275 |
5 |
if (parameterType.equals(Item.class)) { |
276 |
3 |
return context.getItem(); |
277 |
|
} |
278 |
2 |
if (parameterType.equals(UiConfig.class)) { |
279 |
2 |
return new UiConfig(); |
280 |
|
} |
281 |
0 |
if (damConfigClass != null && parameterType == damConfigClass) { |
282 |
0 |
return BeanUtils.instantiate(damConfigClass); |
283 |
|
} |
284 |
0 |
if (parameterType.isAssignableFrom(WebContext.class)) { |
285 |
0 |
return MgnlContext.getWebContext(); |
286 |
|
} |
287 |
0 |
if (parameterType.isAssignableFrom(Context.class)) { |
288 |
0 |
return MgnlContext.getInstance(); |
289 |
|
} |
290 |
0 |
if (parameterType.isAssignableFrom(User.class)) { |
291 |
0 |
return MgnlContext.getUser(); |
292 |
|
} |
293 |
0 |
if (parameterType.isAssignableFrom(MgnlUser.class)) { |
294 |
0 |
return MgnlContext.getUser(); |
295 |
|
} |
296 |
0 |
return super.resolveParameter(parameterType); |
297 |
|
} |
298 |
|
}; |
299 |
|
} |
300 |
|
|
|
|
| 86.7% |
Uncovered Elements: 2 (15) |
Complexity: 5 |
Complexity Density: 0.71 |
|
301 |
41 |
@Override... |
302 |
|
public void afterPropertiesSet() throws Exception { |
303 |
41 |
MagnoliaConfigurationProperties configurationProperties = Components.getComponent(MagnoliaConfigurationProperties.class); |
304 |
41 |
if (this.sortTabsByLabel == null) { |
305 |
41 |
String property = configurationProperties.getProperty(SORT_TABS_BY_LABEL_PROPERTY_NAME); |
306 |
41 |
this.sortTabsByLabel = property != null ? Boolean.valueOf(property) : SORT_TABS_BY_LABEL_PROPERTY_DEFAULT; |
307 |
|
} |
308 |
41 |
if (this.setTabLabels == null) { |
309 |
41 |
String property = configurationProperties.getProperty(SET_TAB_LABELS_PROPERTY_NAME); |
310 |
41 |
this.setTabLabels = property != null ? Boolean.valueOf(property) : SET_TAB_LABELS_PROPERTY_DEFAULT; |
311 |
|
} |
312 |
|
} |
313 |
|
} |