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.sites;
35
36 import info.magnolia.context.MgnlContext;
37 import info.magnolia.jcr.util.NodeTypes;
38 import info.magnolia.module.templatingkit.templates.category.TemplateCategory;
39 import info.magnolia.module.templatingkit.templates.category.TemplateCategoryUtil;
40 import info.magnolia.module.templatingkit.templates.pages.STKPage;
41 import info.magnolia.rendering.template.TemplateDefinition;
42 import info.magnolia.repository.RepositoryConstants;
43
44 import java.util.Collection;
45 import java.util.LinkedHashMap;
46 import java.util.Map;
47
48 import javax.jcr.Node;
49 import javax.jcr.RepositoryException;
50 import javax.jcr.Workspace;
51
52 import org.apache.commons.collections.CollectionUtils;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55
56
57
58
59
60
61
62 public class TemplateAvailability implements info.magnolia.rendering.template.TemplateAvailability {
63
64 private static final Logger log = LoggerFactory.getLogger(TemplateAvailability.class);
65
66 private final Map<String, TemplateConfig> templates = new LinkedHashMap<String, TemplateConfig>();
67
68 public Map<String, TemplateConfig> getTemplates() {
69 return this.templates;
70 }
71
72 public void addTemplate(String name, TemplateConfig templateConfig) {
73 templates.put(templateConfig.getId(), templateConfig);
74 }
75
76 public boolean isAvailable(Node node, STKPage template) {
77
78 Workspace workspace;
79 try {
80 workspace = node.getSession().getWorkspace();
81 if (!RepositoryConstants.WEBSITE.equals(workspace.getName())) {
82 return false;
83 }
84 } catch (RepositoryException e) {
85 log.error("Not able to access the Node's session or workspace.", e);
86 return false;
87 }
88 if (!getTemplates().containsKey(template.getId())) {
89 return false;
90 }
91 TemplateConfig templateConfig = getTemplates().get(template.getId());
92
93
94 final Collection<String> roles = templateConfig.getRoles();
95 if (!roles.isEmpty()) {
96 if (!CollectionUtils.containsAny(MgnlContext.getUser().getAllRoles(), roles)) {
97 return false;
98 }
99 }
100
101 try {
102
103 final String category = template.getCategory();
104 if (node.getDepth() == 1) {
105 return category.equals(TemplateCategory.HOME) || category.equals(TemplateCategory.FUNCTIONAL);
106 }
107
108 final String parentTemplateCategory = TemplateCategoryUtil.getTemplateCategory(node.getParent());
109
110 if (category.equals(TemplateCategory.HOME) && !TemplateCategory.FUNCTIONAL.equals(parentTemplateCategory) && TemplateCategoryUtil.findParentWithTemplateCategory(node, TemplateCategory.HOME) != null) {
111 return false;
112 }
113 if (category.equals(TemplateCategory.SECTION) || category.equals(TemplateCategory.HOME) || category.equals(TemplateCategory.FUNCTIONAL)) {
114 return parentTemplateCategory.equals(TemplateCategory.HOME) || parentTemplateCategory.equals(TemplateCategory.SECTION) || parentTemplateCategory.equals(TemplateCategory.FEATURE) || parentTemplateCategory.equals(TemplateCategory.FUNCTIONAL);
115 }
116
117 if (category.equals(TemplateCategory.CONTENT)) {
118 return parentTemplateCategory.equals(TemplateCategory.SECTION) || parentTemplateCategory.equals(TemplateCategory.CONTENT) || parentTemplateCategory.equals(TemplateCategory.FEATURE) || parentTemplateCategory.equals(TemplateCategory.FUNCTIONAL);
119 }
120 if (category.equals(TemplateCategory.FEATURE)) {
121 if ("form".equals(template.getSubcategory())) {
122
123 if ("stkFormStep".equals(template.getName())) {
124 return node.getParent() != null && NodeTypes.Renderable.getTemplate(node.getParent()).endsWith("stkForm");
125 }
126 }
127 return true;
128 }
129 } catch (RepositoryException e) {
130 throw new IllegalStateException("Can't verify availability of the template", e);
131 }
132 return false;
133 }
134
135 @Override
136 public boolean isAvailable(Node content, TemplateDefinition templateDefinition) {
137 if (templateDefinition instanceof STKPage) {
138 return isAvailable(content, (STKPage) templateDefinition);
139 }
140 return false;
141 }
142
143 }