PhoneGapBuildOperation.java 4.98 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.ui.components.HtmlResponse
 *  com.adobe.granite.workflow.WorkflowException
 *  com.adobe.granite.workflow.WorkflowSession
 *  com.adobe.granite.workflow.exec.Workflow
 *  com.adobe.granite.workflow.exec.WorkflowData
 *  com.adobe.granite.workflow.model.WorkflowModel
 *  com.day.cq.i18n.I18n
 *  javax.servlet.http.HttpServletRequest
 *  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.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.servlets.post.Modification
 *  org.slf4j.Logger
 */
package com.adobe.cq.mobile.platform.impl.operations;

import com.adobe.cq.mobile.platform.impl.operations.MobileApplicationOperation;
import com.adobe.granite.ui.components.HtmlResponse;
import com.adobe.granite.workflow.WorkflowException;
import com.adobe.granite.workflow.WorkflowSession;
import com.adobe.granite.workflow.exec.Workflow;
import com.adobe.granite.workflow.exec.WorkflowData;
import com.adobe.granite.workflow.model.WorkflowModel;
import com.day.cq.i18n.I18n;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
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.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.servlets.post.Modification;
import org.slf4j.Logger;

@Component(metatype=0, label="Create Mobile Page Operation")
@Service
@Properties(value={@Property(name="sling.post.operation", value={"mobileapps:phoneGapBuild"})})
public class PhoneGapBuildOperation
extends MobileApplicationOperation {
    public static final String OPERATION_NAME = "phoneGapBuild";
    public static final String PHONE_GAP_BUILD_MODEL = "/etc/workflow/models/phonegap/initiate-phonegap-build/jcr:content/model";
    public static final String CQ_APPS_ADMIN_PHONEGAP_BUILD_WFHISTORY = "cq-apps-admin-phonegapbuild-wfhistory";

    @Override
    protected void perform(SlingHttpServletRequest request, HtmlResponse response, List<Modification> changes) {
        I18n i18n = new I18n((HttpServletRequest)request);
        Resource appInstance = null;
        RequestParameter appInstanceParam = request.getRequestParameter("appInstance");
        if (appInstanceParam == null) {
            String message = i18n.get("Unable to resolve the mobile app");
            String title = i18n.get("Error");
            this.generateError(response, message, title);
            return;
        }
        appInstance = request.getResourceResolver().getResource(appInstanceParam.toString());
        try {
            this.clearContentSyncBuildCache(response, appInstance, false);
        }
        catch (Exception ex) {
            LOGGER.error("Failed to clear content sync cache", (Throwable)ex);
            String message = i18n.get("Unable to clear content sync cache.  Stopping PhoneGap build process.");
            String title = i18n.get("Error");
            this.generateError(response, message, title, ex);
            return;
        }
        Workflow wf = this.startBuild(appInstance, i18n, response);
        if (wf != null) {
            String title = i18n.get("PhoneGap Build");
            String message = i18n.get("The PhoneGap Build Process has started");
            response.onCreated(wf.getId());
            this.generateResponse(response, 201, message, title);
        }
    }

    private Workflow startBuild(Resource appInstance, I18n i18n, HtmlResponse response) {
        LOGGER.debug("Starting workflow for phonegap build {}", (Object)appInstance.getPath());
        Workflow wf = null;
        WorkflowSession wfSession = (WorkflowSession)appInstance.getResourceResolver().adaptTo(WorkflowSession.class);
        try {
            WorkflowModel modelToStart = wfSession.getModel("/etc/workflow/models/phonegap/initiate-phonegap-build/jcr:content/model");
            WorkflowData workflowData = wfSession.newWorkflowData("JCR_PATH", (Object)appInstance.getPath());
            HashMap metaData = new HashMap();
            wf = wfSession.startWorkflow(modelToStart, workflowData, metaData);
            LOGGER.debug("finished executing phonegap build, workflow Id {} ", (Object)wf.getId());
        }
        catch (WorkflowException ex) {
            String title = i18n.get("Error");
            String message = i18n.get("Unable to start the PhoneGap build process");
            this.generateError(response, message, title, (Throwable)ex);
        }
        return wf;
    }
}