MobileResourceImpl.java 10.7 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.commons.LabeledResource
 *  com.day.cq.commons.inherit.HierarchyNodeInheritanceValueMap
 *  com.day.cq.commons.inherit.InheritanceValueMap
 *  org.apache.commons.lang.StringUtils
 *  org.apache.sling.api.adapter.SlingAdaptable
 *  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
 *  org.apache.sling.api.wrappers.ValueMapDecorator
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.mobile.platform.impl;

import com.adobe.cq.mobile.platform.MobileResource;
import com.adobe.cq.mobile.platform.MobileResourceLocator;
import com.adobe.cq.mobile.platform.MobileResourceType;
import com.adobe.cq.mobile.platform.impl.MobileAppProvider;
import com.adobe.cq.mobile.platform.impl.MobilePlatformInternalAdapterFactory;
import com.day.cq.commons.LabeledResource;
import com.day.cq.commons.inherit.HierarchyNodeInheritanceValueMap;
import com.day.cq.commons.inherit.InheritanceValueMap;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.api.adapter.SlingAdaptable;
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;
import org.apache.sling.api.wrappers.ValueMapDecorator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MobileResourceImpl
extends SlingAdaptable
implements MobileResource {
    private static final Logger log = LoggerFactory.getLogger(MobileResourceImpl.class);
    private final Resource mobileRes;
    private final LabeledResource labeledRes;
    private final ResourceResolver resolver;
    private final Map<String, String> instanceTypeMap;
    private final MobilePlatformInternalAdapterFactory mobilePlatformInternalAdapterFactory;
    private ValueMap properties;

    public MobileResourceImpl(Resource res, Map<String, String> instanceTypeMap, MobilePlatformInternalAdapterFactory mobilePlatformInternalAdapterFactory) {
        if (res == null) {
            throw new IllegalArgumentException("Cannot instantiate mobile resource. Resource cannot be null.");
        }
        this.mobileRes = res;
        this.labeledRes = (LabeledResource)res.adaptTo(LabeledResource.class);
        this.resolver = res.getResourceResolver();
        this.instanceTypeMap = instanceTypeMap;
        this.mobilePlatformInternalAdapterFactory = mobilePlatformInternalAdapterFactory;
    }

    public String getPath() {
        return this.labeledRes.getPath();
    }

    public String getName() {
        return this.labeledRes.getName();
    }

    public String getTitle() {
        return this.labeledRes.getTitle();
    }

    public String getDescription() {
        return this.labeledRes.getDescription();
    }

    @Override
    public ValueMap getProperties() {
        if (this.properties == null) {
            Resource resContent;
            MobileAppProvider appProvider = this.mobilePlatformInternalAdapterFactory.getAdapter((Object)this.mobileRes, MobileAppProvider.class);
            this.properties = appProvider != null ? appProvider.getProperties() : ((resContent = this.getContentResource()) != null && !ResourceUtil.isNonExistingResource((Resource)resContent) ? resContent.getValueMap() : new ValueMapDecorator(new HashMap()));
        }
        return this.properties;
    }

    @Override
    public /* varargs */ boolean isA(String ... types) {
        Set<String> allTypes = this.getAppTypes();
        Resource contentRes = this.getContentResource();
        if (types != null) {
            String[] arr$ = types;
            int len$ = arr$.length;
            for (int i$ = 0; i$ < len$; ++i$) {
                String type;
                String resourceType = type = arr$[i$];
                if (this.instanceTypeMap.containsKey(type)) {
                    resourceType = this.instanceTypeMap.get(type);
                }
                if (contentRes != null && contentRes.isResourceType(resourceType)) {
                    return true;
                }
                for (String typeId : allTypes) {
                    if (!type.equals(typeId)) continue;
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public MobileResourceType getMobileResourceType() {
        for (MobileResourceType type : MobileResourceType.values()) {
            if (!this.isA(type.getType())) continue;
            return type;
        }
        return null;
    }

    @Override
    public Set<String> getAppTypes() {
        Resource contentRes = this.getContentResource();
        if (contentRes != null && !ResourceUtil.isNonExistingResource((Resource)contentRes)) {
            String[] types = (String[])contentRes.getValueMap().get("pge-type", String[].class);
            if (types == null) {
                types = new String[]{};
            }
            LinkedHashSet<String> typeIdSet = new LinkedHashSet<String>(types.length);
            for (String type : types) {
                typeIdSet.add(type);
            }
            String contentType = contentRes.getResourceType();
            if (StringUtils.isNotBlank((String)contentType) && contentRes.isResourceType("mobileapps/core/components/resource")) {
                typeIdSet.add(contentRes.getResourceType());
            }
            return typeIdSet;
        }
        return new HashSet<String>(0);
    }

    @Override
    public boolean addAppType(String type) throws IllegalArgumentException {
        MobileResourceLocator locator = (MobileResourceLocator)this.resolver.adaptTo(MobileResourceLocator.class);
        MobileResource group = locator.findClosestResourceByType(this.mobileRes, MobileResourceType.GROUP.getType(), new String[0]);
        if (type.equals(MobileResourceType.GROUP.getType())) {
            if (group != null) {
                log.error("Unable to add an app type to {}.  An app-group cannot be a descendant of another app-group.", (Object)this.mobileRes.getPath());
                throw new IllegalArgumentException("An app-group cannot be a descendant of an app-group.");
            }
            return this.addTypeProperty(type);
        }
        if (type.equals(MobileResourceType.INSTANCE.getType()) || type.equals(MobileResourceType.CONTENT.getType())) {
            if (group != null) {
                MobileResource instance = locator.findClosestResourceByType(this.mobileRes, MobileResourceType.INSTANCE.getType(), new String[0]);
                MobileResource content = locator.findClosestResourceByType(this.mobileRes, MobileResourceType.CONTENT.getType(), new String[0]);
                if (instance != null) {
                    log.error("Unable to add an app type to {}.  Resource cannot be a descendant of an app-instance.", (Object)this.mobileRes.getPath());
                    throw new IllegalArgumentException("A resource cannot be a descendant of an app-instance.");
                }
                if (content != null) {
                    log.error("Unable to add an app type to {}.  Resource cannot be a descendant of an app-content.", (Object)this.mobileRes.getPath());
                    throw new IllegalArgumentException("Resource cannot be a descendant of an app-content.");
                }
                return this.addTypeProperty(type);
            }
            log.error("Unable to add an app type to {}.  Resource must be a descendant of an app-group.", (Object)this.mobileRes.getPath());
            throw new IllegalArgumentException("Resource must be a descendant of an app-group.");
        }
        return false;
    }

    /*
     * Enabled aggressive block sorting
     * Enabled unnecessary exception pruning
     * Enabled aggressive exception aggregation
     */
    private boolean addTypeProperty(String type) {
        try {
            Resource contentRes = this.getContentResource();
            if (contentRes == null || ResourceUtil.isNonExistingResource((Resource)contentRes)) {
                log.error("Unable to find content resource for {},", (Object)this.mobileRes.getPath());
                return false;
            }
            ModifiableValueMap properties = (ModifiableValueMap)contentRes.adaptTo(ModifiableValueMap.class);
            String[] allTypes = (String[])properties.get("pge-type", String[].class);
            if (allTypes == null) {
                properties.put((Object)"pge-type", (Object)new String[]{type});
                this.resolver.commit();
                return true;
            }
            ArrayList<String> typeList = new ArrayList<String>(allTypes.length);
            typeList.addAll(Arrays.asList(allTypes));
            if (type != null && !typeList.contains(type)) {
                typeList.add(type);
                properties.put((Object)"pge-type", (Object)typeList.toArray(new String[typeList.size()]));
                this.resolver.commit();
                return true;
            }
            log.debug("App type, {}, not added since it already exists.", (Object)type);
            return false;
        }
        catch (PersistenceException e) {
            log.error("Failed to add type to mobile resource.", (Throwable)e);
            return false;
        }
    }

    public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
        if (type == MobileAppProvider.class) {
            return (AdapterType)this.mobilePlatformInternalAdapterFactory.getAdapter((Object)this.mobileRes, MobileAppProvider.class);
        }
        if (type == Resource.class) {
            return (AdapterType)this.mobileRes;
        }
        if (type == ValueMap.class) {
            return (AdapterType)this.properties;
        }
        if (type == InheritanceValueMap.class) {
            return (AdapterType)new HierarchyNodeInheritanceValueMap(this.getContentResource());
        }
        Object ret = super.adaptTo(type);
        if (ret == null) {
            ret = this.mobileRes.adaptTo(type);
        }
        return (AdapterType)ret;
    }

    private Resource getContentResource() {
        if ("jcr:content".equals(this.mobileRes.getName()) || this.mobileRes.getChild("jcr:content") == null) {
            return this.mobileRes;
        }
        return this.mobileRes.getChild("jcr:content");
    }
}