SyncTranslationState.java 16.5 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.aemds.guide.service.GuideLocalizationService
 *  com.adobe.cq.launches.api.LaunchManagerFactory
 *  com.adobe.cq.projects.api.Project
 *  com.adobe.cq.projects.api.ProjectFilter
 *  com.adobe.cq.projects.api.ProjectManager
 *  com.adobe.granite.translation.api.TranslationException
 *  com.adobe.granite.translation.api.TranslationManager
 *  com.adobe.granite.translation.api.xliff.TranslationXLIFFService
 *  com.adobe.granite.translation.core.TranslationCloudConfigUtil
 *  com.adobe.granite.ui.clientlibs.HtmlLibraryManager
 *  com.day.cq.wcm.api.LanguageManager
 *  com.day.cq.wcm.api.PageManagerFactory
 *  com.day.cq.wcm.contentsync.PageExporter
 *  javax.jcr.Node
 *  javax.jcr.PathNotFoundException
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  org.apache.felix.scr.annotations.Activate
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Deactivate
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.ReferenceCardinality
 *  org.apache.felix.scr.annotations.ReferencePolicy
 *  org.apache.felix.scr.annotations.ReferencePolicyOption
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ResourceResolverFactory
 *  org.apache.sling.commons.html.HtmlParser
 *  org.apache.sling.commons.scheduler.Job
 *  org.apache.sling.commons.scheduler.JobContext
 *  org.apache.sling.commons.scheduler.ScheduleOptions
 *  org.apache.sling.commons.scheduler.Scheduler
 *  org.apache.sling.jcr.api.SlingRepository
 *  org.osgi.service.component.ComponentContext
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.wcm.translation.impl.scheduler;

