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.category;
35
36 import info.magnolia.cms.core.Content;
37 import info.magnolia.cms.core.MgnlNodeType;
38 import info.magnolia.cms.core.search.Query;
39 import info.magnolia.cms.util.ContentUtil;
40 import info.magnolia.cms.util.QueryUtil;
41 import info.magnolia.jcr.wrapper.I18nNodeWrapper;
42 import info.magnolia.module.templatingkit.templates.pages.STKPage;
43 import info.magnolia.objectfactory.Components;
44 import info.magnolia.registry.RegistrationException;
45 import info.magnolia.rendering.template.TemplateDefinition;
46 import info.magnolia.rendering.template.registry.TemplateDefinitionRegistry;
47
48 import java.util.ArrayList;
49 import java.util.HashSet;
50 import java.util.Iterator;
51 import java.util.List;
52 import java.util.Set;
53
54 import javax.jcr.Node;
55 import javax.jcr.RepositoryException;
56 import javax.jcr.Session;
57
58 import org.apache.commons.lang.StringUtils;
59 import org.slf4j.Logger;
60 import org.slf4j.LoggerFactory;
61
62
63
64
65
66
67
68
69 public class TemplateCategoryUtil {
70
71 private static TemplateDefinitionRegistry templateDefinitionRegistry = Components.getComponent(TemplateDefinitionRegistry.class);
72
73 private static Logger log = LoggerFactory.getLogger(TemplateCategoryUtil.class);
74
75
76
77
78 public static String getTemplateCategory(Node pageNode) {
79 return getTemplateCategory(ContentUtil.asContent(pageNode));
80 }
81
82
83
84
85
86
87 public static String getTemplateCategory(Content pageNode) {
88 final String templateId = pageNode.getTemplate();
89 try {
90 TemplateDefinition template = templateDefinitionRegistry.getTemplateDefinition(templateId);
91 if (template instanceof STKPage) {
92 STKPage stkTemplate = (STKPage) template;
93 return stkTemplate.getCategory();
94 }
95 return TemplateCategory.CONTENT;
96 } catch (RegistrationException e) {
97 log.debug(e.getMessage(), e);
98 return "";
99 }
100 }
101
102
103
104
105 public static String getTemplateSubCategory(Node pageNode) {
106 return getTemplateSubCategory(ContentUtil.asContent(pageNode));
107 }
108
109
110
111
112
113
114 public static String getTemplateSubCategory(Content pageNode) {
115 final String templateId = pageNode.getTemplate();
116 try {
117 TemplateDefinition template = templateDefinitionRegistry.getTemplateDefinition(templateId);
118 if (template instanceof STKPage) {
119 STKPage stkTemplate = (STKPage) template;
120 return stkTemplate.getSubcategory();
121 }
122 return TemplateCategory.CONTENT;
123 } catch (RegistrationException e) {
124 return "";
125 }
126 }
127
128
129
130
131 public static boolean hasTemplateOfCategory(Node pageNode, String templateCatergory) {
132 return hasTemplateOfCategory(ContentUtil.asContent(pageNode), templateCatergory);
133 }
134
135 public static boolean hasTemplateOfCategory(Content pageNode, String templateCatergory) {
136 return TemplateCategoryUtil.getTemplateCategory(pageNode).equals(templateCatergory);
137 }
138
139
140
141
142 public static Node findParentWithTemplateCategory(Node pageNode, String templateCategory) throws RepositoryException {
143 Content parent = findParentWithTemplateCategory(ContentUtil.asContent(pageNode), templateCategory);
144 return parent != null ? parent.getJCRNode() : null;
145 }
146
147 public static Content findParentWithTemplateCategory(Content pageNode, String templateCategory) throws RepositoryException {
148 Content current = pageNode;
149
150 while(current.getLevel()>=0){
151 if(TemplateCategoryUtil.hasTemplateOfCategory(current, templateCategory)) {
152 return current;
153 }
154
155 if(current.getLevel() == 0) {
156 break;
157 }
158 current = current.getParent();
159 }
160 return null;
161 }
162
163
164
165
166 public static boolean isContentPage(Node pageNode){
167 return isContentPage(ContentUtil.asContent(pageNode));
168 }
169
170 public static boolean isContentPage(Content pageNode){
171 return !TemplateCategoryUtil.hasTemplateOfCategory(pageNode, TemplateCategory.HOME) && !!TemplateCategoryUtil.hasTemplateOfCategory(pageNode, TemplateCategory.SECTION);
172 }
173
174 public static List<Node> getContentListByTemplateCategorySubCategory(Node siteRoot, String category, String subCategory) throws RepositoryException {
175 return getContentListByTemplateCategorySubCategory(siteRoot, category, subCategory,Integer.MAX_VALUE, null, null);
176 }
177
178 public static List<Node> getContentListByTemplateCategorySubCategory(Node siteRoot, String category, String subCategory, int maxResultSize, String andClause, String orderBy) throws RepositoryException {
179 final Set<String> templateIds = new HashSet<String>();
180 final Iterator<TemplateDefinition> templatesIterator = templateDefinitionRegistry.getTemplateDefinitions().iterator();
181 while (templatesIterator.hasNext()) {
182 final TemplateDefinition template = templatesIterator.next();
183 if(template instanceof STKPage) {
184 final STKPage stkTemplate = (STKPage)template;
185 if(StringUtils.equals(stkTemplate.getCategory(), category)
186 && (StringUtils.isEmpty(subCategory) || StringUtils.equals(stkTemplate.getSubcategory(), subCategory) )) {
187 templateIds.add(template.getId());
188 }
189 }
190 }
191 if (!templateIds.isEmpty()) {
192 return getContentListByTemplateNames(siteRoot, templateIds, maxResultSize, andClause, orderBy);
193 } else {
194 return new ArrayList<Node>();
195 }
196 }
197
198 public static List<Node> getContentListByTemplateName(Node searchRoot, String templateName) throws RepositoryException {
199 return getContentListByTemplateName(searchRoot, templateName, Integer.MAX_VALUE, null, null);
200 }
201
202 public static List<Node> getContentListByTemplateName(Node searchRoot, String templateName, int maxResultSize, String andClause, String orderByClause) throws RepositoryException {
203 Set<String> templateNames = new HashSet<String>();
204 templateNames.add(templateName);
205 return getContentListByTemplateNames(searchRoot, templateNames, maxResultSize, andClause, orderByClause);
206 }
207
208
209
210
211
212
213
214
215
216
217
218 public static List<Node> getContentListByTemplateNames(Node searchRoot, Set<String> templateIds, int maxResultSize, String andClause, String orderByClause) throws RepositoryException{
219 Session session = searchRoot.getSession();
220 String path = searchRoot.getPath();
221 String repository = session.getWorkspace().getName();
222
223 StringBuffer sql = new StringBuffer("select * from nt:base where jcr:path like '" + path +"/%'");
224 if (templateIds!=null && !templateIds.isEmpty()) {
225 sql.append(" AND (");
226 Iterator<String> templateNamesIter = templateIds.iterator();
227 do {
228 sql.append("mgnl:template = '" + templateNamesIter.next() + "'");
229 if (templateNamesIter.hasNext()) {
230 sql.append(" OR ");
231 }
232 } while (templateNamesIter.hasNext());
233 sql.append(")");
234 }
235 if (andClause != null) {
236 sql.append(" AND " + andClause);
237 }
238 if (orderByClause != null) {
239 sql.append(" ORDER BY " + orderByClause);
240 }
241
242
243 List<Content> itemsListFromQuery = (List<Content>) QueryUtil.query(repository, sql.toString(), Query.SQL , MgnlNodeType.NT_CONTENT, maxResultSize);
244 List<Node> itemsList = new ArrayList<Node>(itemsListFromQuery.size());
245
246
247 for (int i = 0; i < itemsListFromQuery.size(); i++) {
248 itemsList.add(i, new I18nNodeWrapper(itemsListFromQuery.get(i).getJCRNode()));
249 }
250 return itemsList;
251 }
252
253 public static Node getNearestContentByTemplateCategorySubCategory(Node siteRoot, String category, String subCategory, Node current) throws RepositoryException {
254
255 List<Node> nodeList = getContentListByTemplateCategorySubCategory(siteRoot, category, subCategory);
256 if(!nodeList.isEmpty()) {
257 Node bestMatching = null;
258 int bestMatchingLength =-1;
259 for (Node node : nodeList) {
260 int matchingLegth = StringUtils.indexOfDifference(node.getPath(), current.getPath());
261 if (matchingLegth > bestMatchingLength) {
262 bestMatchingLength = matchingLegth;
263 bestMatching = node;
264 }
265 }
266 return bestMatching;
267 }
268 return null;
269 }
270 }