UpgradeDamAssetContent.java 2.54 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.jcr.Node
 *  javax.jcr.NodeIterator
 *  javax.jcr.Property
 *  javax.jcr.Session
 *  javax.jcr.Workspace
 *  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.cq53;

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

class UpgradeDamAssetContent
implements ProgressInfoProvider {
    private final Logger log;
    private String progressInfo;
    public static final String JCR_LAST_MODIFIED = "jcr:lastModified";

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

    void doUpgrade(Session s) throws Exception {
        String nodetype = "dam:AssetContent";
        Calendar now = Calendar.getInstance();
        this.log.info("Adding jcr:lastModified property to dam:AssetContent nodes where it's missing, using value " + now.getTime());
        int totalNodes = 0;
        int changedNodes = 0;
        String statement = "select * from [dam:AssetContent]";
        Query q = s.getWorkspace().getQueryManager().createQuery("select * from [dam:AssetContent]", "JCR-SQL2");
        NodeIterator it = q.execute().getNodes();
        while (it.hasNext()) {
            this.progressInfo = "" + totalNodes + " nodes checked, " + changedNodes + " nodes modified";
            ++totalNodes;
            Node content = it.nextNode();
            if (!content.hasProperty("jcr:lastModified")) {
                ++changedNodes;
                content.setProperty("jcr:lastModified", now);
                this.log.info("Adding {} property to node {}", (Object)"jcr:lastModified", (Object)content.getPath());
            }
            if (changedNodes % 100 == 0) {
                s.save();
            }
            s.save();
        }
        this.progressInfo = "All done - " + totalNodes + " nodes checked, " + changedNodes + " nodes modified";
    }

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

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