DPSTOCUpdateHandler.java 3.13 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.wcm.foundation.Image
 *  javax.jcr.Node
 *  javax.jcr.Session
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.jcr.resource.JcrResourceResolverFactory
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.mobile.dps.impl.contentsync;

import com.adobe.cq.mobile.dps.impl.utils.DPSUtil;
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.wcm.foundation.Image;
import java.util.Calendar;
import java.util.Date;
import javax.jcr.Node;
import javax.jcr.Session;
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.jcr.resource.JcrResourceResolverFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

    public boolean updateCacheEntry(ConfigEntry configEntry, Long lastUpdated, String configCacheRoot, Session admin, Session session) {
        try {
            boolean changed = false;
            ResourceResolver resolver = this.resolverFactory.getResourceResolver(admin);
            String nodeContainingImageNodePath = configEntry.getContentPath() + "/jcr:content";
            String imageNodePath = nodeContainingImageNodePath + "/image";
            if (DPSUtil.isImageSet(resolver.getResource(imageNodePath))) {
                Image img = new Image(resolver.getResource(nodeContainingImageNodePath));
                img.setItemName("file", "image");
                img.setItemName("fileReference", "imageReference");
                img.setSelector("img");
                if (img.getLastModified() == null || lastUpdated < img.getLastModified().getTime().getTime()) {
                    this.renderResource(img.getHref(), configCacheRoot, admin, session);
                    admin.save();
                    changed = true;
                }
            } else {
                String defaultTOCImagePath = "/libs/media/publishing/content/defaultFolioContent/toc.png";
                JcrUtil.copy((Node)admin.getNode(defaultTOCImagePath), (Node)admin.getNode(configCacheRoot), (String)"toc.png", (boolean)true);
                changed = true;
            }
            return changed;
        }
        catch (Exception ex) {
            log.error("Unexpected error while updating cache for config: " + configEntry.getPath(), (Throwable)ex);
            return false;
        }
    }

    protected String getTargetPath(String path) {
        return "/toc.png";
    }
}