Event.java

package com.tradecloud.domain.model.events;

import com.tradecloud.common.base.PersistenceBase;
import com.tradecloud.domain.model.accounting.Snapshot;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.hibernate.annotations.AccessType;

import javax.persistence.*;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.Serializable;
import java.util.Date;
import java.util.Map;
import java.util.TreeMap;

/**
 * Base type for all events.
 */
@Entity
@Table(name = "event")
@Inheritance(strategy = InheritanceType.JOINED)
@AccessType("field")
@XmlRootElement
public abstract class Event extends PersistenceBase implements Serializable {

    public static enum EventState {
        Consumed, Created, Reinstated, Failed;
    }

    @Column(name = "notes", length = 255)
    private String notes;

    public Event(Long id, String reference, Date timestamp, String organisationalUnit, String eventType, String consumerTag, String notes) {
        super();
        this.id = id;
        this.timestamp = timestamp;
        this.reference = reference;
        this.organisationalUnit = organisationalUnit;
        this.eventType = eventType;
        this.consumerTag = consumerTag;
        this.setNotes(notes);
        this.setState(EventState.Created.name());
    }

    public Event(Long id, String reference, Date timestamp, String organisationalUnit, String eventType, String consumerTag) {
        super();
        this.id = id;
        this.timestamp = timestamp;
        this.reference = reference;
        this.organisationalUnit = organisationalUnit;
        this.eventType = eventType;
        this.consumerTag = consumerTag;
        this.setState(EventState.Created.name());
    }

    protected Event(String reference, Date timestamp, String organisationalUnit, String eventType, String consumerTag) {
        super();
        this.timestamp = timestamp;
        this.reference = reference;
        this.organisationalUnit = organisationalUnit;
        this.eventType = eventType;
        this.consumerTag = consumerTag;
        this.setState(EventState.Created.name());
    }

    protected Event() {
        this.setState(EventState.Created.name());
    }

    /**
     * Default serial version uid.
     */
    private static final long serialVersionUID = 1L;

    /**
     * Time the event occurred.
     */
    @Column(name = "timestamp")
    @Temporal(TemporalType.TIMESTAMP)
    @XmlAttribute
    private Date timestamp;

    /**
     * Purchase order reference.
     */
    @Column(name = "reference")
    @XmlAttribute
    protected String reference;

    /**
     * Purchase order organisationalUnit.
     */
    @Column(name = "organisationalUnit")
    @XmlAttribute
    private String organisationalUnit;

    /**
     * Type of the event.
     */
    @Column(name = "eventtype")
    @XmlAttribute
    protected String eventType;

    /**
     * State of the event.
     */
    @Column(name = "state")
    @XmlAttribute
    private String state;

    @XmlAttribute
    private String consumerTag;

    @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    private Snapshot snapshot;

    @XmlAttribute
    private String reinstateUser;

    private Date consumedDate;//will be null if not processed

    private Long organisationalUnitId;

    public String getReference() {
        return reference;
    }

    public Date getTimestamp() {
        return timestamp;
    }

    public String getOrganisationalUnit() {
        return organisationalUnit;
    }

    public String getEventType() {
        return eventType;
    }

    @Override
    public boolean equals(final Object other) {
        if (!(other instanceof Event)) {
            return false;
        }
        Event castOther = (Event) other;
        return new EqualsBuilder().append(reference, castOther.reference)
                .append(consumerTag, castOther.consumerTag).append(organisationalUnit, castOther.organisationalUnit)
                .append(eventType, castOther.eventType).isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder().append(reference).append(organisationalUnit).append(eventType).append(consumerTag)
                .toHashCode();
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this).append("id", getId()).append("timestamp", timestamp).append("reference", reference)
                .append("organisationalUnit", organisationalUnit).append("consumerTag", consumerTag).append("eventType", eventType).toString();
    }

    public String getConsumerTag() {
        return consumerTag;
    }

    public void setConsumerTag(String consumerTag) {
        this.consumerTag = consumerTag;
    }

    public void setTimestamp(Date timestamp) {
        this.timestamp = timestamp;
    }

    public String getNotes() {
        return notes;
    }

    public void setNotes(String notes) {
        this.notes = notes;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public abstract Class getInstanceClass();

    public static Map<String, Class> getClassesMap() {
        Map<String, Class> classToNameMap = new TreeMap<>();
        classToNameMap.put(LogisticsEvent.class.getSimpleName(), LogisticsEvent.class);
        classToNameMap.put(CostingEvent.class.getSimpleName(), CostingEvent.class);
        classToNameMap.put(AccountingEvent.class.getSimpleName(), AccountingEvent.class);
        classToNameMap.put(DealEvent.class.getSimpleName(), DealEvent.class);
        classToNameMap.put(OrderEvent.class.getSimpleName(), OrderEvent.class);
        classToNameMap.put(ProductEvent.class.getSimpleName(), ProductEvent.class);
        classToNameMap.put(SuppliersEvent.class.getSimpleName(), SuppliersEvent.class);
        return classToNameMap;
    }

    public String getReinstateUser() {
        return reinstateUser;
    }

    public void setReinstateUser(String reinstateUser) {
        this.reinstateUser = reinstateUser;
    }

    public Date getConsumedDate() {
        return consumedDate;
    }

    public void setConsumedDate(Date consumedDate) {
        this.consumedDate = consumedDate;
    }

    public Snapshot getSnapshot() {
        return snapshot;
    }

    public void setSnapshot(Snapshot snapshot) {
        this.snapshot = snapshot;
    }

    public Long getOrganisationalUnitId() {
        return organisationalUnitId;
    }

    public void setOrganisationalUnitId(Long organisationalUnitId) {
        this.organisationalUnitId = organisationalUnitId;
    }
}