ProjectHistoryTimelineEventProvider.java 5.2 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.cq.history.api.HistoryEntry
 *  com.adobe.cq.history.api.HistoryEntry$Action
 *  com.adobe.cq.history.api.HistoryService
 *  com.adobe.cq.history.api.timeline.HistoryTimelineEventType
 *  com.adobe.granite.timeline.TimelineEvent
 *  com.adobe.granite.timeline.TimelineEventProvider
 *  com.adobe.granite.timeline.TimelineEventType
 *  com.day.cq.commons.Filter
 *  com.day.cq.wcm.api.Page
 *  com.day.text.Text
 *  javax.jcr.Session
 *  org.apache.commons.lang.StringUtils
 *  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.ResourceResolver
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.projects.impl.history;

import com.adobe.cq.history.api.HistoryEntry;
import com.adobe.cq.history.api.HistoryService;
import com.adobe.cq.history.api.timeline.HistoryTimelineEventType;
import com.adobe.cq.projects.impl.history.ProjectHistoryEntryFilter;
import com.adobe.granite.timeline.TimelineEvent;
import com.adobe.granite.timeline.TimelineEventProvider;
import com.adobe.granite.timeline.TimelineEventType;
import com.day.cq.commons.Filter;
import com.day.cq.wcm.api.Page;
import com.day.text.Text;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Set;
import javax.jcr.Session;
import org.apache.commons.lang.StringUtils;
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.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
@Service
public class ProjectHistoryTimelineEventProvider
implements TimelineEventProvider {
    private static final Logger log = LoggerFactory.getLogger(ProjectHistoryTimelineEventProvider.class);
    @Reference
    private HistoryService history;
    public static final TimelineEventType EVENT_TYPE = new HistoryTimelineEventType(){

        public String getName() {
            return ProjectHistoryTimelineEventProvider.class.getCanonicalName();
        }
    };

    public boolean accepts(Resource resource) {
        return "cq/gui/components/projects/admin/card/projectcard".equals(resource.getResourceType());
    }

    public Collection<TimelineEvent> getEvents(Resource resource) {
        ArrayList<TimelineEvent> events = new ArrayList<TimelineEvent>();
        ResourceResolver resolver = resource.getResourceResolver();
        long start = System.currentTimeMillis();
        Collection historyEntries = this.history.readEntries(resolver, ((Session)resolver.adaptTo(Session.class)).getUserID(), 10, (Filter)new ProjectHistoryEntryFilter(resource));
        log.debug(">>> retrieved all history entries for resource [{}] in [{}ms]", (Object)resource.getPath(), (Object)(System.currentTimeMillis() - start));
        long startTimelineEntries = System.currentTimeMillis();
        for (HistoryEntry historyEntry : historyEntries) {
            events.add(new HistoryTimelineEvent(historyEntry, resolver));
        }
        log.debug(">>> converted history entries to timeline events for resource [{}] took [{}ms]", (Object)resource.getPath(), (Object)(System.currentTimeMillis() - startTimelineEntries));
        return events;
    }

    public TimelineEventType getType() {
        return EVENT_TYPE;
    }

    protected void bindHistory(HistoryService historyService) {
        this.history = historyService;
    }

    protected void unbindHistory(HistoryService historyService) {
        if (this.history == historyService) {
            this.history = null;
        }
    }

    private static class HistoryTimelineEvent
    implements TimelineEvent {
        private final HistoryEntry historyEntry;
        private final ResourceResolver resolver;

        public HistoryTimelineEvent(HistoryEntry historyEntry, ResourceResolver resolver) {
            this.historyEntry = historyEntry;
            this.resolver = resolver;
        }

        public String getAction() {
            return this.historyEntry.getAction().name();
        }

        public String getDescription() {
            Page historyPage;
            Resource historyRes = this.resolver.getResource(this.historyEntry.getResourcePath());
            if (historyRes != null && this.historyEntry.getResourceTypes().contains("cq:Page") && (historyPage = (Page)historyRes.adaptTo(Page.class)) != null && StringUtils.isNotEmpty((String)historyPage.getTitle())) {
                return historyPage.getTitle();
            }
            return Text.getName((String)this.historyEntry.getResourcePath());
        }

        public String getOrigin() {
            return this.historyEntry.getResourcePath();
        }

        public long getTime() {
            return this.historyEntry.getDate().getTimeInMillis();
        }

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

        public String getUserID() {
            return this.historyEntry.getUserId();
        }
    }

}