View Javadoc

1   /**
2    * This file Copyright (c) 2010-2013 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 java.lang.reflect.Method;
37  import java.util.Collections;
38  import java.util.Comparator;
39  
40  import org.apache.commons.lang.ArrayUtils;
41  import org.apache.commons.lang.StringUtils;
42  import org.springframework.beans.BeanUtils;
43  import org.springframework.util.ClassUtils;
44  
45  import info.magnolia.module.blossom.annotation.TabFactory;
46  import info.magnolia.ui.dialog.config.DialogBuilder;
47  import info.magnolia.ui.dialog.definition.FormDialogDefinition;
48  import info.magnolia.ui.form.config.TabBuilder;
49  import info.magnolia.ui.framework.config.UiConfig;
50  import info.magnolia.module.blossom.support.MethodInvocationUtils;
51  import info.magnolia.module.blossom.support.ParameterResolver;
52  import info.magnolia.ui.dialog.definition.ConfiguredDialogDefinition;
53  import info.magnolia.ui.form.definition.ConfiguredFormDefinition;
54  import info.magnolia.ui.form.definition.TabDefinition;
55  
56  /**
57   * Default implementation of {@link info.magnolia.module.blossom.dialog.DialogCreator}.
58   *
59   * @since 0.5
60   */
61  public class DefaultDialogCreator implements DialogCreator {
62  
63      private static final String DAM_CONFIG_CLASS_NAME = "info.magnolia.dam.asset.config.DamConfig";
64  
65      private Class damConfigClass;
66  
67      public DefaultDialogCreator() {
68          ClassLoader classLoader = this.getClass().getClassLoader();
69          if (ClassUtils.isPresent(DAM_CONFIG_CLASS_NAME, classLoader)) {
70              damConfigClass = ClassUtils.resolveClassName(DAM_CONFIG_CLASS_NAME, classLoader);
71          }
72      }
73  
74      @Override
75      public void createDialog(DialogFactoryMetaData metaData, final DialogCreationContext context) throws Exception {
76  
77          DialogBuilder dialogBuilder = new DialogBuilder(context.getId());
78          context.setDialog(dialogBuilder.definition());
79          context.getDialog().setForm(new ConfiguredFormDefinition());
80  
81          if (metaData.getFactoryMethod() != null) {
82  
83              ParameterResolver parameters = getDialogFactoryParameters(metaData, context, dialogBuilder);
84              MethodInvocationUtils.invoke(metaData.getFactoryMethod(), metaData.getFactoryObject(), parameters);
85          } else {
86  
87              invokeTabFactories(metaData, context, dialogBuilder);
88          }
89  
90          if (StringUtils.isNotEmpty(metaData.getLabel())) {
91              ConfiguredFormDefinition form = (ConfiguredFormDefinition) dialogBuilder.definition().getForm();
92              form.setLabel(metaData.getLabel());
93              dialogBuilder.label(metaData.getLabel());
94          }
95  
96          if (StringUtils.isNotEmpty(metaData.getI18nBasename())) {
97              dialogBuilder.i18nBasename(metaData.getI18nBasename());
98          }
99  
100         if (metaData.getTabOrder() != null) {
101             sortTabs(context.getDialog(), metaData.getTabOrder());
102         }
103     }
104 
105     private void sortTabs(FormDialogDefinition dialog, final String[] order) {
106         Collections.sort(dialog.getForm().getTabs(), new Comparator<Object>() {
107 
108             @Override
109             public int compare(Object o1, Object o2) {
110                 switch (((o1 instanceof TabDefinition) ? 2 : 0) + ((o2 instanceof TabDefinition) ? 1 : 0)) {
111                     case 0:
112                         return 0;
113                     case 1:
114                         return -1;
115                     case 2:
116                         return 1;
117                     case 3:
118                         return ArrayUtils.indexOf(order, ((TabDefinition) o1).getLabel()) - ArrayUtils.indexOf(order, ((TabDefinition) o2).getLabel());
119                 }
120                 return 0; // Will never happen
121             }
122         });
123     }
124 
125     private void invokeTabFactories(final DialogFactoryMetaData metaData, final DialogCreationContext context, DialogBuilder dialogBuilder) {
126 
127         for (Method tabFactory : metaData.getTabFactories()) {
128 
129             TabFactory annotation = tabFactory.getAnnotation(TabFactory.class);
130             TabBuilder tabBuilder = new TabBuilder(annotation.value()).label(annotation.value());
131             dialogBuilder.form().tabs(tabBuilder);
132 
133             ParameterResolver parameters = getTabFactoryParameters(metaData, context, tabBuilder);
134             MethodInvocationUtils.invoke(tabFactory, metaData.getFactoryObject(), parameters);
135         }
136     }
137 
138     protected ParameterResolver getDialogFactoryParameters(DialogFactoryMetaData metaData, final DialogCreationContext context, final DialogBuilder dialogBuilder) {
139         return new ParameterResolver() {
140 
141             @Override
142             public Object resolveParameter(Class<?> parameterType) {
143                 if (damConfigClass != null && parameterType == damConfigClass) {
144                     return BeanUtils.instantiate(damConfigClass);
145                 }
146                 if (parameterType.equals(DialogBuilder.class)) {
147                     return dialogBuilder;
148                 }
149                 if (parameterType.isAssignableFrom(ConfiguredDialogDefinition.class)) {
150                     return context.getDialog();
151                 }
152                 if (parameterType.equals(DialogCreationContext.class)) {
153                     return context;
154                 }
155                 if (parameterType.equals(UiConfig.class)) {
156                     return new UiConfig();
157                 }
158                 return super.resolveParameter(parameterType);
159             }
160         };
161     }
162 
163     protected ParameterResolver getTabFactoryParameters(DialogFactoryMetaData metaData, final DialogCreationContext context, final TabBuilder tabBuilder) {
164         return new ParameterResolver() {
165 
166             @Override
167             public Object resolveParameter(Class<?> parameterType) {
168                 if (damConfigClass != null && parameterType == damConfigClass) {
169                     return BeanUtils.instantiate(damConfigClass);
170                 }
171                 if (parameterType.equals(TabBuilder.class)) {
172                     return tabBuilder;
173                 }
174                 if (parameterType.isAssignableFrom(ConfiguredDialogDefinition.class)) {
175                     return context.getDialog();
176                 }
177                 if (parameterType.equals(DialogCreationContext.class)) {
178                     return context;
179                 }
180                 if (parameterType.equals(UiConfig.class)) {
181                     return new UiConfig();
182                 }
183                 return super.resolveParameter(parameterType);
184             }
185         };
186     }
187 }