// 
// The Waveset SPML General License 
// 
// Version 0.1, April 2003
// Copyright (C) 2003 Waveset Technologies, Inc..
// 6034 West Courtyard Drive, Suite 210, Austin, Texas 78730
// All rights reserved.
// 
// TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
// 
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
// 
// 1. Redistributions of source code must retain the above copyright
//    notice, this list of conditions, and the disclaimers in Sections 6
//    and 7 below.
// 
// 2. Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the disclaimers in Sections 6
//    and 7 below in the documentation and/or other materials provided
//    with the distribution.
// 
// 3. The end-user documentation included with the redistribution, if
//    any, must include the following acknowledgment:
// 
//    "This product includes software developed by 
//     Waveset Technologies, Inc. (www.waveset.com)."
// 
//    Alternately, this acknowledgment may appear in the software itself, if
//    and wherever such third-party acknowledgments normally appear.
//  
// 4. The names "Waveset" and "Waveset Technologies, Inc." must not be
//    used to endorse or promote products derived from this software
//    without the prior written permission of Waveset. For written
//    permission, please contact www.waveset.com.
//  
// 5. Products derived from this software may not be called "Waveset",
//    nor may "Waveset" appear in their name, without the prior written
//    permission of Waveset.
//  
// 6. NO WARRANTY 
// 
//    THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED
//    OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
//    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
//    DISCLAIMED.
// 
// 7.  LIMITATION OF LIABILITY
// 
//    IN NO EVENT SHALL THE WAVESET OR ITS LICENSORS BE LIABLE FOR ANY
//    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
//    DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
//    GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
//    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
//    IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
//    OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
//    ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// 
// End of Terms and Conditions
// ====================================================================
//
// Component Name: org.openspml.message.AddResponse
//
// Author(s): Jeff Larson
//
// Description:
//
// Memory representation for a <spml:addResponse> element.
//
//

package org.openspml.message;

import java.util.*;
import org.openspml.util.*;

/**
 * A class used to represent an <code>addResponse</code> element.
 * <p>
 * This includes the vendor-specific identifier of the object
 * that was added, and potentially a list of attributes that were
 * not included in the <code>addReqeuest</code> but were assigned
 * to the object as a side effect.
 */
public class AddResponse extends SpmlResponse {

    //////////////////////////////////////////////////////////////////////
    //
    // Fields
    //
    //////////////////////////////////////////////////////////////////////

    static final String ELEMENT = "addResponse";

    /**
     * Identifier of the object that was added.
     */
    Identifier _identifier;

    /**
     * A list of Attribute objects.
     * These are used to represent attributes of the object that
     * were not passed in the AddRequest, but were generated
     * by the PST as a side effect.
     */
    List _attributes;

    //////////////////////////////////////////////////////////////////////
    //
    // Constructors
    //
    //////////////////////////////////////////////////////////////////////

    /**
     * Construct an object representing an <code>addReponse</code>
     */
    public AddResponse() {
    }

    AddResponse(XmlElement e) {
	parseXml(e);
    }

    /**
     * Set the identifier of the new object.
     */
    public void setIdentifier(Identifier id) {
        _identifier = id;
    }

    /**
     * Set the identifier of the new object as a string.        
     * This will implicitly create an <code>Identifier</code.
     * object whose type is <code>GenericString</code>
     */
    public void setIdentifier(String id) {
        _identifier = new Identifier(id);
    }

    /**
     * Sets the list of object attributes to include in the response.
     * The list must contain <code>Attribute</code> objects.
     * @see #Attribute
     */
    public void setAttributes(List l) {
        _attributes = l;
    }

    /**
     * Sets the list of attributes from a map of name/value pairs.
     */
    public void setAttributes(Map src) {
        _attributes = Attribute.getAttributes(src);
    }

    /**
     * Set one attribute to be included in the response.
     */
    public void setAttribute(Attribute a) {
        if (_attributes == null)
            _attributes = new ArrayList();
        _attributes.add(a);
    }

    /**
     * Set one attribute to be included in the response.
     */
    public void setAttribute(String name, Object value) {
        setAttribute(new Attribute(name, value));
    }

    //////////////////////////////////////////////////////////////////////
    //
    // Accessors
    //
    //////////////////////////////////////////////////////////////////////

    /**
     * Return the identifier of the object that was added.
     */
    public Identifier getIdentifier() {
        return _identifier;
    }

    /**
     * Return the identifier of the object that was added as a string.
     */
    public String getIdentifierString() {
        return (_identifier != null) ? _identifier.getId() : null;
    }

    /**
     * Return the list of all attributes that were assigned as
     * as a side effect.  The list will contain <code>Attribute</code>
     * objects.
     */
    public List getAttributes() {
        return _attributes;
    }

    /**
     * Retrieve an attribute by name.
     */
    public Attribute getAttribute(String name) {
        return Attribute.getAttribute(_attributes, name);
    }

    /**
     * Retrieve the value of an attribute.
     */
    public Object getAttributeValue(String name) {
        return Attribute.getAttributeValue(_attributes, name);
    }

    //////////////////////////////////////////////////////////////////////
    //
    // XML
    //
    //////////////////////////////////////////////////////////////////////

    String getElementName() {
        return ELEMENT;
    }

    void addSubclassElements(SpmlBuffer b) {

        if (_identifier != null)
            _identifier.toXml(b);

        Attribute.toXml(b, "attributes", _attributes);
    }

    void parseXml(XmlElement e) {

        super.parseXml(e);

	for (XmlElement child = e.getChildElement() ; 
	     child != null ; child = child.next()) {
	    String tag = child.getLocalName();

	    if (tag.equals(Identifier.ELEMENT))
		_identifier = new Identifier(child);

	    else if (tag.equals("attributes") )
                _attributes = Attribute.parseList(child);
        }
    }


}

