ReplicationEvent.java 3.56 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.osgi.service.event.Event
 *  org.osgi.service.event.EventProperties
 */
package com.day.cq.replication;

import com.day.cq.replication.ReplicationAction;
import com.day.cq.replication.ReplicationActionType;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventProperties;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ReplicationEvent
implements Serializable {
    public static final String EVENT_TOPIC = "com/adobe/granite/replication";
    public static final String PATHS = "paths";
    private static final String PROPERTY_MODIFICATIONS = "modifications";
    private static final String TYPE = "type";
    private static final String TIME = "time";
    private static final String USER_ID = "userId";
    private static final String REVISION = "revision";
    private ReplicationAction replicationAction;
    private final boolean isLocal;

    public ReplicationEvent(ReplicationAction replicationAction) {
        this(replicationAction, true);
    }

    public ReplicationEvent(ReplicationAction replicationAction, boolean isLocal) {
        this.replicationAction = replicationAction;
        this.isLocal = isLocal;
    }

    public ReplicationAction getReplicationAction() {
        return this.replicationAction;
    }

    public static ReplicationEvent fromEvent(Event evt) {
        if (!evt.getTopic().equals("com/adobe/granite/replication")) {
            return null;
        }
        ReplicationAction action = null;
        List modProps = (List)evt.getProperty("modifications");
        if (modProps != null && modProps.size() == 1) {
            Map modProp = (Map)modProps.get(0);
            String[] paths = (String[])modProp.get("paths");
            ReplicationActionType type = (ReplicationActionType)((Object)modProp.get("type"));
            Long time = (Long)modProp.get("time");
            String userId = (String)modProp.get("userId");
            String revision = (String)modProp.get("revision");
            action = new ReplicationAction(type, paths, (long)time, userId, revision);
        }
        if (action == null) {
            throw new RuntimeException();
        }
        return new ReplicationEvent(action, evt.getProperty("event.application") == null);
    }

    protected Map<String, Object> getEventProperties(boolean distribute) {
        HashMap<String, Object> properties = new HashMap<String, Object>();
        ArrayList modProps = new ArrayList();
        HashMap<String, Object> actionProps = new HashMap<String, Object>();
        actionProps.put("paths", this.replicationAction.getPaths());
        actionProps.put("type", (Object)this.replicationAction.getType());
        actionProps.put("time", this.replicationAction.getTime());
        actionProps.put("userId", this.replicationAction.getUserId());
        actionProps.put("revision", this.replicationAction.getRevision());
        modProps.add(actionProps);
        properties.put("modifications", modProps);
        if (!this.isLocal) {
            properties.put("event.application", "unknown");
        }
        if (distribute) {
            properties.put("event.distribute", "");
        }
        return new EventProperties(properties);
    }

    public Event toEvent() {
        return new Event("com/adobe/granite/replication", this.getEventProperties(true));
    }

    public Event toNonDistributableEvent() {
        return new Event("com/adobe/granite/replication", this.getEventProperties(false));
    }
}