VersioningTimelineEventProvider.java 5.47 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.jcr.Node
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  javax.jcr.Workspace
 *  javax.jcr.version.Version
 *  javax.jcr.version.VersionHistory
 *  javax.jcr.version.VersionIterator
 *  javax.jcr.version.VersionManager
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.resource.Resource
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.granite.timeline.internal.providers;

import com.adobe.granite.timeline.TimelineEvent;
import com.adobe.granite.timeline.TimelineEventProvider;
import com.adobe.granite.timeline.TimelineEventType;
import com.adobe.granite.timeline.types.VersioningTimelineEventType;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Workspace;
import javax.jcr.version.Version;
import javax.jcr.version.VersionHistory;
import javax.jcr.version.VersionIterator;
import javax.jcr.version.VersionManager;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/*
 * This class specifies class file version 49.0 but uses Java 6 signatures.  Assumed Java 6.
 */
@Component
@Service
public class VersioningTimelineEventProvider
implements TimelineEventProvider {
    private static final Logger log = LoggerFactory.getLogger(VersioningTimelineEventProvider.class);
    public static final TimelineEventType EVENT_TYPE = new VersioningTimelineEventType();
    private static final String ACTION = "Version Created";

    @Override
    public boolean accepts(Resource resource) {
        try {
            Node node = (Node)resource.adaptTo(Node.class);
            return node.isNodeType("mix:versionable") || node.hasNode("jcr:content") && node.getNode("jcr:content").isNodeType("mix:versionable");
        }
        catch (RepositoryException e) {
            log.error("error while checking node type for [{}]: ", (Object)resource.getPath(), (Object)e);
            return false;
        }
    }

    @Override
    public Collection<TimelineEvent> getEvents(Resource resource) {
        ArrayList<TimelineEvent> entries = new ArrayList<TimelineEvent>();
        Node node = (Node)resource.adaptTo(Node.class);
        try {
            if (!node.isNodeType("mix:versionable") && node.hasNode("jcr:content") && node.getNode("jcr:content").isNodeType("mix:versionable")) {
                node = node.getNode("jcr:content");
            }
            long start = System.currentTimeMillis();
            VersionManager versionManager = node.getSession().getWorkspace().getVersionManager();
            VersionHistory versionHistory = versionManager.getVersionHistory(node.getPath());
            VersionIterator allVersions = versionHistory.getAllVersions();
            log.debug(">> retrieved all versions for [{}] in [{}ms]", (Object)node.getPath(), (Object)(System.currentTimeMillis() - start));
            long startTimelineEntries = System.currentTimeMillis();
            while (allVersions.hasNext()) {
                Version v = allVersions.nextVersion();
                if ("jcr:rootVersion".equals(v.getName())) continue;
                entries.add(new VersioningTimelineEvent(v, resource));
            }
            log.debug(">> converted versions to timeline events for [{}] in [{}ms]", (Object)node.getPath(), (Object)(System.currentTimeMillis() - startTimelineEntries));
        }
        catch (RepositoryException e) {
            log.error("error while getting version history for [{}]: ", (Object)resource.getPath(), (Object)e);
        }
        return entries;
    }

    @Override
    public TimelineEventType getType() {
        return EVENT_TYPE;
    }

    private static class VersioningTimelineEvent
    implements TimelineEvent {
        private final Version version;
        private final Resource resource;

        private VersioningTimelineEvent(Version version, Resource resource) {
            this.version = version;
            this.resource = resource;
        }

        public String getAction() {
            return "Version Created";
        }

        public String getDescription() {
            try {
                return this.version.getName();
            }
            catch (RepositoryException e) {
                log.error("error while getting version description for [{}]: ", (Object)this.resource.getPath(), (Object)e);
                return null;
            }
        }

        public String getOrigin() {
            try {
                return this.version.getPath();
            }
            catch (RepositoryException e) {
                log.error("error while getting version path for [{}]: ", (Object)this.resource.getPath(), (Object)e);
                return null;
            }
        }

        public long getTime() {
            try {
                return this.version.getCreated().getTimeInMillis();
            }
            catch (RepositoryException e) {
                log.error("error while getting version date for [{}]: ", (Object)this.resource.getPath(), (Object)e);
                return 0;
            }
        }

        public TimelineEventType getType() {
            return VersioningTimelineEventProvider.EVENT_TYPE;
        }

        public String getUserID() {
            return null;
        }
    }

}