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