MACTenantConfigurationResolverImpl.java 7 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.security.user.UserManagementService
 *  com.day.cq.dam.api.Asset
 *  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.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ValueMap
 */
package com.adobe.cq.dam.mac.sync.helper.impl;

import com.adobe.cq.dam.mac.sync.helper.impl.MACTenantConfiguration;
import com.adobe.cq.dam.mac.sync.helper.impl.MACTenantConfigurationImpl;
import com.adobe.cq.dam.mac.sync.helper.impl.MACTenantConfigurationResolver;
import com.adobe.granite.security.user.UserManagementService;
import com.day.cq.dam.api.Asset;
import com.day.cq.wcm.webservicesupport.Configuration;
import com.day.cq.wcm.webservicesupport.ConfigurationManager;
import com.day.cq.wcm.webservicesupport.ConfigurationManagerFactory;
import java.util.Iterator;
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.ValueMap;

@Component(metatype=0, label="%com.adobe.cq.dam.mac.sync.mactenantconfigresolver.label", description="%com.adobe.cq.dam.mac.sync.mactenantconfigresolver.description")
@Service
public class MACTenantConfigurationResolverImpl
implements MACTenantConfigurationResolver {
    @Reference
    private ConfigurationManagerFactory configurationManagerFactory;
    @Reference
    private UserManagementService userManagementService;

    @Override
    public MACTenantConfiguration getConfiguration(ResourceResolver resolver, String path) {
        Configuration configuration;
        ConfigurationManager configurationManager = this.configurationManagerFactory.getConfigurationManager(resolver);
        MACTenantConfigurationImpl mtc = null;
        if (path != null && (configuration = configurationManager.getConfiguration(path)) != null) {
            mtc = new MACTenantConfigurationImpl(configuration, this.userManagementService);
        }
        return mtc;
    }

    @Override
    public MACTenantConfiguration getConfigurationForResource(Resource resource) {
        return this.getTenantConfigurationForResource(resource, "macConfig");
    }

    @Override
    public MACTenantConfiguration getMPConfigurationForResource(Resource resource) {
        return this.getTenantConfigurationForResource(resource, "mpConfig");
    }

    private MACTenantConfiguration getTenantConfigurationForResource(Resource resource, String property) {
        MACTenantConfiguration mtc = null;
        if (resource != null) {
            while (mtc == null && !"/".equals(resource.getPath())) {
                ValueMap resourceProperties = (ValueMap)resource.adaptTo(ValueMap.class);
                String cloudConfigPath = (String)resourceProperties.get(property, String.class);
                mtc = this.getConfiguration(resource.getResourceResolver(), cloudConfigPath);
                resource = resource.getParent();
            }
        }
        return mtc;
    }

    @Override
    public MACTenantConfiguration getConfigurationForPath(ResourceResolver resolver, String path) {
        return this.getTenantConfigurationForPath(resolver, path, "macConfig");
    }

    @Override
    public MACTenantConfiguration getMPConfigurationForPath(ResourceResolver resolver, String path) {
        return this.getTenantConfigurationForPath(resolver, path, "mpConfig");
    }

    private MACTenantConfiguration getTenantConfigurationForPath(ResourceResolver resolver, String path, String property) {
        MACTenantConfiguration mtc = null;
        if (path != null) {
            while (!"/".equals(path) && path.length() > 0 && mtc == null) {
                Resource resource = resolver.getResource(path);
                if (resource != null) {
                    String configPath;
                    ValueMap properties;
                    if (null == resource.adaptTo(Asset.class)) {
                        properties = (ValueMap)resource.adaptTo(ValueMap.class);
                        configPath = (String)properties.get(property, String.class);
                        mtc = this.getConfiguration(resolver, configPath);
                    } else {
                        properties = (ValueMap)resource.getChild("jcr:content").adaptTo(ValueMap.class);
                        configPath = (String)properties.get(property, String.class);
                        mtc = this.getConfiguration(resolver, configPath);
                    }
                }
                path = path.substring(0, path.lastIndexOf("/"));
            }
        }
        return mtc;
    }

    @Override
    public MACTenantConfiguration getDefaultConfig(ResourceResolver resolver) {
        return this.getDefaultTenantConfig(resolver, "/etc/cloudservices/marketingcloud");
    }

    @Override
    public MACTenantConfiguration getDefaultMPConfig(ResourceResolver resolver) {
        return this.getDefaultTenantConfig(resolver, "/etc/cloudservices/mediaportal");
    }

    private MACTenantConfiguration getDefaultTenantConfig(ResourceResolver resolver, String configRootPath) {
        Resource configRoot = resolver.getResource(configRootPath);
        if (configRoot != null) {
            ConfigurationManager configurationManager = this.configurationManagerFactory.getConfigurationManager(resolver);
            Iterator iterator = configRoot.listChildren();
            while (iterator.hasNext()) {
                MACTenantConfigurationImpl mtc;
                Resource child = (Resource)iterator.next();
                Configuration configuration = configurationManager.getConfiguration(child.getPath());
                if (configuration == null || !(mtc = new MACTenantConfigurationImpl(configuration, this.userManagementService)).isSyncEnabled()) continue;
                return mtc;
            }
        }
        return null;
    }

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

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

    protected void bindUserManagementService(UserManagementService userManagementService) {
        this.userManagementService = userManagementService;
    }

    protected void unbindUserManagementService(UserManagementService userManagementService) {
        if (this.userManagementService == userManagementService) {
            this.userManagementService = null;
        }
    }
}