TimeoutJob.java 2.96 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.osgi.service.event.Event
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.workflow.job;

import com.day.cq.workflow.exec.WorkItem;
import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.Dictionary;
import java.util.Hashtable;
import org.osgi.service.event.Event;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TimeoutJob
implements Serializable {
    private static final Logger log = LoggerFactory.getLogger(TimeoutJob.class);
    private static final long serialVersionUID = 5670996916430565635L;
    public static final String TIMEOUT_JOB_TOPIC = "com/day/cq/workflow/timeout/job";
    public static final String TIMEOUT_JOB = "com.day.cq.workflow.timeout.job";
    protected String itemId;
    String handler;
    protected long timeStarted = 0;

    public TimeoutJob(WorkItem item, String handler) {
        if (item == null || handler == null) {
            throw new IllegalArgumentException("item or handler must not be null.");
        }
        this.itemId = item.getId();
        this.timeStarted = item.getTimeStarted().getTime();
        this.handler = handler;
    }

    public String getWorkItemId() {
        return this.itemId;
    }

    public String getHandler() {
        return this.handler;
    }

    public Event createEvent(boolean executeParallel, long seconds, boolean addOffset) {
        Hashtable<String, Object> props = new Hashtable<String, Object>();
        props.put("com.day.cq.workflow.timeout.job", this);
        props.put("event.job.parallel", executeParallel);
        props.put("event.topic.timed", "org/apache/sling/event/job");
        props.put("event.job.topic", "com/day/cq/workflow/timeout/job");
        props.put("event.job.id", this.itemId + "_" + this.timeStarted);
        Date date = this.calculateDate(seconds, addOffset);
        props.put("event.timed.date", date);
        log.debug("create timeout event {} ", props);
        return new Event("org/apache/sling/event/timed", props);
    }

    public Event cancelEvent(boolean executeParallel) {
        Hashtable<String, Object> props = new Hashtable<String, Object>();
        props.put("com.day.cq.workflow.timeout.job", this);
        props.put("event.job.parallel", executeParallel);
        props.put("event.topic.timed", "org/apache/sling/event/job");
        props.put("event.job.topic", "com/day/cq/workflow/timeout/job");
        props.put("event.job.id", this.itemId + "_" + this.timeStarted);
        log.debug("create timeout cancel event {}", props);
        return new Event("org/apache/sling/event/timed", props);
    }

    private Date calculateDate(long seconds, boolean addOffset) {
        Calendar cal = Calendar.getInstance();
        long millies = seconds * 1000;
        if (addOffset) {
            millies += cal.getTimeInMillis();
        }
        cal.setTimeInMillis(millies);
        return cal.getTime();
    }
}