StatusServiceImpl.java 7.03 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.i18n.I18n
 *  com.day.cq.wcm.webservicesupport.Configuration
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceUtil
 *  org.apache.sling.api.resource.ValueMap
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.JSONObject
 */
package com.day.cq.mcm.campaign.impl;

import com.day.cq.i18n.I18n;
import com.day.cq.mcm.campaign.ACConnectorException;
import com.day.cq.mcm.campaign.CallResults;
import com.day.cq.mcm.campaign.CampaignConnector;
import com.day.cq.mcm.campaign.CampaignCredentials;
import com.day.cq.mcm.campaign.ConnectionException;
import com.day.cq.mcm.campaign.NewsletterStatus;
import com.day.cq.mcm.campaign.StatusService;
import com.day.cq.mcm.campaign.impl.NewsletterStatusImpl;
import com.day.cq.wcm.webservicesupport.Configuration;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;

@Component(metatype=0)
@Service
public class StatusServiceImpl
implements StatusService {
    private static final String PRM_DELIVERY = "deliveryName";
    @Reference
    private CampaignConnector connector;
    private Map<Integer, String> messages;

    private synchronized void initializeMessages(I18n i18n) {
        if (this.messages == null) {
            this.messages = new HashMap<Integer, String>(32);
            this.messages.put(Integer.MIN_VALUE, i18n.get("Not yet published to Adobe Campaign"));
            this.messages.put(-3, i18n.get("Publication to Adobe Campaign failed"));
            this.messages.put(-2, i18n.get("Delivery not available on Adobe Campaign"));
            this.messages.put(-1, i18n.get("Preparing to send to Adobe Campaign ..."));
            this.messages.put(0, i18n.get("Being edited"));
            this.messages.put(11, i18n.get("Targeting pending"));
            this.messages.put(12, i18n.get("Counting in progress"));
            this.messages.put(13, i18n.get("Arbitration in progress"));
            this.messages.put(15, i18n.get("Target ready"));
            this.messages.put(21, i18n.get("Personalization pending"));
            this.messages.put(22, i18n.get("Personalization in progress"));
            this.messages.put(25, i18n.get("Message finalized"));
            this.messages.put(37, i18n.get("Personalization or count failed"));
            this.messages.put(45, i18n.get("Ready to be delivered"));
            this.messages.put(51, i18n.get("Start pending"));
            this.messages.put(55, i18n.get("In progress"));
            this.messages.put(61, i18n.get("Retry pending"));
            this.messages.put(62, i18n.get("Retry in progress"));
            this.messages.put(71, i18n.get("Pause requested"));
            this.messages.put(72, i18n.get("Pause in progress"));
            this.messages.put(75, i18n.get("Paused"));
            this.messages.put(81, i18n.get("Stop requested"));
            this.messages.put(82, i18n.get("Stop in progress"));
            this.messages.put(85, i18n.get("Stopped"));
            this.messages.put(87, i18n.get("Failed"));
            this.messages.put(95, i18n.get("Finished"));
            this.messages.put(100, i18n.get("Deleted"));
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    @Override
    public NewsletterStatus retrieveStatus(Resource resource, I18n i18n) throws ACConnectorException {
        NewsletterStatusImpl nlStatus;
        block11 : {
            this.initializeMessages(i18n);
            ValueMap values = ResourceUtil.getValueMap((Resource)resource);
            if (!values.containsKey((Object)"acDelivery")) {
                nlStatus = !values.containsKey((Object)"acPublishState") ? new NewsletterStatusImpl(Integer.MIN_VALUE, "MCM#none", this.messages.get(Integer.MIN_VALUE)) : ("failed".equals(values.get("acPublishState", String.class)) ? new NewsletterStatusImpl(-3, "MCM#failed", this.messages.get(-3)) : new NewsletterStatusImpl(-1, "MCM#preparing", this.messages.get(-1)));
            } else {
                Configuration config = this.connector.getWebserviceConfig(resource);
                CampaignCredentials credentials = this.connector.retrieveCredentials(config);
                LinkedHashMap<String, String> params = new LinkedHashMap<String, String>(4);
                String deliveryId = (String)values.get("acDelivery", String.class);
                params.put("deliveryName", deliveryId);
                CallResults statusResults = null;
                try {
                    statusResults = this.connector.callFunction("amcGetDeliveryStatus.jssp", params, credentials);
                    String json = statusResults.bodyAsString();
                    JSONObject parsedJSON = new JSONObject(json);
                    int code = parsedJSON.getInt("status");
                    String string = parsedJSON.getString("statusName");
                    String message = "Unknown status: ";
                    String mappedMessage = this.messages.get(code);
                    message = mappedMessage != null ? mappedMessage : message + code + " - " + string;
                    message = message + " (" + deliveryId + ")";
                    nlStatus = new NewsletterStatusImpl(code, string, message);
                }
                catch (JSONException je) {
                    throw new ACConnectorException("Error processing the returned JSON", (Throwable)je);
                }
                catch (IOException ioe) {
                    throw new ACConnectorException("Error while reading newsletter status", ioe);
                }
                catch (ConnectionException ce) {
                    if (ce.getStatusCode() == 404) {
                        String message = this.messages.get(-2);
                        message = message + " (" + deliveryId + ")";
                        nlStatus = new NewsletterStatusImpl(-2, "MCM#missing", message);
                        break block11;
                    }
                    throw ce;
                }
                finally {
                    if (statusResults != null) {
                        statusResults.destroy();
                    }
                }
            }
        }
        return nlStatus;
    }

    protected void bindConnector(CampaignConnector campaignConnector) {
        this.connector = campaignConnector;
    }

    protected void unbindConnector(CampaignConnector campaignConnector) {
        if (this.connector == campaignConnector) {
            this.connector = null;
        }
    }
}