View Javadoc

1   /**
2    * This file Copyright (c) 2010-2014 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.annotation.Annotation;
37  import java.lang.reflect.Method;
38  import java.lang.reflect.Modifier;
39  import java.util.ArrayList;
40  import java.util.List;
41  
42  import org.slf4j.Logger;
43  import org.slf4j.LoggerFactory;
44  import org.springframework.aop.support.AopUtils;
45  import org.springframework.util.ClassUtils;
46  import org.springframework.util.ReflectionUtils;
47  
48  import info.magnolia.module.blossom.annotation.DialogFactory;
49  import info.magnolia.module.blossom.annotation.I18nBasename;
50  import info.magnolia.module.blossom.annotation.TabFactory;
51  import info.magnolia.module.blossom.annotation.TabOrder;
52  
53  /**
54   * Builds dialog descriptions from annotations.
55   *
56   * @since 1.0
57   */
58  public class DialogDescriptionBuilder {
59  
60      private final Logger logger = LoggerFactory.getLogger(getClass());
61  
62      private DialogCreator dialogCreator;
63  
64      public void setDialogCreator(DialogCreator dialogCreator) {
65          this.dialogCreator = dialogCreator;
66      }
67  
68      public DialogCreator getDialogCreator() {
69          if (dialogCreator == null) {
70              dialogCreator = new DefaultDialogCreator();
71          }
72          return dialogCreator;
73      }
74  
75      public BlossomDialogDescription buildDescription(Object factoryObject) {
76          return buildDescription(factoryObject, null);
77      }
78  
79      public List<BlossomDialogDescription> buildDescriptions(final Object handler) {
80  
81          final List<Method> factoryMethods = new ArrayList<Method>();
82  
83          final Class<?> handlerClass = AopUtils.getTargetClass(handler);
84          ReflectionUtils.doWithMethods(handlerClass, new ReflectionUtils.MethodCallback() {
85  
86              @Override
87              public void doWith(Method method) {
88                  DialogFactory dialogFactory = method.getAnnotation(DialogFactory.class);
89                  if (dialogFactory != null && method.equals(ClassUtils.getMostSpecificMethod(method, handlerClass))) {
90                      if (Modifier.isStatic(method.getModifiers())) {
91                          throw new IllegalStateException("DialogFactory annotation is not supported on static methods");
92                      }
93                      factoryMethods.add(method);
94                  }
95              }
96          });
97  
98          List<BlossomDialogDescription> descriptions = new ArrayList<BlossomDialogDescription>();
99  
100         for (Method method : factoryMethods) {
101             descriptions.add(buildDescription(handler, method));
102         }
103 
104         return descriptions;
105     }
106 
107     public BlossomDialogDescription buildDescription(Object factoryObject, Method factoryMethod) {
108         DialogFactory annotation = findAnnotation(DialogFactory.class, factoryObject, factoryMethod);
109         return buildDescription(annotation.value(), annotation.label(), factoryObject, factoryMethod);
110     }
111 
112     public BlossomDialogDescription buildDescription(String id, String label, Object factoryObject) {
113         return buildDescription(id, label, factoryObject, null);
114     }
115 
116     protected BlossomDialogDescription buildDescription(String id, String label, Object factoryObject, Method factoryMethod) {
117         TabOrder tabOrder = findAnnotation(TabOrder.class, factoryObject, factoryMethod);
118         I18nBasename i18nBasename = findAnnotation(I18nBasename.class, factoryObject, factoryMethod);
119         final DialogFactoryMetaData factoryMetaData = new DialogFactoryMetaData();
120         factoryMetaData.setLabel(label);
121         factoryMetaData.setI18nBasename(i18nBasename != null ? i18nBasename.value() : null);
122         factoryMetaData.setFactoryObject(factoryObject);
123         factoryMetaData.setFactoryMethod(factoryMethod);
124         factoryMetaData.setTabOrder(tabOrder != null ? tabOrder.value() : null);
125 
126         final Class<?> factoryClass = AopUtils.getTargetClass(factoryObject);
127 
128         ReflectionUtils.doWithMethods(factoryClass, new ReflectionUtils.MethodCallback() {
129 
130             @Override
131             public void doWith(Method method) {
132                 if (method.isAnnotationPresent(TabFactory.class) && method.equals(ClassUtils.getMostSpecificMethod(method, factoryClass))) {
133                     if (Modifier.isStatic(method.getModifiers())) {
134                         throw new IllegalStateException("TabFactory annotation is not supported on static methods");
135                     }
136                     factoryMetaData.addTabFactory(method);
137                 }
138             }
139         });
140 
141         BlossomDialogDescription dialogDescription = new BlossomDialogDescription();
142         dialogDescription.setId(id);
143         dialogDescription.setFactoryMetaData(factoryMetaData);
144         dialogDescription.setDialogCreator(getDialogCreator());
145 
146         return dialogDescription;
147     }
148 
149     protected <T extends Annotation> T findAnnotation(Class<T> annotationClass, Object factoryObject, Method factoryMethod) {
150         if (factoryMethod != null) {
151             return factoryMethod.getAnnotation(annotationClass);
152         }
153         Class<?> factoryClass = AopUtils.getTargetClass(factoryObject);
154         return factoryClass.getAnnotation(annotationClass);
155     }
156 }