AssetRenditionsUpdateHandler.java 6.09 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.AbstractDefaultContentUpdateHandler
 *  com.day.cq.contentsync.handler.ContentUpdateHandler
 *  com.day.cq.dam.api.Asset
 *  com.day.cq.dam.api.Rendition
 *  com.day.text.Text
 *  javax.jcr.Node
 *  javax.jcr.NodeIterator
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  javax.jcr.Workspace
 *  javax.jcr.query.Query
 *  javax.jcr.query.QueryManager
 *  javax.jcr.query.QueryResult
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.ReferencePolicy
 *  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.screens.impl.handler;

import com.day.cq.commons.jcr.JcrUtil;
import com.day.cq.contentsync.config.ConfigEntry;
import com.day.cq.contentsync.handler.AbstractDefaultContentUpdateHandler;
import com.day.cq.contentsync.handler.ContentUpdateHandler;
import com.day.cq.dam.api.Asset;
import com.day.cq.dam.api.Rendition;
import com.day.text.Text;
import java.util.ArrayList;
import java.util.Iterator;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Workspace;
import javax.jcr.query.Query;
import javax.jcr.query.QueryManager;
import javax.jcr.query.QueryResult;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferencePolicy;
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/assetrenditions")
public class AssetRenditionsUpdateHandler
extends AbstractDefaultContentUpdateHandler
implements ContentUpdateHandler {
    private static final Logger log = LoggerFactory.getLogger(AssetRenditionsUpdateHandler.class);
    private static final String RENDITIONS_CONFIG_PROPERTY = "renditions";
    @Reference(policy=ReferencePolicy.STATIC)
    private JcrResourceResolverFactory resolverFactory;

    public boolean updateCacheEntry(ConfigEntry configEntry, Long lastUpdated, String configCacheRoot, Session admin, Session session) {
        boolean changed = false;
        ResourceResolver resolver = this.resolverFactory.getResourceResolver(session);
        ValueMap configOptions = ResourceUtil.getValueMap((Resource)resolver.getResource(configEntry.getPath()));
        String[] renditionNames = (String[])configOptions.get("renditions", (Object)new String[0]);
        try {
            Iterator<Asset> assets = this.getAssets(configEntry.getContentPath(), admin);
            configCacheRoot = this.getConfigCacheRoot(configEntry, configCacheRoot);
            while (assets.hasNext()) {
                Asset asset = assets.next();
                if (renditionNames.length == 0 && this.isModified(asset.getOriginal(), lastUpdated, configCacheRoot + asset.getPath(), admin)) {
                    Node parent = JcrUtil.createPath((String)(configCacheRoot + Text.getRelativeParent((String)asset.getPath(), (int)1)), (String)"sling:Folder", (Session)admin);
                    JcrUtil.copy((Node)((Node)asset.getOriginal().adaptTo(Node.class)), (Node)parent, (String)asset.getName());
                    changed |= true;
                }
                for (String renditionName : renditionNames) {
                    Rendition rendition = asset.getRendition(renditionName);
                    if (rendition == null || !this.isModified(rendition, lastUpdated, configCacheRoot + rendition.getPath(), admin)) continue;
                    Node parent = JcrUtil.createPath((String)(configCacheRoot + Text.getRelativeParent((String)rendition.getPath(), (int)1)), (String)"sling:Folder", (Session)admin);
                    JcrUtil.copy((Node)((Node)rendition.adaptTo(Node.class)), (Node)parent, (String)rendition.getName());
                    changed |= true;
                }
                admin.save();
            }
        }
        catch (Exception e) {
            log.error("Update of cache entry failed: ", (Throwable)e);
        }
        return changed;
    }

    protected Iterator<Asset> getAssets(String assetFolderPath, Session session) throws RepositoryException {
        ArrayList<Object> assets = new ArrayList<Object>();
        ResourceResolver resolver = this.resolverFactory.getResourceResolver(session);
        StringBuilder queryString = new StringBuilder("/jcr:root");
        queryString.append(assetFolderPath);
        queryString.append("//element(*,dam:Asset)");
        Query query = session.getWorkspace().getQueryManager().createQuery(queryString.toString(), "xpath");
        NodeIterator iter = query.execute().getNodes();
        while (iter.hasNext()) {
            assets.add(resolver.getResource(iter.nextNode().getPath()).adaptTo(Asset.class));
        }
        return assets.iterator();
    }

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

    protected void bindResolverFactory(JcrResourceResolverFactory jcrResourceResolverFactory) {
        this.resolverFactory = jcrResourceResolverFactory;
    }

    protected void unbindResolverFactory(JcrResourceResolverFactory jcrResourceResolverFactory) {
        if (this.resolverFactory == jcrResourceResolverFactory) {
            this.resolverFactory = null;
        }
    }
}