OfferHelper.java 5.79 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.wcm.api.Page
 *  com.day.cq.wcm.api.WCMMode
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  org.apache.commons.lang.StringUtils
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ValueMap
 */
package com.day.cq.analytics.testandtarget.util;

import com.day.cq.analytics.testandtarget.util.MboxHelper;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.WCMMode;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;

public class OfferHelper {
    private static final String DISPLAY_MBOX = "* display mbox *";
    public static final String PN_THIRDPARTYID = "ttThirdPartyId";
    public static final String RT_CAMPAIGN = "cq/personalization/components/campaignpage";
    public static final String RT_EXPERIENCE = "cq/personalization/components/experiencepage";
    public static final String RT_TEASER = "cq/personalization/components/teaserpage";
    public static final String RT_OFFER_PROXY = "cq/personalization/components/offerproxy";
    public static final String PN_TESTANDTARGETCONFIG = "testandtargetconfig";
    public static final String PN_LOCATION = "location";

    public static String getOfferName(String path) {
        return path.substring(1).replace("/", "-");
    }

    public static String getOfferName(Page offerPage, WCMMode wcmMode) {
        String location = (String)offerPage.getProperties().get("location", (Object)"");
        if ("".equals(location)) {
            return OfferHelper.getOfferName(offerPage.getPath());
        }
        StringBuilder offerName = new StringBuilder(OfferHelper.getOfferLocation(offerPage, wcmMode));
        offerName.append("-").append(offerPage.getParent().getName()).append("-").append(offerPage.getName());
        return offerName.toString();
    }

    public static String getOfferName(Page offerPage, WCMMode wcmMode, String ambitName) {
        if (ambitName.equals("master")) {
            return OfferHelper.getOfferName(offerPage, wcmMode);
        }
        StringBuilder offerName = new StringBuilder(OfferHelper.getOfferLocation(offerPage, wcmMode));
        return StringUtils.join((Object[])new String[]{offerName.toString(), ambitName, offerPage.getParent().getName(), offerPage.getName()}, (String)"-");
    }

    public static String getCampaignName(String path) {
        return OfferHelper.getOfferName(path);
    }

    public static synchronized void setThirdPartyID(Node node, String operation, String thirdpartyid) throws RepositoryException {
        if ("saveWidgetOffer".equals(operation)) {
            node.setProperty("ttThirdPartyId", thirdpartyid);
        } else if ("deleteWidgetOffer".equals(operation) && node.hasProperty("ttThirdPartyId")) {
            node.getProperty("ttThirdPartyId").remove();
        }
    }

    public static String getThirdPartyID(Node node) throws RepositoryException {
        String thirdPartyID = null;
        thirdPartyID = node.hasProperty("ttThirdPartyId") ? node.getProperty("ttThirdPartyId").getString() : Long.toString(System.currentTimeMillis());
        return thirdPartyID;
    }

    public static String getThirdPartyCampaignId(Node contentResourceNode) throws RepositoryException {
        if (contentResourceNode.hasProperty("ttThirdPartyId")) {
            return contentResourceNode.getProperty("ttThirdPartyId").getString();
        }
        return OfferHelper.getCampaignName(contentResourceNode.getParent().getPath());
    }

    public static synchronized void setConfigurationPath(String path, Page page) throws RepositoryException {
        Page campaignPage = OfferHelper.getCampaign(page);
        if (campaignPage != null) {
            Node campaignContent = (Node)campaignPage.getContentResource().adaptTo(Node.class);
            campaignContent.setProperty("testandtargetconfig", path);
        }
    }

    public static Page getCampaign(Page page) {
        Page campaignPage;
        for (campaignPage = page; campaignPage != null && !"cq/personalization/components/campaignpage".equals(campaignPage.getContentResource().getResourceType()); campaignPage = campaignPage.getParent()) {
        }
        return campaignPage;
    }

    public static String getOfferLocation(Page offerPage, WCMMode wcmMode) {
        String location = (String)offerPage.getProperties().get("location", (Object)"");
        if ("".equals(location)) {
            return "* display mbox *";
        }
        return MboxHelper.qualifyMboxNameOrId(MboxHelper.getMboxId(location), wcmMode);
    }

    public static String getOfferLocation(Page offerPage, WCMMode wcmMode, String ambitName) {
        if (StringUtils.isEmpty((String)ambitName) || "master".equals(ambitName)) {
            return OfferHelper.getOfferLocation(offerPage, wcmMode);
        }
        String location = (String)offerPage.getProperties().get("location", (Object)"");
        if ("".equals(location)) {
            return "* display mbox *";
        }
        return MboxHelper.qualifyMboxNameOrId(MboxHelper.getMboxId(location), wcmMode);
    }

    public static long getOfferId(Page offerPage) {
        return OfferHelper.getOfferId(offerPage, WCMMode.DISABLED);
    }

    public static long getOfferId(Page offerPage, WCMMode wcmMode) {
        String idProperty = wcmMode == WCMMode.DISABLED ? "cq:publishExternalId" : "cq:authorExternalId";
        ValueMap pageProps = offerPage.getProperties();
        String legacyExternalId = idProperty.replace("cq:", "");
        long offerId = (Long)pageProps.get(idProperty, (Object)0) != 0 ? (Long)pageProps.get(idProperty, (Object)0) : (Long)pageProps.get(legacyExternalId, (Object)0);
        return offerId;
    }
}