/*

 * 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.ArrayList;

import java.util.List;



import meta.rmi.ServiceResponse;



import org.jdom.Element;



import exception.ControllerException;



/**

 * A proxy for all mission data

 */

public class MissionProxy {

	private List<ProxyElement> mission;

	

	/**

	 * Constructor

	 *

	 * @param response The GetMission service response

	 * @throws ControllerException if an invalid service response

	 */

	public MissionProxy(ServiceResponse response) throws ControllerException {

		Element resp    = response.getXmlContent();

		List<?> content = resp.getChildren("composite_argument");

		int     n       = content.size();

		if (n != 1)

			throw new ControllerException("Unexpected mission data. %d " +

					                          "composite_argument values are present", n);

		

		resp        = (Element) content.get(0);

		String type = resp.getAttributeValue("type");

		if (! "mission-segment".equalsIgnoreCase(type))

			throw new ControllerException("Unexpected error: Invalid element type (%s) " +

					                          "for a mission",

					                          (type== null)? "" : type);

		List<?> members = resp.getChildren();

		mission         = new ArrayList<ProxyElement>(members.size());

		for (Object child : members) {

			mission.add(new SimpleProxyElement((Element) child));

		}

	}

	

	/**

	 * Report all elements in the mission

	 * @return A list of mission data

	 */

	public List<ProxyElement> getElements() {

		return new ArrayList<ProxyElement>(mission);

	}

	

	/**

	 * Report the mission size

	 * @return Number of mission elements

	 */

	public int size() {

		return mission.size();

	}

}

