ProjectWizard.java 5.29 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.cq.projects.api.Project
 *  com.adobe.granite.ui.components.ComponentHelper
 *  com.adobe.granite.ui.components.Config
 *  com.adobe.granite.ui.components.ExpressionHelper
 *  com.day.cq.wcm.api.Template
 *  org.apache.commons.lang3.StringUtils
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.request.RequestPathInfo
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ResourceUtil
 *  org.apache.sling.api.resource.ValueMap
 *  org.apache.sling.api.wrappers.ValueMapDecorator
 */
package com.adobe.cq.projects.ui;

import com.adobe.cq.projects.api.Project;
import com.adobe.granite.ui.components.ComponentHelper;
import com.adobe.granite.ui.components.Config;
import com.adobe.granite.ui.components.ExpressionHelper;
import com.day.cq.wcm.api.Template;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.SlingHttpServletRequest;
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.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.wrappers.ValueMapDecorator;

public class ProjectWizard {
    private SlingHttpServletRequest slingRequest;
    private ComponentHelper helper;
    private ValueMap values = null;
    public static final String PROJECT_VALUES_ATTRIBUTE = "cq.ui.project.values";
    private static final String WIZARD_PAGE_RT = "cq/gui/components/projects/admin/wizard/page";
    private static final String PARAM_PROJECT = "project";

    public ProjectWizard(SlingHttpServletRequest slingRequest, ComponentHelper helper) {
        this.slingRequest = slingRequest;
        this.helper = helper;
        if (slingRequest == null || helper == null) {
            throw new IllegalArgumentException("A valid SlingHttpServletRequest and ComponentHelper must be provided.");
        }
        ValueMap projectValues = (ValueMap)slingRequest.getAttribute("cq.ui.project.values");
        if (projectValues == null) {
            Project project;
            projectValues = new ValueMapDecorator(new HashMap());
            ResourceResolver resolver = this.slingRequest.getResourceResolver();
            String projectPath = slingRequest.getParameter("project");
            if (StringUtils.isBlank((CharSequence)projectPath)) {
                projectPath = slingRequest.getRequestPathInfo().getSuffix();
            }
            Resource projectResource = null;
            if (StringUtils.isNotBlank((CharSequence)projectPath)) {
                projectResource = resolver.getResource(projectPath);
            }
            if (projectResource != null && (project = (Project)projectResource.adaptTo(Project.class)) != null) {
                Template projectTemplate;
                Resource assetFolder;
                projectValues.put((Object)"project", (Object)((Resource)project.adaptTo(Resource.class)).getPath());
                projectValues.put((Object)"projectTitle", (Object)project.getTitle());
                Resource projectCover = project.getProjectCover();
                if (projectCover != null && !ResourceUtil.isNonExistingResource((Resource)projectCover)) {
                    projectValues.put((Object)"projectCover", (Object)projectCover.getParent());
                }
                if ((assetFolder = project.getAssetFolder()) != null && !ResourceUtil.isNonExistingResource((Resource)assetFolder)) {
                    projectValues.put((Object)"projectAssetFolderPath", (Object)assetFolder.getPath());
                }
                if ((projectTemplate = (Template)project.adaptTo(Template.class)) != null) {
                    projectValues.put((Object)"projectTemplatePath", (Object)projectTemplate.getPath());
                }
                this.slingRequest.setAttribute("cq.ui.project.values", (Object)projectValues);
            }
        }
        this.values = projectValues;
    }

    public ValueMap getWizardValues(Resource resource) {
        Resource wizardPage;
        for (wizardPage = resource; wizardPage != null && !wizardPage.isResourceType("cq/gui/components/projects/admin/wizard/page"); wizardPage = wizardPage.getParent()) {
        }
        if (wizardPage == null) {
            return null;
        }
        Config wizardCfg = new Config(wizardPage);
        String[] wizardParams = (String[])wizardCfg.get("params", String[].class);
        ValueMapDecorator wizardValues = new ValueMapDecorator(new HashMap());
        if (wizardParams != null) {
            for (String param : wizardParams) {
                String[] p = param.replaceAll("[${}]", "").split("\\.");
                if (p == null || p.length != 2) continue;
                String paramValue = this.helper.getExpressionHelper().getString(param);
                if (StringUtils.isBlank((CharSequence)paramValue)) {
                    paramValue = (String)this.values.get(p[1], String.class);
                }
                wizardValues.put((Object)p[1], (Object)paramValue);
            }
        }
        return wizardValues;
    }

    public ValueMap getProjectValues() {
        return this.values;
    }
}