/*
 * 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 java.util.Map;
import java.util.HashMap;

import org.jdom.Attribute;
import org.jdom.DataConversionException;
import org.jdom.Element;
public class VectorBuilder   {
/**
 * Extract all vector elements from a composite_argument of a service request
 * and return them as a map with the key of vector name. 
 * The type is specified so one can store vectors in any composite_argument type.
 */
  public static Map<String,Vector> build(ServiceRequest sr, String platform, String type) 
  {
	  Map<String,Vector> map = new HashMap<String,Vector>();
	  // a list of composite_argument elements that have vector elements
	  List<?> list = sr.getCompositeArg(platform, type);
	  for (Object o : list ) {
		 Element e = (Element) o; 
		 List<?> children = e.getChildren("vector");
		 for ( Object c : children) {
			   Vector vector = createVector(c);
			   if ( vector == null) return null;
			   String key = vector.getName();
			   map.put(key,vector);
		 }
	  }
	  return map;
  }
  /**
   * A vector element was found 
   * -- create a vector object from it and
   * return it
   */
  private static Vector createVector(Object o) {
	  Element element = (Element) o;
	  String name = element.getAttributeValue("name");
	  String type = element.getAttributeValue("type");
	  String unit = element.getAttributeValue("unit");
	  Vector vector = new Vector(name,type,unit);
	  List<?> children = element.getChildren("element");
	  for ( Object c : children) {
		  Element e = (Element) c;
		  Attribute a = e.getAttribute("id");
		  String value = e.getAttributeValue("value");
		  int id;
		  Double dval;
		  try {
			  id = a.getIntValue();
			  dval = new Double(value);
		} catch (DataConversionException e1) {
			 Log.err(e1.getMessage());
			 return null;
		}
		catch (NumberFormatException e1) {
			 Log.err(e1.getMessage());
			 return null;
		}
		vector.addElement(id,dval);
	  }
	  return vector;
  }
}
