SitecatalystPostProcessor.java 6.28 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.wcm.webservicesupport.Configuration
 *  com.day.cq.wcm.webservicesupport.ConfigurationManager
 *  com.day.cq.wcm.webservicesupport.ConfigurationManagerFactory
 *  javax.jcr.Node
 *  javax.jcr.RepositoryException
 *  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.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ResourceUtil
 *  org.apache.sling.api.resource.ValueMap
 *  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.sitecatalyst.impl;

import com.day.cq.analytics.sitecatalyst.impl.util.ReportConfigUtils;
import com.day.cq.wcm.webservicesupport.Configuration;
import com.day.cq.wcm.webservicesupport.ConfigurationManager;
import com.day.cq.wcm.webservicesupport.ConfigurationManagerFactory;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
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.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
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(value={SlingPostProcessor.class})
public class SitecatalystPostProcessor
implements SlingPostProcessor {
    protected final Logger logger;
    private final String NT_PAGE = "cq:Page";
    private final String PAGE_MARKER = "/jcr:content/";
    public static final String REPORT_NAME_SUFFIX = "ReportConfig";
    public static final String PAGE_REPORTS_RESULTS_ROOT_NODE_NAME = "cq:meta";
    public static final String PAGE_REPORTS_RESULTS_CURRENT = "last30Days";
    public static final String PAGE_REPORTS_RESULTS_LAST_60_DAYS = "last30DaysPrev";
    public static final String PAGE_REPORTS_RESULTS_LAST_90_DAYS = "last90Days";
    public static final String PAGE_REPORTS_RESULTS_LAST_90_DAYS_PREVIOUS = "last90DaysPrev";
    public static final String PAGE_REPORTS_RESULTS_THIS_YEAR = "thisYear";
    public static final String PAGE_REPORTS_RESULTS_LAST_YEAR = "lastYear";
    @Reference
    private ConfigurationManagerFactory configManagerFactory;

    public SitecatalystPostProcessor() {
        this.logger = LoggerFactory.getLogger((String)this.getClass().getName());
        this.NT_PAGE = "cq:Page";
        this.PAGE_MARKER = "/jcr:content/";
    }

    public void process(SlingHttpServletRequest request, List<Modification> changes) throws Exception {
        HashSet<String> fixPaths = new HashSet<String>();
        for (Modification mod : changes) {
            switch (mod.getType()) {
                case ORDER: 
                case MOVE: 
                case COPY: {
                    break;
                }
                case MODIFY: {
                    int pageEndPos = mod.getSource().indexOf("/jcr:content/");
                    if (pageEndPos == -1) break;
                    String pagePath = mod.getSource().substring(0, pageEndPos);
                    fixPaths.add(pagePath);
                    break;
                }
            }
        }
        ResourceResolver resolver = request.getResourceResolver();
        for (String pagePath : fixPaths) {
            String contentPath = pagePath + '/' + "jcr:content";
            this.logger.debug("Checking page {} for analytics configuration", (Object)pagePath);
            Resource contentResource = resolver.getResource(contentPath);
            if (contentResource == null) continue;
            this.fixStructure(contentResource);
        }
    }

    private void fixStructure(Resource resource) {
        if (ResourceUtil.isA((Resource)resource.getParent(), (String)"cq:Page")) {
            ValueMap props = (ValueMap)resource.adaptTo(ValueMap.class);
            String[] configurations = (String[])props.get("cq:cloudserviceconfigs", (Object)new String[0]);
            ConfigurationManager configManager = this.configManagerFactory.getConfigurationManager(resource.getResourceResolver());
            Configuration config = configManager.getConfiguration("sitecatalyst", configurations);
            if (config != null) {
                ReportConfigUtils.addAnalytics(resource, config.getResource(), configManager);
                this.logger.debug("Adding analytics configuration to page {}", (Object)resource.getPath());
            } else {
                this.removeAnalytics(resource);
                this.logger.debug("Removing analytics configuration from page {}", (Object)resource.getPath());
            }
        }
    }

    private void removeAnalytics(Resource resource) {
        Resource analyticsRes = resource.getChild("analytics");
        if (analyticsRes != null) {
            try {
                Node analyticsNode = (Node)analyticsRes.adaptTo(Node.class);
                Node pageContentNode = analyticsNode.getParent();
                ReportConfigUtils.recursivelyRemoveNodes(analyticsNode.getParent().getParent(), Arrays.asList("cq:meta"));
                analyticsNode.remove();
                pageContentNode.removeMixin("cq:metaMixin");
            }
            catch (RepositoryException e) {
                this.logger.error(e.getMessage(), (Throwable)e);
            }
        }
    }

    protected void bindConfigManagerFactory(ConfigurationManagerFactory configurationManagerFactory) {
        this.configManagerFactory = configurationManagerFactory;
    }

    protected void unbindConfigManagerFactory(ConfigurationManagerFactory configurationManagerFactory) {
        if (this.configManagerFactory == configurationManagerFactory) {
            this.configManagerFactory = null;
        }
    }

}