RevisionImpl.java 5.8 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.asset.api.AssetException
 *  com.adobe.granite.asset.api.AssetVersion
 *  com.adobe.granite.asset.api.Rendition
 *  com.day.cq.dam.api.Revision
 *  javax.jcr.Node
 *  javax.jcr.RepositoryException
 *  javax.jcr.version.Version
 *  org.apache.commons.lang.StringUtils
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ResourceUtil
 *  org.apache.sling.api.resource.ValueMap
 *  org.apache.sling.jcr.resource.JcrPropertyMap
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.dam.core.impl;

import com.adobe.granite.asset.api.AssetException;
import com.adobe.granite.asset.api.AssetVersion;
import com.adobe.granite.asset.api.Rendition;
import com.day.cq.dam.api.Revision;
import java.util.Calendar;
import java.util.Iterator;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.version.Version;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
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 AssetVersion assetVersion;
    private final Version version;
    private final String label;
    private final boolean deleted;
    private ValueMap metadataProps;

    public RevisionImpl(AssetVersion assetVersion, Version version, ResourceResolver resolver, boolean deleted) throws RepositoryException {
        String[] labels;
        this.assetVersion = assetVersion;
        this.version = version;
        Resource contentResource = resolver.getResource(assetVersion.getPath() + "/" + "jcr:frozenNode" + "/" + "jcr:content");
        Resource metadataResource = resolver.getResource(contentResource, "metadata");
        if (metadataResource != null) {
            this.metadataProps = ResourceUtil.getValueMap((Resource)metadataResource);
        }
        if ((labels = assetVersion.getLabels()) == null || labels.length == 0) {
            this.label = assetVersion.getName();
        } else {
            StringBuffer buf = new StringBuffer(labels[0]);
            for (int i = 1; i < labels.length; ++i) {
                buf.append(", ");
                buf.append(labels[i]);
            }
            this.label = buf.toString();
        }
        this.deleted = deleted;
    }

    public RevisionImpl(AssetVersion assetVersion, Version version, ResourceResolver resolver) throws RepositoryException {
        this(assetVersion, version, resolver, false);
    }

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

    public ValueMap getProperties() {
        return (ValueMap)this.assetVersion.adaptTo(ValueMap.class);
    }

    public ValueMap getMetadataProperties() {
        return this.metadataProps;
    }

    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("getProperties(relPath): repository error while accessing content for version [{}]: ", (Object)this.getVersionDetails(), (Object)e);
        }
        return null;
    }

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

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

    public boolean isDeleted() {
        return this.deleted;
    }

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

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

    public String getTitle() {
        return this.getMetadata("dc:title");
    }

    public String getDescription() {
        return this.getMetadata("dc:description");
    }

    public Calendar getCreated() {
        return this.assetVersion.getCreated();
    }

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

    public String getRenditionPath(String renditionName) {
        if (StringUtils.isEmpty((String)renditionName)) {
            Iterator it = this.assetVersion.listRenditions();
            if (it.hasNext()) {
                Rendition r = (Rendition)it.next();
                return r.getParent().getPath();
            }
            return this.assetVersion.getPath() + "/" + "renditions";
        }
        try {
            Rendition r = this.assetVersion.getRendition(renditionName);
            if (r != null) {
                return r.getPath();
            }
        }
        catch (AssetException e) {
            log.warn("getRenditionPath: repository error while accessing content for version [{}]: ", (Object)this.getVersionDetails(), (Object)e);
        }
        return null;
    }

    private String getVersionDetails() {
        return "version[id=" + this.assetVersion.getId() + ", name=" + this.assetVersion.getName() + ", assetPath=" + this.assetVersion.getAssetPath() + "]";
    }

    private String get(String name) {
        if (this.getProperties() != null) {
            return (String)this.getProperties().get(name, String.class);
        }
        return null;
    }

    private String getMetadata(String name) {
        if (this.getMetadataProperties() != null) {
            return (String)this.getMetadataProperties().get(name, String.class);
        }
        return null;
    }
}