ChannelOfflineInfo.java 3.62 KB
package libs.screens.dcc.components.dashboard.channelinfo;

import com.adobe.cq.screens.binding.ScreensConstants;
import com.adobe.cq.sightly.WCMUsePojo;
import com.day.cq.commons.Filter;
import com.day.cq.contentsync.ContentSyncManager;
import com.day.cq.contentsync.config.Config;
import com.day.cq.wcm.api.Page;
import org.apache.jackrabbit.util.ISO9075;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;

import javax.jcr.query.Query;
import javax.jcr.Session;

import java.util.Calendar;
import java.util.Iterator;

public class ChannelOfflineInfo extends WCMUsePojo {
    private Resource channel;

    private boolean offline;
    private long lastSyncTimestamp;
    private long channelLastModified;
    private boolean updates;
    private String configPath;
    private String channelPath;

    private long getPageLastModified(Page page) {
        if ( page.getLastModified() != null ) {
            return page.getLastModified().getTimeInMillis();
        } else {
            ValueMap properties = channel.adaptTo(ValueMap.class);
            Calendar creationDate = properties.get("jcr:created", Calendar.class);
            return creationDate.getTimeInMillis();
        }
    }

    @Override
    public void activate() throws Exception {
        channel = getRequest().getRequestPathInfo().getSuffixResource();

        ContentSyncManager contentSyncManager = getSlingScriptHelper().getService(ContentSyncManager.class);
        if ( channel != null && contentSyncManager != null) {
            channelPath = channel.getPath();
            String query = "/jcr:root" + ISO9075.encodePath(channelPath) + "//element(*, cq:ContentSyncConfig)";
            Iterator<Resource> contentSyncConfigIter = channel.getResourceResolver().findResources(query, Query.XPATH);

            if ( contentSyncConfigIter.hasNext() ) {
                Resource contentSyncResource = contentSyncConfigIter.next();
                Config config = contentSyncResource.adaptTo(Config.class);
                Page page = channel.adaptTo(Page.class);
                if ( config != null && page != null) {
                    offline = !page.getContentResource().adaptTo(ValueMap.class).get(ScreensConstants.PN_FORCE_REMOTE_CONTENT, false);
                    configPath = config.getPath();
                    Session session = getResourceResolver().adaptTo(Session.class);
                    lastSyncTimestamp = contentSyncManager.getLatestTimestamp(config, session);
                    channelLastModified = getPageLastModified(page);
                    updates = channelLastModified > lastSyncTimestamp;

                    // Check sub-pages (zones)
                    if (!updates) {
                        Iterator<Page> modifiedZones = page.listChildren(new Filter<Page>() {
                            @Override
                            public boolean includes(Page zone) {
                                return getPageLastModified(zone) > lastSyncTimestamp;
                            }
                        });
                        if (modifiedZones.hasNext()) {
                            updates = true;
                        }
                    }
                }
            }
        }
    }

    public boolean isOffline() {
        return offline;
    }

    public boolean hasUpdates() {
        return updates;
    }

    public long getLastSyncTimestamp() {
        return lastSyncTimestamp;
    }

    public long getChannelLastModified() {
        return channelLastModified;
    }

    public String getConfigPath() {
        return configPath;
    }

    public String getChannelPath() {
        return channelPath;
    }

}