CreateLanguageCopyProcess.java 6.64 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.commons.LanguageUtil
 *  com.day.cq.workflow.WorkflowException
 *  com.day.cq.workflow.WorkflowSession
 *  com.day.cq.workflow.exec.WorkItem
 *  com.day.cq.workflow.exec.WorkflowData
 *  com.day.cq.workflow.exec.WorkflowProcess
 *  com.day.cq.workflow.metadata.MetaDataMap
 *  com.day.text.Text
 *  javax.jcr.Session
 *  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.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ResourceResolverFactory
 *  org.apache.sling.api.resource.ResourceUtil
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.wcm.translation.impl.process;

import com.adobe.cq.wcm.translation.impl.TranslationLanguageCopy;
import com.adobe.cq.wcm.translation.impl.process.AbstractWCMTranslationProcess;
import com.day.cq.commons.LanguageUtil;
import com.day.cq.workflow.WorkflowException;
import com.day.cq.workflow.WorkflowSession;
import com.day.cq.workflow.exec.WorkItem;
import com.day.cq.workflow.exec.WorkflowData;
import com.day.cq.workflow.exec.WorkflowProcess;
import com.day.cq.workflow.metadata.MetaDataMap;
import com.day.text.Text;
import java.util.Collections;
import java.util.Locale;
import java.util.Map;
import javax.jcr.Session;
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.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.api.resource.ResourceUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
@Service(value={WorkflowProcess.class})
@Properties(value={@Property(name="service.description", value={"Create Language Copy Workflow Process"}), @Property(name="service.vendor", value={"Adobe"}), @Property(name="process.label", value={"WCM: Create Language Copy Process"})})
public class CreateLanguageCopyProcess
extends AbstractWCMTranslationProcess {
    private static final Logger log = LoggerFactory.getLogger(CreateLanguageCopyProcess.class);
    private static final String META_CREATE_NON_EMPTY_ANCESTORS_PARAM = "createNonEmptyAncestors";
    @Reference
    private ResourceResolverFactory resourceResolverFactory = null;

    public void execute(WorkItem item, WorkflowSession workflowSession, MetaDataMap args) throws WorkflowException {
        log.debug("Creating language copy for workitem {}", (Object)(item != null ? item.getId() : "<null>"));
        ResourceResolver resourceResolver = null;
        String payloadData = null;
        String destinationPath = null;
        try {
            resourceResolver = this.resourceResolverFactory.getServiceResourceResolver(Collections.singletonMap("sling.service.subservice", "translation-job"));
            Session session = (Session)resourceResolver.adaptTo(Session.class);
            WorkflowData data = item.getWorkflowData();
            MetaDataMap metaData = data.getMetaDataMap();
            boolean deep = "true".equals(metaData.get("deep", String.class));
            boolean copyMissingAncestors = "true".equals(metaData.get("createNonEmptyAncestors", String.class));
            String translationWorkflowModel = (String)metaData.get("translationWorkflowModel", String.class);
            String strProjectFolderPath = (String)metaData.get("projectFolderPath", String.class);
            payloadData = this.getPayloadPath(data);
            Resource pageResource = resourceResolver.getResource(payloadData);
            if (pageResource == null || ResourceUtil.isNonExistingResource((Resource)pageResource)) {
                throw new WorkflowException("Failed to load source path from payload.");
            }
            String langRootPagePath = LanguageUtil.getLanguageRoot((String)payloadData);
            if (langRootPagePath == null) {
                throw new WorkflowException("Language root page does not exist.");
            }
            String contentPath = Text.getRelativeParent((String)langRootPagePath, (int)1);
            String pagePath = langRootPagePath.equals(payloadData) ? "" : "" + '/' + payloadData.substring(langRootPagePath.length() + 1);
            String destLanguage = (String)metaData.get("language", String.class);
            if (StringUtils.isEmpty((CharSequence)destLanguage)) {
                throw new WorkflowException("Failed to load destination language from payload.");
            }
            String destLanguageTitle = LanguageUtil.getLanguage((String)destLanguage).getLocale().getDisplayName();
            if (StringUtils.isEmpty((CharSequence)destLanguageTitle)) {
                throw new WorkflowException("Destination language does not exist.");
            }
            destinationPath = contentPath + '/' + destLanguage + pagePath;
            Resource destResource = resourceResolver.getResource(destinationPath);
            if (destResource != null) {
                throw new WorkflowException("Language copy already exists.");
            }
            destResource = TranslationLanguageCopy.createLanguagePage(resourceResolver, session, payloadData, destinationPath, destLanguage, copyMissingAncestors, deep);
            metaData.remove((Object)"language");
            this.startTranslationWorkflow(translationWorkflowModel, strProjectFolderPath, item, workflowSession, destinationPath, payloadData, metaData);
        }
        catch (Exception e) {
            log.error("Error while creating the copy for {} at {}.", new Object[]{payloadData, destinationPath});
            throw new WorkflowException("Error while creating the copy of " + payloadData + " at " + destinationPath + ".", (Throwable)e);
        }
        finally {
            if (resourceResolver != null && resourceResolver.isLive()) {
                resourceResolver.close();
            }
        }
    }

    protected void bindResourceResolverFactory(ResourceResolverFactory resourceResolverFactory) {
        this.resourceResolverFactory = resourceResolverFactory;
    }

    protected void unbindResourceResolverFactory(ResourceResolverFactory resourceResolverFactory) {
        if (this.resourceResolverFactory == resourceResolverFactory) {
            this.resourceResolverFactory = null;
        }
    }
}