NodeBackupImpl.java 2.25 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.commons.jcr.JcrUtil
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  javax.jcr.Value
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.compat.codeupgrade.impl;

import com.day.cq.commons.jcr.JcrUtil;
import com.day.cq.compat.codeupgrade.internal.api.NodeBackup;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Value;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

class NodeBackupImpl
implements NodeBackup {
    private final Logger log;
    private final String backupRootPath;
    private long fakeTimestamp;
    private final SimpleDateFormat pathFormat;
    public static final String PATH_PROPERTY_NAME = "propertyPath";
    public static final String OLD_VALUE_PROPERTY_NAME = "oldValue";
    public static final String FOLDER_TYPE = "sling:Folder";

    NodeBackupImpl(String backupRootPath) {
        this.log = LoggerFactory.getLogger(this.getClass());
        this.backupRootPath = backupRootPath;
        this.fakeTimestamp = System.currentTimeMillis();
        this.pathFormat = new SimpleDateFormat("yyyy/MM/dd/HHmmss/SSS");
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    @Override
    public void backup(Property p) throws RepositoryException {
        String path = null;
        NodeBackupImpl nodeBackupImpl = this;
        synchronized (nodeBackupImpl) {
            ++this.fakeTimestamp;
            path = this.backupRootPath + "/" + this.pathFormat.format(new Date(this.fakeTimestamp));
        }
        Node backup = JcrUtil.createPath((String)path, (String)"sling:Folder", (Session)p.getSession());
        backup.setProperty("propertyPath", p.getPath());
        Value v = p.getValue();
        if (v.getType() == 2) {
            backup.setProperty("oldValue", v.getStream());
        } else {
            backup.setProperty("oldValue", v.getString());
        }
        this.log.debug("Property {} backed up under {}", (Object)p.getPath(), (Object)backup.getPath());
    }
}