/*
 * 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 aee.engine;

import org.jdom.Attribute;
import org.jdom.DataConversionException;
import org.jdom.Element;

import aee.exception.AeeException;

/**
 * A flow object. These are defined in a flow table
 */
public abstract class Flow extends NamedObject {
	private Integer available;
	private Integer desired;
	private Long    color;
	private Integer unused;
	protected static String ElementName = "flow";
	
	/**
	 * Constructor builds a flow from XML 
	 * @param root The root "flow" element
	 * @throws AeeException if any unexpected XML errors detected
	 */
	public Flow(Element root) throws AeeException {
		super(root, ElementName);
		
		available = null;
		desired   = null;
		color     = null;
		unused    = null;
		
		// All attributes are optional
		String attrName = "";
		try {
			attrName       = "available";
			Attribute attr = root.getAttribute(attrName);
			if (attr != null) {
				if (! "".equals(getAttrValue(attr)))
					available = attr.getIntValue();
			}
			
			attrName = "desired";
			attr     = root.getAttribute(attrName);
			if (attr != null) {
				if (! "".equals(getAttrValue(attr)))
					desired = attr.getIntValue();
			}
			
			attrName = "color";
			attr     = root.getAttribute(attrName);
			if (attr != null) {
				if (! "".equals(getAttrValue(attr)))
					color = attr.getLongValue();
			}
			
			attrName = "unused";
			attr     = root.getAttribute(attrName);
			if (attr != null) {
				if (! "".equals(getAttrValue(attr)))
					unused = attr.getIntValue();
			}
		
		} catch (DataConversionException dcex) {
			throw new AeeException("Element %s Attribute %s: %s",
					                    ElementName, attrName, dcex.getMessage());
		}
	}
	
	/**
	 * Report header 1 fields
	 * @return Header 1
	 */
	public abstract String[] getHeader1();
	
	/**
	 * Report header 2 fields
	 * @return Header 2
	 */
	public abstract String[] getHeader2();

	/**
	 * Report all CSV fields
	 * @return An array of CSV fields
	 */
	public abstract String[] getCsvFields();

	/**
	 * Report the available value
	 * @return null if available is missing else the value
	 */
	protected Integer getAvailable() {
		return (available == null)? null : new Integer(available);
	}
	
	/**
	 * Report the available value as a string
	 * @return "" if available is missing else the value
	 */
	protected String getAvailableAsString() {
		return intToString(available);
	}

	/**
	 * Report the desired value
	 * @return null if desired is missing else the value
	 */
	protected Integer getDesired() {
		return (desired == null)? null : new Integer(desired);
	}
	
	/**
	 * Report the desired value as a string
	 * @return "" if desired is missing else the value
	 */
	protected String getDesiredAsString() {
		return intToString(desired);
	}

	/**
	 * Report the color value
	 * @return null if color is missing else the value
	 */
	protected Long getColor() {
		return (color == null)? null : new Long(color);
	}
	
	/**
	 * Report the color value as a string
	 * @return "" if color is missing else the value
	 */
	protected String getColorAsString() {
		return longToString(color);
	}

	/**
	 * Report the unused value
	 * @return null if unused is missing else the value
	 */
	protected Integer getUnused() {
		return (unused == null)? null : new Integer(unused);
	}
	
	/**
	 * Report the unused value as a string
	 * @return "" if unused is missing else the value
	 */
	protected String getUnusedAsString() {
		return intToString(unused);
	}

}
