Cq62MobileContentUpgrade.java 6.15 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.jcr.Node
 *  javax.jcr.NodeIterator
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  javax.jcr.Workspace
 *  javax.jcr.nodetype.NodeType
 *  javax.jcr.query.Query
 *  javax.jcr.query.QueryManager
 *  javax.jcr.query.QueryResult
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.compat.codeupgrade.impl.cq62;

import com.day.cq.compat.codeupgrade.internal.api.ProgressInfoProvider;
import java.util.HashSet;
import java.util.Set;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Workspace;
import javax.jcr.nodetype.NodeType;
import javax.jcr.query.Query;
import javax.jcr.query.QueryManager;
import javax.jcr.query.QueryResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Cq62MobileContentUpgrade
implements ProgressInfoProvider {
    static final String QUERY_MOBILE_APPS = "SELECT * FROM [nt:base] AS s WHERE ISDESCENDANTNODE([/content]) AND (s.[sling:resourceType] = 'mobileapps/core/components/group' OR s.[pge-type] = 'app-group') ";
    public static final String MIXIN_CQ_MOBILE_APP = "cq:MobileApp";
    private static final String PN_SLING_RESOURCE_TYPE = "sling:resourceType";
    private static final String RT_INSTANCE_PG_OLD = "mobileapps/components/phonegap/instance";
    private static final String RT_INSTANCE_PG = "mobileapps/phonegap/components/instance";
    private static Set<String> pathList = new HashSet<String>(2);
    static final String QUERY_INSTANCE_RESOURCES = "SELECT * FROM [nt:base] AS s WHERE ISDESCENDANTNODE('%s') AND s.[sling:resourceType] = 'mobileapps/components/phonegap/instance'";
    private final Logger log;
    private String progressInfo;

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

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

    void doUpgrade(Session session) {
        this.setProgressInfo("Migrating mobile app resources");
        int num = this.upgradeMobileResources(session);
        this.setProgressInfo(String.format("%s mobile app resources migrated.", num));
        this.setProgressInfo("Migrating PhoneGap resources");
        num = this.upgradePhoneGapResources(session);
        this.setProgressInfo(String.format("%s PhoneGap resources migrated.", num));
    }

    private int upgradeMobileResources(Session session) {
        int i = 0;
        try {
            QueryManager qm = session.getWorkspace().getQueryManager();
            Query q = qm.createQuery("SELECT * FROM [nt:base] AS s WHERE ISDESCENDANTNODE([/content]) AND (s.[sling:resourceType] = 'mobileapps/core/components/group' OR s.[pge-type] = 'app-group') ", "JCR-SQL2");
            NodeIterator groupNodes = q.execute().getNodes();
            while (groupNodes.hasNext()) {
                Node appNode = groupNodes.nextNode();
                try {
                    boolean isDirty = this.upgradeApp(appNode);
                    if (!isDirty) continue;
                    this.save(session);
                    ++i;
                }
                catch (Exception e) {
                    this.log.error(String.format("Cannot update app at %s.", appNode.getPath()), (Throwable)e);
                }
            }
        }
        catch (Exception e) {
            this.log.error("Unable to query repository for mobile app resources", (Throwable)e);
        }
        return i;
    }

    private int upgradePhoneGapResources(Session session) {
        int i = 0;
        try {
            QueryManager qm = session.getWorkspace().getQueryManager();
            for (String path : pathList) {
                String query = String.format("SELECT * FROM [nt:base] AS s WHERE ISDESCENDANTNODE('%s') AND s.[sling:resourceType] = 'mobileapps/components/phonegap/instance'", path);
                Query q = qm.createQuery(query, "JCR-SQL2");
                NodeIterator targetNodes = q.execute().getNodes();
                while (targetNodes.hasNext()) {
                    Node targetNode = targetNodes.nextNode();
                    try {
                        boolean isDirty = this.upgradeType(targetNode);
                        if (!isDirty) continue;
                        this.save(session);
                        ++i;
                    }
                    catch (Exception e) {
                        this.log.error(String.format("Cannot update node of type %s at %s.", "mobileapps/components/phonegap/instance", targetNode.getPath()), (Throwable)e);
                    }
                }
            }
        }
        catch (Exception e) {
            this.log.error(String.format("Cannot query repository for resources of type %s", "mobileapps/components/phonegap/instance"), (Throwable)e);
        }
        return i;
    }

    private boolean upgradeApp(Node appNode) throws RepositoryException {
        boolean hasChanged = this.addMixin(appNode);
        return hasChanged;
    }

    private boolean addMixin(Node groupNode) throws RepositoryException {
        NodeType[] mixinNodeTypes;
        boolean hasMixin = false;
        Node parent = groupNode.getParent();
        for (NodeType mixin : mixinNodeTypes = parent.getMixinNodeTypes()) {
            if (!mixin.isNodeType("cq:MobileApp")) continue;
            hasMixin = true;
            break;
        }
        if (!hasMixin) {
            parent.addMixin("cq:MobileApp");
        }
        return !hasMixin;
    }

    private boolean upgradeType(Node node) throws RepositoryException {
        if (node.hasProperty("sling:resourceType")) {
            node.setProperty("sling:resourceType", "mobileapps/phonegap/components/instance");
            return true;
        }
        return false;
    }

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

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

    static {
        pathList.add("/content");
        pathList.add("/apps");
    }
}