/** 
 * 
 * Copyright 2004 Protique Ltd
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 * 
 **/

package org.codehaus.activemq.message;

import org.codehaus.activemq.service.MessageIdentity;

/**
 * Denotes an object that can be serialized/deserailized using a PacketReader/PacketWriter
 */

public class MessageAck extends AbstractPacket {

    public static final int MESSAGE_READ_INDEX = 0;
    public static final int XA_TRANSACTED_INDEX = 1;
    public static final int PERSISTENT_INDEX = 2;
    public static final int EXPIRED_INDEX = 3;
    private String consumerId;
    private String messageID;
    private ActiveMQDestination  destination;
    private String transactionId;
    private boolean messageRead;
    private boolean xaTransacted;
    private boolean persistent;
    private boolean expired;
   
    private transient MessageIdentity messageIdentity;


    /**
     * Return the type of Packet
     *
     * @return integer representation of the type of Packet
     */

    public int getPacketType() {
        return ACTIVEMQ_MSG_ACK;
    }

    /**
     * @return pretty print of this Packet
     */
    public String toString() {
        return super.toString() + " MessageAck{ " +
                "consumerId = '" + consumerId + "' " +
                ", messageID = '" + messageID + "' " +
                ", destination = " + destination +
                ", transactionId = '" + transactionId + "' " +
                ", messageRead = " + messageRead +
                ", xaTransacted = " + xaTransacted +
                ", persistent = " + persistent +
                ", expired = " + expired +
                ", messageIdentity = " + messageIdentity +
                " }";
    }


    /**
     * @return Returns the transactionId.
     */
    public String getTransactionId() {
        return this.transactionId;
    }

    /**
     * @param newTransactionId The transactionId to set.
     */
    public void setTransactionId(String newTransactionId) {
        this.transactionId = newTransactionId;
    }

    /**
     * @return Returns true if this message is part of a transaction
     */

    public boolean isPartOfTransaction() {
        return this.transactionId != null && this.transactionId.length() > 0;
    }


    /**
     * @return Returns the messageID.
     */
    public String getMessageID() {
        return messageID;
    }

    /**
     * @param messageID The messageID to set.
     */
    public void setMessageID(String messageID) {
        this.messageID = messageID;
    }

    /**
     * @return Returns the messageRead.
     */
    public boolean isMessageRead() {
        return messageRead;
    }

    /**
     * @param messageRead The messageRead to set.
     */
    public void setMessageRead(boolean messageRead) {
        this.messageRead = messageRead;
    }

    /**
     * @return Returns the consumerId.
     */
    public String getConsumerId() {
        return consumerId;
    }

    /**
     * @param consumerId The consumerId to set.
     */
    public void setConsumerId(String consumerId) {
        this.consumerId = consumerId;
    }

    /**
     * @return Returns the xaTransacted.
     */
    public boolean isXaTransacted() {
        return xaTransacted;
    }

    /**
     * @param xaTransacted The xaTransacted to set.
     */
    public void setXaTransacted(boolean xaTransacted) {
        this.xaTransacted = xaTransacted;
    }

    public MessageIdentity getMessageIdentity() {
        if (messageIdentity == null) {
            messageIdentity = new MessageIdentity(messageID);
        }
        return messageIdentity;
    }
    /**
     * @return Returns the destination.
     */
    public ActiveMQDestination getDestination() {
        return destination;
    }
    /**
     * @param destination The destination to set.
     */
    public void setDestination(ActiveMQDestination destination) {
        this.destination = destination;
    }
    /**
     * @return Returns the persistent.
     */
    public boolean isPersistent() {
        return persistent;
    }
    /**
     * @param persistent The persistent to set.
     */
    public void setPersistent(boolean persistent) {
        this.persistent = persistent;
    }
    
    /**
     * @return true the delivered message was to a non-persistent destination
     */
    public boolean isTemporary(){
        return persistent == false || (destination != null && destination.isTemporary());
    }
    
    /**
     * @return Returns the expired.
     */
    public boolean isExpired() {
        return expired;
    }
    /**
     * @param expired The expired to set.
     */
    public void setExpired(boolean expired) {
        this.expired = expired;
    }
}
