MobileDeviceScreensDatasource.java
5.74 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* aQute.bnd.annotation.component.Activate
* com.adobe.granite.ui.components.ds.DataSource
* com.adobe.granite.ui.components.ds.SimpleDataSource
* com.adobe.granite.ui.components.ds.ValueMapResource
* com.day.cq.i18n.I18n
* javax.jcr.RepositoryException
* javax.servlet.ServletException
* javax.servlet.http.HttpServletRequest
* org.apache.commons.collections.Transformer
* org.apache.commons.collections.iterators.TransformIterator
* org.apache.felix.scr.annotations.sling.SlingServlet
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.SlingHttpServletResponse
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ValueMap
* org.apache.sling.api.servlets.SlingAllMethodsServlet
* org.apache.sling.api.wrappers.ValueMapDecorator
* org.osgi.framework.BundleContext
* org.osgi.framework.InvalidSyntaxException
* org.osgi.framework.ServiceReference
* org.osgi.service.component.ComponentContext
*/
package com.adobe.cq.mobile.platform.impl.store;
import aQute.bnd.annotation.component.Activate;
import com.adobe.cq.mobile.platform.impl.store.MobileDevice;
import com.adobe.cq.mobile.platform.impl.store.MobilePlatformProvider;
import com.adobe.cq.mobile.platform.impl.store.ScreenDefinition;
import com.adobe.granite.ui.components.ds.DataSource;
import com.adobe.granite.ui.components.ds.SimpleDataSource;
import com.adobe.granite.ui.components.ds.ValueMapResource;
import com.day.cq.i18n.I18n;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.jcr.RepositoryException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.collections.Transformer;
import org.apache.commons.collections.iterators.TransformIterator;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.api.wrappers.ValueMapDecorator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import org.osgi.service.component.ComponentContext;
@SlingServlet(methods={"GET"}, resourceTypes={"mobileapps/gui/components/dashboard/tiles/info/platforms/screens/datasource"})
public class MobileDeviceScreensDatasource
extends SlingAllMethodsServlet {
private ComponentContext context;
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
final ResourceResolver resolver = request.getResourceResolver();
final I18n i18n = new I18n((HttpServletRequest)request);
try {
ServiceReference[] refs = this.context.getBundleContext().getServiceReferences(MobilePlatformProvider.class.getName(), null);
if (refs == null || refs.length == 0) {
throw new ServletException("Can't find any platform providers");
}
final HashMap<String, Collection<MobileDevice>> devices = new HashMap<String, Collection<MobileDevice>>();
for (ServiceReference ref : refs) {
String platformId = (String)ref.getProperty("cq.mobile.apps.platformprovider");
MobilePlatformProvider provider = (MobilePlatformProvider)this.context.getBundleContext().getService(ref);
devices.put(platformId, provider.getSupportedDevices());
}
SimpleDataSource ds = new SimpleDataSource((Iterator)new TransformIterator(devices.keySet().iterator(), new Transformer(){
public Object transform(Object input) {
try {
String platformId = (String)input;
Collection devicesForPlatform = (Collection)devices.get(platformId);
HashMap deviceToScreens = new HashMap();
for (MobileDevice mobileDevice : devicesForPlatform) {
String deviceId = mobileDevice.getId();
List<ScreenDefinition> screenDefinitions = mobileDevice.getSupportedScreens();
ArrayList<String> screenDescriptions = new ArrayList<String>(screenDefinitions.size());
for (ScreenDefinition sd : screenDefinitions) {
screenDescriptions.add(sd.getDescription(i18n));
}
deviceToScreens.put(deviceId, screenDescriptions);
}
ValueMapDecorator vm = new ValueMapDecorator(new HashMap());
vm.put((Object)"platformId", (Object)platformId);
vm.put((Object)"devices", deviceToScreens);
return new ValueMapResource(resolver, "/mobile/platform/" + platformId, "nt:unstructured", (ValueMap)vm);
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
}));
request.setAttribute(DataSource.class.getName(), (Object)ds);
}
catch (InvalidSyntaxException e) {
throw new ServletException("Failed to find mobile platform providers", (Throwable)e);
}
}
@Activate
protected void activate(ComponentContext context) throws RepositoryException {
this.context = context;
}
}