MobileAssetUpdateHandler.java 7.8 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.commons.jcr.JcrUtil
 *  com.day.cq.contentsync.config.ConfigEntry
 *  com.day.cq.contentsync.handler.AbstractSlingResourceUpdateHandler
 *  com.day.cq.dam.api.Asset
 *  com.day.cq.dam.api.Rendition
 *  com.day.cq.search.PredicateGroup
 *  com.day.cq.search.Query
 *  com.day.cq.search.QueryBuilder
 *  com.day.cq.search.result.SearchResult
 *  com.day.text.Text
 *  javax.jcr.Node
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  org.apache.commons.lang.StringUtils
 *  org.apache.felix.scr.annotations.Component
 *  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.jcr.resource.JcrResourceResolverFactory
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.mobile.platform.impl.contentsync.handler;

import com.adobe.cq.mobile.platform.MobileResource;
import com.adobe.cq.mobile.platform.MobileResourceLocator;
import com.adobe.cq.mobile.platform.MobileResourceType;
import com.day.cq.commons.jcr.JcrUtil;
import com.day.cq.contentsync.config.ConfigEntry;
import com.day.cq.contentsync.handler.AbstractSlingResourceUpdateHandler;
import com.day.cq.dam.api.Asset;
import com.day.cq.dam.api.Rendition;
import com.day.cq.search.PredicateGroup;
import com.day.cq.search.Query;
import com.day.cq.search.QueryBuilder;
import com.day.cq.search.result.SearchResult;
import com.day.text.Text;
import java.util.HashMap;
import java.util.Iterator;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.Component;
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.jcr.resource.JcrResourceResolverFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(metatype=0, factory="com.day.cq.contentsync.handler.ContentUpdateHandler/app-assets")
public class MobileAssetUpdateHandler
extends AbstractSlingResourceUpdateHandler {
    private static final Logger log = LoggerFactory.getLogger(MobileAssetUpdateHandler.class);

    public boolean updateCacheEntry(ConfigEntry configEntry, Long lastUpdated, String configCacheRoot, Session admin, Session session) {
        boolean changed = false;
        try {
            MobileResourceLocator mobileResMgr;
            MobileResource appInstance;
            ResourceResolver resourceResolver = this.resolverFactory.getResourceResolver(admin);
            Resource resource = resourceResolver.getResource(this.getResolvedContentPath(configEntry));
            configCacheRoot = this.getConfigCacheRoot(configEntry, configCacheRoot);
            Resource appContentRoot = this.findAppRoot(resource);
            if (appContentRoot != null) {
                Iterator<Resource> assets = this.getAssets(appContentRoot, "nt:file", admin);
                while (assets.hasNext()) {
                    String cachePath;
                    Resource asset = assets.next();
                    Resource assetContent = asset.getChild("jcr:content");
                    if (this.isModified(assetContent, lastUpdated, cachePath = this.getCachePath(configCacheRoot, asset, appContentRoot), admin)) {
                        Node parent = JcrUtil.createPath((String)Text.getRelativeParent((String)cachePath, (int)1), (String)"sling:Folder", (Session)admin);
                        JcrUtil.copy((Node)((Node)asset.adaptTo(Node.class)), (Node)parent, (String)asset.getName());
                        changed = true;
                    }
                    admin.save();
                }
            }
            if ((appInstance = (mobileResMgr = (MobileResourceLocator)resourceResolver.adaptTo(MobileResourceLocator.class)).findClosestResourceByType(resource, MobileResourceType.INSTANCE.getType(), new String[0])) != null) {
                String appAssetsPath = (String)appInstance.getProperties().get("appAssetPath", String.class);
                Resource assetSourceRes = null;
                if (StringUtils.isNotEmpty((String)appAssetsPath)) {
                    assetSourceRes = resourceResolver.getResource(appAssetsPath);
                }
                if (assetSourceRes != null) {
                    Iterator<Resource> assets = this.getAssets(assetSourceRes, "dam:Asset", admin);
                    while (assets.hasNext()) {
                        Resource assetRes = assets.next();
                        String cachePath = this.getCachePath(configCacheRoot, assetRes, assetSourceRes);
                        Asset asset = (Asset)assetRes.adaptTo(Asset.class);
                        if (this.isModified(asset.getOriginal(), lastUpdated, cachePath, admin)) {
                            Node parent = JcrUtil.createPath((String)Text.getRelativeParent((String)cachePath, (int)1), (String)"sling:Folder", (Session)admin);
                            JcrUtil.copy((Node)((Node)asset.getOriginal().adaptTo(Node.class)), (Node)parent, (String)asset.getName());
                            changed = true;
                        }
                        admin.save();
                    }
                }
            }
        }
        catch (Exception e) {
            log.error("Update of cache entry failed: ", (Throwable)e);
        }
        return changed;
    }

    protected Iterator<Resource> getAssets(Resource assetFolder, String type, Session session) throws RepositoryException {
        ResourceResolver resolver = this.resolverFactory.getResourceResolver(session);
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("path", assetFolder.getPath());
        map.put("type", type);
        QueryBuilder queryBuilder = (QueryBuilder)resolver.adaptTo(QueryBuilder.class);
        Query query = queryBuilder.createQuery(PredicateGroup.create(map), session);
        query.setStart(0);
        query.setHitsPerPage(0);
        SearchResult searchResult = query.getResult();
        return searchResult.getResources();
    }

    private String getCachePath(String configCacheRoot, Resource asset, Resource rootResource) {
        String rootPath = rootResource.getPath();
        String assetPath = asset.getPath();
        if (assetPath.startsWith(rootPath)) {
            assetPath = assetPath.substring(rootPath.length());
        }
        return configCacheRoot + assetPath;
    }

    protected boolean isModified(Resource resource, Long lastUpdated, String cachePath, Session session) throws RepositoryException {
        return this.isModified(resource.getValueMap(), lastUpdated, cachePath, session);
    }

    protected boolean isModified(Rendition rendition, Long lastUpdated, String cachePath, Session session) throws RepositoryException {
        return this.isModified(rendition.getProperties(), lastUpdated, cachePath, session);
    }

    private boolean isModified(ValueMap properties, Long lastUpdated, String cachePath, Session session) throws RepositoryException {
        Long lastModified = (Long)properties.get("jcr:lastModified", Long.class);
        return !session.nodeExists(cachePath) || lastUpdated < lastModified;
    }

    private Resource findAppRoot(Resource baseRes) {
        Resource r;
        if (baseRes != null && baseRes.getChild("www") != null) {
            return baseRes;
        }
        Resource childRes = null;
        Iterable allChildren = baseRes.getChildren();
        Iterator i$ = allChildren.iterator();
        while (i$.hasNext() && ("jcr:content".equals((r = (Resource)i$.next()).getName()) || (childRes = this.findAppRoot(r)) == null || ResourceUtil.isNonExistingResource((Resource)childRes))) {
        }
        return childRes;
    }
}