// 
// 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.ExtendedRequestDefinition
//
// Author(s): Jeff Larson
//
// Description:
//
// Memory representation for an <extendedRequestDefinition> element.
//
//

package org.openspml.message;

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

/**
 * A class representing an <code>extendedRequestDefinition</code> element.
 */
public class ExtendedRequestDefinition {

    //////////////////////////////////////////////////////////////////////
    //
    // Fields
    //
    // In theory, have to allow arbitrary attributes and elements.
    //
    //////////////////////////////////////////////////////////////////////

    static final String ELEMENT = "extendedRequestDefinition";

    /**
     * Parent schema, used to resolve attribute definition references.
     */
    Schema _schema;

    /**
     * Optional description.
     */
    String _description;

    /**
     * Operation identifier.
     */
    OperationIdentifier _id;

    /**
     * Arbitrary properties.
     */
    Map _properties;

    /**
     * A List of AttributeDefinitionReference objects representing
     * the parameters to the extended request.
     */
    List _parameters;

    /**
     * A List of AttributeDefinitionReference objects representing
     * the return values of the request.
     */
    List _returnValues;

    /**
     * Cache of resolved parametere definitins.
     */
    List _parameterDefinitions;

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

    public ExtendedRequestDefinition() {
    }

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

    public void setSchema(Schema s) {
        _schema = s;
        setSchema(_parameters, s);
        setSchema(_returnValues, s);
    }

    private void setSchema(List refs, Schema s) {
        
        if (refs != null) {
            for (int i = 0 ; i < refs.size() ; i++) {
                AttributeDefinitionReference ref = 
                    (AttributeDefinitionReference)refs.get(i);
                ref.setSchema(s);
            }
        }
    }

    public void setDescription(String s) {
	_description = s;
    }

    public void setOperationIdentifier(OperationIdentifier id) {
	_id = id;
    }

    public void setOperationIdentifier(String id) {
        if (id == null)
            _id = null;
        else
            _id = new OperationIdentifier(id);
    }

    public void setProperties(Map props) {
	_properties = props;
    }
    
    public void setProperty(String name, Object value) {
        if (name != null) {
            if (_properties == null)
                _properties = new HashMap();
            _properties.put(name, value);
        }
    }

    public void setParameters(List l) {
	_parameters = l;
    }

    public void addParameter(AttributeDefinitionReference ref) {
        if (ref != null) {
            if (_parameters == null)
                _parameters = new ArrayList();
            _parameters.add(ref);
        }
    }

    public void setReturnValues(List l) {
	_returnValues = l;
    }

    public void addReturnValue(AttributeDefinitionReference ref) {
        if (ref != null) {
            if (_returnValues == null)
                _returnValues = new ArrayList();
            _returnValues.add(ref);
        }
    }

    //////////////////////////////////////////////////////////////////////
    //
    // Accessors
    //
    //////////////////////////////////////////////////////////////////////
    
    public String getDescription() {
	return _description;
    }

    public OperationIdentifier getOperationIdentifier() {
	return _id;
    }

    public String getOperationIdentifierString() {
	return (_id != null) ? _id.getId() : null;
    }

    public Map getProperties() {
	return _properties;
    }
    
    public Object getProperty(String name) {
	return (_properties != null) ? _properties.get(name) : null;
    }

    public List getParameters() {
	return _parameters;
    }

    /**
     * Convenience accessor to return a list of resolved
     * AttributeDefinition objects.
     // Hmm, what we really need is a combination of the definition
     // and the reference so we can combine the required flag with the
     // multi flag etc.
     */
    public List getParameterDefinitions() {

        if (_parameterDefinitions == null) {
            if (_schema != null)
                _parameterDefinitions = 
                    _schema.getAttributeDefinitions(_parameters);
        }
        return _parameterDefinitions;
    }

    public List getReturnValues() {
	return _returnValues;
    }

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

    public String getElementName() {
        return ELEMENT;
    }

    void toXml(SpmlBuffer b) {

        b.addOpenStartTag(ELEMENT);
	b.addAttribute("description", _description);
        b.closeStartTag();
        b.incIndent();

	// spec uses <xsd:sequence> so they must be
	// in this order

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

	Attribute.toXml(b, "properties", _properties);

	if (_parameters != null) {
	    b.addStartTag("parameters");
	    b.incIndent();
            Iterator it = _parameters.iterator();
            while (it.hasNext()) {
                AttributeDefinitionReference ref = 
		    (AttributeDefinitionReference)it.next();
                ref.toXml(b);
            }
	    b.decIndent();
	    b.addEndTag("parameters");
	}

	if (_returnValues != null) {
	    b.addStartTag("returnValues");
	    b.incIndent();
            Iterator it = _returnValues.iterator();
            while (it.hasNext()) {
                AttributeDefinitionReference ref = 
		    (AttributeDefinitionReference)it.next();
                ref.toXml(b);
            }
	    b.decIndent();
	    b.addEndTag("returnValues");
	}

        b.decIndent();
        b.addEndTag(ELEMENT);
    }

    void parseXml(XmlElement e) {

	// in theory, need to recognize and maintain 
	// other arbitrary elements

	_description = e.getAttribute("description");

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

	    String tag = child.getLocalName();

	    if (tag.equals(OperationIdentifier.ELEMENT))
                _id = new OperationIdentifier(child);

	    else if (tag.equals("properties"))
                _properties = Attribute.parseMap(child);

	    else if (tag.equals("parameters")) {
		for (XmlElement prm = child.getChildElement();
		     prm != null ; prm = prm.next()) {
		    addParameter(new AttributeDefinitionReference(prm));
		}
	    }

	    else if (tag.equals("returnValues")) {
		for (XmlElement ret = child.getChildElement();
		     ret != null ; ret = ret.next()) {
		    addReturnValue(new AttributeDefinitionReference(ret));
		}
	    }

	    // in theory, need to recognize and maintain 
	    // other arbitrary elements

        }
    }

}
