channelsinpath.jsp
5.33 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<%--
ADOBE CONFIDENTIAL
Copyright 2015 Adobe Systems Incorporated
All Rights Reserved.
NOTICE: All information contained herein is, and remains
the property of Adobe Systems Incorporated and its suppliers,
if any. The intellectual and technical concepts contained
herein are proprietary to Adobe Systems Incorporated and its
suppliers and may be covered by U.S. and Foreign Patents,
patents in process, and are protected by trade secret or copyright law.
Dissemination of this information or reproduction of this material
is strictly forbidden unless prior written permission is obtained
from Adobe Systems Incorporated.
--%><%
%><%@include file="/libs/granite/ui/global.jsp"%><%
%><%@page session="false"
import="com.adobe.granite.ui.components.Config,
com.adobe.granite.ui.components.ExpressionHelper,
com.adobe.granite.ui.components.ds.DataSource,
com.adobe.granite.ui.components.ds.EmptyDataSource,
com.adobe.granite.ui.components.ds.SimpleDataSource,
com.adobe.granite.ui.components.ds.ValueMapResource,
com.day.cq.wcm.api.Page,
org.apache.commons.collections.Transformer,
org.apache.commons.collections.iterators.TransformIterator,
org.apache.sling.api.resource.Resource,
org.apache.sling.api.resource.ResourceResolver,
org.apache.sling.api.wrappers.ValueMapDecorator,
java.util.Comparator,
java.util.List,
java.util.ArrayList,
java.util.Collection,
java.util.Collections,
java.util.HashMap,
java.util.Map,
java.util.Iterator,
com.adobe.cq.screens.binding.ScreensConstants"%>
<%@ page import="org.slf4j.Logger" %><%
/**
A datasource returning the channels in the specified paths
@datasource
@name ChildPages
@location /libs/screens/dcc/components/datasources/channelsinpath
@property {StringEL} path The path of the resource to start looking in
@property {String} itemResourceType
*/
ExpressionHelper ex = cmp.getExpressionHelper();
Config dsCfg = new Config(resource.getChild(Config.DATASOURCE));
final ResourceResolver resolver = resourceResolver;
String pathList = ex.getString(dsCfg.get("path", String.class));
String[] paths = pathList != null ? pathList.split(",") : null;
List<Resource> resources = new ArrayList<Resource>();
if (paths != null) {
Resource r;
for (String path: paths) {
r = path != null ? resourceResolver.getResource(ex.getString(path)) : null;
if (r != null) {
resources.add(r);
}
}
}
// Iterate over the resource paths and find all possible channels
final Map<String, Resource> channelsMap;
if (resources.size() > 0) {
channelsMap = new HashMap<String, Resource>();
Resource parent;
Iterator<Resource> children;
Resource child;
Page childPage;
for (Resource d: resources) {
parent = d;
while (parent != null) {
children = parent.listChildren();
while (children.hasNext()) {
child = children.next();
childPage = child.adaptTo(Page.class);
if (childPage != null && childPage.getContentResource().isResourceType(ScreensConstants.RT_CHANNEL)) {
channelsMap.put(child.getPath(), child);
}
}
parent = parent.getParent();
}
}
} else {
channelsMap = null;
}
DataSource ds;
if (channelsMap == null) {
ds = EmptyDataSource.instance();
} else {
Collection<Resource> channels = channelsMap.values();
// Sort the channels in alphabetical order
Collections.sort(new ArrayList<Resource>(channels), new Comparator<Resource>(){
@Override
public int compare(final Resource r1, Resource r2) {
return r1.adaptTo(Page.class).getTitle().compareTo(r2.adaptTo(Page.class).getTitle());
}
});
// now create the data source to use for populating the list
ds = new SimpleDataSource(new TransformIterator(channels.iterator(), new Transformer() {
public Object transform(Object input) {
try {
Resource res = (Resource) input;
ValueMap vm = new ValueMapDecorator(new HashMap<String, Object>());
String pageTitle = res.adaptTo(Page.class).getTitle();
String nodeName = res.getName();
// Appending channel title if it differs from the node name
vm.put("text", xssAPI.encodeForHTML(res.getName() + (nodeName.equals(pageTitle) ? "" : i18n.get(" ({0})", null, pageTitle))));
vm.put("value", xssAPI.encodeForHTMLAttr(nodeName));
return new ValueMapResource(resolver, res.getPath(), "nt:unstructured", vm);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}));
}
request.setAttribute(DataSource.class.getName(), ds);
%>