StatusNode.java 3.18 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.commons.jcr.JcrUtil
 *  javax.jcr.Item
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  org.apache.sling.jcr.api.SlingRepository
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.compat.codeupgrade.impl;

import com.day.cq.commons.jcr.JcrUtil;
import java.util.Calendar;
import java.util.Date;
import javax.jcr.Item;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.sling.jcr.api.SlingRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class StatusNode {
    private final Logger log;
    private final String statusNodePath;
    private final String statusPropertyName;
    private final String statusPropertyPath;

    public StatusNode(String path, String propertyName) {
        this.log = LoggerFactory.getLogger(this.getClass());
        this.statusNodePath = path;
        this.statusPropertyName = propertyName;
        this.statusPropertyPath = path + "/" + propertyName;
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public void recordUpgrade(SlingRepository repository) throws RepositoryException {
        Session s = repository.loginService("compat-codeupgrade", null);
        try {
            Node n = null;
            if (s.itemExists(this.statusNodePath)) {
                Item i = s.getItem(this.statusNodePath);
                if (!i.isNode()) {
                    throw new IllegalStateException(i.getPath() + " is not a Node");
                }
                n = (Node)i;
            } else {
                n = JcrUtil.createPath((String)this.statusNodePath, (String)"sling:Folder", (Session)s);
            }
            Calendar now = Calendar.getInstance();
            n.setProperty(this.statusPropertyName, now);
            s.save();
            this.log.info("Upgrade recorded, {} set to {}", (Object)this.statusPropertyPath, (Object)now.getTime());
        }
        finally {
            s.logout();
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public boolean upgradeNeeded(SlingRepository repository) {
        Session s = null;
        boolean result = false;
        try {
            s = repository.loginService("compat-codeupgrade", null);
            boolean exists = s.itemExists(this.statusPropertyPath);
            if (this.log.isInfoEnabled()) {
                if (exists) {
                    this.log.info("{} exists, upgrade already executed", (Object)this.statusPropertyPath);
                } else {
                    this.log.info("{} does not exist, upgrade needed", (Object)this.statusPropertyPath);
                }
            }
            result = !exists;
        }
        catch (Exception e) {
            this.log.warn("RepositoryException in upgradeNeeded(" + this.statusPropertyPath + ")", (Throwable)e);
        }
        finally {
            s.logout();
        }
        return result;
    }

    public String getStatusPropertyPath() {
        return this.statusPropertyPath;
    }
}