AppCacheDataSourceImpl.java 8.24 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.cq.mobile.ui.AppCacheDataSource
 *  com.day.cq.contentsync.config.Config
 *  javax.jcr.Node
 *  javax.jcr.PathNotFoundException
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.commons.json.JSONArray
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.JSONObject
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.mobile.ui.impl;

import com.adobe.cq.mobile.appcache.impl.AppCacheException;
import com.adobe.cq.mobile.appcache.impl.AppCacheManager;
import com.adobe.cq.mobile.appcache.impl.AppCacheManagerAdapterFactory;
import com.adobe.cq.mobile.appcache.impl.CacheReporter;
import com.adobe.cq.mobile.appcache.impl.CacheUpdate;
import com.adobe.cq.mobile.appcache.impl.ConfigUtil;
import com.adobe.cq.mobile.platform.MobileResource;
import com.adobe.cq.mobile.platform.impl.MobileAppProvider;
import com.adobe.cq.mobile.platform.impl.utils.MobileUtil;
import com.adobe.cq.mobile.ui.AppCacheDataSource;
import com.day.cq.contentsync.config.Config;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.jcr.Node;
import javax.jcr.PathNotFoundException;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AppCacheDataSourceImpl
implements AppCacheDataSource {
    private static final Logger LOGGER = LoggerFactory.getLogger(AppCacheDataSourceImpl.class);
    private AppCacheManagerAdapterFactory appCacheManagerAdapterFactory;
    private Session session = null;
    private Config config = null;
    private Resource resource = null;

    public AppCacheDataSourceImpl(Resource resource, AppCacheManagerAdapterFactory appCacheManagerAdapterFactory) {
        if (resource == null) {
            throw new IllegalArgumentException("Cannot instantiate AppCacheDataSource. Resource cannot be null.");
        }
        this.resource = resource;
        this.session = (Session)resource.getResourceResolver().adaptTo(Session.class);
        this.config = this.getConfig(resource);
        this.appCacheManagerAdapterFactory = appCacheManagerAdapterFactory;
    }

    public JSONObject getCacheUpdates() throws Exception {
        if (this.config == null) {
            throw new Exception("Unable to find a content sync config for " + this.resource.getPath());
        }
        AppCacheManager appCacheManager = this.appCacheManagerAdapterFactory.getAdapter((Object)this.session, AppCacheManager.class);
        return this.getJSON(appCacheManager, this.config, this.session);
    }

    public JSONObject getContentListing(long modifiedSince) throws Exception {
        if (this.config == null) {
            throw new Exception("Unable to find a content sync config for " + this.resource.getPath());
        }
        return this.getUpdateFileList(this.config, modifiedSince);
    }

    private JSONObject getJSON(AppCacheManager appCacheManager, Config config, Session session) throws AppCacheException, JSONException {
        JSONObject cacheJSON = new JSONObject();
        if (appCacheManager == null || config == null || session == null) {
            return cacheJSON;
        }
        cacheJSON.put("name", (Object)config.getName());
        String title = null;
        try {
            Node configNode = session.getNode(config.getPath());
            title = configNode.hasProperty("jcr:title") ? configNode.getProperty("jcr:title").getString() : null;
        }
        catch (RepositoryException e) {
            // empty catch block
        }
        cacheJSON.put("title", (Object)title);
        cacheJSON.put("path", (Object)config.getPath());
        cacheJSON.put("authorizable", (Object)config.getAuthorizable());
        cacheJSON.put("updateUser", (Object)config.getUpdateUser());
        Long lastFilled = appCacheManager.getLastFill(config);
        cacheJSON.put("lastFilled", (Object)lastFilled);
        boolean cacheHasPendingChangesNotYetUpdatedOfficially = false;
        CacheUpdate lastUpdated = appCacheManager.getLastUpdate(config);
        if (lastUpdated != null) {
            cacheJSON.put("lastUpdated", lastUpdated.getCreated().getTimeInMillis());
            cacheHasPendingChangesNotYetUpdatedOfficially = lastFilled == null ? false : lastFilled > lastUpdated.getCreated().getTimeInMillis();
        } else {
            cacheHasPendingChangesNotYetUpdatedOfficially = lastFilled == null ? false : lastFilled > new Date(0).getTime();
        }
        cacheJSON.put("modifiedPastUpdate", cacheHasPendingChangesNotYetUpdatedOfficially);
        CacheUpdate lastPublishedUpdate = appCacheManager.getLastPublishedUpdate(config);
        if (lastPublishedUpdate != null && lastPublishedUpdate.getLastPublished() != null) {
            cacheJSON.put("lastPublished", lastPublishedUpdate.getLastPublished().getTimeInMillis());
            String lastPublishedBy = lastPublishedUpdate.getLastPublishedBy();
            if (lastPublishedBy == null) {
                lastPublishedBy = lastPublishedUpdate.getCreatedBy();
            }
            cacheJSON.put("lastPublishedBy", (Object)lastPublishedBy);
        }
        JSONArray updatesJSON = new JSONArray();
        cacheJSON.put("updates", (Object)updatesJSON);
        List<CacheUpdate> cacheUpdateList = appCacheManager.getUpdates(config);
        for (CacheUpdate cacheUpdate : cacheUpdateList) {
            JSONObject updateJSON = new JSONObject();
            updateJSON.put("ts", cacheUpdate.getCreated().getTimeInMillis());
            updateJSON.put("date", (Object)MobileUtil.getDateAsString(cacheUpdate.getCreated().getTime()));
            updateJSON.put("updateUserId", (Object)cacheUpdate.getCreatedBy());
            updateJSON.put("title", (Object)cacheUpdate.getTitle());
            Calendar lastPublishedCal = cacheUpdate.getLastPublished();
            if (lastPublishedCal != null) {
                updateJSON.put("publishedTS", lastPublishedCal.getTimeInMillis());
                updateJSON.put("publishedDate", (Object)MobileUtil.getDateAsString(lastPublishedCal.getTime()));
                updateJSON.put("publishedBy", (Object)cacheUpdate.getLastPublishedBy());
            }
            updateJSON.put("description", (Object)cacheUpdate.getDescription());
            updatesJSON.put((Object)updateJSON);
        }
        JSONArray zipsJSON = appCacheManager.getZips(config);
        cacheJSON.put("zips", (Object)zipsJSON);
        return cacheJSON;
    }

    private JSONObject getUpdateFileList(Config config, Long ifModifiedSince) throws Exception {
        JSONObject jsonResponse = new JSONObject();
        try {
            String cachePath = ConfigUtil.configToCachePath(config, this.session);
            Node cacheFolder = this.session.getNode(cachePath);
            CacheReporter listingUtil = new CacheReporter(cacheFolder);
            jsonResponse = listingUtil.getFileListingJSON(ifModifiedSince);
        }
        catch (PathNotFoundException pathNotFoundException) {
            jsonResponse.put("count", 0);
        }
        catch (RepositoryException repEx) {
            throw new Exception((Throwable)repEx);
        }
        catch (JSONException jsonEx) {
            throw new Exception((Throwable)jsonEx);
        }
        return jsonResponse;
    }

    private Config getConfig(Resource resource) {
        Config cacheConfig = (Config)resource.adaptTo(Config.class);
        if (cacheConfig != null) {
            return cacheConfig;
        }
        MobileResource mobileResource = (MobileResource)resource.adaptTo(MobileResource.class);
        MobileAppProvider appProvider = (MobileAppProvider)mobileResource.adaptTo(MobileAppProvider.class);
        Map<String, Config> configMap = appProvider.findResourceConfigs();
        if (configMap != null && configMap.containsKey("STAGE")) {
            return configMap.get("STAGE");
        }
        return null;
    }
}