Cq62TargetContentUpgrade.java 3.95 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.Value
 *  javax.jcr.Workspace
 *  javax.jcr.query.Query
 *  javax.jcr.query.QueryManager
 *  javax.jcr.query.QueryResult
 *  org.apache.commons.lang.StringUtils
 *  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.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Value;
import javax.jcr.Workspace;
import javax.jcr.query.Query;
import javax.jcr.query.QueryManager;
import javax.jcr.query.QueryResult;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Cq62TargetContentUpgrade
implements ProgressInfoProvider {
    private static final String PN_LOCATION = "location";
    static final String QUERY_TARGET_RESOURCES = "SELECT * FROM [nt:base] AS s WHERE ISDESCENDANTNODE([/content]) AND (s.[sling:resourceType] = 'cq/personalization/components/target' OR s.[sling:resourceType] = 'mobileapps/components/target') AND (s.[location] IS NULL OR s.[location] = '')";
    private static final String RT_TARGET = "cq/personalization/components/target";
    private final Logger log;
    private String progressInfo;

    public Cq62TargetContentUpgrade() {
        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("Migrating target resources");
        int num = this.upgradeTargetResources(session);
        this.setProgressInfo(String.format("%s target resources migrated.", num));
    }

    private int upgradeTargetResources(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] = 'cq/personalization/components/target' OR s.[sling:resourceType] = 'mobileapps/components/target') AND (s.[location] IS NULL OR s.[location] = '')", "JCR-SQL2");
            NodeIterator targetNodes = q.execute().getNodes();
            while (targetNodes.hasNext()) {
                Node targetNode = targetNodes.nextNode();
                try {
                    this.upgradeNode(targetNode);
                    this.save(session);
                    ++i;
                }
                catch (Exception e) {
                    this.log.error(String.format("Cannot update node of type %s at %s.", "cq/personalization/components/target", targetNode.getPath()), (Throwable)e);
                }
            }
        }
        catch (Exception e) {
            this.log.error(String.format("Cannot query repository for resources of type %s", "cq/personalization/components/target"), (Throwable)e);
        }
        return i;
    }

    private void upgradeNode(Node node) throws RepositoryException {
        String locationValue = null;
        if (node.hasProperty("location")) {
            Property location = node.getProperty("location");
            locationValue = !location.isMultiple() ? location.getString() : location.getValues()[0].getString();
        }
        if (StringUtils.isEmpty((String)locationValue)) {
            this.setProgressInfo("Setting property 'location' of " + node.getPath());
            node.setProperty("location", node.getPath());
        }
    }

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