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

import com.day.cq.compat.codeupgrade.internal.api.ProgressInfoProvider;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Cq62PrimaryComponentsUpgrade
implements ProgressInfoProvider {
    private final String Primary_Path = "/foundation/components/primary";
    private final String Root_Path = "/apps";
    private final Logger log;
    private String progressInfo;

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

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

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

    void doUpgrade(Session session) {
        this.setProgressInfo("Transfering of Resource Started");
        this.transferResources(session);
        this.setProgressInfo("Transfering of Resource Completed");
    }

    private void transferResources(Session session) {
        try {
            this.moveNodes(session, "/apps/foundation/components/primary", "/apps");
            this.save(session);
        }
        catch (RepositoryException e) {
            this.log.error("Error while Transferring Resources ", (Throwable)e);
        }
    }

    private void moveNodes(Session session, String sourcePath, String TargetPath) throws RepositoryException {
        if (session.nodeExists(sourcePath)) {
            Node node = session.getNode(sourcePath);
            NodeIterator itr = node.getNodes();
            while (itr.hasNext()) {
                Node nodeTomove = (Node)itr.next();
                String absPath = nodeTomove.getPath().replace(sourcePath, "");
                if (session.nodeExists(TargetPath + absPath)) {
                    this.moveNodes(session, nodeTomove.getPath(), TargetPath + absPath);
                    session.removeItem(nodeTomove.getPath());
                    continue;
                }
                session.move(nodeTomove.getPath(), TargetPath + absPath);
            }
        }
    }

    private void save(Session session) throws RepositoryException {
        if (session.hasPendingChanges()) {
            session.save();
        }
    }
}