PushNotificationDataSourceImpl.java 5.04 KB
/*
 * 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;
    }

}