AnnotationsTimelineEventProvider.java 4.07 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.felix.scr.annotations.Component
 *  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.timeline.TimelineEvent;
import com.adobe.granite.timeline.TimelineEventProvider;
import com.adobe.granite.timeline.TimelineEventType;
import com.adobe.granite.timeline.types.AnnotationsTimelineEventType;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.apache.felix.scr.annotations.Component;
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 AnnotationsTimelineEventProvider
implements TimelineEventProvider {
    private static final Logger log = LoggerFactory.getLogger(AnnotationsTimelineEventProvider.class);
    public static final TimelineEventType EVENT_TYPE = new AnnotationsTimelineEventType();
    private static final String ACTION = "Annotation Created";

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

    @Override
    public Collection<TimelineEvent> getEvents(Resource resource) {
        ArrayList<TimelineEvent> events = new ArrayList<TimelineEvent>();
        long start = System.currentTimeMillis();
        HashMap<String, ValueMap> annotations = new HashMap<String, ValueMap>();
        AnnotationsTimelineEventProvider.collectAnnotations(annotations, resource.getChild("jcr:content"));
        for (Map.Entry<String, ValueMap> entry : annotations.entrySet()) {
            events.add(new AnnotationTimelineEvent(entry.getKey(), entry.getValue()));
        }
        log.debug(">> collected comment timeline events for resource [{}] in [{}ms]", (Object)resource.getPath(), (Object)(System.currentTimeMillis() - start));
        return events;
    }

    private static void collectAnnotations(Map<String, ValueMap> map, Resource root) {
        Iterator children = root.listChildren();
        while (children.hasNext()) {
            Resource child = (Resource)children.next();
            if ("cq:annotations".equals(child.getName())) {
                Iterator annotations = child.listChildren();
                while (annotations.hasNext()) {
                    Resource annotation = (Resource)annotations.next();
                    map.put(annotation.getPath(), (ValueMap)annotation.adaptTo(ValueMap.class));
                }
                continue;
            }
            AnnotationsTimelineEventProvider.collectAnnotations(map, child);
        }
    }

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

    private static class AnnotationTimelineEvent
    implements TimelineEvent {
        private String path;
        private final ValueMap properties;

        public AnnotationTimelineEvent(String path, ValueMap props) {
            this.path = path;
            this.properties = props;
        }

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

        public String getDescription() {
            return (String)this.properties.get("text", (Object)"");
        }

        public String getOrigin() {
            return this.path;
        }

        public long getTime() {
            return ((Calendar)this.properties.get("jcr:lastModified", Calendar.class)).getTimeInMillis();
        }

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

        public String getUserID() {
            return (String)this.properties.get("jcr:createdBy", (Object)"");
        }
    }

}