CreateMobileContentPackageOperation.java 5.75 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.ui.components.HtmlResponse
 *  com.day.cq.i18n.I18n
 *  com.day.cq.wcm.api.Page
 *  javax.jcr.Node
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  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.ModifiableValueMap
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.servlets.post.Modification
 */
package com.adobe.cq.mobile.platform.impl.operations;

import com.adobe.cq.mobile.platform.MobileResource;
import com.adobe.cq.mobile.platform.impl.operations.CreateMobilePageOperation;
import com.adobe.cq.mobile.platform.impl.operations.MobileOperationException;
import com.adobe.granite.ui.components.HtmlResponse;
import com.day.cq.i18n.I18n;
import com.day.cq.wcm.api.Page;
import java.util.List;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
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.ModifiableValueMap;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.servlets.post.Modification;

@Component(metatype=0, label="Create Content Package Operation")
@Service
@Properties(value={@Property(name="sling.post.operation", value={"mobileapps:createMobileContentPackage"})})
public class CreateMobileContentPackageOperation
extends CreateMobilePageOperation {
    public static final String OPERATION_NAME = "createMobileContentPackage";
    public static final String PARAM_MOBILE_TYPE = "mobileType";
    public static final String PARAM_CS_CONFIG_PATH = "contentSyncConfigPath";
    public static final String PARAM_DESCRIPTION = "description";

    @Override
    protected void perform(SlingHttpServletRequest request, HtmlResponse response, List<Modification> modifications) {
        I18n i18n = new I18n((HttpServletRequest)request);
        String mobileAppType = request.getParameter("mobileType");
        String contentSyncConfigPath = request.getParameter("contentSyncConfigPath");
        String description = request.getParameter("description");
        String parentPath = request.getParameter("parentPath");
        String pageLabel = request.getParameter("pageName");
        if (StringUtils.isBlank((CharSequence)mobileAppType)) {
            String message = i18n.get("Missing mandatory parameter {0}", null, new Object[]{"mobileType"});
            String title = i18n.get("Error");
            this.generateError(response, message, title);
            return;
        }
        if (StringUtils.isBlank((CharSequence)contentSyncConfigPath)) {
            String message = i18n.get("Missing mandatory parameter {0}", null, new Object[]{"contentSyncConfigPath"});
            String title = i18n.get("Error");
            this.generateError(response, message, title);
            return;
        }
        Session session = (Session)request.getResourceResolver().adaptTo(Session.class);
        try {
            if (session.nodeExists(parentPath + "/" + pageLabel)) {
                String message = i18n.get("Target folder already exists {0}", null, new Object[]{parentPath + "/" + pageLabel});
                String title = i18n.get("Error");
                this.generateError(response, message, title);
                return;
            }
        }
        catch (RepositoryException repEx) {
            String message = i18n.get("Target folder already exists {0}", null, new Object[]{parentPath + "/" + pageLabel});
            String title = i18n.get("Error");
            this.generateError(response, message, title);
            return;
        }
        Page page = null;
        try {
            page = super.createPage(request, response, modifications, i18n);
            Resource newResource = (Resource)page.adaptTo(Resource.class);
            MobileResource mobileResource = (MobileResource)newResource.adaptTo(MobileResource.class);
            mobileResource.addAppType(mobileAppType);
            Resource newAppContentRes = page.getContentResource();
            ModifiableValueMap properties = (ModifiableValueMap)newAppContentRes.adaptTo(ModifiableValueMap.class);
            properties.put((Object)"phonegap-exportTemplate", (Object)contentSyncConfigPath);
            if (description != null) {
                properties.put((Object)"jcr:description", (Object)description);
            }
            newAppContentRes.getResourceResolver().commit();
        }
        catch (Exception ex) {
            String message = i18n.get("The server has problem processing your request");
            if (page != null) {
                Node newNode = (Node)page.adaptTo(Node.class);
                try {
                    newNode.remove();
                }
                catch (Exception nodeRemoveEx) {
                    message = message + ". " + i18n.get("Content Package partially created.");
                }
            }
            String title = i18n.get("Error");
            this.generateError(response, message, title);
        }
        catch (MobileOperationException e) {
            String title = i18n.get("Error");
            this.generateError(response, e.getMessage(), title);
        }
    }
}