AccountOptionsUpdater.java 5.03 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.polling.importer.ImportException
 *  com.day.cq.polling.importer.Importer
 *  com.day.cq.wcm.webservicesupport.Configuration
 *  com.day.cq.wcm.webservicesupport.ConfigurationManager
 *  com.day.cq.wcm.webservicesupport.ConfigurationManagerFactory
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Property
 *  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.commons.osgi.PropertiesUtil
 *  org.osgi.service.component.ComponentContext
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.analytics.testandtarget.impl;

import com.day.cq.analytics.testandtarget.TestandtargetException;
import com.day.cq.analytics.testandtarget.impl.TestandtargetPrivateService;
import com.day.cq.polling.importer.ImportException;
import com.day.cq.polling.importer.Importer;
import com.day.cq.wcm.webservicesupport.Configuration;
import com.day.cq.wcm.webservicesupport.ConfigurationManager;
import com.day.cq.wcm.webservicesupport.ConfigurationManagerFactory;
import java.util.Dictionary;
import org.apache.felix.scr.annotations.Component;
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.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(metatype=1, label="Adobe Target account options updater", description="Updates the Adobe Target cloud configuration options with the account options from Taget")
@Service
@Property(name="importer.scheme", value={"adobe-target-account-options"}, propertyPrivate=1)
public class AccountOptionsUpdater
implements Importer {
    private static final boolean DEFAULT_ENABLED = true;
    private static final Logger LOG = LoggerFactory.getLogger(AccountOptionsUpdater.class);
    @Property(label="Enabled", description="Controls whether this importer is enabled or not. Defaults to true on author instances and false on publish instances.", boolValue={1})
    private static final String PROPERTY_ENABLED = "cq.analytics.testandtarget.accountoptionsupdater.enabled";
    @Reference
    private TestandtargetPrivateService testandtargetService;
    @Reference
    private ConfigurationManagerFactory configurationManagerFactory;
    private boolean enabled;

    protected void activate(ComponentContext ctx) {
        this.enabled = PropertiesUtil.toBoolean(ctx.getProperties().get("cq.analytics.testandtarget.accountoptionsupdater.enabled"), (boolean)true);
    }

    public void importData(String scheme, String dataSource, Resource target) throws ImportException {
        if (!this.enabled) {
            LOG.debug("Component is not enabled, skipping import.");
            return;
        }
        ResourceResolver resolver = target.getResourceResolver();
        LOG.info("Updating account options for Adobe Target cloud config at {}", (Object)dataSource);
        Resource cloudConfig = resolver.getResource(dataSource);
        if (cloudConfig == null) {
            LOG.error("No cloud service configuration found at {}", (Object)dataSource);
            return;
        }
        ConfigurationManager configurationManager = this.configurationManagerFactory.getConfigurationManager(resolver);
        Configuration config = configurationManager.getConfiguration(cloudConfig.getPath());
        if (config == null) {
            LOG.warn("Unable to update account at {} since the resource at {} can not be adapted to a '{}'", new Object[]{target.getPath(), cloudConfig.getPath(), Configuration.class.getName()});
            return;
        }
        try {
            this.testandtargetService.updateAccountOptions(config);
            LOG.info("Updated account options at {}", (Object)config.getPath());
        }
        catch (TestandtargetException e) {
            LOG.error(e.getMessage(), (Throwable)e);
        }
    }

    protected void bindTestandtargetService(TestandtargetPrivateService testandtargetPrivateService) {
        this.testandtargetService = testandtargetPrivateService;
    }

    protected void unbindTestandtargetService(TestandtargetPrivateService testandtargetPrivateService) {
        if (this.testandtargetService == testandtargetPrivateService) {
            this.testandtargetService = null;
        }
    }

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

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