AmbitChangePostProcessor.java 8.25 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.commons.collections.CollectionUtils
 *  org.apache.commons.lang.StringUtils
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Properties
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.resource.LoginException
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ResourceResolverFactory
 *  org.apache.sling.api.resource.ValueMap
 *  org.apache.sling.event.jobs.Job
 *  org.apache.sling.event.jobs.JobManager
 *  org.apache.sling.event.jobs.consumer.JobConsumer
 *  org.apache.sling.event.jobs.consumer.JobConsumer$JobResult
 *  org.apache.sling.servlets.post.Modification
 *  org.apache.sling.servlets.post.ModificationType
 *  org.apache.sling.servlets.post.SlingPostProcessor
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.analytics.testandtarget.impl;

import com.day.cq.analytics.testandtarget.TargetMediator;
import com.day.cq.analytics.testandtarget.TestandtargetException;
import com.day.cq.analytics.testandtarget.impl.TargetHelperService;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
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.resource.LoginException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.event.jobs.Job;
import org.apache.sling.event.jobs.JobManager;
import org.apache.sling.event.jobs.consumer.JobConsumer;
import org.apache.sling.servlets.post.Modification;
import org.apache.sling.servlets.post.ModificationType;
import org.apache.sling.servlets.post.SlingPostProcessor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
@Service
@Properties(value={@Property(name="job.topics", value={"com/adobe/integrations/target/ambitchange"})})
public class AmbitChangePostProcessor
implements SlingPostProcessor,
JobConsumer {
    public static final String JOB_TOPIC = "com/adobe/integrations/target/ambitchange";
    private static final String JOB_PROPERTY_CAMPAIGNPATHS = "campaignpaths";
    private static final String PN_TARGETAMBITS = "cq:target-ambits";
    private final Logger LOG;
    @Reference
    private ResourceResolverFactory resolverFactory;
    @Reference
    private TargetHelperService targetHelperService;
    @Reference
    private JobManager jobMgr;
    @Reference
    private TargetMediator targetMediator;

    public AmbitChangePostProcessor() {
        this.LOG = LoggerFactory.getLogger(this.getClass());
    }

    public void process(SlingHttpServletRequest request, List<Modification> changes) throws Exception {
        String[] affectedCampaigns;
        if (this.handle(changes) && (affectedCampaigns = this.getAffectedCampaigns(request)).length > 0) {
            HashMap<String, String[]> payload = new HashMap<String, String[]>();
            payload.put("campaignpaths", affectedCampaigns);
            this.jobMgr.addJob("com/adobe/integrations/target/ambitchange", payload);
        }
    }

    public JobConsumer.JobResult process(Job job) {
        String[] affectedCampaigns;
        for (String campaignPath : affectedCampaigns = (String[])job.getProperty("campaignpaths", String[].class)) {
            try {
                this.targetMediator.syncAuthorCampaign(campaignPath);
                continue;
            }
            catch (TestandtargetException e) {
                this.LOG.error(String.format("Unable to sync %s to Target", campaignPath), (Throwable)e);
            }
        }
        return JobConsumer.JobResult.OK;
    }

    private boolean handle(List<Modification> changes) {
        for (Modification modification : changes) {
            if (!ModificationType.MODIFY.equals((Object)modification.getType()) || !StringUtils.endsWith((String)modification.getSource(), (String)"cq:target-ambits")) continue;
            return true;
        }
        return false;
    }

    private String[] getAffectedCampaigns(SlingHttpServletRequest request) {
        String[] persisted = this.getPersistedAmbits(request.getResource().getPath());
        String[] submitted = request.getParameterValues("./cq:target-ambits");
        HashSet<String> campaigns = new HashSet<String>();
        try {
            ArrayList<String> oldambits = new ArrayList<String>(Arrays.asList(persisted));
            ArrayList<String> newambits = new ArrayList<String>(Arrays.asList(submitted));
            Collection intersection = CollectionUtils.intersection(oldambits, newambits);
            newambits.removeAll(intersection);
            if (!newambits.isEmpty()) {
                ResourceResolver resolver = request.getResourceResolver();
                for (String ambitPath : newambits) {
                    Resource ambitResource = resolver.getResource(ambitPath);
                    Set<Resource> targetActivities = this.targetHelperService.getTargetCampaigns(ambitResource);
                    for (Resource r : targetActivities) {
                        campaigns.add(r.getPath());
                    }
                }
            }
        }
        catch (UnsupportedOperationException e) {
            this.LOG.error("Unable to determine affected campaigns due to unexpected error.", (Throwable)e);
        }
        return campaigns.toArray(new String[0]);
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    private String[] getPersistedAmbits(String path) {
        ResourceResolver serviceResolver = null;
        try {
            serviceResolver = this.resolverFactory.getServiceResourceResolver(Collections.singletonMap("sling.service.subservice", "target"));
            Resource persistedResource = serviceResolver.getResource(path);
            if (persistedResource != null) {
                String[] arrstring = (String[])persistedResource.getValueMap().get("cq:target-ambits", (Object)new String[0]);
                return arrstring;
            }
        }
        catch (LoginException e) {
            this.LOG.error("Unable to get persisted ambit values.", (Throwable)e);
        }
        finally {
            if (serviceResolver != null) {
                serviceResolver.close();
            }
        }
        return new String[0];
    }

    protected void bindResolverFactory(ResourceResolverFactory resourceResolverFactory) {
        this.resolverFactory = resourceResolverFactory;
    }

    protected void unbindResolverFactory(ResourceResolverFactory resourceResolverFactory) {
        if (this.resolverFactory == resourceResolverFactory) {
            this.resolverFactory = null;
        }
    }

    protected void bindTargetHelperService(TargetHelperService targetHelperService) {
        this.targetHelperService = targetHelperService;
    }

    protected void unbindTargetHelperService(TargetHelperService targetHelperService) {
        if (this.targetHelperService == targetHelperService) {
            this.targetHelperService = null;
        }
    }

    protected void bindJobMgr(JobManager jobManager) {
        this.jobMgr = jobManager;
    }

    protected void unbindJobMgr(JobManager jobManager) {
        if (this.jobMgr == jobManager) {
            this.jobMgr = null;
        }
    }

    protected void bindTargetMediator(TargetMediator targetMediator) {
        this.targetMediator = targetMediator;
    }

    protected void unbindTargetMediator(TargetMediator targetMediator) {
        if (this.targetMediator == targetMediator) {
            this.targetMediator = null;
        }
    }
}