LiveRelationshipEditingUtil.java 9.71 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.wcm.api.Page
 *  com.day.cq.wcm.api.PageManager
 *  com.day.cq.wcm.api.WCMException
 *  com.day.cq.wcm.msm.api.LiveCopy
 *  com.day.cq.wcm.msm.api.LiveRelationship
 *  com.day.cq.wcm.msm.api.LiveRelationshipManager
 *  com.day.cq.wcm.msm.api.LiveStatus
 *  com.day.cq.wcm.msm.api.RolloutConfig
 *  javax.jcr.Node
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  javax.jcr.nodetype.NodeType
 *  org.apache.commons.lang.StringUtils
 *  org.apache.jackrabbit.util.Text
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.resource.ModifiableValueMap
 *  org.apache.sling.api.resource.PersistenceException
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceMetadata
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ResourceUtil
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.wcm.msm.impl;

import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.api.WCMException;
import com.day.cq.wcm.msm.api.LiveCopy;
import com.day.cq.wcm.msm.api.LiveRelationship;
import com.day.cq.wcm.msm.api.LiveRelationshipManager;
import com.day.cq.wcm.msm.api.LiveStatus;
import com.day.cq.wcm.msm.api.RolloutConfig;
import com.day.cq.wcm.msm.impl.LiveCopyManagerImpl;
import com.day.cq.wcm.msm.impl.Utils;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.nodetype.NodeType;
import org.apache.commons.lang.StringUtils;
import org.apache.jackrabbit.util.Text;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.ModifiableValueMap;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceMetadata;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LiveRelationshipEditingUtil {
    private static final String DEEP_CANCEL_PARAM = "msm:status/msm:isCancelledForChildren";
    private static Logger log = LoggerFactory.getLogger(LiveRelationshipEditingUtil.class);

    private LiveRelationshipEditingUtil() {
    }

    public static Resource createResource(LiveRelationship relationship, Resource resource, LiveRelationshipManager relationshipManager) throws WCMException, RepositoryException {
        Node parent;
        ResourceResolver resolver = resource.getResourceResolver();
        Resource targetParent = resolver.getResource(Text.getRelativeParent((String)resource.getPath(), (int)1));
        if (!ResourceUtil.isSyntheticResource((Resource)targetParent) && (parent = (Node)targetParent.adaptTo(Node.class)) != null) {
            Session session = parent.getSession();
            LiveRelationship parentShip = relationshipManager.getLiveRelationship(targetParent, false);
            if (parentShip != null) {
                Node target;
                String name = resource.getName();
                if (!StringUtils.isEmpty((String)resource.getResourceMetadata().getResolutionPathInfo())) {
                    name = name.substring(0, name.indexOf(resource.getResourceMetadata().getResolutionPathInfo()));
                }
                if (session.nodeExists(relationship.getSourcePath())) {
                    String nt = session.getNode(relationship.getSourcePath()).getPrimaryNodeType().getName();
                    target = parent.addNode(name, nt);
                } else {
                    target = parent.isNodeType("cq:LiveSync") ? parent.addNode(name, "nt:unstructured") : parent.addNode(name);
                }
                return resolver.getResource(target.getPath());
            }
        }
        return null;
    }

    public static void processCancel(SlingHttpServletRequest request, LiveRelationshipManager relationshipManager) throws WCMException, RepositoryException {
        Resource resource = request.getResource();
        ResourceResolver resolver = resource.getResourceResolver();
        LiveRelationship currentRelation = relationshipManager.getLiveRelationship(resource, true);
        if (currentRelation == null) {
            return;
        }
        boolean isCancelled = currentRelation.getStatus().isCancelled();
        String cancelParamValue = request.getParameter("msm:status/msm:isCancelled");
        boolean requestCancel = Boolean.valueOf(cancelParamValue);
        boolean requestCancelDeep = Boolean.valueOf(request.getParameter("msm:status/msm:isCancelledForChildren"));
        if (requestCancel != isCancelled) {
            if (requestCancel) {
                if (ResourceUtil.isNonExistingResource((Resource)resource) && (resource = LiveRelationshipEditingUtil.createResource(currentRelation, resource, relationshipManager)) != null) {
                    currentRelation = relationshipManager.getLiveRelationship(resource, false);
                }
                if (currentRelation != null && currentRelation.getStatus().isTargetExisting()) {
                    relationshipManager.cancelRelationship(resolver, currentRelation, requestCancelDeep, true);
                }
            } else {
                relationshipManager.reenableRelationship(resolver, currentRelation, false);
                if (!currentRelation.getStatus().isPage()) {
                    LiveRelationship pageRelation;
                    Date compRollout;
                    Page page;
                    PageManager pm = (PageManager)resolver.adaptTo(PageManager.class);
                    Page page2 = page = pm == null ? null : pm.getContainingPage(resource);
                    if (page != null && page.hasContent() && (pageRelation = relationshipManager.getLiveRelationship(page.getContentResource(), false)).getStatus().getLastRolledOut() != null && ((compRollout = currentRelation.getStatus().getLastRolledOut()) == null || compRollout.before(pageRelation.getStatus().getLastRolledOut()))) {
                        ModifiableValueMap contentProp = (ModifiableValueMap)page.getContentResource().adaptTo(ModifiableValueMap.class);
                        if (compRollout == null) {
                            contentProp.remove((Object)"cq:lastRolledout");
                        } else {
                            Calendar newVal = Calendar.getInstance();
                            newVal.setTime(compRollout);
                            contentProp.put((Object)"cq:lastRolledout", (Object)newVal);
                        }
                    }
                }
                ((Session)resolver.adaptTo(Session.class)).save();
            }
        } else if (isCancelled && requestCancelDeep != currentRelation.getStatus().isCancelledForChildren()) {
            relationshipManager.cancelRelationship(resolver, currentRelation, requestCancelDeep, true);
        }
    }

    public static /* varargs */ void editLiveCopy(ResourceResolver resolver, LiveCopyManagerImpl lcManager, LiveRelationshipManager relationshipManager, LiveRelationship relationship, boolean setDeep, String ... rolloutConfigs) throws RepositoryException, WCMException {
        Boolean deepToSave;
        Boolean bl = deepToSave = setDeep == relationship.getLiveCopy().isDeep() ? null : Boolean.valueOf(setDeep);
        if (deepToSave != null || rolloutConfigs != null) {
            LiveCopyManagerImpl.LiveCopyImpl lc;
            log.debug("LiveCopy at {} changed: try to update", (Object)relationship.getLiveCopy().getPath());
            String pagePath = Utils.getPagePath(relationship.getTargetPath());
            if (relationship.getLiveCopy().getPath().equals(pagePath)) {
                log.debug("Edit local LiveCopy");
                lc = lcManager.get(relationship.getLiveCopy().getPath());
                if (deepToSave != null) {
                    lc.setDeep(deepToSave);
                    if (!deepToSave.booleanValue()) {
                        Resource resource = resolver.getResource(lc.getPath());
                        Page page = (Page)resource.adaptTo(Page.class);
                        if (page != null) {
                            Iterator children = page.listChildren();
                            while (children.hasNext()) {
                                relationshipManager.endRelationship((Resource)((Page)children.next()).adaptTo(Resource.class), false);
                            }
                        } else {
                            for (Resource child : resource.getChildren()) {
                                relationshipManager.endRelationship(child, false);
                            }
                        }
                    }
                }
                if (rolloutConfigs != null) {
                    lc.setRolloutConfigs(rolloutConfigs);
                }
            } else {
                log.debug("Start new LiveCopy to apply changes in settings at {}", (Object)pagePath);
                lc = lcManager.create(Utils.getPagePath(relationship.getTargetPath()), Utils.getPagePath(relationship.getSourcePath()), setDeep, new RolloutConfig[0]);
                if (rolloutConfigs != null) {
                    lc.setRolloutConfigs(rolloutConfigs);
                }
            }
            try {
                resolver.commit();
                lcManager.get(lc.getPath());
                log.debug("Updated LiveCopy, LiveCopy {}", (Object)pagePath);
            }
            catch (PersistenceException e) {
                throw new WCMException("Failed to commit changes to Configuration:", (Throwable)e);
            }
        }
    }

    public static void updateRolloutDate(ResourceResolver resolver, LiveRelationship liveRelationship) {
    }
}