package com.dragonsoft.tryapp.common;

import java.util.LinkedList;
import java.util.List;

/**
 * This class may need to be a remote object if we later decide to 
 * run enterprise beans outside of the JBoss container running this webapp.
 */
public class ProcessMonitor {

    /**
     * A unique identifier for this process monitor.
     */
    private String strMonitorID;

    /**
     * The latest status update.
     */
    private ProcessUpdate latestStatus;

    /**
     * History of all status updates.
     */
    private LinkedList processUpdates;

    /**
     * True if the process being monitored has finished successfully.
     */
    private boolean processFinishedSuccessfully;

    /**
     * True if the process being monitored has failed.
     */
    private boolean processFailed;

    /**
     * Creates a new process monitor.
     * 
     * @param    monitorID    a unique ID for this monitor
     */
    public ProcessMonitor(String monitorID) {

        strMonitorID = monitorID;

        processUpdates = new LinkedList();

        processFinishedSuccessfully = false;
        processFailed = false;
    }

    /**
     * Returns the ID of this monitor.
     * 
     * @return    the ID of this monitor
     */
    public String getMonitorID() {
        return (strMonitorID);
    }

    /**
     * Called to indicate that the status of the process has changed.
     * 
     * @param    processUpdate    the status update
     */
    public void updateStatus(ProcessUpdate processUpdate) {
        latestStatus = processUpdate;

        processUpdates.add(processUpdate);
    }

    /**
     * Returns the latest status.
     * 
     * @return    the latest status
     */
    public ProcessUpdate getLatestStatus() {
        return (latestStatus);
    }

    /**
     * Returns a history of all status updates.
     * 
     * @return    a history of all status updates
     */
    public List getAllUpdates() {
        return (processUpdates);
    }

    /**
     * Returns true if the process being monitored has finished 
     * successfully and false otherwise.
     * 
     * @return    true if the process finished successfully
     */
    public boolean hasFinishedSuccessfully() {
        return (processFinishedSuccessfully);
    }

    /**
     * Called to indicate that the process has finished successfully.
     * 
     * @param    processUpdate    the last process update
     */
    public void finished(ProcessUpdate processUpdate) {
        updateStatus(processUpdate);

        processFinishedSuccessfully = true;
    }

    /**
     * Returns true if the process being monitored has failed and 
     * false otherwise.
     * 
     * @return    true if the process failed
     */
    public boolean hasFailed() {
        return (processFailed);
    }

    /**
     * Called to indicate that the process has failed.
     * 
     * @param    processUpdate    should indicate the failure
     */
    public void failed(ProcessUpdate processUpdate) {
        updateStatus(processUpdate);

        processFailed = true;
    }

    /**
     * Returns true if the given object is a ProcessMonitor and 
     * has the same monitor ID.
     * 
     * @param    obj    the object to compare to
     * 
     * @return    true if the given object has the same ID
     */
    public boolean equals(Object obj) {
        return ((obj instanceof ProcessMonitor) && (((ProcessMonitor) obj)
                .getMonitorID().equals(getMonitorID())));
    }

} // ProcessMonitor