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.templating;
35  
36  import info.magnolia.cms.core.Content;
37  import info.magnolia.cms.core.ItemType;
38  import info.magnolia.cms.beans.config.ObservedManager;
39  import info.magnolia.content2bean.Content2BeanUtil;
40  import info.magnolia.content2bean.Content2BeanException;
41  import info.magnolia.objectfactory.Components;
42  
43  import java.util.Collection;
44  import java.util.Hashtable;
45  import java.util.Map;
46  
47  import org.apache.commons.lang.StringUtils;
48  
49  /**
50   * Manages the paragraphs on the system. Modules can register the nodes where
51   * the paragraphs are defined.
52   *
53   * @author philipp
54   */
55  public class ParagraphManager extends ObservedManager {
56  
57      private static final String DEFAULT_PARA_TYPE = "jsp";
58  
59      /**
60       * Gets the current singleton instance.
61       */
62      public static ParagraphManager getInstance() {
63          return Components.getSingleton(ParagraphManager.class);
64      }
65  
66      /**
67       * Cached paragraphs.
68       */
69      private Map<String, Paragraph> paragraphs = new Hashtable<String, Paragraph>();
70  
71      /**
72       * Returns the cached content of the requested template. TemplateInfo
73       * properties :
74       * <ol>
75       * <li>title - title describing template</li>
76       * <li>type - jsp / servlet</li>
77       * <li>path - jsp / servlet path</li>
78       * <li>description - description of a template</li>
79       * </ol>
80       *
81       * @return a Paragraph instance
82       * @deprecated since 4.0 Use {@link #getParagraphDefinition(String)} instead
83       */
84      public Paragraph getInfo(String key) {
85          return getParagraphDefinition(key);
86      }
87  
88      /**
89       * Returns the cached content of the requested template. TemplateInfo
90       * properties :
91       * <ol>
92       * <li>title - title describing template</li>
93       * <li>type - jsp / servlet</li>
94       * <li>path - jsp / servlet path</li>
95       * <li>description - description of a template</li>
96       * </ol>
97       *
98       * @return a Paragraph instance
99       */
100     public Paragraph getParagraphDefinition(String key) {
101         return paragraphs.get(key);
102     }
103 
104     /**
105      * Get a map of all registered paragraphs.
106      */
107     public Map<String, Paragraph> getParagraphs() {
108         return paragraphs;
109     }
110 
111     /**
112      * Register all the paragraphs under this and subnodes.
113      */
114     protected void onRegister(Content node) {
115         // register a listener
116 
117         Collection<Content> paragraphNodes = node.getChildren(ItemType.CONTENTNODE);
118         for (Content paragraphNode : paragraphNodes) {
119             try {
120                 addParagraphToCache(paragraphNode);
121             } catch (Exception e) {
122                 log.error("Can't reload the node " + paragraphNode.getUUID() + " on location: " + paragraphNode.getHandle());
123             }
124         }
125 
126         Collection<Content> subDefinitions = node.getChildren(ItemType.CONTENT);
127         for (Content subNode : subDefinitions) {
128             // do not register other observations
129             onRegister(subNode);
130         }
131     }
132 
133     /**
134      * Adds paragraph definition to ParagraphInfo cache.
135      */
136     protected void addParagraphToCache(Content c) {
137         try {
138             final Paragraph p = (Paragraph) Content2BeanUtil.toBean(c, true, Paragraph.class);
139             addParagraphToCache(p);
140         } catch (Content2BeanException e) {
141             throw new RuntimeException(e); // TODO
142         }
143     }
144 
145     /**
146      * @param paragraph
147      */
148     public void addParagraphToCache(final Paragraph paragraph) {
149         if (StringUtils.isEmpty(paragraph.getType())) {
150             paragraph.setType(DEFAULT_PARA_TYPE);
151         }
152         if (StringUtils.isEmpty(paragraph.getDialog())) {
153             paragraph.setDialog(paragraph.getName());
154         }
155         log.debug("Registering paragraph [{}] of type [{}]", paragraph.getName(), paragraph.getType()); //$NON-NLS-1$
156         paragraphs.put(paragraph.getName(), paragraph);
157     }
158 
159     public void onClear() {
160         this.paragraphs.clear();
161     }
162 
163 }