/**
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 *
 * Copyright (C)
 * <a href="http://www.inab.org">Spanish National Institute of Bioinformatics (INB)</a>
 * <a href="http://www.bsc.es">Barcelona Supercomputing Center (BSC)</a>
 * <a href="http://inb.bsc.es">Computational Node 6</a>
 */

package org.inb.biomoby.shared.message;

import java.io.Serializable;
import javax.xml.bind.annotation.XmlAnyElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.bind.annotation.XmlTransient;
import org.inb.biomoby.shared.datatypes.*;
import org.w3c.dom.Element;

/**
 * @author Dmitry Repchevsky
 */

@XmlRootElement(name="Simple")

// explicitly load primitive types to the context... needed by Endpoints
@XmlSeeAlso({MobyBinary.class, MobyBoolean.class, MobyDateTime.class, MobyFloat.class, MobyInteger.class, MobyString.class})
public class MobySimple extends MobyDataElement implements Serializable
{   
    private AbstractMobyObject object;
    
    public MobySimple() {}

    public MobySimple(AbstractMobyObject object)
    {
        this(object, "");
    }

    public MobySimple(AbstractMobyObject object, String articleName)
    {
        this.object = object;
        super.setArticleName(articleName);
    }

    @XmlAnyElement(lax = true)
    Object getJaxbObject()
    {
        if (object instanceof AnyMobyObject)
        {
            AnyMobyObject anyMobyObject = (AnyMobyObject)object;
            
            return anyMobyObject.marshal();
        }
        return object;
    }

    void setJaxbObject(Object object)
    {
        if (object instanceof Element)
        {
            Element element = (Element)object;

            this.object = AnyMobyObject.unmarshal(element);
        }
        else
        {
            this.object = (AbstractMobyObject)object;
        }
    }

    @XmlTransient
    public AbstractMobyObject getMobyObject()
    {
        return object;
    }

    public void setMobyObject(AbstractMobyObject object)
    {
        this.object = object;
    }

    @XmlTransient
    public <T extends AbstractMobyObject> T getObject()
    {
        return (T)object;
    }
}
