ReplicationJob.java 4.83 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.sling.event.jobs.Job
 *  org.apache.sling.event.jobs.consumer.JobExecutionContext
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.replication.impl.queue;

import com.day.cq.replication.ReplicationAction;
import com.day.cq.replication.ReplicationActionType;
import com.day.cq.replication.ReplicationContentFacade;
import java.util.Calendar;
import java.util.Date;
import org.apache.sling.event.jobs.Job;
import org.apache.sling.event.jobs.consumer.JobExecutionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ReplicationJob {
    private static final Logger log = LoggerFactory.getLogger(ReplicationJob.class);
    public static final String JOB_TOPIC = "com/day/cq/replication/job";
    private static final int JOB_TOPIC_LENGTH = "com/day/cq/replication/job".length();
    public static final String PROPERTY_REPLICATION_CONTENT = "replicationContent";
    @Deprecated
    private static final String PROPERTY_REPLICATION_ACTION = "replicationAction";
    public static final String PROPERTY_CQ_ACTION_PATH = "cq:path";
    public static final String PROPERTY_CQ_ACTION_PATHS = "cq:paths";
    public static final String PROPERTY_CQ_ACTION_REV = "cq:revision";
    public static final String PROPERTY_CQ_ACTION_TIME = "cq:time";
    public static final String PROPERTY_CQ_ACTION_TYPE = "cq:type";
    public static final String PROPERTY_CQ_ACTION_USER = "cq:user";
    public static final String NODE_PROPERTY_LAST_PUBLISHED = "cq:lastPublished";
    public static final String NODE_PROPERTY_LAST_PUBLISHED_BY = "cq:lastPublishedBy";
    public static final String NODE_PROPERTY_LAST_REPLICATED = "cq:lastReplicated";
    public static final String NODE_PROPERTY_LAST_REPLICATED_BY = "cq:lastReplicatedBy";
    public static final String NODE_PROPERTY_LAST_REPLICATION_ACTION = "cq:lastReplicationAction";
    public static final String NODE_PROPERTY_LAST_REPLICATION_STATUS = "cq:lastReplicationStatus";
    public static final String NODE_TYPE = "cq:ReplicationStatus";
    private final Job job;
    private final JobExecutionContext context;
    private ReplicationAction action;

    public ReplicationJob(Job job) {
        this(job, null);
    }

    public ReplicationJob(Job job, JobExecutionContext context) {
        this.job = job;
        if (!job.getTopic().startsWith("com/day/cq/replication/job/")) {
            throw new IllegalArgumentException("Invalid topic for replication job: " + job.getTopic());
        }
        this.context = context;
    }

    public Object getProperty(String name) {
        if (":sling:jobs:asynchandler".equals(name)) {
            return this.context;
        }
        return this.job.getProperty(name);
    }

    public String getEventId() {
        return this.job.getId();
    }

    public String getPath() {
        ReplicationAction action;
        String path = (String)this.getProperty("cq:path");
        if (path == null && (action = this.getAction()) != null) {
            path = action.getPath();
        }
        return path;
    }

    public String getQueueName() {
        String name = this.job.getTopic();
        if (name == null || !name.startsWith("com/day/cq/replication/job/")) {
            log.error("Invalid queue name: {}", (Object)name);
            return null;
        }
        return name.substring(JOB_TOPIC_LENGTH + 1);
    }

    public ReplicationContentFacade getContent() {
        return (ReplicationContentFacade)this.getProperty("replicationContent");
    }

    public ReplicationAction getAction() {
        if (this.action == null) {
            this.action = (ReplicationAction)this.getProperty("replicationAction");
            if (this.action == null) {
                Object lastMod = this.getProperty("cq:time");
                long time = 0;
                if (lastMod instanceof Date) {
                    time = ((Date)lastMod).getTime();
                } else if (lastMod instanceof Calendar) {
                    time = ((Calendar)lastMod).getTimeInMillis();
                } else if (lastMod instanceof Long) {
                    time = (Long)lastMod;
                }
                String[] paths = (String[])this.getProperty("cq:paths");
                if (paths == null) {
                    paths = new String[]{(String)this.getProperty("cq:path")};
                }
                this.action = new ReplicationAction(ReplicationActionType.fromName((String)this.getProperty("cq:type")), paths, time, (String)this.getProperty("cq:user"), (String)this.getProperty("cq:revision"));
            }
        }
        return this.action;
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("ReplicationJob");
        sb.append("{job=").append((Object)this.job);
        sb.append('}');
        return sb.toString();
    }
}