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