1. Project Clover database Fri Apr 29 2016 13:24:33 CEST
  2. Package info.magnolia.module.blossom.dialog

File DefaultDialogCreator.java

 

Coverage histogram

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

Code metrics

56
97
15
1
313
224
50
0.52
6.47
15
3.33
4.5% of code in this file is excluded from these metrics.

Classes

Class Line # Actions
DefaultDialogCreator 77 97 4.5% 50 42
0.7575%
 

Contributing tests

This file is covered by 24 tests. .

Source view

1    /**
2    * This file Copyright (c) 2010-2016 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.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    * Default implementation of {@link info.magnolia.module.blossom.dialog.DialogCreator}.
70    * <p/>
71    * If the property <code>magnolia.blossom.setTabLabels</code> is set to false labels won't be set on tabs.
72    * <p/>
73    * If the property <code>magnolia.blossom.sortTabsByLabel</code> is set to false tabs will be sorted by name instead.
74    *
75    * @since 0.5
76    */
 
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", // DAM 2.0+
87    "info.magnolia.dam.asset.config.DamConfig" // DAM 1.x
88    };
89   
90    private Class damConfigClass;
91    private Boolean setTabLabels;
92    private Boolean sortTabsByLabel;
93   
 
94  41 toggle 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   
 
104    toggle public Boolean getSetTabLabels() {
105    return setTabLabels;
106    }
107   
 
108    toggle public void setSetTabLabels(Boolean setTabLabels) {
109    this.setTabLabels = setTabLabels;
110    }
111   
 
112    toggle public Boolean getSortTabsByLabel() {
113    return sortTabsByLabel;
114    }
115   
 
116    toggle public void setSortTabsByLabel(Boolean sortTabsByLabel) {
117    this.sortTabsByLabel = sortTabsByLabel;
118    }
119   
 
120  20 toggle @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   
 
141  4 toggle 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   
 
155  16 toggle 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   
 
198  3 toggle protected void sortTabs(FormDialogDefinition dialog, final String[] order) {
199  3 Collections.sort(dialog.getForm().getTabs(), new Comparator<Object>() {
200   
 
201  8 toggle @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; // Will never happen
218    }
219    });
220    }
221   
 
222  4 toggle protected ParameterResolver getDialogFactoryParameters(DialogFactoryMetaData metaData, DialogCreationContext context, final DialogBuilder dialogBuilder) {
223  4 return new ParameterResolver(getStandardParameters(metaData, context)) {
224   
 
225  7 toggle @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   
 
235  24 toggle protected ParameterResolver getTabFactoryParameters(DialogFactoryMetaData metaData, DialogCreationContext context, final TabBuilder tabBuilder) {
236  24 return new ParameterResolver(getStandardParameters(metaData, context)) {
237   
 
238  14 toggle @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   
 
248  9 toggle protected ParameterResolver getPostCreateCallbackParameters(DialogFactoryMetaData metaData, DialogCreationContext context, final DialogBuilder dialogBuilder) {
249  9 return new ParameterResolver(getStandardParameters(metaData, context)) {
250   
 
251  6 toggle @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   
 
261  37 toggle protected ParameterResolver getStandardParameters(DialogFactoryMetaData metaData, final DialogCreationContext context) {
262  37 return new ParameterResolver() {
263   
 
264  12 toggle @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   
 
301  41 toggle @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    }