GridEvent



GridEvent.java




GridEvent.java

package plgrid.event;

import java.io.Reader;
import java.io.StringReader;
import nu.xom.Attribute;
import nu.xom.Builder;
import nu.xom.Document;
import nu.xom.Element;

public class GridEvent {
    private String error;
    private String jobId;
    private String taskId;

    public GridEvent() {
    }

    public GridEvent(String jobId, String taskId) {
        this.jobId = jobId;
        this.taskId = taskId;
    }

    public GridEvent(String jobId, String taskId, String error) {
        this.jobId = jobId;
        this.taskId = taskId;
        this.error = error;
    }

    public void setJobId(String jobId) {
        this.jobId = jobId;
    }

    public String getJobId() {
        return this.jobId;
    }

    public void setTaskId(String taskId) {
        this.taskId = taskId;
    }

    public String getTaskId() {
        return this.taskId;
    }

    public boolean hasError() {
        if (this.error != null) {
            return true;
        }
        return false;
    }

    public void setError(String error) {
        this.error = error;
    }

    public String getError() {
        return this.error;
    }

    public Element toXMLElement() {
        Element geElement = new Element("gridEvent");
        geElement.addAttribute(new Attribute("type", this.getClass().getName()));
        if (this.jobId != null && this.jobId.length() > 0) {
            geElement.addAttribute(new Attribute("jobId", this.jobId));
        }
        if (this.taskId != null && this.taskId.length() > 0) {
            geElement.addAttribute(new Attribute("taskId", this.taskId));
        }
        if (this.error != null && this.error.length() > 0) {
            geElement.addAttribute(new Attribute("error", this.error));
        }
        if (this instanceof EventFinished) {
            EventFinished ef = (EventFinished)this;
            geElement.addAttribute(new Attribute("endTime", Long.toString(ef.getFinishTime())));
            geElement.addAttribute(new Attribute("startTime", Long.toString(ef.getStartTime())));
            geElement.addAttribute(new Attribute("exitStatus", Integer.toString(ef.getExitStatus())));
        }
        return geElement;
    }

    public String toXML() {
        Document doc = new Document(this.toXMLElement());
        return doc.toXML();
    }

    public static GridEvent fromXMLElement(Element geElement) {
        String str = null;
        GridEvent ge = null;
        str = geElement.getAttributeValue("type");
        if (str.equals(EventFinished.class.getName())) {
            ge = new EventFinished();
            str = geElement.getAttributeValue("endTime");
            if (str != null) {
                ((EventFinished)ge).setFinishTime(Long.parseLong(str));
            }
            if ((str = geElement.getAttributeValue("startTime")) != null) {
                ((EventFinished)ge).setStartTime(Long.parseLong(str));
            }
            if ((str = geElement.getAttributeValue("exitStatus")) != null) {
                ((EventFinished)ge).setExitStatus(Integer.parseInt(str));
            }
        } else if (str.equals(EventQueued.class.getName())) {
            ge = new EventQueued();
        } else if (str.equals(EventRunning.class.getName())) {
            ge = new EventRunning();
        }
        if ((str = geElement.getAttributeValue("jobId")) != null) {
            ge.setJobId(str);
        }
        if ((str = geElement.getAttributeValue("taskId")) != null) {
            ge.setTaskId(str);
        }
        if ((str = geElement.getAttributeValue("error")) != null) {
            ge.setError(str);
        }
        return ge;
    }

    public static GridEvent fromXML(String xml) {
        StringReader sr = new StringReader(xml);
        Builder builder = new Builder();
        Document doc = null;
        try {
            doc = builder.build((Reader)sr);
        }
        catch (Exception ex) {
            return null;
        }
        return GridEvent.fromXMLElement(doc.getRootElement());
    }
}