ReplicationStatusImpl.java 5.3 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceUtil
 *  org.apache.sling.api.resource.ValueMap
 *  org.apache.sling.api.wrappers.ValueMapDecorator
 */
package com.day.cq.replication.impl;

import com.day.cq.replication.Agent;
import com.day.cq.replication.AgentManager;
import com.day.cq.replication.ReplicationAction;
import com.day.cq.replication.ReplicationActionType;
import com.day.cq.replication.ReplicationQueue;
import com.day.cq.replication.ReplicationStatus;
import com.day.cq.replication.impl.queue.ReplicationQueueImpl;
import java.util.Calendar;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.wrappers.ValueMapDecorator;

public class ReplicationStatusImpl
implements ReplicationStatus {
    private final AgentManager agentMgr;
    private final String path;
    private final ValueMap properties;
    private List<ReplicationQueue.Entry> status;
    private List<ReplicationQueue.Entry> pending;
    private Boolean delivered;

    public ReplicationStatusImpl(AgentManager agentMgr, String path, Resource resource) {
        this(agentMgr, path, ResourceUtil.getValueMap((Resource)resource));
    }

    public ReplicationStatusImpl(AgentManager agentMgr, String path, ValueMap properties) {
        this.agentMgr = agentMgr;
        this.path = path;
        this.properties = new ValueMapDecorator((Map)properties);
    }

    @Override
    public List<ReplicationQueue.Entry> getQueueStatus() {
        if (this.status == null) {
            this.status = new LinkedList<ReplicationQueue.Entry>();
            for (Agent agent : this.agentMgr.getAgents().values()) {
                ReplicationQueue queue;
                if (agent.isInMaintenanceMode() || (queue = agent.getQueue()) == null) continue;
                for (ReplicationQueue.Entry entry : queue.entries(this.path)) {
                    this.status.add(entry);
                }
            }
        }
        return this.status;
    }

    @Override
    public boolean isPending() {
        boolean isPending = false;
        if (this.pending != null) {
            isPending = !this.pending.isEmpty();
        } else {
            for (Agent agent : this.agentMgr.getAgents().values()) {
                ReplicationQueue queue;
                if (agent.isInMaintenanceMode() || (queue = agent.getQueue()) == null) continue;
                if (queue instanceof ReplicationQueueImpl) {
                    if (!((ReplicationQueueImpl)queue).hasEntries(this.path)) continue;
                    isPending = true;
                    break;
                }
                if (queue.entries(this.path).isEmpty()) continue;
                isPending = true;
                break;
            }
        }
        return isPending;
    }

    @Override
    public List<ReplicationQueue.Entry> getPending() {
        if (this.pending == null) {
            this.pending = new LinkedList<ReplicationQueue.Entry>();
            ReplicationActionType type = this.getLastReplicationAction();
            Calendar last = this.getLastPublished();
            if (type != null && last != null) {
                long time = last.getTimeInMillis();
                for (ReplicationQueue.Entry s : this.getQueueStatus()) {
                    if (s.getAction().getType() != type || s.getAction().getTime() != time) continue;
                    this.pending.add(s);
                }
            }
        }
        return this.pending;
    }

    @Override
    public boolean isActivated() {
        return this.getLastReplicationAction() == ReplicationActionType.ACTIVATE;
    }

    @Override
    public boolean isDeactivated() {
        return this.getLastReplicationAction() == ReplicationActionType.DEACTIVATE;
    }

    @Override
    public boolean isDelivered() {
        if (this.delivered == null) {
            ReplicationActionType type = this.getLastReplicationAction();
            this.delivered = type == ReplicationActionType.ACTIVATE ? Boolean.valueOf(!this.isPending()) : (type == ReplicationActionType.DEACTIVATE || type == ReplicationActionType.DELETE ? Boolean.valueOf(this.isPending()) : Boolean.valueOf(false));
        }
        return this.delivered;
    }

    @Override
    public boolean isPublished() {
        return this.isDelivered();
    }

    @Override
    public Calendar getLastPublished() {
        if (this.properties.containsKey((Object)"cq:lastReplicated")) {
            return (Calendar)this.properties.get("cq:lastReplicated", Calendar.class);
        }
        return (Calendar)this.properties.get("cq:lastPublished", Calendar.class);
    }

    @Override
    public String getLastPublishedBy() {
        if (this.properties.containsKey((Object)"cq:lastReplicatedBy")) {
            return (String)this.properties.get("cq:lastReplicatedBy", String.class);
        }
        return (String)this.properties.get("cq:lastPublishedBy", String.class);
    }

    @Override
    public ReplicationActionType getLastReplicationAction() {
        return ReplicationActionType.fromName((String)this.properties.get("cq:lastReplicationAction", String.class));
    }
}