PushNotificationImpl.java 11.6 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.commons.lang3.StringUtils
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ValueMap
 *  org.apache.sling.commons.json.JSONArray
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.JSONObject
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.mobile.notifications.impl.push;

import com.adobe.cq.mobile.notifications.impl.LocalizedNotification;
import com.adobe.cq.mobile.notifications.impl.Notification;
import com.adobe.cq.mobile.notifications.impl.NotificationState;
import com.adobe.cq.mobile.notifications.impl.exceptions.NotificationException;
import com.adobe.cq.mobile.notifications.impl.push.LocalizedNotificationImpl;
import com.adobe.cq.mobile.platform.MobileResource;
import com.adobe.cq.mobile.platform.MobileResourceType;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PushNotificationImpl
implements Notification {
    private String id;
    private String title;
    private String description;
    private Map<String, LocalizedNotification> localizedNotifications;
    private NotificationState state;
    private Calendar lastSentDate = null;
    private String[] platforms;
    private String filterParameters;
    private String path;
    private final Logger logger = LoggerFactory.getLogger(PushNotificationImpl.class);

    private PushNotificationImpl() {
    }

    public PushNotificationImpl(String[] platforms, String appId, LocalizedNotification localizedNotification) throws NotificationException {
        if (platforms == null || platforms.length == 0) {
            throw new NotificationException("No platforms were specified for this notification.");
        }
        if (appId == null) {
            throw new NotificationException("No application ID was provided for this notification.");
        }
        if (localizedNotification == null) {
            throw new NotificationException("No message was specified for this notification.");
        }
        this.addLocalizedNotification(localizedNotification);
        this.setPlatforms(platforms);
        this.setState(NotificationState.READY);
        this.setId(appId + "-" + System.currentTimeMillis());
    }

    public PushNotificationImpl(Resource resource) throws NotificationException {
        MobileResource notification = (MobileResource)resource.adaptTo(MobileResource.class);
        if (notification == null || !notification.isA(MobileResourceType.NOTIFICATION.getType())) {
            throw new NotificationException("The resource is not a notification resource: " + resource.getPath());
        }
        this.setPath(resource.getPath());
        Resource resourceContent = resource.getChild("jcr:content");
        ValueMap vm = resourceContent.getValueMap();
        this.setId((String)vm.get("id", String.class));
        this.setTitle((String)vm.get("jcr:title", String.class));
        this.setDescription((String)vm.get("jcr:description", String.class));
        this.setFilterParameters((String)vm.get("targetFilter", String.class));
        this.setPlatforms((String[])vm.get("platforms", String[].class));
        String stateStr = "";
        try {
            stateStr = (String)vm.get("state", (Object)NotificationState.READY.asString());
            this.setState(NotificationState.getFromString(stateStr.trim()));
        }
        catch (IllegalArgumentException iaEx) {
            throw new NotificationException("The notification does not have a valid state: " + stateStr + " - " + resource.getPath());
        }
        this.lastSentDate = (Calendar)vm.get("lastSent", Calendar.class);
        Iterator messages = resourceContent.getChildren().iterator();
        while (messages.hasNext()) {
            LocalizedNotificationImpl nextNotification = new LocalizedNotificationImpl((Resource)messages.next());
            this.addLocalizedNotification(nextNotification);
        }
    }

    private void setId(String id) {
        this.id = id;
    }

    @Override
    public String getId() {
        return this.id;
    }

    @Override
    public void setTitle(String title) {
        this.title = title;
    }

    @Override
    public String getTitle() {
        return this.title;
    }

    @Override
    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public String getDescription() {
        return this.description;
    }

    @Override
    public void setPath(String notificationPath) {
        this.path = notificationPath;
    }

    @Override
    public String getPath() {
        return this.path;
    }

    @Override
    public void addLocalizedNotification(LocalizedNotification localizedNotification) throws NotificationException {
        if (this.localizedNotifications != null) {
            throw new NotificationException("Currently only one message per notification is supported.");
        }
        this.localizedNotifications = new HashMap<String, LocalizedNotification>();
        String locale = localizedNotification.getLocaleAsString();
        if (!locale.equals("en_US")) {
            throw new NotificationException("Currently only the 'en_US' locale is supported for localizedNotifications.");
        }
        this.localizedNotifications.put(localizedNotification.getLocaleAsString(), localizedNotification);
    }

    @Override
    public Map<String, LocalizedNotification> getLocalizedNotifications() {
        return this.localizedNotifications;
    }

    @Override
    public LocalizedNotification getLocalizedNotification(String locale) {
        return this.localizedNotifications.get(locale);
    }

    @Override
    public void setState(NotificationState state) {
        this.state = state;
        if (NotificationState.SENT.equals((Object)state)) {
            this.lastSentDate = Calendar.getInstance();
        }
    }

    @Override
    public NotificationState getState() {
        return this.state;
    }

    @Override
    public Calendar getLastSent() {
        return this.lastSentDate;
    }

    @Override
    public void setFilterParameters(String filterParameters) {
        this.filterParameters = filterParameters;
    }

    @Override
    public String getFilterParameters() {
        return this.filterParameters;
    }

    @Override
    public void setPlatforms(String[] platforms) {
        this.platforms = platforms;
    }

    @Override
    public String[] getPlatforms() {
        return this.platforms;
    }

    @Override
    public String getPlatformsString() {
        if (this.platforms == null || this.platforms.length == 0) {
            return "[]";
        }
        return StringUtils.join((Object[])this.platforms, (String)",");
    }

    @Override
    public JSONObject toJSONObject() {
        JSONObject json = new JSONObject();
        this.addJsonStringProperty(json, "path", this.getPath());
        this.addJsonStringProperty(json, "jcr:title", this.getTitle());
        this.addJsonStringProperty(json, "jcr:description", this.getDescription());
        this.addJsonStringProperty(json, "state", this.getState().toString());
        this.addJsonStringProperty(json, "id", this.getId());
        this.addJsonStringProperty(json, "targetFilter", this.getFilterParameters());
        JSONArray platforms = new JSONArray();
        if (this.getPlatforms() == null || this.getPlatforms().length == 0) {
            platforms.put((Object)"all");
        } else {
            for (int i = 0; i < this.getPlatforms().length; ++i) {
                platforms.put((Object)this.getPlatforms()[i]);
            }
        }
        this.addJsonArrayProperty(json, "platforms", platforms);
        JSONArray messageArray = new JSONArray();
        if (this.getLocalizedNotifications() != null && !this.getLocalizedNotifications().isEmpty()) {
            Iterator<Map.Entry<String, LocalizedNotification>> messagesIt = this.getLocalizedNotifications().entrySet().iterator();
            while (messagesIt.hasNext()) {
                LocalizedNotification notMess = messagesIt.next().getValue();
                messageArray.put((Object)notMess.toJSONObject());
            }
        }
        this.addJsonArrayProperty(json, "message", messageArray);
        return json;
    }

    @Override
    public Map<String, Object> getProperties() {
        HashMap<String, Object> props = new HashMap<String, Object>();
        this.addPropertyToMap(props, "id", this.getId());
        this.addPropertyToMap(props, "platforms", this.getPlatforms());
        this.addPropertyToMap(props, "jcr:title", this.getTitle());
        this.addPropertyToMap(props, "jcr:description", this.getDescription());
        this.addPropertyToMap(props, "state", this.getState().toString());
        this.addPropertyToMap(props, "lastSent", this.getLastSent());
        this.addPropertyToMap(props, "targetFilter", this.getFilterParameters());
        return Collections.unmodifiableMap(props);
    }

    public String toString() {
        String platformsStr = this.getPlatforms() == null ? "[]" : Arrays.toString(this.getPlatforms());
        String message = "";
        String notificationTitle = "";
        String linkText = "";
        String link = "";
        if (this.localizedNotifications != null) {
            message = Integer.toString(this.localizedNotifications.size());
            if (this.localizedNotifications.size() == 1) {
                LocalizedNotification localizedNotification = this.localizedNotifications.entrySet().iterator().next().getValue();
                message = localizedNotification.getMessage();
                notificationTitle = localizedNotification.getTitle();
                linkText = localizedNotification.getLinkText();
                link = localizedNotification.getLink();
            }
        }
        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String lastSentString = this.lastSentDate == null ? "" : format1.format(this.lastSentDate.getTime());
        return "Platforms: " + platformsStr + ", Notification ID: " + this.id + ", Title: " + notificationTitle + ", Message: " + message + ", Link Text: " + linkText + ", Link: " + link + ", State: " + (this.state == null ? "null" : this.state.toString()) + (this.lastSentDate == null ? "" : new StringBuilder().append(", Last Sent: ").append(lastSentString).toString()) + ", Target Filter: " + this.filterParameters + ", App Path: " + this.path;
    }

    private void addPropertyToMap(Map<String, Object> map, String key, Object value) {
        if (value != null) {
            map.put(key, value);
        }
    }

    private void addJsonStringProperty(JSONObject json, String key, String value) {
        try {
            json.put(key, (Object)value);
        }
        catch (JSONException jsonEx) {
            this.logger.warn("Error converting Notification to JSON with value '" + key + "'", (Throwable)jsonEx);
        }
    }

    private void addJsonArrayProperty(JSONObject json, String key, JSONArray value) {
        try {
            json.put(key, (Object)value);
        }
        catch (JSONException jsonEx) {
            this.logger.warn("Error converting Notification to JSON with value '" + key + "'", (Throwable)jsonEx);
        }
    }
}