RevisionImpl.java 6.3 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.wcm.api.Revision
 *  javax.jcr.Item
 *  javax.jcr.Node
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  javax.jcr.version.Version
 *  javax.jcr.version.VersionHistory
 *  org.apache.sling.api.resource.ValueMap
 *  org.apache.sling.jcr.resource.JcrPropertyMap
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.wcm.core.impl;

import com.day.cq.wcm.api.Revision;
import java.util.Calendar;
import javax.jcr.Item;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.version.Version;
import javax.jcr.version.VersionHistory;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.jcr.resource.JcrPropertyMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RevisionImpl
implements Revision {
    private static final Logger log = LoggerFactory.getLogger(RevisionImpl.class);
    private final Version version;
    private final String id;
    private final String label;
    private final String existingPath;
    private JcrPropertyMap contentProps;

    public RevisionImpl(Version version) throws RepositoryException {
        this(version, RevisionImpl.getPagePath(version));
    }

    private static String getPagePath(Version version) {
        try {
            String uuid = version.getContainingHistory().getVersionableIdentifier();
            Node existing = version.getSession().getNodeByIdentifier(uuid);
            return existing.getParent().getPath();
        }
        catch (RepositoryException e) {
            log.warn("Error while evaluating 'isBaseVersion'", (Throwable)e);
            return null;
        }
    }

    public RevisionImpl(Version version, String existingPath) throws RepositoryException {
        this.version = version;
        this.id = version.getIdentifier();
        this.existingPath = existingPath;
        String[] labels = version.getContainingHistory().getVersionLabels(version);
        if (labels == null || labels.length == 0) {
            this.label = version.getName();
        } else {
            StringBuilder buf = new StringBuilder(labels[0]);
            for (int i = 1; i < labels.length; ++i) {
                buf.append(", ");
                buf.append(labels[i]);
            }
            this.label = buf.toString();
        }
    }

    public Version getVersion() {
        return this.version;
    }

    public ValueMap getProperties() {
        if (this.contentProps == null) {
            Node contentNode = null;
            try {
                if (this.version.hasNode("jcr:frozenNode")) {
                    contentNode = this.version.getNode("jcr:frozenNode");
                }
            }
            catch (RepositoryException e) {
                log.warn("Repository error while accessing content.");
            }
            this.contentProps = new JcrPropertyMap(contentNode);
        }
        return this.contentProps;
    }

    public ValueMap getProperties(String relPath) {
        if (relPath.startsWith("/")) {
            throw new IllegalArgumentException("Absolute paths not allowed");
        }
        relPath = "jcr:frozenNode/" + relPath;
        try {
            if (this.version.hasNode(relPath)) {
                return new JcrPropertyMap(this.version.getNode(relPath));
            }
        }
        catch (RepositoryException e) {
            log.warn("Repository error while accessing content.");
        }
        return null;
    }

    public String getId() {
        return this.id;
    }

    public String getLabel() {
        return this.label;
    }

    public boolean isDeleted() {
        return this.existingPath == null;
    }

    public String getExistingPagePath() {
        return this.existingPath;
    }

    public boolean isBaseVersion() {
        if (this.existingPath == null) {
            return false;
        }
        try {
            String uuid = this.version.getContainingHistory().getVersionableIdentifier();
            Node existing = this.version.getSession().getNodeByIdentifier(uuid);
            return existing.getBaseVersion().isSame((Item)this.version);
        }
        catch (RepositoryException e) {
            log.warn("Error while evaluating 'isBaseVersion'", (Throwable)e);
            return false;
        }
    }

    public String getName() {
        return (String)this.getProperties().get("cq:name", String.class);
    }

    public String getParentPath() {
        return (String)this.getProperties().get("cq:parentPath", String.class);
    }

    public String getTitle() {
        return (String)this.getProperties().get("jcr:title", String.class);
    }

    public String getDescription() {
        return (String)this.getProperties().get("jcr:description", String.class);
    }

    public String getPageTitle() {
        return (String)this.getProperties().get("pageTitle", String.class);
    }

    public String getNavigationTitle() {
        return (String)this.getProperties().get("navTitle", String.class);
    }

    public boolean isHideInNav() {
        return (Boolean)this.getProperties().get("hideInNav", (Object)Boolean.FALSE);
    }

    public boolean hasContent() {
        try {
            return this.version.hasNode("jcr:frozenNode") && !this.getName().startsWith("jcr:");
        }
        catch (RepositoryException e) {
            log.warn("Repository error while accessing content.");
            return false;
        }
    }

    public String getVanityUrl() {
        return (String)this.getProperties().get("vanityUrl", this.getProperties().get("sling:vanityPath", String.class));
    }

    public Calendar getCreated() {
        try {
            return this.version.getCreated();
        }
        catch (RepositoryException e) {
            log.warn("Unable to access created date.");
            return null;
        }
    }

    public String getComment() {
        return (String)this.getProperties().get("cq:versionComment", String.class);
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("RevisionImpl");
        sb.append("{id='").append(this.id).append('\'');
        sb.append(", label='").append(this.label).append('\'');
        sb.append(", existingPath='").append(this.existingPath).append('\'');
        sb.append('}');
        return sb.toString();
    }
}