/** 
 * 
 * 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.service.impl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.activemq.message.ActiveMQMessage;
import org.codehaus.activemq.message.ConsumerInfo;
import org.codehaus.activemq.message.MessageAck;
import org.codehaus.activemq.service.MessageContainerAdmin;
import org.codehaus.activemq.service.MessageIdentity;
import org.codehaus.activemq.service.Subscription;
import org.codehaus.activemq.service.TopicMessageContainer;
import org.codehaus.activemq.store.TopicMessageStore;

import javax.jms.JMSException;

/**
 * A default implemenation of a Durable Topic based
 * {@link org.codehaus.activemq.service.MessageContainer}
 * which acts as an adapter between the {@link org.codehaus.activemq.service.MessageContainerManager}
 * requirements and those of the persistent {@link TopicMessageStore} implementations.
 *
 * @version $Revision: 1.4 $
 */
public class DurableTopicMessageContainer implements TopicMessageContainer, MessageContainerAdmin {
    private static final Log log = LogFactory.getLog(DurableTopicMessageContainer.class);

    private TopicMessageStore messageStore;
    private String destinationName;
    private MessageIdentity lastMessageIdentity;

    public DurableTopicMessageContainer(TopicMessageStore messageStore, String destinationName) {
        this.messageStore = messageStore;
        this.destinationName = destinationName;
    }

    public String getDestinationName() {
        return destinationName;
    }

    public MessageIdentity addMessage(ActiveMQMessage message) throws JMSException {
        MessageIdentity answer = messageStore.addMessage(message);
        lastMessageIdentity = answer;
        return answer;
    }

    public void delete(MessageIdentity messageID, MessageAck ack) throws JMSException {
        // only called in MessagePointer and so shouldn't really delete
        //messageStore.removeMessage(new MessageIdentity(messageID));
    }

    public boolean containsMessage(MessageIdentity messageIdentity) throws JMSException {
        /** TODO: make more optimal implementation */
        return getMessage(messageIdentity) != null;
    }

    public ActiveMQMessage getMessage(MessageIdentity messageID) throws JMSException {
        return messageStore.getMessage(messageID);
    }

    public void registerMessageInterest(MessageIdentity messageIdentity) throws JMSException {
        messageStore.incrementMessageCount(messageIdentity);
    }

    public void unregisterMessageInterest(MessageIdentity messageIdentity, MessageAck ack) throws JMSException {
        messageStore.decrementMessageCountAndMaybeDelete(messageIdentity, ack);
    }


    public void setLastAcknowledgedMessageID(Subscription subscription, MessageIdentity messageIdentity) throws JMSException {
        messageStore.setLastAcknowledgedMessageIdentity(subscription, messageIdentity);
    }

    public void recoverSubscription(Subscription subscription) throws JMSException {
        messageStore.recoverSubscription(subscription, lastMessageIdentity);
    }

    public void storeSubscription(ConsumerInfo info, Subscription subscription) throws JMSException {
        messageStore.setSubscriberEntry(info, subscription.getSubscriptionEntry());
    }

    public void start() throws JMSException {
        messageStore.setMessageContainer(this);
        lastMessageIdentity = messageStore.getLastestMessageIdentity();
        messageStore.start();
    }

    public void stop() throws JMSException {
        messageStore.stop();
    }

    /**
     * @see org.codehaus.activemq.service.MessageContainer#getMessageContainerAdmin()
     */
    public MessageContainerAdmin getMessageContainerAdmin() {
        return this;
    }

    /**
     * @see org.codehaus.activemq.service.MessageContainerAdmin#empty()
     */
    public void empty() throws JMSException {
        messageStore.removeAllMessages();
    }

    /**
     * @see org.codehaus.activemq.service.MessageContainer#isDeadLetterQueue()
     */
    public boolean isDeadLetterQueue() {
        return false;
    }
}
