/*

 * Delivered to the U.S. Government in Accordance with Contract FA8650-10-C-7080.

 *

 * EXPORT CONTROLLED

 * ECCN#   EAR99

 * Warning: This document contains technical data which is controlled under the

 * Bureau of Industry & Security's Export Administration Regulations and may not be

 * exported or re-exported without authorization by the Bureau of Industry & Security.

 * Diversion contrary to US law is prohibited.

 *

 * (c) 2011 United Technologies Corp.

 * Developed with the sponsorship of the Defense Advanced Research Projects Agency (DARPA).

 *

 * Permission is hereby granted, free of charge, to any person obtaining a copy of this data,

 * including any software or models in source or binary form, as well as any drawings, 

 * specifications, and documentation (collectively "the Data"), to deal in the Data without 

 * restriction, including without limitation the rights to use, copy, modify, merge, publish,

 * distribute, sublicense, and/or sell copies of the Data, and to permit persons to whom the Data

 * is furnished to do so, subject to the following conditions:

 * 

 * The above copyright notice and this permission notice shall be included in all copies or

 * substantial portions of the Data.

 *

 * THE DATA IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING

 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND

 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS, SPONSORS, DEVELOPERS, CONTRIBUTORS, OR

 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION

 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE DATA OR THE

 * USE OR OTHER DEALINGS IN THE DATA.

*/



package proxy;



import java.util.List;



import meta.rmi.ServiceResponse;



import org.jdom.Attribute;

import org.jdom.Element;



import exception.ControllerException;



/**

 * An SOA proxy attribute that comes from a composite_argument in a ServiceResponse

 */

public abstract class ServiceResponseCompositeProxyElement implements ProxyElement {

	private ServiceResponse response;

	

	/**

	 * Constructor

	 *

	 * @param response The service response containing the architecture

	 */

	public ServiceResponseCompositeProxyElement(ServiceResponse response) {

		this.response = response;

	}



	/* (non-Javadoc)

	 * @see proxy.ProxyAttribute#getAttribute()

	 */

	public Element getElement() throws ControllerException {

		Element responseXml = response.getXmlContent();

		return (Element) getElement(responseXml, getElementName(), getElementType())

		                .clone();

	}



	/**

	 * Report the desired element from the response's XML

	 * @param responseXml The root element of the response

	 * @param name        The name of the child element

	 * @param type        The type of the child element

	 * @return            The element wanted

	 */

	private Element getElement(Element responseXml, String name, String type)

		throws ControllerException {

		String  eleName  = "composite_argument";

		List<?> children = responseXml.getChildren(eleName);

		Element child    = null;

		

		// There should be a single child with the given name and type

		for (int i = 0; i < children.size(); i++) {

			Element   element  = (Element) children.get(i);

			Attribute nameAttr =  element.getAttribute("name");

			Attribute typeAttr =  element.getAttribute("type");

			if (nameAttr != null  &&  typeAttr != null) {

				String n = nameAttr.getValue();

				String t = typeAttr.getValue();

				if (n.equalsIgnoreCase(name)  &&  t.equalsIgnoreCase(type)) {

					if (child == null)

						child = element;

					else

						throw new ControllerException("For service response \"%s\" there are " +

								                          "multiple %s elements with name=%s and type=%s",

								                          responseXml.getAttribute("name"),

								                          eleName, name, type);

				}

			}

		}  // i

		if (child == null)

			throw new ControllerException("The \"%s\" service response does " +

          "not have any \"%s\" elements with name=%s and type=%s",

          responseXml.getAttribute("name"), 

          eleName, name, type);

		

		return child;

	}





	/**

	 * Report the composite_argument name attribute needed for the proxy element

	 * @return The child element with this name

	 */

	public abstract String getElementName();



	/**

	 * Report the composite_argument type attribute needed for the proxy element

	 * @return The child element with this name

	 */

	public abstract String getElementType();

}

