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