MobilePagesUpdateHandler.java 3.59 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.contentsync.config.ConfigEntry
 *  com.day.cq.wcm.api.Page
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ValueMap
 *  org.slf4j.Logger
 */
package com.adobe.cq.mobile.platform.impl.contentsync.handler;

import com.adobe.cq.mobile.platform.impl.contentsync.handler.AbstractPagesUpdateHandler;
import com.day.cq.contentsync.config.ConfigEntry;
import com.day.cq.wcm.api.Page;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.felix.scr.annotations.Component;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import org.slf4j.Logger;

@Component(metatype=1, factory="com.day.cq.contentsync.handler.ContentUpdateHandler/mobileapppages", inherit=1)
public class MobilePagesUpdateHandler
extends AbstractPagesUpdateHandler {
    protected static final int MAX_DEPTH_NONE = -1;
    protected static final int MAX_DEPTH_DEFAULT = -1;
    protected static final String PN_MAX_DEPTH = "modificationCheckMaxDepth";

    @Override
    protected boolean isModified(Page page, String uri, Long lastUpdated, ConfigEntry configEntry, String configCacheRoot, Session session) throws RepositoryException {
        boolean modified = true;
        if (this.includeOnlyModifiedPages(configEntry)) {
            if (!session.nodeExists(configCacheRoot + uri)) {
                return true;
            }
            modified = this.isModified(page, lastUpdated, 0, this.getMaxDepth(configEntry));
        }
        return modified;
    }

    private int getMaxDepth(ConfigEntry configEntry) {
        int maxDepth = -1;
        String maxDepthStr = configEntry.getValue("modificationCheckMaxDepth");
        if (maxDepthStr != null) {
            try {
                maxDepth = Integer.parseInt(maxDepthStr);
            }
            catch (NumberFormatException e) {
                // empty catch block
            }
        }
        return maxDepth;
    }

    protected boolean isModified(Page page, Long lastUpdated, int currentDepth, int maxDepth) throws RepositoryException {
        boolean modified = false;
        if (page.getContentResource().getResourceType() == null || page.getContentResource().getResourceType().equals("cq:PageContent")) {
            this.log.debug("No sling resource type detected, ignoring page" + page.getPath());
        } else {
            Calendar cal = page.getLastModified();
            if (cal == null) {
                this.log.debug("No lastmodified set on page: " + page.getPath());
                cal = (Calendar)page.getProperties().get("jcr:created", Calendar.class);
            }
            if (cal != null) {
                long lastModified = cal.getTime().getTime();
                boolean bl = modified = lastUpdated < lastModified || lastModified == -1;
                if (modified) {
                    return modified;
                }
            }
        }
        if (maxDepth == -1 || currentDepth < maxDepth) {
            Iterator children = page.listChildren();
            while (children.hasNext()) {
                Page childPage = (Page)children.next();
                boolean bl = this.isModified(childPage, lastUpdated, currentDepth + 1, maxDepth) || modified;
                modified = bl;
                if (!modified) continue;
                return modified;
            }
        }
        return modified;
    }
}