Cq561SocialContentUpgrade.java 10.3 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.jcr.Node
 *  javax.jcr.NodeIterator
 *  javax.jcr.PathNotFoundException
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.compat.codeupgrade.impl.cq561;

import com.day.cq.compat.codeupgrade.internal.api.ProgressInfoProvider;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.PathNotFoundException;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Cq561SocialContentUpgrade
implements ProgressInfoProvider {
    private static final String SOCIAL_TALLY_COMPONENTS_RESPONSE = "social/tally/components/response";
    private static final String SOCIAL_TALLY_COMPONENT_PREFIX = "social/tally/components/";
    private static final String CQ_COMMENT_NODE_TYPE = "cq:Comment";
    private static final String TALLYCOUNT = "tallycount";
    private static final String TALLYRESPONSES = "tallyresponses";
    private static final String SLING_FOLDER = "sling:Folder";
    private final Logger log;
    private String progressInfo;
    private static String SLING_RESOURCE_TYPE = "sling:resourceType";
    private static String NT_UNSTRUCTURED_NODE_TYPE = "nt:unstructured";
    private static String COMMENT_CONTENT_NODE_TYPE = "cq:CommentContent";
    private static String PATH_UGC = "/content/usergenerated";
    String PROP_COMPONENT;

    public Cq561SocialContentUpgrade() {
        this.log = LoggerFactory.getLogger(this.getClass());
        this.progressInfo = "No info yet";
        this.PROP_COMPONENT = "commentsNode";
    }

    @Override
    public String getProgressInfo() {
        return this.progressInfo;
    }

    void setProgressInfo(String info) {
        this.progressInfo = info;
        this.log.info(this.progressInfo);
    }

    void doUpgrade(Session sess) throws Exception {
        this.setProgressInfo("Start upgrade.");
        Map<UPGRADE_TYPE, List<Node>> nodesToBeUpdatedByType = this.findNodesToBeUpdatedByType(sess);
        List<Node> commentNodes = nodesToBeUpdatedByType.get((Object)UPGRADE_TYPE.COMMENT);
        this.setProgressInfo("Upgrading comments.");
        if (!commentNodes.isEmpty()) {
            this.upgradeCommentsRootResourceType(sess, commentNodes);
        }
        List<Node> attachmentNodes = nodesToBeUpdatedByType.get((Object)UPGRADE_TYPE.ATTACHMENT);
        this.setProgressInfo("Upgrading attachments.");
        if (!attachmentNodes.isEmpty()) {
            this.upgradeAttachmentNodeType(sess, attachmentNodes);
        }
        List<Node> tallyNodes = nodesToBeUpdatedByType.get((Object)UPGRADE_TYPE.TALLY);
        this.setProgressInfo("Upgrading tally nodes.");
        if (!tallyNodes.isEmpty()) {
            this.upgradeTallyNodeType(sess, tallyNodes);
        }
        this.setProgressInfo("All done.");
    }

    private void upgradeTallyNodeType(Session sess, List<Node> tallyNodes) {
        for (Node tallyNode : tallyNodes) {
            try {
                this.setProgressInfo("upgrading tally node: " + (Object)tallyNode);
                tallyNode.setPrimaryType("sling:Folder");
                Cq561SocialContentUpgrade.save(sess);
            }
            catch (RepositoryException e) {
                try {
                    this.log.error("Failed to upgrade " + tallyNode.getPath(), (Throwable)e);
                    continue;
                }
                catch (RepositoryException e1) {
                    this.log.error("Failed to upgrade attachment", (Throwable)e);
                }
            }
        }
    }

    private void upgradeAttachmentNodeType(Session sess, List<Node> attachmentNodes) {
        for (Node attachmentFolder : attachmentNodes) {
            try {
                this.setProgressInfo("upgrading attachment node: " + (Object)attachmentFolder);
                attachmentFolder.setPrimaryType("sling:Folder");
                Cq561SocialContentUpgrade.save(sess);
            }
            catch (RepositoryException e) {
                try {
                    this.log.error("Failed to upgrade " + attachmentFolder.getPath(), (Throwable)e);
                    continue;
                }
                catch (RepositoryException e1) {
                    this.log.error("Failed to upgrade attachment", (Throwable)e);
                }
            }
        }
    }

    private Map<UPGRADE_TYPE, List<Node>> findNodesToBeUpdatedByType(Session sess) throws PathNotFoundException, RepositoryException {
        Node root = sess.getNode(PATH_UGC);
        ArrayList<Node> tallyNodes = new ArrayList<Node>(50);
        ArrayList<Node> attachmentNodes = new ArrayList<Node>(10);
        ArrayList<Node> commentNodes = new ArrayList<Node>(50);
        this.findNodesToBeUpdated(sess, root, commentNodes, attachmentNodes, tallyNodes);
        HashMap<UPGRADE_TYPE, List<Node>> nodesToBeUpdated = new HashMap<UPGRADE_TYPE, List<Node>>(3);
        nodesToBeUpdated.put(UPGRADE_TYPE.COMMENT, commentNodes);
        nodesToBeUpdated.put(UPGRADE_TYPE.ATTACHMENT, attachmentNodes);
        nodesToBeUpdated.put(UPGRADE_TYPE.TALLY, tallyNodes);
        return nodesToBeUpdated;
    }

    private void findNodesToBeUpdated(Session session, Node root, List<Node> commentNodes, List<Node> attachmentNodes, List<Node> tallyNodes) throws RepositoryException {
        NodeIterator iterator = root.getNodes();
        while (iterator.hasNext()) {
            Node child = (Node)iterator.next();
            boolean searchChildren = true;
            try {
                if (this.isCommentsRootNode(session, child)) {
                    commentNodes.add(child);
                }
                if (this.isTallyNode(session, child)) {
                    tallyNodes.add(child);
                }
                if (this.isAttachmentNode(session, child, root)) {
                    attachmentNodes.add(child);
                    searchChildren = false;
                }
                if (!searchChildren) continue;
                this.findNodesToBeUpdated(session, child, commentNodes, attachmentNodes, tallyNodes);
            }
            catch (RepositoryException e) {
                this.log.error("Failed to retrieve comment roots", (Throwable)e);
            }
        }
    }

    private boolean isAttachmentNode(Session session, Node node, Node parent) throws PathNotFoundException, RepositoryException {
        if (node != null && node.getName().equals("attachments") && parent.isNodeType("cq:Comment") && node.isNodeType(NT_UNSTRUCTURED_NODE_TYPE)) {
            return true;
        }
        return false;
    }

    private boolean isTallyNode(Session session, Node node) throws PathNotFoundException, RepositoryException {
        if (node == null || !node.isNodeType(NT_UNSTRUCTURED_NODE_TYPE)) {
            return false;
        }
        if (node.getName().equals("tallycount") || node.getName().equals("tallyresponses")) {
            return true;
        }
        if (node.hasProperty(SLING_RESOURCE_TYPE) && node.getProperty(SLING_RESOURCE_TYPE).getString().indexOf("social/tally/components/") == 0 && !"social/tally/components/response".equals(node.getProperty(SLING_RESOURCE_TYPE).getString())) {
            return true;
        }
        return false;
    }

    private boolean isCommentsRootNode(Session sess, Node node) throws RepositoryException {
        boolean retVal = false;
        if (node.hasProperty(this.PROP_COMPONENT) && node.isNodeType(NT_UNSTRUCTURED_NODE_TYPE)) {
            String component = node.getProperty(this.PROP_COMPONENT).getString();
            if (component.endsWith(".html")) {
                retVal = component.endsWith("comments.social.createcomment.html");
            } else {
                Node componentNode = sess.getNode(component);
                if (componentNode.hasProperty(SLING_RESOURCE_TYPE)) {
                    String resourceType = componentNode.getProperty(SLING_RESOURCE_TYPE).getString();
                    retVal = !resourceType.contains("collab");
                }
            }
        }
        return retVal;
    }

    private void upgradeCommentsRootResourceType(Session sess, List<Node> commentsRoots) {
        for (Node commentRoot : commentsRoots) {
            try {
                this.setProgressInfo("upgrade " + (Object)commentRoot);
                Node pageNode = commentRoot.getParent();
                String nodeName = commentRoot.getName();
                Cq561SocialContentUpgrade.renameNode(commentRoot, "old" + nodeName);
                Node tempNode = pageNode.addNode(nodeName, COMMENT_CONTENT_NODE_TYPE);
                this.moveChildren(tempNode, commentRoot);
                commentRoot.remove();
                Cq561SocialContentUpgrade.save(sess);
            }
            catch (RepositoryException e) {
                try {
                    this.log.error("Failed to upgrade " + commentRoot.getPath(), (Throwable)e);
                    continue;
                }
                catch (RepositoryException e1) {
                    this.log.error("Failed to upgrade comment", (Throwable)e);
                }
            }
        }
    }

    public static void save(Session session) throws RepositoryException, IllegalArgumentException {
        if (session == null) {
            throw new IllegalArgumentException("resolver must be adaptable to session");
        }
        try {
            session.save();
        }
        catch (RepositoryException e) {
            session.refresh(false);
            throw e;
        }
    }

    private static void renameNode(Node node, String newName) throws RepositoryException {
        node.getSession().move(node.getPath(), node.getParent().getPath() + "/" + newName);
    }

    private void moveChildren(Node dest, Node src) throws RepositoryException {
        Session session = src.getSession();
        NodeIterator iterator = src.getNodes();
        while (iterator.hasNext()) {
            Node child = (Node)iterator.next();
            session.move(child.getPath(), dest.getPath() + "/" + child.getName());
        }
    }

    private static enum UPGRADE_TYPE {
        COMMENT,
        ATTACHMENT,
        TALLY;
        

        private UPGRADE_TYPE() {
        }
    }

}