View Javadoc

1   /**
2    * This file Copyright (c) 2003-2011 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.InvocationTargetException;
37  import java.lang.reflect.Method;
38  import java.util.Collections;
39  import java.util.Comparator;
40  import javax.jcr.RepositoryException;
41  
42  import org.apache.commons.lang.ArrayUtils;
43  import org.apache.commons.lang.StringUtils;
44  
45  import info.magnolia.cms.gui.dialog.Dialog;
46  import info.magnolia.cms.gui.dialog.DialogTab;
47  import info.magnolia.cms.util.AlertUtil;
48  import info.magnolia.module.admininterface.SaveHandler;
49  import info.magnolia.module.blossom.annotation.TabFactory;
50  import info.magnolia.module.blossom.annotation.TabValidator;
51  import info.magnolia.module.blossom.support.MethodInvocationUtils;
52  import info.magnolia.module.blossom.support.ParameterResolver;
53  
54  /**
55   * Default implementation of {@link info.magnolia.module.blossom.dialog.DialogCreator}.
56   *
57   * @since 0.5
58   */
59  public class DefaultDialogCreator implements DialogCreator {
60  
61      @Override
62      public void createDialog(DialogFactoryMetaData metaData, final DialogCreationContext context) throws Exception {
63  
64          Dialog dialog = new Dialog() {
65              @Override
66              public boolean validate() {
67                  if (!super.validate()) {
68                      return false;
69                  }
70                  context.executeValidators();
71                  return !AlertUtil.isMessageSet();
72              }
73          };
74          dialog.init(context.getRequest(), context.getResponse(), context.getWebsiteNode(), context.getConfigNode());
75  
76          context.setDialog(dialog);
77  
78          DialogBuilder dialogBuilder = new DialogBuilder(context);
79  
80          if (metaData.getFactoryMethod() != null) {
81  
82              ParameterResolver parameters = getDialogFactoryParameters(metaData, context, dialogBuilder);
83              MethodInvocationUtils.invoke(metaData.getFactoryMethod(), metaData.getFactoryObject(), parameters);
84          } else {
85  
86              invokeTabFactoriesAndRegisterValidators(metaData, context, dialogBuilder);
87          }
88  
89          invokeInitSaveHandlerMethod(metaData, context);
90  
91          if (StringUtils.isNotEmpty(metaData.getLabel())) {
92              context.getDialog().setLabel(metaData.getLabel());
93          }
94  
95          if (StringUtils.isNotEmpty(metaData.getI18nBasename())) {
96              context.getDialog().setConfig("i18nBasename", metaData.getI18nBasename());
97          }
98  
99          if (metaData.getTabOrder() != null) {
100             sortTabs(context.getDialog(), metaData.getTabOrder());
101         }
102     }
103 
104     private void invokeInitSaveHandlerMethod(DialogFactoryMetaData metaData, DialogCreationContext context) throws RepositoryException {
105         if (metaData.getInitSaveHandlerMethod() != null) {
106             try {
107                 SaveHandler saveHandler = (SaveHandler) metaData.getInitSaveHandlerMethod().invoke(metaData.getFactoryObject());
108                 context.setSaveHandler(saveHandler);
109             } catch (IllegalAccessException e) {
110                 throw new RepositoryException(e);
111             } catch (InvocationTargetException e) {
112                 throw new RepositoryException(e);
113             }
114         }
115     }
116 
117     private void sortTabs(Dialog dialog, final String[] order) {
118         Collections.sort(dialog.getSubs(), new Comparator<Object>() {
119 
120             @Override
121             public int compare(Object o1, Object o2) {
122                 switch (((o1 instanceof DialogTab) ? 2 : 0) + ((o2 instanceof DialogTab) ? 1 : 0)) {
123                     case 0:
124                         return 0;
125                     case 1:
126                         return -1;
127                     case 2:
128                         return 1;
129                     case 3:
130                         return ArrayUtils.indexOf(order, ((DialogTab) o1).getLabel()) - ArrayUtils.indexOf(order, ((DialogTab) o2).getLabel());
131                 }
132                 return 0; // Will never happen
133             }
134         });
135     }
136 
137     private void invokeTabFactoriesAndRegisterValidators(final DialogFactoryMetaData metaData, final DialogCreationContext context, DialogBuilder dialogBuilder) {
138 
139         for (Method tabFactory : metaData.getTabFactories()) {
140 
141             TabFactory annotation = tabFactory.getAnnotation(TabFactory.class);
142             TabBuilder tabBuilder = dialogBuilder.addTab(annotation.value());
143 
144             ParameterResolver parameters = getTabFactoryParameters(metaData, context, tabBuilder);
145             MethodInvocationUtils.invoke(tabFactory, metaData.getFactoryObject(), parameters);
146         }
147 
148         for (final Method tabValidator : metaData.getTabValidators()) {
149             context.addValidator(new ValidationCallback() {
150 
151                 @Override
152                 public void validate(Dialog dialog) {
153                     TabValidator annotation = tabValidator.getAnnotation(TabValidator.class);
154                     DialogTab dialogTab = context.getTab(annotation.value());
155                     if (dialogTab != null) {
156 
157                         ParameterResolver parameters = getTabValidatorParameters(metaData, context, dialogTab);
158                         MethodInvocationUtils.invoke(tabValidator, metaData.getFactoryObject(), parameters);
159                     }
160                 }
161             });
162         }
163 
164         for (final Method dialogValidator : metaData.getDialogValidators()) {
165             context.addValidator(new ValidationCallback() {
166 
167                 @Override
168                 public void validate(Dialog dialog) {
169                     ParameterResolver parameters = getDialogValidatorParameters(metaData, context);
170                     MethodInvocationUtils.invoke(dialogValidator, metaData.getFactoryObject(), parameters);
171                 }
172             });
173         }
174     }
175 
176     protected ParameterResolver getDialogFactoryParameters(DialogFactoryMetaData metaData, final DialogCreationContext context, final DialogBuilder dialogBuilder) {
177         return new ParameterResolver() {
178 
179             @Override
180             public Object resolveParameter(Class<?> parameterType) {
181                 if (parameterType.equals(DialogBuilder.class)) {
182                     return dialogBuilder;
183                 }
184                 if (parameterType.equals(Dialog.class)) {
185                     return context.getDialog();
186                 }
187                 if (parameterType.equals(DialogCreationContext.class)) {
188                     return context;
189                 }
190                 return super.resolveParameter(parameterType);
191             }
192         };
193     }
194 
195     protected ParameterResolver getTabFactoryParameters(DialogFactoryMetaData metaData, final DialogCreationContext context, final TabBuilder tabBuilder) {
196         return new ParameterResolver() {
197 
198             @Override
199             public Object resolveParameter(Class<?> parameterType) {
200                 if (parameterType.equals(TabBuilder.class)) {
201                     return tabBuilder;
202                 }
203                 if (parameterType.equals(Dialog.class)) {
204                     return context.getDialog();
205                 }
206                 if (parameterType.equals(DialogCreationContext.class)) {
207                     return context;
208                 }
209                 return super.resolveParameter(parameterType);
210             }
211         };
212     }
213 
214     protected ParameterResolver getTabValidatorParameters(DialogFactoryMetaData metaData, final DialogCreationContext context, final DialogTab dialogTab) {
215         return new ParameterResolver() {
216 
217             @Override
218             public Object resolveParameter(Class<?> parameterType) {
219                 if (parameterType.equals(DialogTab.class)) {
220                     return dialogTab;
221                 }
222                 if (parameterType.equals(Dialog.class)) {
223                     return context.getDialog();
224                 }
225                 return super.resolveParameter(parameterType);
226             }
227         };
228     }
229 
230     protected ParameterResolver getDialogValidatorParameters(DialogFactoryMetaData metaData, final DialogCreationContext context) {
231         return new ParameterResolver() {
232 
233             @Override
234             public Object resolveParameter(Class<?> parameterType) {
235                 if (parameterType.equals(Dialog.class)) {
236                     return context.getDialog();
237                 }
238                 return super.resolveParameter(parameterType);
239             }
240         };
241     }
242 }