Message.java

package com.tradecloud.domain.model.messagestore;

import com.tradecloud.common.base.PersistenceBase;
import org.apache.commons.lang.builder.CompareToBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

import javax.persistence.*;
import java.util.Date;

/**
 * Representation of a service call message.
 */
@Entity
@Table(name = "message")
@Access(AccessType.FIELD)
@SuppressWarnings("unused")
public class Message extends PersistenceBase implements Comparable<Message> {

    private static final long serialVersionUID = 1L;

    /**
     * Message type.
     */

    public static enum Type {
        /**
         * request type.
         */
        REQUEST,
        /**
         * response type.
         */
        RESPONSE;
    }

    /**
     * Mule correlation id.
     */
    private String correlationId;

    /**
     * Mule message type: request/response.
     */
    @Enumerated(EnumType.STRING)
    private Type type;

    /**
     * Timestamp for message.
     */
    private Date timeStamp;

    /**
     * Message payload.
     */
    @OneToOne(cascade = {CascadeType.ALL})
    private Payload payload;

    /**
     * Constructor for Message. used by hibernate.
     */
    private Message() {
    }

    public Message(final String correlationId, final Date timeStamp, final Type type, final String payload) {
        this.correlationId = correlationId;
        this.timeStamp = timeStamp;
        this.type = type;
        this.payload = new Payload(payload);

    }

    public String getCorrelationId() {
        return correlationId;
    }

    private void setCorrelationId(final String correlationId) {
        this.correlationId = correlationId;
    }

    public Date getTimeStamp() {
        return timeStamp;
    }

    private void setTimeStamp(final Date timeStamp) {
        this.timeStamp = timeStamp;
    }

    private String getTypeName() {
        return type.name();
    }

    private void setTypeName(final String typeName) {
        this.type = Type.valueOf(typeName);
    }

    public Type getType() {
        return type;
    }

    private void setType(final Type type) {
        this.type = type;
    }

    @Override
    public final String toString() {
        return "Message [correlationId=" + correlationId + ", id=" + getId() + ", timeStamp=" + timeStamp + ", type=" + type + "]";
    }

    @Override
    public final boolean equals(final Object other) {
        if (!(other instanceof Message)) {
            return false;
        }
        Message castOther = (Message) other;
        return new EqualsBuilder().append(correlationId, castOther.correlationId).append(type, castOther.type)
                .append(timeStamp, castOther.timeStamp).append(payload, castOther.payload).isEquals();
    }

    @Override
    public final int hashCode() {
        return new HashCodeBuilder().append(correlationId).append(type).append(timeStamp).append(payload).toHashCode();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int compareTo(final Message other) {
        return new CompareToBuilder().append(type, other.type).toComparison();
    }

    public Payload getPayload() {
        return payload;
    }

    public void setPayload(Payload payload) {
        this.payload = payload;
    }

}