PushNotificationDataSourceImpl.java
5.04 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.cq.mobile.ui.PushNotificationDataSource
* com.day.cq.wcm.api.Page
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.osgi.framework.BundleContext
* org.osgi.framework.ServiceReference
* org.osgi.service.component.ComponentContext
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.mobile.ui.impl;
import com.adobe.cq.mobile.notifications.impl.services.NotificationService;
import com.adobe.cq.mobile.platform.MobileResource;
import com.adobe.cq.mobile.platform.MobileResourceLocator;
import com.adobe.cq.mobile.platform.MobileResourceType;
import com.adobe.cq.mobile.ui.PushNotificationDataSource;
import com.day.cq.wcm.api.Page;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class PushNotificationDataSourceImpl
implements PushNotificationDataSource {
private static final Logger LOGGER = LoggerFactory.getLogger(PushNotificationDataSourceImpl.class);
private MobileResource mobileResource = null;
private MobileResourceLocator mobileResourceLocator = null;
private ComponentContext context;
private final String LABEL_JSON_NOTIFICATION = "notifications";
public PushNotificationDataSourceImpl(Resource resource, ComponentContext context) {
if (resource == null) {
throw new IllegalArgumentException("Cannot instantiate NotificationDataSource. Resource cannot be null.");
}
this.mobileResource = (MobileResource)resource.adaptTo(MobileResource.class);
if (this.mobileResource == null) {
throw new IllegalArgumentException("Cannot instantiate NotificationDataSource. Resource must be a Mobile Resource.");
}
this.mobileResourceLocator = (MobileResourceLocator)resource.getResourceResolver().adaptTo(MobileResourceLocator.class);
if (this.mobileResourceLocator == null) {
throw new IllegalArgumentException("Cannot instantiate NotificationDataSource. Could not instantiate a MobileResourceLocator.");
}
this.context = context;
}
public List<Resource> getNotifications() throws Exception {
List notificationList = new ArrayList<Resource>();
MobileResource group = this.mobileResource.isA(MobileResourceType.GROUP.getType()) ? this.mobileResource : this.mobileResourceLocator.findAncestorResourceByType((Resource)this.mobileResource.adaptTo(Resource.class), MobileResourceType.GROUP.getType(), new String[0]);
if (group == null) {
throw new Exception("Cannot locate the Application Group.");
}
Resource notificationsRes = ((Resource)group.adaptTo(Resource.class)).getChild("notifications");
if (notificationsRes != null) {
notificationList = this.getNotificationList(notificationsRes);
Collections.sort(notificationList, new Comparator<Resource>(){
@Override
public int compare(Resource o1, Resource o2) {
Page p = (Page)o1.adaptTo(Page.class);
Page p2 = (Page)o2.adaptTo(Page.class);
return p2.getLastModified().compareTo(p.getLastModified());
}
});
}
return notificationList;
}
private List<Resource> getNotificationList(Resource notificationRoot) {
ArrayList<Resource> notificationList = new ArrayList<Resource>();
Iterable bucketFolders = notificationRoot.getChildren();
for (Resource folder : bucketFolders) {
Iterable notifications = folder.getChildren();
for (Resource notification : notifications) {
Resource content;
MobileResource mr;
Page page = (Page)notification.adaptTo(Page.class);
if (page == null || !(mr = (MobileResource)(content = page.getContentResource()).adaptTo(MobileResource.class)).isA(MobileResourceType.NOTIFICATION.getType())) continue;
notificationList.add(notification);
}
}
return notificationList;
}
public List<ServiceReference> getNotificationServices() throws Exception {
List notificationServices = new ArrayList<ServiceReference>();
try {
ServiceReference[] refs;
if (this.context != null && (refs = this.context.getBundleContext().getAllServiceReferences(NotificationService.class.getName(), null)) != null) {
notificationServices = Arrays.asList(refs);
}
}
catch (Throwable e) {
LOGGER.error("Error retrieving Notification Services.", e);
}
return notificationServices;
}
}