EntityUploadOperation.java 6.05 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.dps.client.producer.EntityType
 *  com.adobe.granite.ui.components.HtmlResponse
 *  com.day.cq.i18n.I18n
 *  javax.servlet.http.HttpServletRequest
 *  org.apache.commons.lang3.StringUtils
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Properties
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.servlets.post.Modification
 *  org.slf4j.Logger
 */
package com.adobe.cq.mobile.dps.impl.operations;

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.DPSProject;
import com.adobe.cq.mobile.dps.impl.DPSClient;
import com.adobe.cq.mobile.dps.impl.DPSEntityImporter;
import com.adobe.cq.mobile.dps.impl.ImportStatus;
import com.adobe.cq.mobile.dps.impl.operations.MobilePublishAbstractOperation;
import com.adobe.cq.mobile.dps.impl.utils.DPSUtil;
import com.adobe.dps.client.producer.EntityType;
import com.adobe.granite.ui.components.HtmlResponse;
import com.day.cq.i18n.I18n;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.servlets.post.Modification;
import org.slf4j.Logger;

@Component(metatype=0, label="Experience Manager Mobile Uploader")
@Service
@Properties(value={@Property(name="sling.post.operation", value={"dpsapps:dpsUpload"}), @Property(name="sling.servlet.methods", value={"POST"})})
public class EntityUploadOperation
extends MobilePublishAbstractOperation {
    public static final String PARAM_CREATE_IF_MISSING = "createIfMissing";
    public static final boolean PARAM_CREATE_IF_MISSING_DEFAULT = false;
    public static final String PARAM_INCLUDE_CONTENT = "includeContent";
    public static final boolean PARAM_INCLUDE_CONTENT_DEFAULT = false;
    public static final String PARAM_TARGET_COLLECTION = "targetCollection";

    @Override
    protected void perform(SlingHttpServletRequest request, HtmlResponse response, List<Modification> modifications) {
        I18n i18n = new I18n((HttpServletRequest)request);
        Resource resource = request.getResource();
        boolean createIfMissing = this.getParameter(request, "createIfMissing", false);
        boolean incContent = this.getParameter(request, "includeContent", false);
        String targetCollection = this.getParameter(request, "targetCollection", null);
        String dpsApp = null;
        String entityName = "Entity";
        try {
            DPSEntity dpsEntity = this.getDPSEntity(request);
            DPSClient dpsClient = this.getDPSClient(request);
            if (dpsEntity == null) {
                String message = i18n.get("Invalid dps resource {0}", "resource path", new Object[]{resource.getPath()});
                throw new DPSException(message);
            }
            EntityType entityType = DPSUtil.getEntityType(dpsEntity);
            entityName = entityType.name().toLowerCase();
            String entityCapitalName = entityName.substring(0, 1).toUpperCase() + entityName.substring(1);
            dpsApp = dpsEntity.getProject().getPath();
            dpsClient.uploadEntity(dpsEntity, createIfMissing, incContent);
            if (StringUtils.isNotEmpty((CharSequence)targetCollection)) {
                DPSProject dpsProject = dpsEntity.getProject();
                DPSEntityImporter dpsEntityImporter = this.getDPSEntityImporter(request);
                DPSCollection dpsTargetCollection = dpsProject.getCollection(targetCollection);
                if (dpsTargetCollection == null) {
                    throw new DPSException("Target collection " + targetCollection + " does not exist in AEM.");
                }
                if (dpsTargetCollection.getId() == null) {
                    throw new DPSException("Target collection " + targetCollection + " does not exist in Experience Manager Mobile On-Demand Services.");
                }
                dpsClient.addContentToCollection(dpsTargetCollection, dpsEntity);
                dpsEntityImporter.importDPSEntity(dpsTargetCollection);
            }
            response.setPath(resource.getPath());
            String title = i18n.get("{0} uploaded", "entity type name", new Object[]{entityCapitalName});
            String message = i18n.get("{0} {1} has been uploaded.", "entity type name and entity path", new Object[]{entityCapitalName, resource.getPath()});
            this.generateResponse(response, 200, message, title);
        }
        catch (Exception ex) {
            String title = i18n.get("Error");
            String cause = this.getCauseMessage(ex);
            if (StringUtils.isEmpty((CharSequence)cause)) {
                cause = ex.getLocalizedMessage();
            }
            String message = i18n.get("Error uploading {0}: '{1}'. {2}", "Entity type name, entity path and root cause", new Object[]{entityName, resource.getName(), cause});
            this.getLogger().error(message, (Throwable)ex);
            String redirectUrl = null;
            String redirectTitle = null;
            if (!StringUtils.isEmpty((CharSequence)cause) && cause.contains("Collection has not been uploaded yet")) {
                redirectUrl = "/libs/mobileapps/dps/admin/content/dashboard/collections.html";
                redirectUrl = redirectUrl + dpsApp;
                redirectUrl = redirectUrl + "?item=";
                redirectUrl = redirectUrl + dpsApp + "/" + "collections" + "/" + targetCollection;
                redirectTitle = i18n.get("View Collection");
            }
            this.generateError(response, message, title, ex, redirectUrl, redirectTitle);
        }
    }
}