import com.adobe.aemds.guide.service.GuideLocalizationService;
import com.adobe.cq.launches.api.LaunchManagerFactory;
import com.adobe.cq.projects.api.Project;
import com.adobe.cq.projects.api.ProjectFilter;
import com.adobe.cq.projects.api.ProjectManager;
import com.adobe.cq.wcm.translation.impl.TranslationPlatformConfiguration;
import com.adobe.cq.wcm.translation.impl.TranslationPodImpl;
import com.adobe.cq.wcm.translation.impl.TranslationRuleConfigurationFile;
import com.adobe.cq.wcm.translation.impl.TranslationUtils;
import com.adobe.granite.translation.api.TranslationException;
import com.adobe.granite.translation.api.TranslationManager;
import com.adobe.granite.translation.api.xliff.TranslationXLIFFService;
import com.adobe.granite.translation.core.TranslationCloudConfigUtil;
import com.adobe.granite.ui.clientlibs.HtmlLibraryManager;
import com.day.cq.wcm.api.LanguageManager;
import com.day.cq.wcm.api.PageManagerFactory;
import com.day.cq.wcm.contentsync.PageExporter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import javax.jcr.Node;
import javax.jcr.PathNotFoundException;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.felix.scr.annotations.ReferencePolicyOption;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.commons.html.HtmlParser;
import org.apache.sling.commons.scheduler.Job;
import org.apache.sling.commons.scheduler.JobContext;
import org.apache.sling.commons.scheduler.ScheduleOptions;
import org.apache.sling.commons.scheduler.Scheduler;
import org.apache.sling.jcr.api.SlingRepository;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(name="Translation Sync Job")
public class SyncTranslationState
implements Job {
    private static long iSyncTimeout = 0;
    private static final Logger log = LoggerFactory.getLogger(SyncTranslationState.class);
    @Reference
    private SlingRepository referred_repository = null;
    @Reference
    private TranslationManager referred_translationManager = null;
    @Reference
    private PageManagerFactory refered_pageManagerFactory = null;
    @Reference
    private TranslationCloudConfigUtil referred_translationCloudConfigUtil = null;
    @Reference
    private ResourceResolverFactory referred_resolverFactory = null;
    @Reference
    private LanguageManager referred_languageManager = null;
    @Reference
    LaunchManagerFactory referred_launchesManagerFactory = null;
    @Reference
    private Scheduler scheduler;
    @Reference
    private HtmlLibraryManager referred_htmlLibraryManager = null;
    @Reference
    private PageExporter pageExporter = null;
    @Reference
    private HtmlParser htmlParser = null;
    @Reference
    private TranslationPlatformConfiguration translationPlatformConfiguration;
    @Reference(policy=ReferencePolicy.DYNAMIC, policyOption=ReferencePolicyOption.RELUCTANT, cardinality=ReferenceCardinality.OPTIONAL_UNARY)
    private TranslationXLIFFService xliffService = null;
    private HtmlLibraryManager m_htmlLibraryManager = null;
    private SlingRepository m_repository;
    private TranslationManager m_translationManager;
    private ResourceResolverFactory m_resolverFactory;
    TranslationCloudConfigUtil m_translationCloudConfigUtil;
    private PageManagerFactory m_pageManagerFactory;
    private LanguageManager m_languageManager;
    private LaunchManagerFactory m_launchesManagerFactory = null;

    @Activate
    private void setup(ComponentContext context) {
        try {
            String cronExpression = this.translationPlatformConfiguration.getCronExpressionForSync();
            iSyncTimeout = this.translationPlatformConfiguration.getSyncTimeout();
            ScheduleOptions scheduleOptions = this.scheduler.EXPR(cronExpression);
            boolean bUnScheduled = this.scheduler.unschedule(SyncTranslationState.class.getCanonicalName());
            log.info("Unscheduled old scheduled job ", (Object)bUnScheduled);
            scheduleOptions = scheduleOptions.name(SyncTranslationState.class.getCanonicalName()).onSingleInstanceOnly(true);
            if (!this.scheduler.schedule((Object)new SyncTranslationState(this.referred_repository, this.referred_resolverFactory, this.referred_translationManager, this.referred_translationCloudConfigUtil, this.refered_pageManagerFactory, this.referred_htmlLibraryManager), scheduleOptions)) {
                log.error("Error while scheduling the sync translation state job");
            }
        }
        catch (Exception ex) {
            log.error("Error while scheduling the sync translation state job", (Throwable)ex);
        }
    }

    public static long getSyncLockTimeout() {
        return iSyncTimeout;
    }

    public SyncTranslationState() {
    }

    public static String getRulesFilePath() {
        return "/etc/workflow/models/translation/translation_rules.xml";
    }

    public SyncTranslationState(SlingRepository referred_repository, ResourceResolverFactory referred_resolverFactory, TranslationManager referred_translationManager, TranslationCloudConfigUtil referred_translationCloudConfigUtil, PageManagerFactory referred_pageManagerFactory, HtmlLibraryManager referred_htmlLibraryManager) throws RepositoryException {
        this.m_repository = referred_repository;
        this.m_resolverFactory = referred_resolverFactory;
        this.m_translationManager = referred_translationManager;
        this.m_translationCloudConfigUtil = referred_translationCloudConfigUtil;
        this.m_pageManagerFactory = referred_pageManagerFactory;
        this.m_htmlLibraryManager = referred_htmlLibraryManager;
        this.m_languageManager = this.referred_languageManager;
        this.m_launchesManagerFactory = this.referred_launchesManagerFactory;
    }

    @Deactivate
    private void cleanup() {
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public void execute(JobContext arg) {
        log.info("Starting sync process now");
        ArrayList<Project> translationProjectArray = null;
        ResourceResolver resourceResolver = null;
        TranslationRuleConfigurationFile ruleFile = null;
        try {
            resourceResolver = this.m_resolverFactory.getServiceResourceResolver(Collections.singletonMap("sling.service.subservice", "translation-job"));
            Session serviceUserSession = (Session)resourceResolver.adaptTo(Session.class);
            ruleFile = new TranslationRuleConfigurationFile(resourceResolver, SyncTranslationState.getRulesFilePath(), null, null, this.m_translationManager, this.m_translationCloudConfigUtil, this.m_pageManagerFactory, this.m_htmlLibraryManager, null, this.m_languageManager, this.m_launchesManagerFactory, this.pageExporter, this.htmlParser);
            ruleFile.setXLIFFService(this.xliffService);
            ProjectManager pm = (ProjectManager)resourceResolver.adaptTo(ProjectManager.class);
            translationProjectArray = this.getTranslationProjectList(pm);
            for (Project translationProject : translationProjectArray) {
                log.trace("Project: {}", (Object)translationProject.getTitle());
                this.syncTranslationProject(translationProject, resourceResolver, serviceUserSession, ruleFile);
            }
        }
        catch (Exception e) {
            log.error("Error while getting translation project list", (Throwable)e);
        }
        finally {
            if (resourceResolver != null && resourceResolver.isLive()) {
                resourceResolver.close();
            }
            log.info("Sync process complete");
        }
    }

    private void syncTranslationProject(Project project, ResourceResolver resourceResolver, Session adminSession, TranslationRuleConfigurationFile ruleFile) {
        ArrayList<TranslationPodImpl> podList = null;
        try {
            podList = TranslationPodImpl.getTranslationPodList(resourceResolver, project, adminSession, this.m_translationManager);
        }
        catch (Exception e) {
            log.error("Error while getting the pod list", (Throwable)e);
        }
        if (podList != null) {
            for (TranslationPodImpl pod : podList) {
                try {
                    ruleFile.setLanguageSet(pod.getSourceLanguage(), pod.getDestinationLanguage());
                    log.trace("Job: {}", (Object)pod.getNode().getName());
                    this.syncTranslationPodNow(pod, ruleFile, adminSession);
                }
                catch (Exception e) {
                    log.error("Error while syncing pod ", (Throwable)e);
                }
            }
        }
    }

    private void syncTranslationPodNow(TranslationPodImpl pod, TranslationRuleConfigurationFile ruleFile, Session adminSession) throws TranslationException, RepositoryException, InterruptedException {
        pod.processPendingCancellationRequest(ruleFile);
        Random rand = new Random();
        int MAX_SLEEP = 300000;
        int iSleepBeforeSync = rand.nextInt(MAX_SLEEP);
        adminSession.save();
        Thread.sleep(iSleepBeforeSync);
        adminSession.refresh(false);
        pod.syncTranslationStateIfRequired(ruleFile, adminSession);
    }

    private ArrayList<Project> getTranslationProjectList(ProjectManager pm) throws PathNotFoundException, RepositoryException {
        ArrayList<Project> retVal = new ArrayList<Project>();
        Iterator projectList = pm.getProjects(null, 0, 0);
        if (projectList != null) {
            while (projectList.hasNext()) {
                Project project = (Project)projectList.next();
                if (!TranslationUtils.isTranslationProject(project)) continue;
                retVal.add(project);
            }
        }
        return retVal;
    }

    protected void bindReferred_repository(SlingRepository slingRepository) {
        this.referred_repository = slingRepository;
    }

    protected void unbindReferred_repository(SlingRepository slingRepository) {
        if (this.referred_repository == slingRepository) {
            this.referred_repository = null;
        }
    }

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

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

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

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

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

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

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

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

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

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

    protected void bindReferred_launchesManagerFactory(LaunchManagerFactory launchManagerFactory) {
        this.referred_launchesManagerFactory = launchManagerFactory;
    }

    protected void unbindReferred_launchesManagerFactory(LaunchManagerFactory launchManagerFactory) {
        if (this.referred_launchesManagerFactory == launchManagerFactory) {
            this.referred_launchesManagerFactory = null;
        }
    }

    protected void bindScheduler(Scheduler scheduler) {
        this.scheduler = scheduler;
    }

    protected void unbindScheduler(Scheduler scheduler) {
        if (this.scheduler == scheduler) {
            this.scheduler = null;
        }
    }

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

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

    protected void bindPageExporter(PageExporter pageExporter) {
        this.pageExporter = pageExporter;
    }

    protected void unbindPageExporter(PageExporter pageExporter) {
        if (this.pageExporter == pageExporter) {
            this.pageExporter = null;
        }
    }

    protected void bindHtmlParser(HtmlParser htmlParser) {
        this.htmlParser = htmlParser;
    }

    protected void unbindHtmlParser(HtmlParser htmlParser) {
        if (this.htmlParser == htmlParser) {
            this.htmlParser = null;
        }
    }

    protected void bindTranslationPlatformConfiguration(TranslationPlatformConfiguration translationPlatformConfiguration) {
        this.translationPlatformConfiguration = translationPlatformConfiguration;
    }

    protected void unbindTranslationPlatformConfiguration(TranslationPlatformConfiguration translationPlatformConfiguration) {
        if (this.translationPlatformConfiguration == translationPlatformConfiguration) {
            this.translationPlatformConfiguration = null;
        }
    }

    protected void bindXliffService(TranslationXLIFFService translationXLIFFService) {
        this.xliffService = translationXLIFFService;
    }

    protected void unbindXliffService(TranslationXLIFFService translationXLIFFService) {
        if (this.xliffService == translationXLIFFService) {
            this.xliffService = null;
        }
    }
}