TargetMediatorImpl.java 4.28 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.wcm.api.Page
 *  com.day.cq.wcm.api.WCMMode
 *  com.day.cq.wcm.webservicesupport.Configuration
 *  org.apache.commons.lang.StringUtils
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.sling.api.resource.LoginException
 *  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.ResourceResolverFactory
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.analytics.testandtarget.impl;

import com.day.cq.analytics.testandtarget.SaveOfferRequest;
import com.day.cq.analytics.testandtarget.TargetMediator;
import com.day.cq.analytics.testandtarget.TestandtargetException;
import com.day.cq.analytics.testandtarget.impl.TestandtargetCampaignMediatorImpl;
import com.day.cq.analytics.testandtarget.impl.TestandtargetPrivateService;
import com.day.cq.analytics.testandtarget.util.OfferHelper;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.WCMMode;
import com.day.cq.wcm.webservicesupport.Configuration;
import java.util.Collections;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.Reference;
import org.apache.sling.api.resource.LoginException;
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.ResourceResolverFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TargetMediatorImpl
extends TestandtargetCampaignMediatorImpl
implements TargetMediator {
    private static final Logger LOG = LoggerFactory.getLogger(TargetMediator.class);
    @Reference
    private TestandtargetPrivateService testandtargetService;
    @Reference
    private ResourceResolverFactory resolverFactory;

    @Override
    public long syncOffer(Configuration configuration, String offerPath) throws TestandtargetException {
        ResourceResolver serviceResolver = null;
        if (StringUtils.isEmpty((String)offerPath)) {
            LOG.warn("Empty path provided, there's nothing to sync");
        }
        try {
            serviceResolver = this.resolverFactory.getServiceResourceResolver(Collections.singletonMap("sling.service.subservice", "target"));
            return this.syncOffer(offerPath, configuration, serviceResolver);
        }
        catch (LoginException e) {
            LOG.error(e.getMessage(), (Throwable)e);
            throw new TestandtargetException((Throwable)e);
        }
        catch (PersistenceException e) {
            LOG.error(e.getMessage(), (Throwable)e);
            throw new TestandtargetException((Throwable)e);
        }
        catch (TestandtargetException e) {
            LOG.error(e.getMessage(), (Throwable)e);
            throw e;
        }
    }

    @Override
    public long syncSegment(Configuration configuration, String segmentPath) {
        return 0;
    }

    private long syncOffer(String offerPath, Configuration configuration, ResourceResolver resourceResolver) throws TestandtargetException, PersistenceException {
        String offerName;
        SaveOfferRequest saveOfferRequest;
        long newOfferId;
        Resource offerResource = resourceResolver.getResource(offerPath);
        Page offerPage = (Page)offerResource.adaptTo(Page.class);
        long offerId = OfferHelper.getOfferId(offerPage);
        if (offerId != (newOfferId = this.testandtargetService.createHTMLOffer(configuration, saveOfferRequest = new SaveOfferRequest(offerId, offerName = OfferHelper.getOfferName(offerPage, WCMMode.PREVIEW), offerPage.getPath())))) {
            ModifiableValueMap mvm = (ModifiableValueMap)offerPage.getContentResource().adaptTo(ModifiableValueMap.class);
            mvm.put((Object)"cq:authorExternalId", (Object)newOfferId);
            String legacyExternalId = "cq:authorExternalId".replace("cq:", "");
            if (mvm.containsKey((Object)legacyExternalId)) {
                mvm.remove((Object)legacyExternalId);
            }
            resourceResolver.commit();
        }
        return newOfferId;
    }
}