/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.apache.activeblaze.jms;

import org.apache.activeblaze.Destination;
import org.apache.activeblaze.jndi.JNDIStorable;
import org.apache.activeblaze.wire.Buffer;

import javax.jms.*;
import java.io.Externalizable;
import java.util.Map;


/**
 * Jms Destination
 */
public class BlazeJmsDestination extends JNDIStorable implements Externalizable, javax.jms.Destination,
        Comparable<BlazeJmsDestination> {
    protected transient final Destination destination;

    /**
     * Constructor
     */
    public BlazeJmsDestination() {
        this.destination = new Destination();
    }

    /**
     * Constructor
     */
    public BlazeJmsDestination(Destination dest) {
        this.destination = dest;
    }

    /**
     * Constructor
     */
    public BlazeJmsDestination(String name) {
        this();
        this.destination.setName(new Buffer(name));
    }

    /**
     * @return the destination
     */
    public Destination getDestination() {
        return this.destination;
    }

    /**
     * @return name of destination
     */
    public String getName() {
        return this.destination.getName().toString();
    }

    /**
     * @return the topic
     */
    public boolean isTopic() {
        return this.destination.isTopic();
    }

    /**
     * @return the temporary
     */
    public boolean isTemporary() {
        return this.destination.isTemporary();
    }

    /**
     * @return true if a Topic
     */
    public boolean isQueue() {
        return this.destination.isQueue();
    }

    /**
     * @see org.apache.activeblaze.jndi.JNDIStorable#buildFromProperties(java.util.Properties)
     */
    @Override
    protected void buildFromProperties(Map<String, String> props) {

        this.destination.setName(new Buffer(getProperty(props, "name", "")));
        Boolean bool = Boolean.valueOf(getProperty(props, "topic", Boolean.TRUE.toString()));
        this.destination.setTopic(bool.booleanValue());
        bool = Boolean.valueOf(getProperty(props, "temporary", Boolean.FALSE.toString()));
        this.destination.setTemporary(bool.booleanValue());
    }

    /**
     * @see org.apache.activeblaze.jndi.JNDIStorable#populateProperties(java.util.Properties)
     */
    @Override
    protected void populateProperties(Map<String, String> props) {
        props.put("name", this.destination.getName().toString());
        props.put("topic", Boolean.toString(this.destination.isTopic()));
        props.put("temporary", Boolean.toString(this.destination.isTemporary()));
    }

    /**
     * @param other the Object to be compared.
     * @return a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than
     *         the specified object.
     * @see java.lang.Comparable#compareTo(java.lang.Object)
     */
    public int compareTo(BlazeJmsDestination other) {
        if (other != null) {
            if (this.destination.isTemporary() == other.destination.isTemporary()) {
                return this.destination.getName().compareTo(other.destination.getName());
            }
            return -1;
        }
        return -1;
    }

    /**
     * Transform a javax.jms.Destination to a BlazeJmsDestination
     *
     * @return a BlazeJmsDestination
     */
    public static BlazeJmsDestination transform(javax.jms.Destination dest) throws JMSException {
        if (dest == null) {
            return null;
        }
        if (dest instanceof BlazeJmsDestination) {
            return (BlazeJmsDestination) dest;
        }
        if (dest instanceof TemporaryQueue) {
            return new BlazeJmsTempQueue(((TemporaryQueue) dest).getQueueName());
        }
        if (dest instanceof TemporaryTopic) {
            return new BlazeJmsTempTopic(((TemporaryTopic) dest).getTopicName());
        }
        if (dest instanceof Queue) {
            return new BlazeJmsQueue(((Queue) dest).getQueueName());
        }
        if (dest instanceof Topic) {
            return new BlazeJmsTopic(((Topic) dest).getTopicName());
        }
        throw new JMSException("Could not transform the destination into a ActiveMQ destination: " + dest);
    }

    /**
     * @return a JMS destination
     */
    public static BlazeJmsDestination createJmsDestination(Destination dest) {
        if (dest.isTopic()) {
            if (!dest.isTemporary()) {
                return new BlazeJmsTopic(dest);
            }
            return new BlazeJmsTempTopic(dest);
        }
        if (!dest.isTemporary()) {
            return new BlazeJmsQueue(dest);
        }
        return new BlazeJmsTempQueue(dest);
    }
}
