InjectMasterLanguageIntoLaunchProcess.java 17.3 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.aemds.guide.service.GuideLocalizationService
 *  com.adobe.cq.launches.api.Launch
 *  com.adobe.cq.launches.api.LaunchManager
 *  com.adobe.cq.launches.api.LaunchManagerFactory
 *  com.adobe.granite.taskmanagement.Task
 *  com.adobe.granite.taskmanagement.TaskAction
 *  com.adobe.granite.taskmanagement.TaskManager
 *  com.adobe.granite.taskmanagement.TaskManagerFactory
 *  com.adobe.granite.translation.api.TranslationConfig
 *  com.adobe.granite.translation.api.TranslationManager
 *  com.adobe.granite.translation.core.TranslationCloudConfigUtil
 *  com.adobe.granite.ui.clientlibs.HtmlLibraryManager
 *  com.day.cq.commons.LanguageUtil
 *  com.day.cq.wcm.api.LanguageManager
 *  com.day.cq.wcm.api.PageManager
 *  com.day.cq.wcm.api.PageManagerFactory
 *  com.day.cq.workflow.WorkflowException
 *  com.day.cq.workflow.WorkflowSession
 *  com.day.cq.workflow.exec.WorkItem
 *  com.day.cq.workflow.exec.Workflow
 *  com.day.cq.workflow.exec.WorkflowData
 *  com.day.cq.workflow.exec.WorkflowProcess
 *  com.day.cq.workflow.metadata.MetaDataMap
 *  com.day.text.Text
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  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.ModifiableValueMap
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ResourceResolverFactory
 *  org.apache.sling.jcr.api.SlingRepository
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.wcm.translation.impl.process;

