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