ServiceCall.java

package com.tradecloud.domain.model.messagestore;

import com.tradecloud.common.base.PersistenceBase;
import org.hibernate.annotations.NaturalId;
import org.hibernate.annotations.Sort;
import org.hibernate.annotations.SortType;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

/**
 * Represents a service call. A service call contains both the request and
 * response of the service invocation.
 */
@Entity
@Table(name = "ServiceCall")
@Access(AccessType.FIELD)
@SuppressWarnings("unused")
@NamedQueries({
        @NamedQuery(query = "select sc from ServiceCall sc where sc.uniqueConversationId = :uniqueConversationId", name
                = "findServiceCallByUniqueConversationId"),
        @NamedQuery(query = "select sc from ServiceCall sc where sc.userName = :username", name = "findServiceCallsByUserName"),
        @NamedQuery(query = "select sc from ServiceCall sc where sc.userName = :userName and sc.operation = :operation",
                name = "findServiceCallsByUserNameAndOperation")})
public class ServiceCall extends PersistenceBase {

    private static final long serialVersionUID = 1L;

    @NaturalId
    private String uniqueConversationId;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    protected Set<PayloadValue> payloadValueMap;

    /**
     * username for user making the service call.
     */
    private String userName;

    /**
     * client user is associated with.
     */
    private String clientKey;

    /**
     * Service call identifier.
     */
    private String uri;

    /**
     * Can be used for soap operations.
     */
    private String operation;

    private String errorMessage;

    private String reference;

    private String type;

    private String replayUser;

    private String messageState;

    private Long resolvedCallId;

    private String additionalInfo;

    /**
     * Request and response messages.
     */
    @OneToMany(orphanRemoval = true, cascade = CascadeType.ALL)
    @Sort(type = SortType.NATURAL)
    private SortedSet<Message> messages;

    /**
     * Constructor for ServiceCall.
     */
    public ServiceCall() {
    }

    public ServiceCall(String uniqueConversationId, String userName, String clientKey, String uri, String operation,
                       String reference, String errorMessage, String type) {
        super();
        this.uniqueConversationId = uniqueConversationId;
        this.userName = userName;
        this.clientKey = clientKey;
        this.uri = uri;
        this.operation = operation;
        this.reference = reference;
        this.errorMessage = errorMessage;
        this.type = type;
    }

    public String getClientKey() {
        return clientKey;
    }

    public String getUri() {
        return uri;
    }

    public String getUserName() {
        return userName;
    }

    /**
     * @return lazy initialized {@link java.util.Set} of messages.
     */
    public SortedSet<Message> getMessages() {
        if (messages == null) {
            messages = new TreeSet<Message>();
        }
        return messages;
    }

    /**
     * @return the Request message out of {@link #messages}.
     */
    public Message getRequest() {
        for (Message message : messages) {
            if (message.getType() == Message.Type.REQUEST) {
                return message;
            }
        }
        return null;
    }

    /**
     * @return the response message out of {@link #messages}.
     */
    public Message getResponse() {
        for (Message message : messages) {
            if (message.getType() == Message.Type.RESPONSE) {
                return message;
            }
        }
        return null;
    }

    /**
     * @return the operation
     */
    public String getOperation() {
        return operation;
    }

    /**
     * @param message to add to {@link #messages}.
     * @return {@link java.util.Set#add(Object)}.
     */
    public boolean add(final Message message) {
        return getMessages().add(message);
    }

    @Override
    public String toString() {
        return "ServiceCall [uniqueConversationId=" + uniqueConversationId + ", userName=" + userName + ", clientKey=" + clientKey + ", uri=" + uri
                + ", operation=" + operation + "]";
    }

    public String getUniqueConversationId() {
        return uniqueConversationId;
    }

    public void setUniqueConversationId(String uniqueConversationId) {
        this.uniqueConversationId = uniqueConversationId;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public void setClientKey(String clientKey) {
        this.clientKey = clientKey;
    }

    public void setUri(String uri) {
        this.uri = uri;
    }

    public void setOperation(String operation) {
        this.operation = operation;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((clientKey == null) ? 0 : clientKey.hashCode());
        result = prime * result + ((operation == null) ? 0 : operation.hashCode());
        result = prime * result + ((uniqueConversationId == null) ? 0 : uniqueConversationId.hashCode());
        result = prime * result + ((userName == null) ? 0 : userName.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        ServiceCall other = (ServiceCall) obj;
        if (clientKey == null) {
            if (other.clientKey != null)
                return false;
        } else if (!clientKey.equals(other.clientKey))
            return false;
        if (operation == null) {
            if (other.operation != null)
                return false;
        } else if (!operation.equals(other.operation))
            return false;
        if (uniqueConversationId == null) {
            if (other.uniqueConversationId != null)
                return false;
        } else if (!uniqueConversationId.equals(other.uniqueConversationId))
            return false;
        if (userName == null) {
            if (other.userName != null)
                return false;
        } else if (!userName.equals(other.userName))
            return false;
        return true;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setReference() {
    }

    public String getReference() {
        return reference;
    }

    public void setReference(String reference) {
        this.reference = reference;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getReplayUser() {
        return replayUser;
    }

    public void setReplayUser(String replayUser) {
        this.replayUser = replayUser;
    }

    public Set<PayloadValue> getPayloadValueMap() {
        if (payloadValueMap == null) {
            payloadValueMap = new HashSet<>();
        }
        return payloadValueMap;
    }

    public void setPayloadValueMap(Set<PayloadValue> payloadValueMap) {
        this.payloadValueMap = payloadValueMap;
    }

    public String getMessageState() {
        return messageState;
    }

    public void setMessageState(String messageState) {
        this.messageState = messageState;
    }

    public Long getResolvedCallId() {
        return resolvedCallId;
    }

    public void setResolvedCallId(Long resolvedCallId) {
        this.resolvedCallId = resolvedCallId;
    }

    public String getAdditionalInfo() {
        return additionalInfo;
    }

    public void setAdditionalInfo(String additionalInfo) {
        this.additionalInfo = additionalInfo;
    }
}