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.templatingkit.templates;
35
36 import info.magnolia.module.resources.ResourcesModule;
37 import info.magnolia.module.resources.templates.ResourceTemplate;
38 import info.magnolia.module.templatingkit.functions.STKTemplatingFunctions;
39 import info.magnolia.module.templatingkit.sites.Site;
40 import info.magnolia.module.templatingkit.templates.pages.STKPage;
41 import info.magnolia.objectfactory.Components;
42 import info.magnolia.rendering.template.TemplateAvailability;
43 import info.magnolia.rendering.template.TemplateDefinition;
44
45 import javax.jcr.Node;
46 import javax.jcr.RepositoryException;
47
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50
51
52
53
54 public class STKTemplateAvailability implements TemplateAvailability {
55
56 private static final Logger log = LoggerFactory.getLogger(STKTemplateAvailability.class);
57
58 @Override
59 public boolean isAvailable(Node node, TemplateDefinition templateDefinition) {
60
61 final String workspaceName = getNodeWorkspaceName(node);
62
63 if (ResourcesModule.DEFAULT_WORKSPACE.equals(workspaceName) && !(templateDefinition instanceof ResourceTemplate)) {
64 return false;
65 }
66
67 if (!(templateDefinition instanceof STKPage)) {
68 return true;
69 }
70
71 Site site = Components.getComponent(STKTemplatingFunctions.class).site(node);
72
73 if (site == null || site.getTemplates() == null || site.getTemplates().getAvailability() == null) {
74
75
76 return false;
77 }
78
79 return site.getTemplates().getAvailability().isAvailable(node, (STKPage) templateDefinition);
80 }
81
82 private String getNodeWorkspaceName(final Node node) {
83 try {
84 return node.getSession().getWorkspace().getName();
85 } catch (RepositoryException e) {
86 log.error("An error occurred while trying to access session for jcr node {}", node, e);
87 return null;
88 }
89 }
90 }