MobileAppsArticleCreationOperation.java 6.78 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.granite.ui.components.HtmlResponse
 *  com.adobe.granite.xss.XSSAPI
 *  com.day.cq.i18n.I18n
 *  com.day.cq.wcm.api.Page
 *  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.request.RequestParameter
 *  org.apache.sling.api.request.RequestPathInfo
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.servlets.post.Modification
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.mobile.dps.impl.operations;

import com.adobe.cq.mobile.dps.impl.DPSClient;
import com.adobe.cq.mobile.dps.impl.DPSEntityImporter;
import com.adobe.cq.mobile.dps.impl.operations.MobilePublishAbstractOperation;
import com.adobe.cq.mobile.dps.impl.utils.EntityCreationUtil;
import com.adobe.cq.mobile.platform.MobileResource;
import com.adobe.cq.mobile.platform.MobileResourceLocator;
import com.adobe.cq.mobile.platform.MobileResourceType;
import com.adobe.granite.ui.components.HtmlResponse;
import com.adobe.granite.xss.XSSAPI;
import com.day.cq.i18n.I18n;
import com.day.cq.wcm.api.Page;
import com.day.text.Text;
import java.util.ArrayList;
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.request.RequestParameter;
import org.apache.sling.api.request.RequestPathInfo;
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;
import org.slf4j.LoggerFactory;

@Component(metatype=0, label="Experience Manager Mobile Article Creator")
@Service
@Properties(value={@Property(name="sling.post.operation", value={"dpsapps:createArticle"}), @Property(name="sling.servlet.methods", value={"POST"})})
public class MobileAppsArticleCreationOperation
extends MobilePublishAbstractOperation {
    private static final String PARAM_ARTICLE_NAME = "articleName";
    private static final String PARAM_ARTICLE_IMAGE = "articleImage";
    private static final String PARAM_ARTICLE_SOCIAL_IMAGE = "socialShareImage";
    public static final String PARAM_UPLOAD = "upload";
    public static final boolean PARAM_UPLOAD_DEFAULT = true;
    private static final Logger LOGGER = LoggerFactory.getLogger(MobileAppsArticleCreationOperation.class);
    private static final String CQ_APPS_ADMIN_CREATEARTICLE_EDIT = "cq-apps-admin-createarticle-edit";
    private final String[] allowedProperties = new String[]{"dps-title", "dps-shortTitle", "dps-abstract", "dps-shortAbstract", "dps-author", "dps-authorURL", "dps-department", "dps-category", "dps-keywords", "dps-internalKeywords", "dps-access", "dps-importance", "dps-hideFromBrowsePage", "dps-isTrustedContent", "dps-isAdvertisement", "dps-adType", "dps-adCategory", "dps-advertiser", "dps-socialShareURL", "dps-articleText", "dps-url", "pge-type"};
    private final String[] mandatoryParameters = new String[]{"template", "dps-title", "articleName"};

    @Override
    protected void perform(SlingHttpServletRequest request, HtmlResponse response, List<Modification> modifications) {
        MobileResource group;
        I18n i18n = new I18n((HttpServletRequest)request);
        String errorTitle = i18n.get("Error");
        Page newArticle = null;
        String appInstanceSuffix = request.getRequestPathInfo().getSuffix();
        ResourceResolver resolver = request.getResourceResolver();
        MobileResourceLocator locator = (MobileResourceLocator)resolver.adaptTo(MobileResourceLocator.class);
        Resource appPublishInstance = resolver.getResource(appInstanceSuffix);
        ArrayList<String> warnings = new ArrayList<String>();
        try {
            group = locator.findClosestResourceByType(appPublishInstance, MobileResourceType.GROUP.getType(), new String[0]);
            String title = request.getRequestParameter("dps-title").getString("utf-8");
            newArticle = EntityCreationUtil.createEntity(request, this.allowedProperties, this.mandatoryParameters, this.getDPSClient(request), true, "dps:Article".substring(4), "articles", "articleName", group, title, warnings);
        }
        catch (Exception dpsEx) {
            this.generateError(response, dpsEx.getMessage(), errorTitle);
            return;
        }
        for (int i = 0; i < warnings.size(); ++i) {
            this.addLink(response, "cq-apps-dps-create-entity-warning-" + i, "", warnings.get(i));
        }
        try {
            EntityCreationUtil.saveProperties(request, newArticle, this.allowedProperties, "dps:Article", this.getDPSClient(request), this.getDPSEntityImporter(request), group);
        }
        catch (Exception dpsEx) {
            this.generateError(response, dpsEx.getMessage(), errorTitle);
            return;
        }
        try {
            EntityCreationUtil.setImage(request, newArticle, "dps:Article".substring(4), "articleImage", "image");
        }
        catch (Exception dpsEx) {
            this.generateError(response, dpsEx.getMessage(), errorTitle);
            return;
        }
        try {
            EntityCreationUtil.setImage(request, newArticle, "dps:Article".substring(4), "socialShareImage", "social-share-image");
        }
        catch (Exception dpsEx) {
            this.generateError(response, dpsEx.getMessage(), errorTitle);
            return;
        }
        String redirectTo = this.getRedirect(request);
        if (StringUtils.isBlank((CharSequence)redirectTo)) {
            redirectTo = "/libs/mobileapps/admin/content/dashboard.html" + Text.escapePath((String)group.getPath());
        }
        response.setPath(newArticle.getPath());
        response.setLocation(appInstanceSuffix);
        response.addLink("cq-apps-admin-createarticle-edit", this.xssAPI.getValidHref("/editor.html" + newArticle.getPath() + ".html"), i18n.get("Edit Article"));
        String title = i18n.get("Article created");
        String message = i18n.get("Your Article, {0}, has been created.", "Article title", new Object[]{newArticle.getTitle()});
        this.generateResponse(response, 201, message, title, redirectTo, i18n.get("Done"));
    }
}