DTMPostProcessor.java 6.91 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.workflow.WorkflowService
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.resource.PersistenceException
 *  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.apache.sling.commons.scheduler.Scheduler
 *  org.apache.sling.servlets.post.Modification
 *  org.apache.sling.servlets.post.ModificationType
 *  org.apache.sling.servlets.post.SlingPostProcessor
 */
package com.adobe.cq.dtm.impl;

import com.adobe.cq.dtm.impl.constants.DTMServerType;
import com.adobe.cq.dtm.impl.util.DTMConfigurationUtil;
import com.day.cq.workflow.WorkflowService;
import java.util.Iterator;
import java.util.List;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.PersistenceException;
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.apache.sling.commons.scheduler.Scheduler;
import org.apache.sling.servlets.post.Modification;
import org.apache.sling.servlets.post.ModificationType;
import org.apache.sling.servlets.post.SlingPostProcessor;

@Component
@Service
public class DTMPostProcessor
implements SlingPostProcessor {
    @Reference
    private ResourceResolverFactory rrf = null;
    @Reference
    private Scheduler scheduler = null;
    @Reference
    private WorkflowService workflowService = null;

    public void process(SlingHttpServletRequest request, List<Modification> changes) throws Exception {
        Resource currentResource = request.getResource();
        ResourceResolver resourceResolver = request.getResourceResolver();
        if (currentResource.getPath().matches("/etc/cloudservices/dynamictagmanagement/[^/]+/jcr:content")) {
            String dtmConfigPath = currentResource.getParent().getPath();
            boolean isStagingPollingPresent = false;
            boolean isProductionPollingPresent = false;
            boolean isStagingUseSelfHostPresent = false;
            boolean isProductionUseSelfHostPresent = false;
            for (Modification change : changes) {
                if (!ModificationType.MODIFY.equals((Object)change.getType())) continue;
                if (change.getSource().endsWith((Object)((Object)DTMServerType.STAGING) + "PollingEnabler")) {
                    isStagingPollingPresent = true;
                    DTMConfigurationUtil.scheduleOrUnscheduleDTMJob(this.rrf, this.workflowService, this.scheduler, currentResource, DTMServerType.STAGING);
                    this.createOrRemoveDTMHookNode(resourceResolver, currentResource, DTMServerType.STAGING, false);
                    continue;
                }
                if (change.getSource().endsWith((Object)((Object)DTMServerType.PRODUCTION) + "PollingEnabler")) {
                    isProductionPollingPresent = true;
                    DTMConfigurationUtil.scheduleOrUnscheduleDTMJob(this.rrf, this.workflowService, this.scheduler, currentResource, DTMServerType.PRODUCTION);
                    this.createOrRemoveDTMHookNode(resourceResolver, currentResource, DTMServerType.PRODUCTION, false);
                    continue;
                }
                if (change.getSource().endsWith((Object)((Object)DTMServerType.STAGING) + "UseSelfHosting")) {
                    isStagingUseSelfHostPresent = true;
                    continue;
                }
                if (!change.getSource().endsWith((Object)((Object)DTMServerType.PRODUCTION) + "UseSelfHosting")) continue;
                isProductionUseSelfHostPresent = true;
            }
            if (!isStagingPollingPresent) {
                this.scheduler.unschedule(DTMConfigurationUtil.getDTMJobName(dtmConfigPath, DTMServerType.STAGING));
                if (isStagingUseSelfHostPresent) {
                    this.createOrRemoveDTMHookNode(resourceResolver, currentResource, DTMServerType.STAGING, true);
                } else {
                    this.createOrRemoveDTMHookNode(resourceResolver, currentResource, DTMServerType.STAGING, false);
                }
            }
            if (!isProductionPollingPresent) {
                this.scheduler.unschedule(DTMConfigurationUtil.getDTMJobName(dtmConfigPath, DTMServerType.PRODUCTION));
                if (isProductionUseSelfHostPresent) {
                    this.createOrRemoveDTMHookNode(resourceResolver, currentResource, DTMServerType.PRODUCTION, true);
                } else {
                    this.createOrRemoveDTMHookNode(resourceResolver, currentResource, DTMServerType.STAGING, false);
                }
            }
        }
    }

    private void createOrRemoveDTMHookNode(ResourceResolver resourceResolver, Resource currentResource, DTMServerType dtmServerType, boolean isCreate) throws PersistenceException {
        String dtmCloudConfigName = currentResource.getParent().getName();
        String dtmHookPath = "/etc/dtm-hook/" + dtmCloudConfigName + "/" + (Object)((Object)dtmServerType);
        if (isCreate) {
            ResourceUtil.getOrCreateResource((ResourceResolver)resourceResolver, (String)dtmHookPath, (String)"etc/dtm/hook", (String)"nt:unstructured", (boolean)true);
        } else {
            Resource dtmHook = resourceResolver.getResource(dtmHookPath);
            if (dtmHook != null) {
                resourceResolver.delete(dtmHook);
                Resource dtmHookParent = dtmHook.getParent();
                if (!dtmHookParent.getChildren().iterator().hasNext()) {
                    resourceResolver.delete(dtmHookParent);
                }
                resourceResolver.commit();
            }
        }
    }

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

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

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

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

    protected void bindWorkflowService(WorkflowService workflowService) {
        this.workflowService = workflowService;
    }

    protected void unbindWorkflowService(WorkflowService workflowService) {
        if (this.workflowService == workflowService) {
            this.workflowService = null;
        }
    }
}