AnalyticsContentUpdater.java 6.89 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.search.Predicate
 *  com.day.cq.search.PredicateGroup
 *  com.day.cq.search.Query
 *  com.day.cq.search.QueryBuilder
 *  com.day.cq.search.result.Hit
 *  com.day.cq.search.result.SearchResult
 *  com.day.cq.wcm.webservicesupport.Configuration
 *  com.day.cq.wcm.webservicesupport.ConfigurationManager
 *  com.day.cq.wcm.webservicesupport.ConfigurationManagerFactory
 *  javax.jcr.Session
 *  org.apache.felix.scr.annotations.Activate
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  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.osgi.service.component.ComponentContext
 *  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.search.Predicate;
import com.day.cq.search.PredicateGroup;
import com.day.cq.search.Query;
import com.day.cq.search.QueryBuilder;
import com.day.cq.search.result.Hit;
import com.day.cq.search.result.SearchResult;
import com.day.cq.wcm.webservicesupport.Configuration;
import com.day.cq.wcm.webservicesupport.ConfigurationManager;
import com.day.cq.wcm.webservicesupport.ConfigurationManagerFactory;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.jcr.Session;
import org.apache.felix.scr.annotations.Activate;
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.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.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(name="Analytics Content Updater Component", label="Analytics Content Updater Component", description="Automatically detects and updates the content structure that relates to the analytics integration", immediate=1)
@Service(value={AnalyticsContentUpdater.class})
public class AnalyticsContentUpdater {
    private static final Logger LOG = LoggerFactory.getLogger(AnalyticsContentUpdater.class);
    private static final String ANALYTICS_CONTENT_UPDATER_SUBSERVICE = "content-updater";
    private boolean performedUpgrade = false;
    @Reference
    private QueryBuilder queryBuilder;
    @Reference
    private ResourceResolverFactory resourceResolverFactory;
    @Reference
    private ConfigurationManagerFactory configManagerFactory;

    @Activate
    protected void activate(ComponentContext ctx) {
        this.doUpgrade();
    }

    public void doUpgrade() {
        if (this.performedUpgrade) {
            return;
        }
        PredicateGroup rootPred = new PredicateGroup("attachedaaconfigs");
        Predicate pathPred = new Predicate("path");
        pathPred.set("path", "/content");
        rootPred.add(pathPred);
        Predicate typePred = new Predicate("nodeType", "property");
        typePred.set("property", "sling:resourceType");
        typePred.set("value", "cq/analytics/components/sitecatalyst");
        rootPred.add(typePred);
        Predicate namePred = new Predicate("nodeName", "nodename");
        namePred.set("nodename", "analytics");
        rootPred.add(namePred);
        try {
            ResourceResolver resolver = this.resourceResolverFactory.getServiceResourceResolver(Collections.singletonMap("sling.service.subservice", "content-updater"));
            Session session = (Session)resolver.adaptTo(Session.class);
            LOG.info("Searching for all content pages having an attached Analytics cloud configuration...");
            Query query = this.queryBuilder.createQuery(rootPred, session);
            SearchResult result = query.getResult();
            List hits = result.getHits();
            for (Hit searchHit : hits) {
                String analyticsNodePath = searchHit.getPath();
                LOG.info("Checking analytics report configurations for path " + analyticsNodePath);
                Resource reportConfigRes = resolver.getResource(analyticsNodePath + "/" + "reportConfigs");
                Resource analyticsResource = resolver.getResource(analyticsNodePath);
                if (reportConfigRes == null) {
                    LOG.info("Old analytics cloud configuration detected for path " + analyticsNodePath + " updating configuration... ");
                    Resource pageContentResource = analyticsResource.getParent();
                    ValueMap props = (ValueMap)pageContentResource.adaptTo(ValueMap.class);
                    String[] configurations = (String[])props.get("cq:cloudserviceconfigs", (Object)new String[0]);
                    ConfigurationManager configManager = this.configManagerFactory.getConfigurationManager(resolver);
                    Configuration scCloudConfig = configManager.getConfiguration("sitecatalyst", configurations);
                    ReportConfigUtils.addAnalytics(pageContentResource, scCloudConfig.getResource(), configManager);
                    resolver.commit();
                    LOG.info("Analytics cloud configuration at path " + analyticsNodePath + " successfully updated!");
                    continue;
                }
                LOG.info("Latest analytics cloud configuration detected for path " + analyticsNodePath + ", not performing any upgrades!");
            }
            this.performedUpgrade = true;
        }
        catch (Exception e) {
            LOG.error("Can't update Analytics content structure!", (Throwable)e);
        }
    }

    protected void bindQueryBuilder(QueryBuilder queryBuilder) {
        this.queryBuilder = queryBuilder;
    }

    protected void unbindQueryBuilder(QueryBuilder queryBuilder) {
        if (this.queryBuilder == queryBuilder) {
            this.queryBuilder = null;
        }
    }

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

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

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

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