EntityDeleteOperation.java 5.82 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.cq.mobile.platform.MobileResource
 *  com.adobe.cq.mobile.platform.MobileResourceLocator
 *  com.adobe.cq.mobile.platform.MobileResourceType
 *  com.adobe.dps.client.producer.EntityType
 *  com.adobe.granite.ui.components.HtmlResponse
 *  com.day.cq.i18n.I18n
 *  com.day.text.Text
 *  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.api.resource.ResourceResolver
 *  org.apache.sling.servlets.post.Modification
 *  org.slf4j.Logger
 */
package com.adobe.cq.mobile.dps.impl.operations;

import com.adobe.cq.mobile.dps.DPSEntity;
import com.adobe.cq.mobile.dps.DPSException;
import com.adobe.cq.mobile.dps.impl.DPSClient;
import com.adobe.cq.mobile.dps.impl.operations.MobilePublishAbstractOperation;
import com.adobe.cq.mobile.dps.impl.utils.DPSUtil;
import com.adobe.cq.mobile.platform.MobileResource;
import com.adobe.cq.mobile.platform.MobileResourceLocator;
import com.adobe.cq.mobile.platform.MobileResourceType;
import com.adobe.dps.client.producer.EntityType;
import com.adobe.granite.ui.components.HtmlResponse;
import com.day.cq.i18n.I18n;
import com.day.text.Text;
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.api.resource.ResourceResolver;
import org.apache.sling.servlets.post.Modification;
import org.slf4j.Logger;

@Component(metatype=0, label="Experience Manager Mobile Deletion")
@Service
@Properties(value={@Property(name="sling.post.operation", value={"dpsapps:dpsDelete"}), @Property(name="sling.servlet.methods", value={"POST"})})
public class EntityDeleteOperation
extends MobilePublishAbstractOperation {
    public static final String PARAM_DELETE_LOCAL = "deleteLocal";
    public static final boolean PARAM_DELETE_LOCAL_DEFAULT = false;
    private static final String SUCCESS_DISPLAY_MESSAGE_REL = "cq-apps-dps-display-message";

    @Override
    protected void perform(SlingHttpServletRequest request, HtmlResponse response, List<Modification> modifications) {
        I18n i18n = new I18n((HttpServletRequest)request);
        Resource resource = request.getResource();
        String entityName = "entity";
        String warningMessage = null;
        boolean deleteLocal = this.getParameter(request, "deleteLocal", false);
        try {
            DPSEntity dpsEntity = this.getDPSEntity(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();
            DPSClient dpsClient = this.getDPSClient(request);
            try {
                dpsClient.deleteEntity(dpsEntity, deleteLocal);
            }
            catch (DPSException dpsEx) {
                warningMessage = dpsEx.getMessage();
                if (!warningMessage.startsWith("Warning:")) {
                    throw dpsEx;
                }
                warningMessage = warningMessage.substring(9);
            }
            MobileResourceLocator locator = (MobileResourceLocator)resource.getResourceResolver().adaptTo(MobileResourceLocator.class);
            MobileResource group = locator.findAncestorResourceByType(resource, MobileResourceType.GROUP.getType(), new String[0]);
            String redirectTo = this.getRedirect(request);
            if (group != null) {
                if (StringUtils.isBlank((CharSequence)redirectTo)) {
                    redirectTo = "/libs/mobileapps/admin/content/dashboard.html";
                }
                redirectTo = redirectTo + Text.escapePath((String)group.getPath());
            } else {
                redirectTo = "/aem/apps.html";
            }
            if (StringUtils.isBlank((CharSequence)dpsEntity.getId())) {
                this.addLink(response, "cq-apps-dps-display-message", "/content/mobileapps", "Item is not associated with an Entity in Experience Manager Mobile.");
            } else if (StringUtils.isNotBlank((CharSequence)warningMessage)) {
                this.addLink(response, "cq-apps-dps-display-message", resource.getPath(), warningMessage);
            }
            response.setPath(resource.getPath());
            String title = i18n.get("The {0} was deleted.", "entity type name", new Object[]{entityName});
            String message = i18n.get("The {0} '{1}' has been deleted.", "entity type name and entity path", new Object[]{entityName, resource.getPath()});
            this.generateResponse(response, 200, message, title, redirectTo, i18n.get("Done"));
        }
        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 deleting {0}: {1}. {2}", "entity type name, entity name and root cause", new Object[]{entityName, resource.getName(), cause});
            this.getLogger().error(message, (Throwable)ex);
            this.generateError(response, message, title, ex);
        }
    }
}