import com.adobe.aemds.guide.service.GuideLocalizationService;
import com.adobe.cq.launches.api.Launch;
import com.adobe.cq.launches.api.LaunchManager;
import com.adobe.cq.launches.api.LaunchManagerFactory;
import com.adobe.cq.wcm.translation.impl.TranslationLanguageCopy;
import com.adobe.cq.wcm.translation.impl.TranslationRuleConfigurationFile;
import com.adobe.cq.wcm.translation.impl.process.AbstractWCMTranslationProcess;
import com.adobe.granite.taskmanagement.Task;
import com.adobe.granite.taskmanagement.TaskAction;
import com.adobe.granite.taskmanagement.TaskManager;
import com.adobe.granite.taskmanagement.TaskManagerFactory;
import com.adobe.granite.translation.api.TranslationConfig;
import com.adobe.granite.translation.api.TranslationManager;
import com.adobe.granite.translation.core.TranslationCloudConfigUtil;
import com.adobe.granite.ui.clientlibs.HtmlLibraryManager;
import com.day.cq.commons.LanguageUtil;
import com.day.cq.wcm.api.LanguageManager;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.api.PageManagerFactory;
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.Workflow;
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.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.jcr.Node;
import javax.jcr.Property;
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.Reference;
import org.apache.felix.scr.annotations.Service;
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.api.resource.ResourceResolverFactory;
import org.apache.sling.jcr.api.SlingRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
@Service(value={WorkflowProcess.class})
@Properties(value={@org.apache.felix.scr.annotations.Property(name="service.description", value={"Inject the content of master language into the language copy launch"}), @org.apache.felix.scr.annotations.Property(name="service.vendor", value={"Adobe"}), @org.apache.felix.scr.annotations.Property(name="process.label", value={"WCM: Inject Master Language into Language Copy Launch Process"})})
public class InjectMasterLanguageIntoLaunchProcess
extends AbstractWCMTranslationProcess {
    private static final Logger log = LoggerFactory.getLogger(InjectMasterLanguageIntoLaunchProcess.class);
    public static final String TASK_TYPE = "Notification";
    @Reference
    private ResourceResolverFactory resourceResolverFactory = null;
    @Reference
    LaunchManagerFactory launchesManagerFactory = null;
    @Reference
    private TranslationManager translationManager = null;
    @Reference
    private TranslationCloudConfigUtil translationCloudConfigUtil = null;
    @Reference
    private LanguageManager languageManager;
    @Reference
    private SlingRepository repository;
    @Reference
    protected TranslationConfig transConfig;
    @Reference
    PageManagerFactory pageManagerFactory;
    @Reference
    private HtmlLibraryManager htmlLibraryManager = null;
    @Reference
    private GuideLocalizationService guideLocalizationService = null;

    public void execute(WorkItem item, WorkflowSession workflowSession, MetaDataMap args) throws WorkflowException {
        block11 : {
            Session session = workflowSession.getSession();
            ResourceResolver resourceResolver = null;
            String launchDest = null;
            try {
                resourceResolver = this.resourceResolverFactory.getServiceResourceResolver(Collections.singletonMap("sling.service.subservice", "translation-job"));
                session = (Session)resourceResolver.adaptTo(Session.class);
                PageManager pageManager = (PageManager)resourceResolver.adaptTo(PageManager.class);
                LaunchManager launchManager = this.launchesManagerFactory.getLaunchManager(resourceResolver);
                WorkflowData data = item.getWorkflowData();
                MetaDataMap metaData = data.getMetaDataMap();
                String launchPath = (String)metaData.get("launchRootResourcePath", (Object)"");
                String translationWorkflowModel = (String)metaData.get("translationWorkflowModel", String.class);
                String strProjectFolderPath = (String)metaData.get("projectFolderPath", String.class);
                String strLanguageRefCount = (String)metaData.get("projectFolderLanguageRefCount", String.class);
                this.setProjectFolderCount(resourceResolver, session, strProjectFolderPath, strLanguageRefCount);
                Launch launch = launchManager.getLaunch(launchPath);
                if (launch == null) {
                    throw new WorkflowException("Failed to load launch resource from payload.");
                }
                boolean deep = "true".equals(metaData.get("deep", String.class));
                String masterLanguage = (String)metaData.get("language", String.class);
                if (StringUtils.isEmpty((CharSequence)masterLanguage)) {
                    throw new WorkflowException("Failed to load master language from payload.");
                }
                String destLanguage = (String)metaData.get("destinationLanguage", String.class);
                Resource launchSourceRootRes = launch.getSourceRootResource();
                String launchSrcRootPath = launchSourceRootRes.getPath();
                String launchLangRootPath = LanguageUtil.getLanguageRoot((String)launchSrcRootPath);
                String contentPath = Text.getRelativeParent((String)launchLangRootPath, (int)1);
                String pagePath = launchLangRootPath.equals(launchSrcRootPath) ? "" : "" + '/' + launchSrcRootPath.substring(launchLangRootPath.length() + 1);
                String masterPagePath = contentPath + '/' + masterLanguage + pagePath;
                launchDest = launchPath + launchSrcRootPath;
                Resource masterPageResource = resourceResolver.getResource(masterPagePath);
                if (masterPageResource == null) {
                    throw new WorkflowException("Master page resource does not exist.");
                }
                TranslationLanguageCopy languageCopy = new TranslationLanguageCopy();
                ArrayList<Resource> resourceList = languageCopy.injectMasterLanguageIntoLaunch(new TranslationRuleConfigurationFile(resourceResolver, "/etc/workflow/models/translation/translation_rules.xml", masterLanguage, destLanguage, this.translationManager, this.translationCloudConfigUtil, this.pageManagerFactory, this.htmlLibraryManager, this.guideLocalizationService, true, true), pageManager, resourceResolver, launch, masterLanguage, session, deep, true);
                Resource launchDestRes = resourceResolver.getResource(resourceList.get(0).getPath());
                if (launchDestRes != null) {
                    if (launchLangRootPath.equals(launchSrcRootPath)) {
                        String languageRootPageTitle = ((Node)launchSourceRootRes.getChild("jcr:content").adaptTo(Node.class)).getProperty("jcr:title").getString();
                        ((Node)launchDestRes.getChild("jcr:content").adaptTo(Node.class)).setProperty("jcr:title", languageRootPageTitle);
                        log.info("Set Page Title {} to new Language Copy root language at {}", (Object)languageRootPageTitle, (Object)launchDestRes.getPath());
                        session.save();
                    }
                    this.startTranslationWorkflow(translationWorkflowModel, strProjectFolderPath, item, workflowSession, launchDestRes.getPath(), launchSrcRootPath, metaData);
                    break block11;
                }
                this.cleanUpProjectFolderIfRequired(resourceResolver, session, strProjectFolderPath);
                this.addInboxNotificationNow(resourceResolver, item.getWorkflow().getInitiator(), masterPagePath, launchSrcRootPath);
                log.error("There is no change in source path {}", (Object)launchLangRootPath);
            }
            catch (Exception e) {
                log.error("Error while updating the content of {}.", new Object[]{launchDest});
                throw new WorkflowException("Error while updating the content of  " + launchDest + ".", (Throwable)e);
            }
            finally {
                if (resourceResolver != null && resourceResolver.isLive()) {
                    resourceResolver.close();
                }
            }
        }
    }

    private void cleanUpProjectFolderIfRequired(ResourceResolver resourceResolver, Session session, String strProjectFolderPath) {
        if (!StringUtils.isEmpty((CharSequence)strProjectFolderPath)) {
            try {
                ModifiableValueMap properties;
                Resource resource = resourceResolver.getResource(strProjectFolderPath);
                if (resource != null && (properties = (ModifiableValueMap)resource.adaptTo(ModifiableValueMap.class)).containsKey((Object)"projectFolderLanguageRefCount")) {
                    long iValue = ((Integer)properties.get("projectFolderLanguageRefCount", (Object)0)).intValue();
                    properties.put((Object)"projectFolderLanguageRefCount", (Object)(--iValue));
                    if (iValue <= 0) {
                        Node project = (Node)resource.adaptTo(Node.class);
                        if (!resource.hasChildren()) {
                            project.remove();
                        }
                    }
                    session.save();
                }
            }
            catch (Exception ex) {
                log.error("Error while reducing the Project Folder Language Ref Count");
            }
        }
    }

    private void setProjectFolderCount(ResourceResolver resourceResolver, Session session, String strProjectFolderPath, String strLanguageRefCount) {
        if (!StringUtils.isEmpty((CharSequence)strProjectFolderPath) && !StringUtils.isEmpty((CharSequence)strLanguageRefCount)) {
            long iValue = Long.parseLong(strLanguageRefCount);
            try {
                ModifiableValueMap properties;
                Resource resource = resourceResolver.getResource(strProjectFolderPath);
                if (resource != null && !(properties = (ModifiableValueMap)resource.adaptTo(ModifiableValueMap.class)).containsKey((Object)"projectFolderLanguageRefCount")) {
                    properties.put((Object)"projectFolderLanguageRefCount", (Object)iValue);
                    session.save();
                }
            }
            catch (Exception ex) {
                log.error("Error while setting up Project Folder Language Ref Count");
            }
        }
    }

    private void addInboxNotificationNow(ResourceResolver resourceResolver, String strInitiator, String masterPagePath, String launchSrcRootPath) {
        try {
            TaskManager tm = (TaskManager)resourceResolver.adaptTo(TaskManager.class);
            TaskManagerFactory tmf = tm.getTaskManagerFactory();
            StringBuffer description = new StringBuffer(String.format("No translation project created, There is no change detected between %s and %s", masterPagePath, launchSrcRootPath));
            Task newTask = tmf.newTask("Notification");
            newTask.setName(String.format("No translation project created.", new Object[0]));
            TaskAction newTaskAction = tmf.newTaskAction("Remove");
            newTask.setActions(Collections.singletonList(newTaskAction));
            newTask.setContentPath(launchSrcRootPath);
            newTask.setCurrentAssignee(strInitiator);
            newTask.setDescription(description.toString());
            tm.createTask(newTask);
        }
        catch (Exception e) {
            log.error("Unable to send Task notificaiton {}", (Throwable)e);
        }
    }

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

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

    protected void bindLaunchesManagerFactory(LaunchManagerFactory launchManagerFactory) {
        this.launchesManagerFactory = launchManagerFactory;
    }

    protected void unbindLaunchesManagerFactory(LaunchManagerFactory launchManagerFactory) {
        if (this.launchesManagerFactory == launchManagerFactory) {
            this.launchesManagerFactory = null;
        }
    }

    protected void bindTranslationManager(TranslationManager translationManager) {
        this.translationManager = translationManager;
    }

    protected void unbindTranslationManager(TranslationManager translationManager) {
        if (this.translationManager == translationManager) {
            this.translationManager = null;
        }
    }

    protected void bindTranslationCloudConfigUtil(TranslationCloudConfigUtil translationCloudConfigUtil) {
        this.translationCloudConfigUtil = translationCloudConfigUtil;
    }

    protected void unbindTranslationCloudConfigUtil(TranslationCloudConfigUtil translationCloudConfigUtil) {
        if (this.translationCloudConfigUtil == translationCloudConfigUtil) {
            this.translationCloudConfigUtil = null;
        }
    }

    protected void bindLanguageManager(LanguageManager languageManager) {
        this.languageManager = languageManager;
    }

    protected void unbindLanguageManager(LanguageManager languageManager) {
        if (this.languageManager == languageManager) {
            this.languageManager = null;
        }
    }

    protected void bindRepository(SlingRepository slingRepository) {
        this.repository = slingRepository;
    }

    protected void unbindRepository(SlingRepository slingRepository) {
        if (this.repository == slingRepository) {
            this.repository = null;
        }
    }

    protected void bindTransConfig(TranslationConfig translationConfig) {
        this.transConfig = translationConfig;
    }

    protected void unbindTransConfig(TranslationConfig translationConfig) {
        if (this.transConfig == translationConfig) {
            this.transConfig = null;
        }
    }

    protected void bindPageManagerFactory(PageManagerFactory pageManagerFactory) {
        this.pageManagerFactory = pageManagerFactory;
    }

    protected void unbindPageManagerFactory(PageManagerFactory pageManagerFactory) {
        if (this.pageManagerFactory == pageManagerFactory) {
            this.pageManagerFactory = null;
        }
    }

    protected void bindHtmlLibraryManager(HtmlLibraryManager htmlLibraryManager) {
        this.htmlLibraryManager = htmlLibraryManager;
    }

    protected void unbindHtmlLibraryManager(HtmlLibraryManager htmlLibraryManager) {
        if (this.htmlLibraryManager == htmlLibraryManager) {
            this.htmlLibraryManager = null;
        }
    }

    protected void bindGuideLocalizationService(GuideLocalizationService guideLocalizationService) {
        this.guideLocalizationService = guideLocalizationService;
    }

    protected void unbindGuideLocalizationService(GuideLocalizationService guideLocalizationService) {
        if (this.guideLocalizationService == guideLocalizationService) {
            this.guideLocalizationService = null;
        }
    }
}