LocalizedNotificationImpl.java 6.85 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.commons.lang.LocaleUtils
 *  org.apache.commons.lang3.StringUtils
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ValueMap
 *  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.exceptions.NotificationException;
import com.adobe.cq.mobile.platform.MobileResource;
import com.adobe.cq.mobile.platform.MobileResourceType;
import java.util.Collections;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import org.apache.commons.lang.LocaleUtils;
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.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LocalizedNotificationImpl
implements LocalizedNotification {
    private Locale locale;
    private String message;
    private String title;
    private String link;
    private String linkText;
    private final Logger logger = LoggerFactory.getLogger(LocalizedNotificationImpl.class);

    private LocalizedNotificationImpl() {
    }

    public LocalizedNotificationImpl(String locale, String title, String message) throws NotificationException {
        this(locale, title, message, null, null);
    }

    public LocalizedNotificationImpl(String locale, String title, String message, String link, String linkText) throws NotificationException {
        this.setMessage(message);
        this.setTitle(title);
        this.setLocale(LocaleUtils.toLocale((String)locale));
        this.setLink(link);
        this.setLinkText(linkText);
    }

    public LocalizedNotificationImpl(Resource resource) throws NotificationException {
        if (resource == null) {
            throw new NotificationException("Invalid resource.");
        }
        MobileResource mobileResource = (MobileResource)resource.getParent().getParent().adaptTo(MobileResource.class);
        if (mobileResource == null || !mobileResource.isA(MobileResourceType.NOTIFICATION.getType())) {
            throw new NotificationException("Invalid resource. Resource must be a notification message.");
        }
        ValueMap vm = resource.getChild("jcr:content").getValueMap();
        this.setMessage((String)vm.get("message", String.class));
        this.setTitle((String)vm.get("title", String.class));
        this.setLink((String)vm.get("link", String.class));
        this.setLinkText((String)vm.get("linkText", String.class));
        this.setLocale(LocaleUtils.toLocale((String)((String)vm.get("locale", String.class))));
    }

    @Override
    public Locale getLocale() {
        return this.locale;
    }

    @Override
    public String getLocaleAsString() {
        String localeStr = "";
        if (this.locale != null) {
            if (!StringUtils.isBlank((CharSequence)this.locale.getLanguage())) {
                localeStr = localeStr + this.locale.getLanguage();
            }
            if (!StringUtils.isBlank((CharSequence)this.locale.getCountry())) {
                localeStr = localeStr + "_" + this.locale.getCountry();
            }
            if (!StringUtils.isBlank((CharSequence)this.locale.getVariant())) {
                localeStr = localeStr + "_" + this.locale.getVariant();
            }
        }
        return localeStr;
    }

    @Override
    public void setLocale(Locale locale) throws NotificationException {
        if (locale == null) {
            throw new NotificationException("No valid locale was specified for this notification message.");
        }
        this.locale = locale;
    }

    @Override
    public void setMessage(String message) throws NotificationException {
        if (StringUtils.isBlank((CharSequence)message)) {
            throw new NotificationException("No message was specified for this notification message: " + this.getLocaleAsString());
        }
        if (message.trim().length() > 250) {
            throw new NotificationException("Notification message exceeded maximum length. The maximum length is 250.");
        }
        this.message = message.trim();
    }

    @Override
    public String getMessage() {
        return this.message;
    }

    @Override
    public void setTitle(String title) throws NotificationException {
        if (title == null) {
            this.title = "";
        } else {
            if (title.trim().length() > 50) {
                throw new NotificationException("Notification title exceeded maximum length. The maximum length is 50.");
            }
            this.title = title.trim();
        }
    }

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

    @Override
    public String getLink() {
        return this.link;
    }

    @Override
    public void setLink(String link) throws NotificationException {
        this.link = link;
    }

    @Override
    public String getLinkText() {
        return this.linkText;
    }

    @Override
    public void setLinkText(String linkText) throws NotificationException {
        this.linkText = StringUtils.isBlank((CharSequence)linkText) ? "OK" : linkText;
    }

    @Override
    public Map<String, Object> getProperties() {
        HashMap<String, Object> props = new HashMap<String, Object>();
        this.addPropertyToMap(props, "locale", this.getLocaleAsString());
        this.addPropertyToMap(props, "message", this.getMessage());
        this.addPropertyToMap(props, "title", this.getTitle());
        this.addPropertyToMap(props, "linkText", this.getLinkText());
        this.addPropertyToMap(props, "link", this.getLink());
        return Collections.unmodifiableMap(props);
    }

    @Override
    public JSONObject toJSONObject() {
        JSONObject json = new JSONObject();
        this.addJsonStringProperty(json, "locale", this.getLocaleAsString());
        this.addJsonStringProperty(json, "title", this.getTitle());
        this.addJsonStringProperty(json, "message", this.getMessage());
        this.addJsonStringProperty(json, "linkText", this.getLinkText());
        this.addJsonStringProperty(json, "link", this.getLink());
        return json;
    }

    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);
        }
    }
}