AbstractMobileAppProvider.java 3.59 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.contentsync.config.Config
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ValueMap
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.mobile.platform.impl;

import com.adobe.cq.mobile.platform.impl.MobileAppProvider;
import com.adobe.cq.mobile.platform.impl.MobileValueMapDecorator;
import com.adobe.cq.mobile.platform.impl.utils.MobileUtil;
import com.day.cq.contentsync.config.Config;
import java.util.HashMap;
import java.util.Map;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class AbstractMobileAppProvider
implements MobileAppProvider {
    private static final Logger log = LoggerFactory.getLogger(AbstractMobileAppProvider.class);
    protected static final String PN_CS_CONFIG_PATH = "phonegap-contentSyncConfigPath";
    protected Resource resource;
    protected ResourceResolver resolver;

    protected AbstractMobileAppProvider(Resource resource) {
        if (resource == null) {
            throw new IllegalArgumentException("The mobile resource cannot be null.");
        }
        this.resource = resource;
        this.resolver = resource.getResourceResolver();
    }

    @Override
    public ValueMap getProperties() {
        Resource contentRes = this.getContentResource();
        Resource resolvedResource = contentRes != null ? contentRes : this.resource;
        return new MobileValueMapDecorator(resolvedResource);
    }

    @Override
    public Map<String, Config> findResourceConfigs() {
        HashMap<String, Config> envList = new HashMap<String, Config>();
        ValueMap properties = this.getContentResource().getValueMap();
        String exportTemplate = (String)properties.get("phonegap-exportTemplate", String.class);
        if (exportTemplate == null) {
            exportTemplate = (String)properties.get("phonegap-contentSyncConfigPath", String.class);
        }
        this.addConfigToEnv("STAGE", exportTemplate, envList);
        String configPath = (String)properties.get("phonegap-build-exportTemplate", String.class);
        this.addConfigToEnv("BUILD-STAGE", configPath, envList);
        configPath = (String)properties.get("phonegap-build-exportTemplate-dev", String.class);
        this.addConfigToEnv("BUILD-DEV", configPath, envList);
        return envList;
    }

    private void addConfigToEnv(String envKey, String configPath, HashMap<String, Config> envList) {
        if (configPath != null) {
            Resource configResource = this.resolver.getResource(configPath = MobileUtil.normalizePath(configPath, this.getContentResource()));
            if (configResource != null) {
                envList.put(envKey, (Config)configResource.adaptTo(Config.class));
            } else {
                log.error("Non existing config {} for key {}", (Object[])new String[]{configPath, envKey});
            }
        }
    }

    protected Config getDefaultConfig() {
        Map<String, Config> configMap = this.findResourceConfigs();
        if (configMap != null && configMap.containsKey("STAGE")) {
            configMap.get("STAGE");
        }
        return null;
    }

    public Resource getContentResource() {
        if (!"jcr:content".equals(this.resource.getName()) && this.resource.getChild("jcr:content") != null) {
            return this.resource.getChild("jcr:content");
        }
        return this.resource;
    }
}