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.cms.beans.config.ContentRepository;
37  import info.magnolia.cms.core.Content;
38  import info.magnolia.cms.core.HierarchyManager;
39  import info.magnolia.context.MgnlContext;
40  import info.magnolia.module.blossom.dispatcher.BlossomDispatcher;
41  import info.magnolia.module.blossom.support.RepositoryUtils;
42  import org.apache.commons.lang.StringUtils;
43  import org.apache.jackrabbit.value.StringValue;
44  import org.springframework.beans.factory.InitializingBean;
45  
46  import javax.jcr.RepositoryException;
47  import java.util.concurrent.ConcurrentHashMap;
48  import java.util.concurrent.ConcurrentMap;
49  
50  /**
51   * Default implementation of {@link info.magnolia.module.blossom.paragraph.BlossomParagraphRegistry}.
52   */
53  public class DefaultBlossomParagraphRegistry implements BlossomParagraphRegistry, InitializingBean {
54  
55      private static final String PARAGRAPHS_PATH = "/modules/blossom/paragraphs/autodetected";
56  
57      private String placeholderParagraphName = "blossom";
58      private String handlerPathNodeDataName = "handlerPath";
59  
60      private ParagraphDescriptionBuilder descriptionBuilder;
61      private final ConcurrentMap<String, BlossomParagraphDescription> paragraphs = new ConcurrentHashMap<String, BlossomParagraphDescription>();
62      private final ConcurrentMap<String, BlossomParagraphDescription> paragraphsByHandlerPath = new ConcurrentHashMap<String, BlossomParagraphDescription>();
63  
64      public void setDescriptionBuilder(ParagraphDescriptionBuilder descriptionBuilder) {
65          this.descriptionBuilder = descriptionBuilder;
66      }
67  
68      public String getHandlerPathNodeDataName() {
69          return handlerPathNodeDataName;
70      }
71  
72      public void setHandlerPathNodeDataName(String handlerPathNodeDataName) {
73          this.handlerPathNodeDataName = handlerPathNodeDataName;
74      }
75  
76      public String getPlaceholderParagraphName() {
77          return placeholderParagraphName;
78      }
79  
80      public void setPlaceholderParagraphName(String placeholderParagraphName) {
81          this.placeholderParagraphName = placeholderParagraphName;
82      }
83  
84      public BlossomParagraphDescription getParagraph(String name) {
85          return paragraphs.get(name);
86      }
87  
88      public BlossomParagraphDescription getParagraph(Content content) throws RepositoryException {
89          String paragraphName = content.getMetaData().getTemplate();
90          return getParagraph(content, paragraphName);
91      }
92  
93      public BlossomParagraphDescription getParagraph(Content content, String paragraphName) throws RepositoryException {
94          if (paragraphName.equals(placeholderParagraphName)) {
95              if (content.hasNodeData(handlerPathNodeDataName)) {
96                  String handlerPath = RepositoryUtils.getNodeData(content, handlerPathNodeDataName);
97                  return paragraphsByHandlerPath.get(handlerPath);
98              }
99              return null;
100         }
101 
102         return getParagraph(paragraphName);
103     }
104 
105     public ConcurrentMap<String, BlossomParagraphDescription> getParagraphs() {
106         return paragraphs;
107     }
108 
109     public void registerParagraph(BlossomDispatcher dispatcher, Object handler, String handlerPath) throws RepositoryException {
110         addParagraph(descriptionBuilder.buildDescription(dispatcher, handler, handlerPath));
111     }
112 
113     protected void addParagraph(BlossomParagraphDescription description) throws RepositoryException {
114         paragraphs.put(description.getName(), description);
115         paragraphsByHandlerPath.put(description.getHandlerPath(), description);
116         writeParagraphDefinition(description);
117     }
118 
119     protected void writeParagraphDefinition(BlossomParagraphDescription description) throws RepositoryException {
120         HierarchyManager hierarchyManager = MgnlContext.getSystemContext().getHierarchyManager(ContentRepository.CONFIG);
121         Content configNode = RepositoryUtils.createContentNode(hierarchyManager, PARAGRAPHS_PATH, description.getName());
122         configNode.createNodeData("name", new StringValue(description.getName()));
123         configNode.createNodeData("title", new StringValue(description.getTitle()));
124         configNode.createNodeData("description", new StringValue(description.getDescription()));
125         configNode.createNodeData("type", new StringValue("blossom"));
126         if (StringUtils.isNotBlank(description.getI18nBasename()))
127             configNode.createNodeData("i18nBasename", new StringValue(description.getI18nBasename()));
128         configNode.getParent().save();
129     }
130 
131     public void afterPropertiesSet() throws Exception {
132 
133         HierarchyManager hierarchyManager = MgnlContext.getSystemContext().getHierarchyManager(ContentRepository.CONFIG);
134         RepositoryUtils.deleteContent(hierarchyManager, PARAGRAPHS_PATH);
135 
136         if (descriptionBuilder == null)
137             descriptionBuilder = new ParagraphDescriptionBuilder();
138     }
139 }