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