View Javadoc

1   /**
2    * This file Copyright (c) 2003-2010 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.paragraph;
35  
36  import info.magnolia.module.blossom.annotation.I18nBasename;
37  import info.magnolia.module.blossom.annotation.Paragraph;
38  import info.magnolia.module.blossom.annotation.ParagraphDescription;
39  import info.magnolia.module.blossom.annotation.TabOrder;
40  import info.magnolia.module.blossom.dialog.DialogFactoryMetaData;
41  import info.magnolia.module.blossom.dispatcher.BlossomDispatcher;
42  import org.apache.commons.lang.StringUtils;
43  
44  /**
45   * Builds paragraph descriptions from annotations.
46   */
47  public class ParagraphDescriptionBuilder {
48  
49      public BlossomParagraphDescription buildDescription(BlossomDispatcher dispatcher, Object handler, String handlerPath) {
50  
51          Paragraph annotation = handler.getClass().getAnnotation(Paragraph.class);
52          I18nBasename i18nBasename = handler.getClass().getAnnotation(I18nBasename.class);
53  
54          BlossomParagraphDescription description = new BlossomParagraphDescription();
55          description.setName(resolveName(handlerPath, handler, annotation));
56          description.setTitle(resolveTitle(handlerPath, handler, annotation));
57          description.setDescription(resolveDescription(handlerPath, handler, annotation));
58          description.setHandlerPath(handlerPath);
59          description.setI18nBasename(i18nBasename != null ? i18nBasename.value() : null);
60          description.setHandler(handler);
61          description.setDispatcher(dispatcher);
62          description.setDialogName(StringUtils.trimToNull(annotation.dialog()));
63  
64          if (description.getDialogName() == null) {
65              TabOrder tabOrder = handler.getClass().getAnnotation(TabOrder.class);
66              DialogFactoryMetaData factoryMetaData = new DialogFactoryMetaData();
67              factoryMetaData.setLabel(description.getTitle());
68              factoryMetaData.setI18nBasename(description.getI18nBasename());
69              factoryMetaData.setFactoryObject(handler);
70              factoryMetaData.setTabOrder(tabOrder != null ? tabOrder.value() : null);
71              description.setDialogFactoryMetaData(factoryMetaData);
72          }
73  
74          return description;
75      }
76  
77      protected String resolveName(String handlerPath, Object handler, Paragraph annotation) {
78          if (StringUtils.isNotEmpty(annotation.name()))
79              return annotation.name();
80          return StringUtils.replaceChars(StringUtils.strip(handlerPath, "/"), '/', '_');
81      }
82  
83      protected String resolveTitle(String handlerPath, Object handler, Paragraph annotation) {
84          if (StringUtils.isNotEmpty(annotation.value()))
85              return annotation.value();
86          return handlerPath;
87      }
88  
89      protected String resolveDescription(String handlerPath, Object handler, Paragraph annotation) {
90          ParagraphDescription paragraphDescription = handler.getClass().getAnnotation(ParagraphDescription.class);
91          if (paragraphDescription != null && StringUtils.isNotEmpty(paragraphDescription.value()))
92              return paragraphDescription.value();
93          return "";
94      }
95  }