pendingdevices.jsp
8.55 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<%--
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.cq.screens.device.registration.PendingDevice,
com.adobe.cq.screens.device.registration.RegistrationService,
com.adobe.cq.screens.device.registration.PendingDevice.State,
com.adobe.granite.ui.components.Config,
com.adobe.granite.ui.components.ExpressionHelper,
com.adobe.granite.ui.components.PagingIterator,
com.adobe.granite.ui.components.ds.AbstractDataSource,
com.adobe.granite.ui.components.ds.DataSource,
com.adobe.granite.ui.components.ds.EmptyDataSource,
org.apache.commons.collections.IteratorUtils,
org.apache.commons.collections.Predicate,
org.apache.commons.collections.Transformer,
org.apache.commons.collections.iterators.FilterIterator,
org.apache.commons.collections.iterators.TransformIterator,
org.apache.sling.api.resource.Resource,
org.apache.sling.api.resource.ResourceResolver,
org.apache.sling.api.resource.ResourceWrapper,
org.apache.sling.api.resource.SyntheticResource,
org.apache.sling.commons.json.JSONException,
org.apache.sling.commons.json.JSONObject,
java.util.Collections,
java.util.Comparator,
java.util.Iterator,
java.util.List,
java.util.regex.Pattern"%>
<%@ page import="org.slf4j.Logger" %><%
/**
A datasource returning the pending devices
@datasource
@name ChildPages
@location /libs/screens/dcc/components/datasources/pendingdevices
@property {String} itemResourceType
*/
ExpressionHelper ex = cmp.getExpressionHelper();
Config dsCfg = new Config(resource.getChild(Config.DATASOURCE));
final String status = ex.get(dsCfg.get("status", String.class), String.class);
final State state = status != null ? State.valueOf(status) : null;
final Integer offset = ex.get(dsCfg.get("offset", String.class), Integer.class);
final Integer limit = ex.get(dsCfg.get("limit", String.class), Integer.class);
final String filter = dsCfg.get("exclude", "");
final Pattern pattern = Pattern.compile(filter);
final String sortName = slingRequest.getParameter("sortName");
final String sortDir = slingRequest.getParameter("sortDir");
final RegistrationService registrationService = sling.getService(RegistrationService.class);
DataSource ds;
if (registrationService == null) {
ds = EmptyDataSource.instance();
} else {
final Iterator<PendingDevice> sortedDevicesIterator =
((sortName != null && sortDir != null)
? getSortedIterator(registrationService.getDevices(state), sortName, sortDir)
: registrationService.getDevices(state));
final ResourceResolver resResolver = resourceResolver;
final String itemRT = dsCfg.get("itemResourceType", String.class);
@SuppressWarnings("unchecked")
DataSource datasource = new AbstractDataSource() {
@SuppressWarnings("unchecked")
public Iterator<Resource> iterator() {
Iterator<Resource> it = new PagingIterator<Resource>(new FilterIterator(sortedDevicesIterator, new Predicate() {
public boolean evaluate(Object o) {
PendingDevice pendingDevice = (PendingDevice) o;
if (State.REGISTERED.equals(pendingDevice.getState())) {
return false;
}
Resource r = new PendingDeviceResource(resResolver, pendingDevice);
if (pattern.matcher(r.getPath()).matches()) {
return false;
}
return true;
}
}), offset, limit);
return new TransformIterator(it, new Transformer() {
public Object transform(Object o) {
PendingDevice pendingDevice = (PendingDevice) o;
Resource r = new PendingDeviceResource(resResolver, pendingDevice);
return new ResourceWrapper(r) {
public String getResourceType() {
return itemRT;
}
};
}
});
}
};
ds = datasource;
}
request.setAttribute(DataSource.class.getName(), ds);
%>
<%!
class PendingDeviceResource extends SyntheticResource {
private final PendingDevice device;
public PendingDeviceResource(ResourceResolver resolver, PendingDevice device) {
super(resolver, "", "");
this.device = device;
}
public String getName() {
return device.getToken();
}
public String getPath() {
return device.getToken();
}
public String getResourceType() {
return "screens/core/components/service/registration";
}
public JSONObject getMetadata() {
return device.getMetadata();
}
}
Iterator<PendingDevice> getSortedIterator(Iterator<PendingDevice> devices, String sortName, String sortDir){
List<PendingDevice> devicesList = IteratorUtils.toList(devices);
Comparator comparator = getComparator(sortName, sortDir.toLowerCase().equals("desc"));
Collections.sort(devicesList, comparator);
return devicesList.iterator();
}
private Comparator<PendingDevice> getComparator(final String sortName, final boolean reverse) {
Comparator comparator = new Comparator<PendingDevice>() {
public int compare(PendingDevice pd1, PendingDevice pd2) {
switch (sortName) {
case "title":
return pd1.getToken().compareToIgnoreCase(pd2.getToken());
case "uuid":
String uuid1 = getProperty(pd1, "uuid");
String uuid2 = getProperty(pd2, "uuid");
return uuid1.compareToIgnoreCase(uuid2);
case "model":
String model1 = getProperty(pd1, "model");
String model2 = getProperty(pd2, "model");
return model1.compareToIgnoreCase(model2);
case "platform":
String platform1 = getProperty(pd1, "platform");
String platform2 = getProperty(pd2, "platform");
return platform1.compareToIgnoreCase(platform2);
case "version":
String version1 = getProperty(pd1, "version");
String version2 = getProperty(pd2, "version");
return version1.compareToIgnoreCase(version2);
default:
return 0;
}
}
};
// reverses order if necessary
return reverse ? Collections.reverseOrder(comparator) : comparator;
}
private int compareProperty(Resource r1, Resource r2, String prop) {
Resource cr1 = r1.getChild("jcr:content");
if (cr1 == null) {
cr1 = r1;
}
Resource cr2 = r2.getChild("jcr:content");
if (cr2 == null) {
cr2 = r2;
}
ValueMap vm1 = cr1.getValueMap();
ValueMap vm2 = cr2.getValueMap();
return vm1.get(prop, "").compareToIgnoreCase(vm2.get(prop, ""));
}
private String getProperty(PendingDevice p, String property) {
try {
return p.getMetadata().getString(property);
}
catch (JSONException ex) {
return "";
}
}
%>