package com.k_int.discover.update;

import com.k_int.discover.datamodel.updates.UpdateStatusEnum;
import java.io.Serializable;

/**
 * A simple class to manage all of the information to be returned back to the
 * calling code as part of an update request
 * @author rpb rich@k-int.com
 * @version 1.0 03.02.11
 */
public class UpdateRequestResponse implements Serializable {
	private static final long serialVersionUID = 1726574058970699025L;
	public boolean successful;
    public UpdateStatusEnum requestStatus;
    public String message;
    
    public UpdateRequestResponse() {
        super();
    }

    /**
     * @return The message assigned to the response
     */
    public String getMessage() {
        return message;
    }

    /**
     * @param message The message to be assigned to the response
     */
    public void setMessage(String message) {
        this.message = message;
    }

    /**
     * @return The status of the request
     */
    public UpdateStatusEnum getRequestStatus() {
        return requestStatus;
    }

    /**
     * @param requestStatus The status to be assigned to the request response
     */
    public void setRequestStatus(UpdateStatusEnum requestStatus) {
        this.requestStatus = requestStatus;
    }

    /**
     * @return The success or failure of the request
     */
    public boolean getSuccessful() {
        return successful;
    }

    /**
     * @param successful The success or failure of the request to be assigned
     */
    public void setSuccessful(boolean successful) {
        this.successful = successful;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final UpdateRequestResponse other = (UpdateRequestResponse) obj;
        if (this.successful != other.successful) {
            return false;
        }
        if (this.requestStatus != other.requestStatus) {
            return false;
        }
        if ((this.message == null) ? (other.message != null) : !this.message.equals(other.message)) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 11 * hash + (this.successful ? 1 : 0);
        hash = 11 * hash + (this.requestStatus != null ? this.requestStatus.hashCode() : 0);
        hash = 11 * hash + (this.message != null ? this.message.hashCode() : 0);
        return hash;
    }

    public String toXML() {
        StringBuilder builder = new StringBuilder();

        builder.append("<update>");
        builder.append("<successful>").append(this.getSuccessful()).append("</successful>");
        if ( this.getRequestStatus() != null )
            builder.append("<status>").append(this.getRequestStatus().toString()).append("</status>");
        else
            builder.append("<status/>");
        builder.append("<message>").append(this.getMessage()).append("</message>");
        builder.append("</update>");

        return builder.toString();
    }

    public String toJSON() {
        StringBuilder builder = new StringBuilder();
        builder.append("{\"successful\":").append(this.getSuccessful()).append(",");
        if ( this.getRequestStatus() != null )
            builder.append("\"status\":\"").append(this.getRequestStatus().toString()).append("\",");
        else
            builder.append("\"status\":\"\",");
        builder.append("\"message\":\"").append(this.getMessage()).append("\"}");

        return builder.toString();
    }
    
    
}
