View Javadoc
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.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      public DefaultDialogCreator() {
95          ClassLoader classLoader = this.getClass().getClassLoader();
96          for (String damConfigClassName : DAM_CONFIG_CLASS_NAMES) {
97              if (ClassUtils.isPresent(damConfigClassName, classLoader)) {
98                  damConfigClass = ClassUtils.resolveClassName(damConfigClassName, classLoader);
99                  break;
100             }
101         }
102     }
103 
104     public Boolean getSetTabLabels() {
105         return setTabLabels;
106     }
107 
108     public void setSetTabLabels(Boolean setTabLabels) {
109         this.setTabLabels = setTabLabels;
110     }
111 
112     public Boolean getSortTabsByLabel() {
113         return sortTabsByLabel;
114     }
115 
116     public void setSortTabsByLabel(Boolean sortTabsByLabel) {
117         this.sortTabsByLabel = sortTabsByLabel;
118     }
119 
120     @Override
121     public void createDialog(DialogFactoryMetaData metaData, final DialogCreationContext context) throws Exception {
122 
123         DialogBuilder dialogBuilder = new DialogBuilder(context.getId());
124         context.setDialog(dialogBuilder.definition());
125         context.getDialog().setForm(new ConfiguredFormDefinition());
126         context.getDialog().setPresenterClass(BlossomFormDialogPresenter.class);
127 
128         if (StringUtils.isNotEmpty(metaData.getLabel())) {
129             ConfiguredFormDefinition form = (ConfiguredFormDefinition) dialogBuilder.definition().getForm();
130             form.setLabel(metaData.getLabel());
131             dialogBuilder.label(metaData.getLabel());
132         }
133 
134         if (metaData.getFactoryMethod() != null) {
135             invokeMethodDialogFactory(metaData, context, dialogBuilder);
136         } else {
137             invokeClassDialogFactory(metaData, context, dialogBuilder);
138         }
139     }
140 
141     protected void invokeMethodDialogFactory(DialogFactoryMetaData metaData, DialogCreationContext context, DialogBuilder dialogBuilder) {
142 
143         if (StringUtils.isNotEmpty(metaData.getI18nBasename())) {
144             dialogBuilder.i18nBasename(metaData.getI18nBasename());
145         }
146 
147         ParameterResolver parameters = getDialogFactoryParameters(metaData, context, dialogBuilder);
148         MethodInvocationUtils.invoke(metaData.getFactoryMethod(), metaData.getFactoryObject(), parameters);
149 
150         if (metaData.getTabOrder() != null) {
151             sortTabs(context.getDialog(), metaData.getTabOrder());
152         }
153     }
154 
155     protected void invokeClassDialogFactory(DialogFactoryMetaData metaData, DialogCreationContext context, DialogBuilder dialogBuilder) {
156 
157         if (metaData.getI18nBasename() != null) {
158             dialogBuilder.i18nBasename(metaData.getI18nBasename());
159         }
160 
161         Object factoryObject = metaData.getFactoryObject();
162 
163         for (DialogFactoryClassMetaData classMetaData : metaData.getClassMetaData()) {
164 
165             int tabPosition = 0;
166 
167             for (Method tabFactory : classMetaData.getTabFactories()) {
168 
169                 TabFactory annotation = tabFactory.getAnnotation(TabFactory.class);
170                 TabBuilder tabBuilder = new TabBuilder(annotation.value());
171                 if (setTabLabels == null || setTabLabels) {
172                     tabBuilder.label(annotation.value());
173                 }
174                 dialogBuilder.form().definition().getTabs().add(tabPosition++, tabBuilder.definition());
175 
176                 ParameterResolver parameters = getTabFactoryParameters(metaData, context, tabBuilder);
177                 MethodInvocationUtils.invoke(tabFactory, metaData.getFactoryObject(), parameters);
178             }
179 
180             for (Method postCreateMethod : classMetaData.getPostCreateCallbacks()) {
181 
182                 ParameterResolver parameters = getPostCreateCallbackParameters(metaData, context, dialogBuilder);
183                 MethodInvocationUtils.invoke(postCreateMethod, factoryObject, parameters);
184             }
185         }
186 
187         for (Method postCreateMethod : metaData.getPostCreateCallbacks()) {
188 
189             ParameterResolver parameters = getPostCreateCallbackParameters(metaData, context, dialogBuilder);
190             MethodInvocationUtils.invoke(postCreateMethod, factoryObject, parameters);
191         }
192 
193         if (metaData.getTabOrder() != null) {
194             sortTabs(context.getDialog(), metaData.getTabOrder());
195         }
196     }
197 
198     protected void sortTabs(FormDialogDefinition dialog, final String[] order) {
199         Collections.sort(dialog.getForm().getTabs(), new Comparator<Object>() {
200 
201             @Override
202             public int compare(Object o1, Object o2) {
203                 switch (((o1 instanceof TabDefinition) ? 2 : 0) + ((o2 instanceof TabDefinition) ? 1 : 0)) {
204                 case 0:
205                     return 0;
206                 case 1:
207                     return -1;
208                 case 2:
209                     return 1;
210                 case 3:
211                     if (sortTabsByLabel == null || sortTabsByLabel) {
212                         return ArrayUtils.indexOf(order, ((TabDefinition) o1).getLabel()) - ArrayUtils.indexOf(order, ((TabDefinition) o2).getLabel());
213                     } else {
214                         return ArrayUtils.indexOf(order, ((TabDefinition) o1).getName()) - ArrayUtils.indexOf(order, ((TabDefinition) o2).getName());
215                     }
216                 }
217                 return 0; // Will never happen
218             }
219         });
220     }
221 
222     protected ParameterResolver getDialogFactoryParameters(DialogFactoryMetaData metaData, DialogCreationContext context, final DialogBuilder dialogBuilder) {
223         return new ParameterResolver(getStandardParameters(metaData, context)) {
224 
225             @Override
226             public Object resolveParameter(Class<?> parameterType) {
227                 if (parameterType.equals(DialogBuilder.class)) {
228                     return dialogBuilder;
229                 }
230                 return super.resolveParameter(parameterType);
231             }
232         };
233     }
234 
235     protected ParameterResolver getTabFactoryParameters(DialogFactoryMetaData metaData, DialogCreationContext context, final TabBuilder tabBuilder) {
236         return new ParameterResolver(getStandardParameters(metaData, context)) {
237 
238             @Override
239             public Object resolveParameter(Class<?> parameterType) {
240                 if (parameterType.equals(TabBuilder.class)) {
241                     return tabBuilder;
242                 }
243                 return super.resolveParameter(parameterType);
244             }
245         };
246     }
247 
248     protected ParameterResolver getPostCreateCallbackParameters(DialogFactoryMetaData metaData, DialogCreationContext context, final DialogBuilder dialogBuilder) {
249         return new ParameterResolver(getStandardParameters(metaData, context)) {
250 
251             @Override
252             public Object resolveParameter(Class<?> parameterType) {
253                 if (parameterType.isAssignableFrom(DialogBuilder.class)) {
254                     return dialogBuilder;
255                 }
256                 return super.resolveParameter(parameterType);
257             }
258         };
259     }
260 
261     protected ParameterResolver getStandardParameters(DialogFactoryMetaData metaData, final DialogCreationContext context) {
262         return new ParameterResolver() {
263 
264             @Override
265             public Object resolveParameter(Class<?> parameterType) {
266                 if (parameterType.isAssignableFrom(ConfiguredFormDialogDefinition.class)) {
267                     return context.getDialog();
268                 }
269                 if (parameterType.equals(DialogCreationContext.class)) {
270                     return context;
271                 }
272                 if (parameterType.equals(Node.class)) {
273                     return context.getContentNode();
274                 }
275                 if (parameterType.equals(Item.class)) {
276                     return context.getItem();
277                 }
278                 if (parameterType.equals(UiConfig.class)) {
279                     return new UiConfig();
280                 }
281                 if (damConfigClass != null && parameterType == damConfigClass) {
282                     return BeanUtils.instantiate(damConfigClass);
283                 }
284                 if (parameterType.isAssignableFrom(WebContext.class)) {
285                     return MgnlContext.getWebContext();
286                 }
287                 if (parameterType.isAssignableFrom(Context.class)) {
288                     return MgnlContext.getInstance();
289                 }
290                 if (parameterType.isAssignableFrom(User.class)) {
291                     return MgnlContext.getUser();
292                 }
293                 if (parameterType.isAssignableFrom(MgnlUser.class)) {
294                     return MgnlContext.getUser();
295                 }
296                 return super.resolveParameter(parameterType);
297             }
298         };
299     }
300 
301     @Override
302     public void afterPropertiesSet() throws Exception {
303         MagnoliaConfigurationProperties configurationProperties = Components.getComponent(MagnoliaConfigurationProperties.class);
304         if (this.sortTabsByLabel == null) {
305             String property = configurationProperties.getProperty(SORT_TABS_BY_LABEL_PROPERTY_NAME);
306             this.sortTabsByLabel = property != null ? Boolean.valueOf(property) : SORT_TABS_BY_LABEL_PROPERTY_DEFAULT;
307         }
308         if (this.setTabLabels == null) {
309             String property = configurationProperties.getProperty(SET_TAB_LABELS_PROPERTY_NAME);
310             this.setTabLabels = property != null ? Boolean.valueOf(property) : SET_TAB_LABELS_PROPERTY_DEFAULT;
311         }
312     }
313 }