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.HTMLEscapingNodeWrapper;
42 import info.magnolia.jcr.wrapper.I18nNodeWrapper;
43 import info.magnolia.module.templatingkit.templates.pages.STKPage;
44 import info.magnolia.objectfactory.Components;
45 import info.magnolia.registry.RegistrationException;
46 import info.magnolia.rendering.template.TemplateDefinition;
47 import info.magnolia.rendering.template.registry.TemplateDefinitionRegistry;
48
49 import java.util.ArrayList;
50 import java.util.HashSet;
51 import java.util.Iterator;
52 import java.util.List;
53 import java.util.Set;
54
55 import javax.jcr.LoginException;
56 import javax.jcr.Node;
57 import javax.jcr.NodeIterator;
58 import javax.jcr.RepositoryException;
59 import javax.jcr.Session;
60
61 import org.apache.commons.lang.StringUtils;
62 import org.slf4j.Logger;
63 import org.slf4j.LoggerFactory;
64
65
66
67
68
69
70
71 public class TemplateCategoryUtil {
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 = Components.getComponent(TemplateDefinitionRegistry.class).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 if (StringUtils.isBlank(templateId)) {
117 log.debug("Page [" + pageNode.getHandle() + "] doesn't have assigned any template.");
118 return "";
119 }
120 try {
121 TemplateDefinition template = Components.getComponent(TemplateDefinitionRegistry.class).getTemplateDefinition(templateId);
122 if (template instanceof STKPage) {
123 STKPage stkTemplate = (STKPage) template;
124 return stkTemplate.getSubcategory();
125 }
126 return TemplateCategory.CONTENT;
127 } catch (RegistrationException e) {
128 return "";
129 }
130 }
131
132
133
134
135 public static boolean hasTemplateOfCategory(Node pageNode, String templateCatergory) {
136 return hasTemplateOfCategory(ContentUtil.asContent(pageNode), templateCatergory);
137 }
138
139 public static boolean hasTemplateOfCategory(Content pageNode, String templateCatergory) {
140 return TemplateCategoryUtil.getTemplateCategory(pageNode).equals(templateCatergory);
141 }
142
143
144
145
146 public static Node findParentWithTemplateCategory(Node pageNode, String templateCategory) throws RepositoryException {
147 Content parent = findParentWithTemplateCategory(ContentUtil.asContent(pageNode), templateCategory);
148 return parent != null ? parent.getJCRNode() : null;
149 }
150
151 public static Content findParentWithTemplateCategory(Content pageNode, String templateCategory) throws RepositoryException {
152 Content current = pageNode;
153
154 while (current.getLevel() >= 0) {
155 if (TemplateCategoryUtil.hasTemplateOfCategory(current, templateCategory)) {
156 return current;
157 }
158
159 if (current.getLevel() == 0) {
160 break;
161 }
162 current = current.getParent();
163 }
164 return null;
165 }
166
167
168
169
170 public static boolean isContentPage(Node pageNode) {
171 return isContentPage(ContentUtil.asContent(pageNode));
172 }
173
174 public static boolean isContentPage(Content pageNode) {
175 return !TemplateCategoryUtil.hasTemplateOfCategory(pageNode, TemplateCategory.HOME) && !!TemplateCategoryUtil.hasTemplateOfCategory(pageNode, TemplateCategory.SECTION);
176 }
177
178 public static List<Node> getContentListByTemplateCategorySubCategory(Node siteRoot, String category, String subCategory) throws RepositoryException {
179 return getContentListByTemplateCategorySubCategory(siteRoot, category, subCategory, Integer.MAX_VALUE, null, null);
180 }
181
182 public static List<Node> getContentListByTemplateCategorySubCategory(Node siteRoot, String category, String subCategory, int maxResultSize, String andClause, String orderBy) throws RepositoryException {
183 final Set<String> templateIds = new HashSet<String>();
184 final Iterator<TemplateDefinition> templatesIterator = Components.getComponent(TemplateDefinitionRegistry.class).getTemplateDefinitions().iterator();
185 while (templatesIterator.hasNext()) {
186 final TemplateDefinition template = templatesIterator.next();
187 if (template instanceof STKPage) {
188 final STKPage stkTemplate = (STKPage) template;
189 if (StringUtils.equals(stkTemplate.getCategory(), category)
190 && (StringUtils.isEmpty(subCategory) || StringUtils.equals(stkTemplate.getSubcategory(), subCategory))) {
191 templateIds.add(template.getId());
192 }
193 }
194 }
195 if (!templateIds.isEmpty()) {
196 return getContentListByTemplateNames(siteRoot, templateIds, maxResultSize, andClause, orderBy);
197 } else {
198 return new ArrayList<Node>();
199 }
200 }
201
202 public static List<Node> getContentListByTemplateName(Node searchRoot, String templateName) throws RepositoryException {
203 return getContentListByTemplateName(searchRoot, templateName, Integer.MAX_VALUE, null, null);
204 }
205
206 public static List<Node> getContentListByTemplateName(Node searchRoot, String templateName, int maxResultSize, String andClause, String orderByClause) throws RepositoryException {
207 Set<String> templateNames = new HashSet<String>();
208 templateNames.add(templateName);
209 return getContentListByTemplateNames(searchRoot, templateNames, maxResultSize, andClause, orderByClause);
210 }
211
212
213
214
215
216
217
218
219
220
221
222
223 public static List<Node> getContentListByTemplateNames(Node searchRoot, Set<String> templateIds, int maxResultSize, String andClause, String orderByClause) throws RepositoryException {
224 if (log.isDebugEnabled()) {
225 log.debug("Node: [" + searchRoot.getPath() + "]; Template IDs: [" + StringUtils.join(templateIds, ", ") + "]; MaxResultSize: [" + maxResultSize + "]; AndClause: [" + andClause + "]; OrderByClause: [" + orderByClause + "]");
226 }
227 Session session = searchRoot.getSession();
228 String path = searchRoot.getPath();
229 String repository = session.getWorkspace().getName();
230
231 StringBuffer sql = new StringBuffer("select * from nt:base where jcr:path like '" + path + "/%'");
232 if (templateIds != null && !templateIds.isEmpty()) {
233 sql.append(" AND (");
234 Iterator<String> templateNamesIter = templateIds.iterator();
235 do {
236 sql.append("mgnl:template = '" + templateNamesIter.next() + "'");
237 if (templateNamesIter.hasNext()) {
238 sql.append(" OR ");
239 }
240 } while (templateNamesIter.hasNext());
241 sql.append(")");
242 }
243 if (andClause != null) {
244 sql.append(" AND " + andClause);
245 }
246 if (orderByClause != null) {
247 sql.append(" ORDER BY " + orderByClause);
248 }
249
250 return getWrappedNodesFromQuery(sql.toString(), repository, maxResultSize);
251 }
252
253 private static List<Node> getWrappedNodesFromQuery(String sql, String repository, long maxResultSize) throws LoginException, RepositoryException {
254 List<Node> itemsListFromQuery = new ArrayList<Node>();
255 log.debug("SQL query: [" + sql + "]");
256 NodeIterator items = QueryUtil.search(repository, sql.toString(), Query.SQL, MgnlNodeType.NT_CONTENT);
257 log.debug("SQL query done, now will wrap all items with wrappers HTMLEscaping and i18n wrappers");
258 long count = 1;
259 while (items.hasNext() && count <= maxResultSize) {
260 itemsListFromQuery.add(new HTMLEscapingNodeWrapper(new I18nNodeWrapper(items.nextNode()), false));
261 count++;
262 }
263 log.debug("Wrapped " + count + "items.");
264 return itemsListFromQuery;
265 }
266
267 public static Node getNearestContentByTemplateCategorySubCategory(Node siteRoot, String category, String subCategory, Node current) throws RepositoryException {
268
269 List<Node> nodeList = getContentListByTemplateCategorySubCategory(siteRoot, category, subCategory);
270 if (!nodeList.isEmpty()) {
271 Node bestMatching = null;
272 int bestMatchingLength = -1;
273 for (Node node : nodeList) {
274 int matchingLegth = StringUtils.indexOfDifference(node.getPath(), current.getPath());
275 if (matchingLegth > bestMatchingLength) {
276 bestMatchingLength = matchingLegth;
277 bestMatching = node;
278 }
279 }
280 return bestMatching;
281 }
282 return null;
283 }
284 }