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.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
51
52
53
54
55 public class ParagraphManager extends ObservedManager {
56
57 private static final String DEFAULT_PARA_TYPE = "jsp";
58
59
60
61
62 public static ParagraphManager getInstance() {
63 return Components.getSingleton(ParagraphManager.class);
64 }
65
66
67
68
69 private Map<String, Paragraph> paragraphs = new Hashtable<String, Paragraph>();
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84 public Paragraph getInfo(String key) {
85 return getParagraphDefinition(key);
86 }
87
88
89
90
91
92
93
94
95
96
97
98
99
100 public Paragraph getParagraphDefinition(String key) {
101 return paragraphs.get(key);
102 }
103
104
105
106
107 public Map<String, Paragraph> getParagraphs() {
108 return paragraphs;
109 }
110
111
112
113
114 protected void onRegister(Content node) {
115
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
129 onRegister(subNode);
130 }
131 }
132
133
134
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);
142 }
143 }
144
145
146
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());
156 paragraphs.put(paragraph.getName(), paragraph);
157 }
158
159 public void onClear() {
160 this.paragraphs.clear();
161 }
162
163 }