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.Content2BeanException;
40 import info.magnolia.content2bean.Content2BeanUtil;
41 import info.magnolia.objectfactory.Components;
42
43 import javax.jcr.RepositoryException;
44 import java.util.ArrayList;
45 import java.util.Collection;
46 import java.util.Hashtable;
47 import java.util.Iterator;
48 import java.util.List;
49 import java.util.Map;
50
51
52
53
54
55
56 public class TemplateManager extends ObservedManager {
57
58 private static final String DELETED_PAGE_TEMPLATE = "mgnlDeleted";
59
60
61
62
63 private final Map<String, Template> cachedContent = new Hashtable<String, Template>();
64
65
66
67
68 private final List<Template> visibleTemplates = new ArrayList<Template>();
69
70
71
72
73 @Override
74 protected void onRegister(Content node) {
75 try {
76 log.info("Loading Template info from {}", node.getHandle());
77
78
79 Collection<Content> children = collectChildren(node);
80
81 if ((children != null) && !(children.isEmpty())) {
82 Iterator<Content> templates = children.iterator();
83 cacheContent(templates);
84 }
85
86 log.debug("Template info loaded from {}", node.getHandle());
87 }
88 catch (Exception re) {
89 log.error("Failed to load Template info from " + node.getHandle() + ": " + re.getMessage(), re);
90 }
91
92 }
93
94 @Override
95 protected void onClear() {
96 this.cachedContent.clear();
97 this.visibleTemplates.clear();
98 }
99
100
101
102
103
104
105
106
107
108
109
110
111 @Deprecated
112 public Template getInfo(String key) {
113 return getTemplateDefinition(key);
114 }
115
116
117
118
119
120
121
122
123
124
125
126 public Template getTemplateDefinition(String key) {
127 return cachedContent.get(key);
128 }
129
130
131
132
133
134
135
136
137
138
139
140 public Template getInfo(String key, String extension) {
141 Template template = cachedContent.get(key);
142
143 if (template == null) {
144 return null;
145 }
146 Template subtemplate = template.getSubTemplate(extension);
147 if (subtemplate != null) {
148 return subtemplate;
149 }
150
151 return template;
152 }
153
154
155
156
157
158
159 private void addTemplatesToCache(Iterator<Content> templates, List<Template> visibleTemplates) {
160 while (templates.hasNext()) {
161 Content c = templates.next();
162
163 try {
164 Template ti = (Template) Content2BeanUtil.toBean(c, true, Template.class);
165 cachedContent.put(ti.getName(), ti);
166 if (ti.isVisible() && !DELETED_PAGE_TEMPLATE.equals(ti.getName())) {
167 visibleTemplates.add(ti);
168 }
169
170 log.debug("Registering template [{}]", ti.getName());
171 }
172 catch (Content2BeanException e) {
173 log.error("Can't register template ["+c.getName()+"]",e);
174 }
175
176 }
177 }
178
179
180
181
182
183 private void cacheContent(Iterator<Content> templates) {
184 if (templates != null) {
185 addTemplatesToCache(templates, visibleTemplates);
186 }
187 }
188
189
190
191
192
193
194
195 private Collection<Content> collectChildren(Content cnt) {
196
197 Collection<Content> children = cnt.getChildren(ItemType.CONTENTNODE);
198
199
200 Collection<Content> subFolders = cnt.getChildren(ItemType.CONTENT);
201 if ((subFolders != null) && !(subFolders.isEmpty())) {
202
203 for (Content subCnt : subFolders) {
204 Collection<Content> grandChildren = collectChildren(subCnt);
205
206 if ((grandChildren != null) && !(grandChildren.isEmpty())) {
207 children.addAll(grandChildren);
208 }
209 }
210
211 }
212
213 return children;
214 }
215
216 public Iterator<Template> getAvailableTemplates(Content node) {
217 List<Template> templateList = new ArrayList<Template>();
218
219 try {
220 if (node != null && node.hasMixin(ItemType.DELETED_NODE_MIXIN)) {
221 templateList.add(getTemplateDefinition(DELETED_PAGE_TEMPLATE));
222 return templateList.iterator();
223 }
224 } catch (RepositoryException e) {
225 log.error("Failed to check node for deletion status.", e);
226 }
227 for (Template template : visibleTemplates) {
228
229 if (template.isAvailable(node)) {
230 templateList.add(template);
231 }
232 }
233 return templateList.iterator();
234 }
235
236
237
238
239
240 public Iterator<Template> getAvailableTemplates() {
241 return visibleTemplates.iterator();
242 }
243
244
245
246
247 public Template getDefaultTemplate(Content node) {
248 Template tmpl;
249 try {
250
251 tmpl = this.getTemplateDefinition(node.getParent().getTemplate());
252 if(tmpl != null && tmpl.isAvailable(node)){
253 return tmpl;
254 }
255
256 else{
257 Iterator<Template> templates = getAvailableTemplates(node);
258 if (templates.hasNext()) {
259 return templates.next();
260 }
261 }
262 }
263 catch (RepositoryException e) {
264 log.error("Can't resolve default template for node " + node.getHandle(), e);
265 }
266 return null;
267 }
268
269
270
271
272 public static TemplateManager getInstance() {
273 return Components.getSingleton(TemplateManager.class);
274 }
275
276 }