DPSPageManagerImpl.java 10.5 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.wcm.webservicesupport.ConfigurationManager
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  org.apache.commons.lang.StringUtils
 *  org.apache.sling.api.adapter.AdapterManager
 *  org.apache.sling.commons.json.JSONArray
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.JSONObject
 *  org.osgi.service.event.EventAdmin
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.mobile.dps.impl.service;

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.DPSEntity;
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.DPSPageManager;
import com.adobe.cq.mobile.dps.impl.utils.MetadataJSONUtil;
import com.day.cq.wcm.webservicesupport.ConfigurationManager;
import java.util.HashSet;
import java.util.List;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.api.adapter.AdapterManager;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.osgi.service.event.EventAdmin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DPSPageManagerImpl
implements DPSPageManager {
    private static final Logger LOGGER = LoggerFactory.getLogger(DPSPageManagerImpl.class);
    private static String[] UNLINK_PROPERTY_NAMES = new String[]{"dps-id", "dps-lastUploaded", "dps-lastUploadedBy", "dps-lastSynced"};
    private Session userSession = null;
    private ConfigurationManager configurationManagerService = null;
    private AdapterManager adapterManager = null;
    protected EventAdmin eventAdminService = null;

    public DPSPageManagerImpl(Session userSession, ConfigurationManager configurationManagerService, EventAdmin eventAdminService, AdapterManager adapterManager) {
        this.userSession = userSession;
        this.configurationManagerService = configurationManagerService;
        this.eventAdminService = eventAdminService;
        this.adapterManager = adapterManager;
    }

    @Override
    public JSONArray getAEMArticles(DPSProject dpsProject) throws DPSException {
        JSONArray articles = new JSONArray();
        try {
            List<DPSArticle> aemArticles = dpsProject.getArticles();
            for (DPSArticle dpsArticle : aemArticles) {
                JSONObject articleJSON = MetadataJSONUtil.getArticleJSON(dpsArticle);
                this.validateJSONDataForPublishing(articleJSON);
                articles.put((Object)articleJSON);
            }
        }
        catch (Exception ex) {
            if (dpsProject != null && StringUtils.isNotEmpty((String)dpsProject.getName())) {
                throw new DPSException("Failed to get AEM articles for project: " + dpsProject.getName(), ex);
            }
            throw new DPSException("Failed to get AEM articles.", ex);
        }
        return articles;
    }

    @Override
    public JSONArray getAEMBanners(DPSProject dpsProject) throws DPSException {
        JSONArray entities = new JSONArray();
        try {
            List<DPSBanner> aemBanners = dpsProject.getBanners();
            for (DPSBanner dpsBanner : aemBanners) {
                JSONObject bannerJSON = MetadataJSONUtil.getBannerJSON(dpsBanner);
                this.validateJSONDataForPublishing(bannerJSON);
                entities.put((Object)bannerJSON);
            }
        }
        catch (Exception ex) {
            if (dpsProject != null && StringUtils.isNotEmpty((String)dpsProject.getName())) {
                throw new DPSException("Failed to get AEM banners for project: " + dpsProject.getName(), ex);
            }
            throw new DPSException("Failed to get AEM banner.", ex);
        }
        return entities;
    }

    @Override
    public JSONObject getAEMCollection(DPSCollection dpsCollection, boolean includeReferences, boolean incStatus, boolean incContentDetails) throws DPSException {
        JSONObject jsonObject = null;
        try {
            jsonObject = MetadataJSONUtil.getCollectionJSON(dpsCollection);
        }
        catch (Exception ex) {
            throw new DPSException("Failed to get AEM collection " + dpsCollection.getPath(), ex);
        }
        return jsonObject;
    }

    @Override
    public JSONArray getAEMCollections(DPSProject dpsProject, boolean flat) throws DPSException {
        JSONArray collections = new JSONArray();
        try {
            JSONObject collectionJSON;
            List<DPSCollection> aemCollections = dpsProject.getCollections();
            HashSet<String> collectionThatAreNested = new HashSet<String>();
            for (DPSCollection dpsCollection : aemCollections) {
                JSONArray dpsContentLinks;
                collectionJSON = MetadataJSONUtil.getCollectionJSON(dpsCollection);
                this.validateJSONDataForPublishing(collectionJSON);
                collections.put((Object)collectionJSON);
                if (flat || (dpsContentLinks = dpsCollection.getContentLinks()).length() <= 0) continue;
                for (int i = 0; i < dpsContentLinks.length(); ++i) {
                    Object next = dpsContentLinks.get(i);
                    if (next instanceof JSONObject) {
                        JSONObject linkJSON = (JSONObject)next;
                        String name = linkJSON.getString("name");
                        String type = linkJSON.getString("type");
                        if (type.equals("collection")) {
                            collectionThatAreNested.add(name);
                            continue;
                        }
                        if (type.equals("article") || type.equals("banner")) continue;
                        LOGGER.warn("Invalid link type " + type);
                        continue;
                    }
                    LOGGER.warn("Invalid link entry " + next);
                }
            }
            if (!flat) {
                JSONArray rootCollections = new JSONArray();
                for (int i = 0; i < collections.length(); ++i) {
                    collectionJSON = collections.getJSONObject(i);
                    if (collectionThatAreNested.contains(collectionJSON.getString("name"))) continue;
                    rootCollections.put((Object)collectionJSON);
                }
                collections = rootCollections;
            }
        }
        catch (Exception ex) {
            if (dpsProject != null && StringUtils.isNotEmpty((String)dpsProject.getName())) {
                throw new DPSException("Failed to get AEM collections for project: " + dpsProject.getName(), ex);
            }
            throw new DPSException("Failed to get AEM collections.", ex);
        }
        return collections;
    }

    @Override
    public void unlink(DPSObject dpsObject) throws DPSException {
        try {
            if (dpsObject instanceof DPSProject) {
                LOGGER.info("Unlinking content from project " + dpsObject.getPath());
                DPSProject dpsProject = (DPSProject)dpsObject;
                int unlinked = 0;
                unlinked += this.unlinkContent(dpsProject.getCollections());
                unlinked += this.unlinkContent(dpsProject.getArticles());
                LOGGER.info("Unlinked {} entities from {}", (Object)(unlinked += this.unlinkContent(dpsProject.getBanners())), (Object)dpsObject.getPath());
            } else if (dpsObject instanceof DPSEntity) {
                this.unlinkImpl((DPSEntity)dpsObject);
            }
        }
        catch (Exception dpsEx) {
            throw new DPSException("Failed to unlink " + dpsObject.getPath());
        }
    }

    private int unlinkContent(List<? extends DPSEntity> entities) throws RepositoryException {
        int unlinked = 0;
        for (DPSEntity entity : entities) {
            boolean cleaned;
            if (entity.getId() == null || !(cleaned = this.unlinkImpl(entity))) continue;
            ++unlinked;
        }
        return unlinked;
    }

    private boolean unlinkImpl(DPSEntity entity) throws RepositoryException {
        boolean cleaned = false;
        if (!entity.isImported()) {
            LOGGER.info("Unlinking " + entity.getPath());
            Node node = (Node)entity.adaptTo(Node.class);
            if (node.hasNode("jcr:content")) {
                node = node.getNode("jcr:content");
                for (String key : UNLINK_PROPERTY_NAMES) {
                    if (!node.hasProperty(key)) continue;
                    node.getProperty(key).setValue((String)null);
                    cleaned = true;
                    LOGGER.debug("Cleaned up {} for {}", (Object)key, (Object)node.getPath());
                }
            }
        } else {
            LOGGER.debug("Skipping unlinking of imported entity {} ", (Object)entity.getPath());
        }
        return cleaned;
    }

    public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
        if (Session.class == type) {
            return (AdapterType)this.userSession;
        }
        return null;
    }

    private void validateJSONDataForPublishing(JSONObject json) throws JSONException {
        String type = json.getString("entityType");
        JSONObject validationError = new JSONObject();
        JSONArray missingProperties = new JSONArray();
        if (!type.isEmpty()) {
            if ("collection".equals(type)) {
                if (!json.has("productId")) {
                    missingProperties.put((Object)"productId");
                }
                if (!json.has("image") && "AEM".equals(json.getString("source"))) {
                    missingProperties.put((Object)"image");
                }
                if (!json.has("layout")) {
                    missingProperties.put((Object)"layout");
                }
            } else if ("banner".equals(type)) {
                if (!json.has("image")) {
                    missingProperties.put((Object)"image");
                }
            } else if ("article".equals(type) && !json.has("image")) {
                missingProperties.put((Object)"image");
            }
            if (missingProperties.length() > 0) {
                validationError.put("missingProperties", (Object)missingProperties);
                json.put("validations", (Object)validationError);
            }
        }
    }
}