DPSProjectImpl.java 12.4 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.commons.Filter
 *  com.day.cq.wcm.api.Page
 *  com.day.cq.wcm.webservicesupport.Configuration
 *  com.day.cq.wcm.webservicesupport.ConfigurationManager
 *  javax.jcr.RepositoryException
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ValueMap
 *  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.dps.impl.metadata;

import com.adobe.cq.mobile.dps.DPSArticle;
import com.adobe.cq.mobile.dps.DPSBanner;
import com.adobe.cq.mobile.dps.DPSCollection;
import com.adobe.cq.mobile.dps.DPSException;
import com.adobe.cq.mobile.dps.DPSObject;
import com.adobe.cq.mobile.dps.DPSProject;
import com.adobe.cq.mobile.dps.impl.metadata.AbstractDPSObjectImpl;
import com.adobe.cq.mobile.dps.impl.utils.DPSUtil;
import com.day.cq.commons.Filter;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.webservicesupport.Configuration;
import com.day.cq.wcm.webservicesupport.ConfigurationManager;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.jcr.RepositoryException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
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 DPSProjectImpl
extends AbstractDPSObjectImpl
implements DPSProject {
    private static final Logger LOGGER = LoggerFactory.getLogger(DPSProjectImpl.class);
    private Map<String, DPSArticle> articleMap = null;
    private Map<String, DPSBanner> bannerMap = null;
    private Map<String, DPSCollection> collectionMap = null;

    public DPSProjectImpl(Page page) {
        super(page);
        if (!this.getDPSResourceType().equals("dps:Project")) {
            throw new IllegalArgumentException("Underlying page, " + page.getPath() + ", is not a " + "dps:Project" + ".");
        }
    }

    @Override
    public String getProjectId() {
        return this.getId();
    }

    @Override
    public String getId() {
        String id = (String)this.page.getProperties().get("dps-projectId", String.class);
        if (id != null && id.isEmpty()) {
            id = null;
        }
        return id;
    }

    @Override
    public String getName() {
        return (String)this.page.getProperties().get("jcr:title", String.class);
    }

    @Override
    public String getTitle() {
        return this.page.getTitle();
    }

    private Map<String, DPSArticle> getArticlesMap() {
        if (this.articleMap == null) {
            this.loadMap();
        }
        return this.articleMap;
    }

    private Map<String, DPSBanner> getBannersMap() {
        if (this.bannerMap == null) {
            this.loadMap();
        }
        return this.bannerMap;
    }

    private Map<String, DPSCollection> getCollectionsMap() {
        if (this.collectionMap == null) {
            this.loadMap();
        }
        return this.collectionMap;
    }

    private synchronized void loadMap() {
        LinkedHashMap<String, DPSArticle> articlesHash = new LinkedHashMap<String, DPSArticle>();
        LinkedHashMap<String, DPSBanner> bannersHash = new LinkedHashMap<String, DPSBanner>();
        LinkedHashMap<String, DPSCollection> collectionsHash = new LinkedHashMap<String, DPSCollection>();
        Iterator children = this.page.listChildren(null, true);
        while (children.hasNext()) {
            Page nextChild = (Page)children.next();
            DPSObject dpsObject = DPSUtil.getDPSObject(nextChild);
            if (dpsObject instanceof DPSArticle) {
                articlesHash.put(nextChild.getName(), (DPSArticle)dpsObject);
                continue;
            }
            if (dpsObject instanceof DPSBanner) {
                bannersHash.put(nextChild.getName(), (DPSBanner)dpsObject);
                continue;
            }
            if (!(dpsObject instanceof DPSCollection)) continue;
            collectionsHash.put(nextChild.getName(), (DPSCollection)dpsObject);
        }
        this.articleMap = articlesHash;
        this.bannerMap = bannersHash;
        this.collectionMap = collectionsHash;
    }

    @Override
    public List<DPSArticle> getArticles() {
        ArrayList<DPSArticle> articles = new ArrayList<DPSArticle>();
        articles.addAll(this.getArticlesMap().values());
        return articles;
    }

    @Override
    public List<DPSArticle> getArticles(Resource folderResource) {
        ArrayList<DPSArticle> articles = new ArrayList<DPSArticle>();
        Iterator children = folderResource.listChildren();
        while (children.hasNext()) {
            Resource childResource = (Resource)children.next();
            Page childPage = (Page)childResource.adaptTo(Page.class);
            if (childPage != null) {
                DPSArticle childArticle = (DPSArticle)childPage.adaptTo(DPSArticle.class);
                if (childArticle == null) continue;
                articles.add(childArticle);
                continue;
            }
            if (!childResource.isResourceType("sling:Folder")) continue;
            articles.addAll(this.getArticles(childResource));
        }
        return articles;
    }

    @Override
    public DPSArticle getArticle(String name) {
        DPSArticle dpsArticle = null;
        Map<String, DPSArticle> articles = this.getArticlesMap();
        if (articles.containsKey(name)) {
            dpsArticle = articles.get(name);
        }
        if (dpsArticle == null && name.contains("/")) {
            for (Map.Entry<String, DPSArticle> article : articles.entrySet()) {
                if (!name.contains(article.getKey()) || !article.getValue().getPath().equals(name)) continue;
                dpsArticle = article.getValue();
                break;
            }
        }
        return dpsArticle;
    }

    @Override
    public List<DPSBanner> getBanners() {
        ArrayList<DPSBanner> banners = new ArrayList<DPSBanner>();
        banners.addAll(this.getBannersMap().values());
        return banners;
    }

    @Override
    public List<DPSBanner> getBanners(Resource folder) {
        ArrayList<DPSBanner> banners = new ArrayList<DPSBanner>();
        Iterator children = folder.listChildren();
        while (children.hasNext()) {
            Resource childResource = (Resource)children.next();
            Page childPage = (Page)childResource.adaptTo(Page.class);
            if (childPage != null) {
                DPSBanner childBanner = (DPSBanner)childPage.adaptTo(DPSBanner.class);
                if (childBanner == null) continue;
                banners.add(childBanner);
                continue;
            }
            if (!childResource.isResourceType("sling:Folder")) continue;
            banners.addAll(this.getBanners(childResource));
        }
        return banners;
    }

    @Override
    public DPSBanner getBanner(String name) {
        DPSBanner dpsBanner = null;
        Map<String, DPSBanner> banners = this.getBannersMap();
        if (banners.containsKey(name)) {
            dpsBanner = banners.get(name);
        }
        if (dpsBanner == null && name.contains("/")) {
            for (Map.Entry<String, DPSBanner> banner : banners.entrySet()) {
                if (!name.contains(banner.getKey()) || !banner.getValue().getPath().equals(name)) continue;
                dpsBanner = banner.getValue();
                break;
            }
        }
        return dpsBanner;
    }

    @Override
    public List<DPSCollection> getCollections() {
        ArrayList<DPSCollection> collections = new ArrayList();
        Resource pageResource = (Resource)this.page.adaptTo(Resource.class);
        Resource collectionsRootResource = pageResource.getChild("collections");
        if (collectionsRootResource != null) {
            collections = this.getCollections(collectionsRootResource);
        } else {
            LOGGER.debug("Article root folder not found for project at " + pageResource.getPath());
        }
        return collections;
    }

    @Override
    public List<DPSCollection> getCollections(Resource folderResource) {
        ArrayList<DPSCollection> collections = new ArrayList<DPSCollection>();
        Iterator children = folderResource.listChildren();
        while (children.hasNext()) {
            Resource childResource = (Resource)children.next();
            Page childPage = (Page)childResource.adaptTo(Page.class);
            if (childPage != null) {
                DPSCollection childCollection = (DPSCollection)childPage.adaptTo(DPSCollection.class);
                if (childCollection == null) continue;
                collections.add(childCollection);
                continue;
            }
            if (!childResource.isResourceType("sling:Folder")) continue;
            collections.addAll(this.getCollections(childResource));
        }
        return collections;
    }

    @Override
    public DPSCollection getCollection(String name) {
        DPSCollection dpsCollection = null;
        Map<String, DPSCollection> collections = this.getCollectionsMap();
        if (collections.containsKey(name)) {
            dpsCollection = collections.get(name);
        }
        if (dpsCollection == null && name.contains("/")) {
            for (Map.Entry<String, DPSCollection> collection : collections.entrySet()) {
                if (!name.contains(collection.getKey()) || !collection.getValue().getPath().equals(name)) continue;
                dpsCollection = collection.getValue();
                break;
            }
        }
        return dpsCollection;
    }

    @Override
    public String getExportTemplatePath() {
        return (String)this.page.getProperties().get("dps-exportTemplate", String.class);
    }

    @Override
    public Configuration getDPSConfiguration() throws DPSException {
        Configuration configuration = null;
        try {
            ResourceResolver resourceResolver = this.page.getContentResource().getResourceResolver();
            ConfigurationManager configurationManager = (ConfigurationManager)resourceResolver.adaptTo(ConfigurationManager.class);
            String cloudServiceConfigPath = (String)this.page.getProperties().get("dps-cloudConfig", String.class);
            configuration = DPSUtil.getDPSConfiguration(configurationManager, cloudServiceConfigPath);
        }
        catch (RepositoryException e) {
            throw new DPSException("Failed to retrieve Experience Manager Mobile CloudService configuration from " + this.getPath());
        }
        return configuration;
    }

    @Override
    public String getLastHTMLResourceDPSUploadBy() {
        return (String)this.page.getProperties().get("dps-sharedHTMLResources-lastUploadedBy", String.class);
    }

    @Override
    public Date getLastHTMLResourceDPSUpload() {
        return (Date)this.page.getProperties().get("dps-sharedHTMLResources-lastUploaded", Date.class);
    }

    @Override
    public JSONObject toJSON() {
        JSONObject jsonObject = new JSONObject();
        JSONObject projectJSON = new JSONObject();
        try {
            projectJSON.put("path", (Object)this.getPath());
            projectJSON.put("name", (Object)this.page.getName());
            projectJSON.put("dps-lastModified", (Object)DPSUtil.getJSONDateValue(this.getLastCQModified()));
            projectJSON.put("jcr:title", (Object)this.getTitle());
            projectJSON.put("jcr:description", (Object)this.getDescription());
            projectJSON.put("dps-exportTemplate", (Object)this.getExportTemplatePath());
            projectJSON.put("dps-projectId", (Object)this.getId());
            JSONArray articlesJSON = new JSONArray();
            List<DPSArticle> articles = this.getArticles();
            for (DPSArticle dpsArticle : articles) {
                articlesJSON.put((Object)dpsArticle.toJSON());
            }
            projectJSON.put("articles", (Object)articlesJSON);
            jsonObject.put("folio", (Object)projectJSON);
        }
        catch (JSONException e) {
            LOGGER.error("Error while getting data for JSON object", (Throwable)e);
            jsonObject = null;
        }
        return jsonObject;
    }
}