ColumnCtlPostProcessor.java 5.86 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.text.Text
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  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.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.jcr.api.SlingRepository
 *  org.apache.sling.servlets.post.Modification
 *  org.apache.sling.servlets.post.ModificationType
 *  org.apache.sling.servlets.post.SlingPostProcessor
 *  org.osgi.service.component.ComponentContext
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.wcm.foundation.impl;

import com.day.cq.wcm.foundation.ParagraphSystem;
import com.day.text.Text;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
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.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.jcr.api.SlingRepository;
import org.apache.sling.servlets.post.Modification;
import org.apache.sling.servlets.post.ModificationType;
import org.apache.sling.servlets.post.SlingPostProcessor;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(metatype=0)
@Service(value={SlingPostProcessor.class})
public class ColumnCtlPostProcessor
implements SlingPostProcessor {
    private Logger log;
    private static final String COLCTL_POSTPROCESSOR_SERVICE = "colctl-postprocessor-service";
    @Reference
    private SlingRepository repository;
    private Session serviceSession;

    public ColumnCtlPostProcessor() {
        this.log = LoggerFactory.getLogger(this.getClass());
    }

    protected void activate(ComponentContext context) {
        try {
            this.serviceSession = this.repository.loginService("colctl-postprocessor-service", null);
        }
        catch (RepositoryException e) {
            this.log.error("unable to initialize service session.");
        }
    }

    protected void deactivate(ComponentContext componentContext) {
        if (this.serviceSession != null) {
            this.serviceSession.logout();
            this.serviceSession = null;
        }
    }

    public void process(SlingHttpServletRequest request, List<Modification> changes) throws Exception {
        if (this.serviceSession == null) {
            return;
        }
        Session session = (Session)request.getResourceResolver().adaptTo(Session.class);
        String prt = request.getParameter("parentResourceType");
        if (prt != null && prt.length() == 0) {
            prt = null;
        }
        HashMap<String, String> parsys = new HashMap<String, String>();
        block9 : for (Modification mod : changes) {
            switch (mod.getType()) {
                case DELETE: {
                    String colctrlProperty = null;
                    try {
                        String rt;
                        if (!this.serviceSession.nodeExists(mod.getSource()) || !(rt = this.serviceSession.getProperty(colctrlProperty = mod.getSource() + "/sling:resourceType").getString()).endsWith("/colctrl")) continue block9;
                        parsys.put(Text.getRelativeParent((String)mod.getSource(), (int)1), "");
                    }
                    catch (RepositoryException e) {}
                    break;
                }
                case ORDER: 
                case MOVE: 
                case COPY: {
                    break;
                }
                case MODIFY: 
                case CREATE: {
                    String path = mod.getSource();
                    if (!path.endsWith("/sling:resourceType")) break;
                    if (session.propertyExists(path)) {
                        String rt = session.getProperty(path).getString();
                        String parSysPath = Text.getRelativeParent((String)path, (int)2);
                        if (rt.endsWith("/colctrl")) {
                            parsys.put(parSysPath, Text.getRelativeParent((String)rt, (int)1));
                            break;
                        }
                        if (prt == null) continue block9;
                        try {
                            if (session.propertyExists(parSysPath + "/sling:resourceType")) continue block9;
                            parsys.put(parSysPath, prt);
                            prt = null;
                        }
                        catch (RepositoryException e) {}
                        break;
                    }
                    this.log.info("Could not find property {} in jcr session.", (Object)path);
                }
            }
        }
        for (String path : parsys.keySet()) {
            String rt;
            this.log.info("columns control modified - repairing parsys at {}", (Object)path);
            if (!session.propertyExists(path + "/sling:resourceType") && (rt = (String)parsys.get(path)).length() > 0) {
                session.getNode(path).setProperty("sling:resourceType", rt);
            }
            Resource res = request.getResourceResolver().getResource(path);
            ParagraphSystem p = new ParagraphSystem(res);
            p.repair();
        }
    }

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

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

}