1. Project Clover database Fri Mar 6 2015 14:07:48 CET
  2. Package info.magnolia.module.blossom.dialog

File DefaultDialogCreator.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart8.png
42% of files have more coverage

Code metrics

44
85
14
1
261
183
41
0.48
6.07
14
2.93

Classes

Class Line # Actions
DefaultDialogCreator 70 85 0% 41 40
0.720279772%
 

Contributing tests

This file is covered by 20 tests. .

Source view

1    /**
2    * This file Copyright (c) 2010-2015 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.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    * Default implementation of {@link info.magnolia.module.blossom.dialog.DialogCreator}.
67    *
68    * @since 0.5
69    */
 
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", // DAM 2.0+
74    "info.magnolia.dam.asset.config.DamConfig" // DAM 1.x
75    };
76   
77    private Class damConfigClass;
78   
 
79  33 toggle 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   
 
89  16 toggle @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   
 
109  4 toggle 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   
 
123  12 toggle 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   
 
163  1 toggle protected void sortTabs(FormDialogDefinition dialog, final String[] order) {
164  1 Collections.sort(dialog.getForm().getTabs(), new Comparator<Object>() {
165   
 
166  4 toggle @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; // Will never happen
179    }
180    });
181    }
182   
 
183  4 toggle protected ParameterResolver getDialogFactoryParameters(DialogFactoryMetaData metaData, DialogCreationContext context, final DialogBuilder dialogBuilder) {
184  4 return new ParameterResolver(getStandardParameters(metaData, context)) {
185   
 
186  7 toggle @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   
 
196  14 toggle protected ParameterResolver getTabFactoryParameters(DialogFactoryMetaData metaData, DialogCreationContext context, final TabBuilder tabBuilder) {
197  14 return new ParameterResolver(getStandardParameters(metaData, context)) {
198   
 
199  8 toggle @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   
 
209  9 toggle protected ParameterResolver getPostCreateCallbackParameters(DialogFactoryMetaData metaData, DialogCreationContext context, final DialogBuilder dialogBuilder) {
210  9 return new ParameterResolver(getStandardParameters(metaData, context)) {
211   
 
212  6 toggle @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   
 
222  27 toggle protected ParameterResolver getStandardParameters(DialogFactoryMetaData metaData, final DialogCreationContext context) {
223  27 return new ParameterResolver() {
224   
 
225  12 toggle @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    }