CommentsTimelineEventProvider.java 3.83 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.comments.Comment
 *  com.adobe.granite.comments.CommentCollection
 *  com.adobe.granite.comments.CommentManager
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ValueMap
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.granite.timeline.internal.providers;

import com.adobe.granite.comments.Comment;
import com.adobe.granite.comments.CommentCollection;
import com.adobe.granite.comments.CommentManager;
import com.adobe.granite.timeline.TimelineEvent;
import com.adobe.granite.timeline.TimelineEventProvider;
import com.adobe.granite.timeline.TimelineEventType;
import com.adobe.granite.timeline.types.CommentsTimelineEventType;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.List;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
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 CommentsTimelineEventProvider
implements TimelineEventProvider {
    private static final Logger log = LoggerFactory.getLogger(CommentsTimelineEventProvider.class);
    public static final TimelineEventType EVENT_TYPE = new CommentsTimelineEventType();
    private static final String ACTION = "Comment Created";
    @Reference
    private CommentManager commentManager;

    @Override
    public boolean accepts(Resource resource) {
        return null != resource.getChild("./jcr:content/comments");
    }

    @Override
    public Collection<TimelineEvent> getEvents(Resource resource) {
        ArrayList<TimelineEvent> events = new ArrayList<TimelineEvent>();
        long start = System.currentTimeMillis();
        CommentCollection collection = this.commentManager.getCollection(resource, CommentCollection.class);
        for (Comment comment : collection.getCommentList()) {
            events.add(new CommentTimelineEvent(comment));
        }
        log.debug(">> collected comment timeline events for resource [{}] in [{}ms]", (Object)resource.getPath(), (Object)(System.currentTimeMillis() - start));
        return events;
    }

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

    protected void bindCommentManager(CommentManager commentManager) {
        this.commentManager = commentManager;
    }

    protected void unbindCommentManager(CommentManager commentManager) {
        if (this.commentManager == commentManager) {
            this.commentManager = null;
        }
    }

    private static class CommentTimelineEvent
    implements TimelineEvent {
        private final Comment comment;
        private final ValueMap properties;

        private CommentTimelineEvent(Comment comment) {
            this.comment = comment;
            this.properties = comment.getProperties();
        }

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

        public String getDescription() {
            return this.comment.getMessage();
        }

        public String getOrigin() {
            return this.comment.getPath();
        }

        public long getTime() {
            return this.comment.getCreated().getTimeInMillis();
        }

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

        public String getUserID() {
            return this.comment.getAuthorName();
        }
    }

}