1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
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 }