AbstractMobileClientCloudServiceHandler.java 6.87 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.commons.jcr.JcrUtil
 *  com.day.cq.i18n.I18n
 *  javax.jcr.Node
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  org.apache.commons.lang3.StringUtils
 *  org.apache.sling.api.resource.ModifiableValueMap
 *  org.apache.sling.api.resource.PersistenceException
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ResourceUtil
 *  org.apache.sling.api.resource.ValueMap
 */
package com.adobe.cq.mobile.mobilecloudservices.impl;

import com.adobe.cq.mobile.mobilecloudservices.impl.MobileClientCloudServiceHandler;
import com.adobe.cq.mobile.platform.impl.MobileAppException;
import com.adobe.cq.mobile.platform.impl.utils.MobileCloudServiceConfigsUtil;
import com.day.cq.commons.jcr.JcrUtil;
import com.day.cq.i18n.I18n;
import java.util.Map;
import java.util.Set;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.resource.ModifiableValueMap;
import org.apache.sling.api.resource.PersistenceException;
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;

public abstract class AbstractMobileClientCloudServiceHandler
implements MobileClientCloudServiceHandler {
    protected static final String PROPERTY_CONFIG_NAME = "configName";

    @Override
    public abstract Resource createConfiguration(Map<String, Object> var1, I18n var2, ResourceResolver var3) throws MobileAppException;

    @Override
    public abstract void checkProperties(Map<String, Object> var1, I18n var2) throws MobileAppException;

    @Override
    public abstract String getServicePath();

    @Override
    public void updateConfiguration(Resource resource, Map<String, Object> properties) {
    }

    @Override
    public void saveCloudConfigProperty(Resource resource, String propertyName, String configPath) throws MobileAppException {
        Resource content = resource.getChild("jcr:content");
        if (content == null || ResourceUtil.isNonExistingResource((Resource)content)) {
            throw new MobileAppException("Invalid Resource specified to write Cloud Configuration to: " + resource.getPath());
        }
        if (StringUtils.isNotBlank((CharSequence)propertyName)) {
            ModifiableValueMap modifiableValueMap = (ModifiableValueMap)content.adaptTo(ModifiableValueMap.class);
            modifiableValueMap.put((Object)propertyName, (Object)configPath);
        }
        MobileCloudServiceConfigsUtil.setCloudConfigProperty(resource, configPath);
        try {
            resource.getResourceResolver().commit();
        }
        catch (PersistenceException perEx) {
            throw new MobileAppException("Could not save Cloud Configuration property: " + configPath, (Throwable)perEx);
        }
    }

    @Override
    public void unlinkCloudConfigProperty(Resource resource, String propertyName) throws MobileAppException {
        MobileCloudServiceConfigsUtil.unlinkCloudConfigProperty(resource, propertyName, this.getServicePath());
        try {
            resource.getResourceResolver().commit();
        }
        catch (PersistenceException perEx) {
            throw new MobileAppException("Could not unlink Cloud Configuration from: " + resource.getPath(), (Throwable)perEx);
        }
    }

    protected String getValidPageName(String str) {
        if (str == null) {
            return null;
        }
        return JcrUtil.createValidName((String)str, (String[])JcrUtil.HYPHEN_LABEL_CHAR_MAPPING);
    }

    protected String getStringProperty(Map<String, Object> properties, String propertyName) {
        String value;
        block4 : {
            if (!properties.containsKey(propertyName)) {
                return null;
            }
            value = null;
            try {
                value = (String)properties.get(propertyName);
                if (StringUtils.isBlank((CharSequence)value)) {
                    value = null;
                }
            }
            catch (ClassCastException ex) {
                String[] propertyArray = (String[])properties.get(propertyName);
                if (propertyArray.length != 1) break block4;
                value = propertyArray[0];
            }
        }
        return value;
    }

    protected void putProperty(ModifiableValueMap mvp, String field, String value) {
        if (StringUtils.isNotBlank((CharSequence)value) && StringUtils.isNotBlank((CharSequence)field)) {
            mvp.put((Object)field, (Object)value);
        }
    }

    protected String getName(Map<String, Object> properties, String defaultName) {
        String pageName = null;
        if (properties.keySet().contains("configName") && StringUtils.isNotBlank((CharSequence)this.getStringProperty(properties, "configName"))) {
            String pageTitle = this.getStringProperty(properties, "configName");
            pageName = this.getValidPageName(pageTitle);
        } else {
            pageName = this.getValidPageName(defaultName);
            if (StringUtils.isBlank((CharSequence)pageName)) {
                pageName = defaultName;
            }
        }
        return pageName;
    }

    protected void saveConfigurationPublicNode(Resource parent) throws MobileAppException {
        if (parent == null || ResourceUtil.isNonExistingResource((Resource)parent)) {
            return;
        }
        ResourceResolver resolver = parent.getResourceResolver();
        Session session = (Session)resolver.adaptTo(Session.class);
        String publicPath = "";
        String title = null;
        Resource content = parent.getChild("jcr:content");
        if (content != null) {
            title = (String)content.getValueMap().get("jcr:title", String.class);
        }
        if (StringUtils.isBlank((CharSequence)title)) {
            title = parent.getName();
        }
        String targetPath = content == null ? parent.getPath() : content.getPath();
        try {
            publicPath = targetPath + "/public";
            Node publicNode = JcrUtil.createPath((String)publicPath, (String)"nt:unstructured", (Session)session);
            ModifiableValueMap mvp = (ModifiableValueMap)resolver.getResource(publicNode.getPath()).adaptTo(ModifiableValueMap.class);
            mvp.put((Object)"jcr:title", (Object)title);
        }
        catch (RepositoryException repEx) {
            throw new MobileAppException("Could not create public note for " + parent.getPath() + ". " + repEx.getMessage());
        }
        try {
            if (session.hasPendingChanges()) {
                session.save();
            }
        }
        catch (RepositoryException repEx) {
            throw new MobileAppException("Could not save the public node properly: " + publicPath);
        }
    }
}