/*
 * 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 meta.rmi;

import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.xpath.XPath;

/**
 * A META SOA Service Request
 */
public class ServiceRequest {
	private static final String XPATH_CA  = "/service-request/service-arguments/composite_argument[@name='%s' and @type='%s']";
	private static final String XPATH_CAN = "/service-request/service-arguments/composite_argument[@name='%s']";
	private static final String XPATH_CAT = "/service-request/service-arguments/composite_argument[@type='%s']";
	private static final String XPATH_ARG = "/service-request/service-arguments/argument[@name='%s' and @type='%s']";
	private Document doc;

	public ServiceRequest(Document doc) {
		this.doc = doc;
	}
	/**
	 * Report the argument with the given name 
	 * and type attributes. Returns null if not found
	 */
	public Argument getArgument(String name, String type) {
	   Argument arg = null;
		String xpath = String.format(XPATH_ARG, name, type);
		try {
			Object o = XPath.selectSingleNode(doc, xpath);
			if ( o != null ) {
				if ( o instanceof Element ) {
					Element element = (Element) o;
					String value = element.getAttributeValue("value");
					arg = new Argument(name,type,value);
				}
			}
		} catch (JDOMException e) {
			// then return null
		}
	   return arg;
	}
	/**
	 * Report a node list of all composite elements matching 
	 * <composite_argument name="engine" type="architecture">
	 * 
	 */
	public List<?> getCompositeArg(String name, String type) {
		List<?> nodes = null;
		String xpath = String.format(XPATH_CA, name, type);
		try {
			nodes = XPath.selectNodes(doc, xpath);
		} catch (JDOMException e) {
			// then return null
		}
		return nodes;
	}
	
	/**
	 * Report a node list of all composite elements matching just a type
	 * @param type  The type to match
	 * @return      The matches
	 */
	public List<?> getCompositeArgByType(String type) {
		List<?> nodes = null;
		String  xpath = String.format(XPATH_CAT, type);
		try {
			nodes = XPath.selectNodes(doc, xpath);
		} catch (JDOMException e) {
			// then return null
		}
		return nodes;
	}
	
	/**
	 * Report a node list of all composite elements matching just a name
	 * @param name  The name to match
	 * @return      The matches
	 */
	public List<?> getCompositeArgByName(String name) {
		List<?> nodes = null;
		String  xpath = String.format(XPATH_CAN, name);
		try {
			nodes = XPath.selectNodes(doc, xpath);
		} catch (JDOMException e) {
			// then return null
		}
		return nodes;
	}

	/**
	 * Report the service request name
	 * @return Value of the service-request "name" attribute
	 */
	public String getRequestName() {
		return getRequestAttribute("name");
	}
	
	/**
	 * Report the service request id
	 * @return Value of the service-request "id" attribute
	 */
	public String getRequestId() {
		return getRequestAttribute("id");
	}
	
	/**
	 * Utility finds the value of an attribute on the service-request
	 * @param attrName  The attribute name to find
	 * @return          The attribute value or null if none
	 */
	private String getRequestAttribute(String attrName) {
		 Element reqRoot = doc.getRootElement();
		 String  name    = reqRoot.getAttributeValue(attrName);
		 return name;
	}
	
}
