CampaignStateCommand.java 9.75 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.replication.ReplicationActionType
 *  com.day.cq.replication.ReplicationException
 *  com.day.cq.replication.Replicator
 *  com.day.cq.wcm.api.Page
 *  com.day.cq.wcm.api.PageManager
 *  com.day.cq.wcm.webservicesupport.Configuration
 *  com.day.cq.wcm.webservicesupport.ConfigurationManager
 *  javax.jcr.Session
 *  javax.servlet.ServletException
 *  org.apache.commons.lang.StringUtils
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.SlingHttpServletResponse
 *  org.apache.sling.api.resource.ModifiableValueMap
 *  org.apache.sling.api.resource.PersistenceException
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ValueMap
 *  org.apache.sling.api.servlets.HtmlResponse
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.JSONObject
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.analytics.testandtarget.impl.servlets.commands;

import com.day.cq.analytics.testandtarget.TestandtargetCommand;
import com.day.cq.analytics.testandtarget.TestandtargetException;
import com.day.cq.analytics.testandtarget.impl.TestandtargetPrivateService;
import com.day.cq.analytics.testandtarget.impl.TntCampaignState;
import com.day.cq.analytics.testandtarget.util.CampaignType;
import com.day.cq.replication.ReplicationActionType;
import com.day.cq.replication.ReplicationException;
import com.day.cq.replication.Replicator;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.webservicesupport.Configuration;
import com.day.cq.wcm.webservicesupport.ConfigurationManager;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.List;
import javax.jcr.Session;
import javax.servlet.ServletException;
import org.apache.commons.lang.StringUtils;
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.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.ModifiableValueMap;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.servlets.HtmlResponse;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
@Service
public class CampaignStateCommand
implements TestandtargetCommand {
    private static final Logger LOG = LoggerFactory.getLogger(CampaignStateCommand.class);
    private static final String PARAM_CAMPAIGN_STATE = "campaignState";
    private static final String PARAM_CAMPAIGN_PATH = "campaignPath";
    private List<String> acceptedStates = Arrays.asList("approved", "deactivated");
    @Reference
    protected Replicator replicator;
    @Reference
    private TestandtargetPrivateService testandtargetService;

    @Override
    public String getName() {
        return "campaignState";
    }

    @Override
    public HtmlResponse performCommand(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        String campaignPath = request.getParameter("campaignPath");
        if (StringUtils.isEmpty((String)campaignPath)) {
            this.sendError(response, 400, "The parameter campaignPath is missing.");
            return null;
        }
        String campaignState = request.getParameter("campaignState");
        if (StringUtils.isEmpty((String)campaignPath)) {
            this.sendError(response, 400, "The parameter campaignState is missing.");
            return null;
        }
        if (!this.acceptedStates.contains(campaignState)) {
            this.sendError(response, 400, "The state can only be \"approved\" and \"deactivated\".");
            return null;
        }
        LOG.debug("Changing state for campaign at {}", (Object)campaignPath);
        PageManager pMgr = (PageManager)request.getResourceResolver().adaptTo(PageManager.class);
        Page campaignPage = pMgr.getPage(campaignPath);
        if (campaignPage == null) {
            this.sendError(response, 404, "The path " + campaignPath + " doesn't point to a campaign page.");
            return null;
        }
        ValueMap properties = campaignPage.getProperties();
        String tntConfig = (String)properties.get("cq:cloudserviceconfigs", (Object)"");
        if (StringUtils.isEmpty((String)tntConfig)) {
            this.sendError(response, 500, "The campaign doesn't have a Target cloud service configuration attached.");
            return null;
        }
        ConfigurationManager configMgr = (ConfigurationManager)request.getResourceResolver().adaptTo(ConfigurationManager.class);
        Configuration configuration = configMgr.getConfiguration(tntConfig);
        if (configuration == null) {
            this.sendError(response, 500, "The configuration at " + tntConfig + " cannot be found.");
            return null;
        }
        String campaignType = (String)properties.get("campaignType", (Object)"");
        LOG.debug("Campaign type is {}", (Object)campaignType);
        String authorCampaignId = (String)properties.get("cq:authorExternalId", (Object)"");
        if (StringUtils.isEmpty((String)authorCampaignId)) {
            this.sendError(response, 500, "The campaign at doesn't have a Target id. Maybe it's not synchronized yet.");
        }
        LOG.debug("Campaign is synchronized, id is {}", (Object)authorCampaignId);
        try {
            LOG.debug("Deactivating the author campaign");
            this.testandtargetService.setCampaignState(configuration, campaignState, authorCampaignId, null, CampaignType.fromString(campaignType));
            String publishCampaignId = (String)properties.get("publishCampaignId", (Object)"");
            if (StringUtils.isNotEmpty((String)publishCampaignId)) {
                if (TntCampaignState.APPROVED.name().equals(campaignState)) {
                    this.syncPage(campaignPage, ReplicationActionType.ACTIVATE);
                } else if (TntCampaignState.DEACTIVATED.name().equals(campaignState)) {
                    this.syncPage(campaignPage, ReplicationActionType.DEACTIVATE);
                }
                this.testandtargetService.setCampaignState(configuration, campaignState, publishCampaignId, null, CampaignType.fromString(campaignType));
            }
            this.updateCampaignState(campaignPage, campaignState);
            this.sendOk(response, "The state of the activity has been changed on Target.");
        }
        catch (TestandtargetException e) {
            this.sendError(response, 500, e.getMessage());
        }
        catch (ReplicationException e) {
            this.sendError(response, 500, e.getMessage());
        }
        return null;
    }

    private void updateCampaignState(Page campaignPage, String state) throws PersistenceException {
        Resource res = campaignPage.getContentResource();
        ModifiableValueMap mvm = (ModifiableValueMap)res.adaptTo(ModifiableValueMap.class);
        boolean campaignActive = TntCampaignState.APPROVED.name().equalsIgnoreCase(state);
        mvm.put((Object)"campaignActive", (Object)campaignActive);
        res.getResourceResolver().commit();
    }

    private void syncPage(Page page, ReplicationActionType actionType) throws ReplicationException {
        LOG.debug("Unpublishing page at {}", (Object)page.getPath());
        if (this.isPublished(page)) {
            Session session = (Session)page.getContentResource().getResourceResolver().adaptTo(Session.class);
            this.replicator.replicate(session, actionType, page.getPath());
        }
    }

    private boolean isPublished(Page page) {
        ValueMap properties = page.getProperties();
        return properties.containsKey((Object)"cq:lastReplicationAction") && "Activate".equals(properties.get("cq:lastReplicationAction", (Object)""));
    }

    private void sendOk(SlingHttpServletResponse response, String msg) throws IOException {
        response.setContentType("application/json");
        response.setStatus(200);
        PrintWriter out = response.getWriter();
        out.print(this.buildMessageObject(msg));
    }

    private void sendError(SlingHttpServletResponse response, int status, String msg) throws IOException {
        response.setContentType("application/json");
        response.setStatus(status);
        PrintWriter out = response.getWriter();
        out.print(this.buildMessageObject(msg));
    }

    private String buildMessageObject(String msg) {
        JSONObject messageObject = new JSONObject();
        try {
            messageObject.put("message", (Object)msg);
            return messageObject.toString();
        }
        catch (JSONException e) {
            return msg;
        }
    }

    protected void bindReplicator(Replicator replicator) {
        this.replicator = replicator;
    }

    protected void unbindReplicator(Replicator replicator) {
        if (this.replicator == replicator) {
            this.replicator = null;
        }
    }

    protected void bindTestandtargetService(TestandtargetPrivateService testandtargetPrivateService) {
        this.testandtargetService = testandtargetPrivateService;
    }

    protected void unbindTestandtargetService(TestandtargetPrivateService testandtargetPrivateService) {
        if (this.testandtargetService == testandtargetPrivateService) {
            this.testandtargetService = null;
        }
    }
}