/*******************************************************************************
 *  Copyright 2017 Centre for Development of Advanced Computing(C-DAC), Pune
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *    http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *******************************************************************************/
package cdac.medinfo.sdk.hl7282.baselibs;  

import cdac.medinfo.sdk.hl7282.baselibs.segmentfactory.DataElementFactory;
import cdac.medinfo.sdk.hl7282.commoninterface.IDataType;
import cdac.medinfo.sdk.hl7282.commoninterface.ISingleCollection;
import cdac.medinfo.sdk.hl7282.exceptions.InvalidComponentTypeException;
import cdac.medinfo.sdk.hl7282.exceptions.InvalidDataTypeException;
import cdac.medinfo.sdk.hl7282.exceptions.MissingValueException;
import cdac.medinfo.sdk.hl7282.utils.EnumDataType;
import cdac.medinfo.sdk.hl7282.utils.IDataTypeMap;

/**
 * ED - Encapsulated Data<p>
 * This data type transmits encapsulated data from a source system to a 
 * destination system. It contains the identity of the source system, 
 * the type of data, the encoding method of the data, and the data itself
 * <p>Represents an HL7 ED data type. 
 * This data type has the following attributes:</p><p>
 *  1. Source Application (HD)<b>Optional</b><br>
 *  2. Type of Data (ID)<b>Required</b><br>
 *  3. Data Subtype (ID)<b>Optional</b><br>
 *  4. Encoding (ID)<b>Required</b><br>
 *  5. Data (TX)<b>Required</b><br><p>
 * This class provides accessors & mutators for the elements mentioned above.
 * This class override the equal method, to check whether some other object is "equal to" this one.
 * 
 * <p>User can also create Data Type through {@link DataElementFactory} 
 */
public class ED extends CompositeDataType    
{
	

	/**
	 * Default Constructor for ED
	 */
	public ED()
	{
		super(EnumDataType.HL7_DT_ED);
		 
	}

	/**
	 * Constructor  for ED class which takes four arguments and 
	 * sets the value in the respective member variable.
	 * @param iMinLength The Minimum length the DataType can have.
	 * @param iMaxLength The Maximum length the DataType can have.
	 * @param iCLength The Conformance length the DataType can have.
	 * @param iComponentType The Type of the component i.e Component or a subcomponent.
	 * @throws InvalidComponentTypeException - if Component Type is not specified or valid. 
	 */ 
	public ED(int iMinLength, int iMaxLength, String strCLength,int iComponentType) throws InvalidComponentTypeException
	{
		super(EnumDataType.HL7_DT_ED);
		this.iMinLength = iMinLength;
		this.iMaxLength = iMaxLength;
		this.iConformanceLength = strCLength;
		this.iComponentType = iComponentType;
		if(!isValidComponentType())
		{
			throw new InvalidComponentTypeException(this.getClass().getSimpleName());
		}
	}

	/**
	 * Constructor for ED class which takes five arguments and 
	 * sets the value in the respective member variable.
	 * @param iMinLength The Minimum length the DataType can have.
	 * @param iMaxLength The Maximum length the DataType can have.
	 * @param iCLength The Conformance length the DataType can have.
	 * @param iTableNo The Table number to which the value for the component is specified. 
	 * @param iComponentType The Type of the component i.e Component or a subcomponent.
	 * @throws InvalidComponentTypeException - if Component Type is not specified or valid. 
	 */
	public ED(int iMinLength, int iMaxLength, String strCLength,int iTableNo,int iComponentType) throws InvalidComponentTypeException
	{
		super(EnumDataType.HL7_DT_ED);
		this.iMinLength = iMinLength;
		this.iMaxLength = iMaxLength;
		this.iConformanceLength = strCLength;
		this.iTableNo = iTableNo;
		this.iComponentType = iComponentType;
		if(!isValidComponentType())
		{
			throw new InvalidComponentTypeException(this.getClass().getSimpleName());		
		}
	}
	/**
	 * Constructor for ED class which takes two arguments and 
	 * sets the value in the respective member variable.
	 * @param objIDataTypeMap object of Data Type Map.
	 * @param iComponentType The Type of the component i.e Component or a subcomponent.
	 * @throws InvalidComponentTypeException - if Component Type is not specified or valid. 
	 */
	public ED(int iComponentType,IDataTypeMap objIDataTypeMap)
	{
		super(EnumDataType.HL7_DT_ED);
		this.objIDataTypeMap = objIDataTypeMap;
		this.iComponentType = iComponentType;
		if(!isValidComponentType())
		{
			throw new InvalidComponentTypeException(this.getClass().getSimpleName());
		}
	}
	/**
	 * Constructor for ED class which takes five arguments and 
	 * sets the value in the respective member variable.
	 * @param objIDataTypeMap object of Data Type Map.
	 * @param iComponentType The Type of the component i.e Component or a subcomponent.
	 * @param iMinLength The Minimum length the DataType can have.
	 * @param iMaxLength The Maximum length the DataType can have.
	 * @param iCLength The Conformance length the DataType can have.
	 * @throws InvalidComponentTypeException - if Component Type is not specified or valid. 
	 */
	public ED(int iComponentType,int iMinLength, int iMaxLength, String strCLength,IDataTypeMap objIDataTypeMap)
	{
		super(EnumDataType.HL7_DT_ED);
		this.objIDataTypeMap = objIDataTypeMap;
		this.iComponentType = iComponentType;
		this.iMinLength = iMinLength;
		this.iMaxLength = iMaxLength;
		this.iConformanceLength = strCLength;

		if(!isValidComponentType())
		{
			throw new InvalidComponentTypeException(this.getClass().getSimpleName());
		}
	}
	/**
	 * Constructor for ED class which takes six arguments and 
	 * sets the value in the respective member variable.
	 * @param objIDataTypeMap object of Data Type Map.
	 * @param iComponentType The Type of the component i.e Component or a subcomponent.
	 * @param iMinLength The Minimum length the DataType can have.
	 * @param iMaxLength The Maximum length the DataType can have.
	 * @param iCLength The Conformance length the DataType can have.
	 * @param iTableNo The Table number to which the value for the component is specified. 
	 * @throws InvalidComponentTypeException - if Component Type is not specified or valid. 
	 */
	public ED(int iComponentType,int iMinLength, int iMaxLength, String strCLength,int ITableNo,IDataTypeMap objIDataTypeMap)
	{
		super(EnumDataType.HL7_DT_ED);
		this.objIDataTypeMap = objIDataTypeMap;
		this.iComponentType = iComponentType;
		this.iTableNo = ITableNo;
		this.iMinLength = iMinLength;
		this.iMaxLength = iMaxLength;
		this.iConformanceLength = strCLength;

		if(!isValidComponentType())
		{
			throw new InvalidComponentTypeException(this.getClass().getSimpleName());
		}
	}



	/**
	 *Sets the SourceApplication.
	 *@param objSourceApplication Specifies A unique name that identifies the system which was the source of the data. 
	 */
	public void setSourceApplication(HD objSourceApplication)
	{
		setComponentType(1,objSourceApplication);
	}

	/**
	 *Sets the TypeOfData.
	 *@param strTypeOfData Specifies an ID data type that declares the general type of data.  
	 */
	public void setTypeOfData(String strTypeOfData)
	{
		if(strTypeOfData!=null)
			setComponentType(2, strTypeOfData);
		else
			setTypeOfData((ID)null);
	}

	/**
	 *Sets the TypeOfData.
	 *@param objID Specifies an ID data type that declares the general type of data. 
	 */
	public void setTypeOfData(ID objID)
	{
		setComponentType(2,objID);
	}

	/**
	 *Sets the DataSubtype.
	 *@param strDataSubtype Specifies an ID data type declaring the format for the data of subcomponent <main type>.  
	 */
	public void setDataSubtype(String strDataSubtype)
	{
		if(strDataSubtype!=null)
			setComponentType(3, strDataSubtype);
		else
			setDataSubtype((ID)null);
	}

	/**
	 *Sets the DataSubtype.
	 *@param objID Specifies an ID data type declaring the format for the data of subcomponent <main type>. 
	 */
	public void setDataSubtype(ID objID)
	{
		setComponentType(3,objID);
	}

	/**
	 *Sets the Encoding.
	 *@param strEncoding Specifies the type of encoding used to represent successive octets of binary data 
	 * 						as displayable ASCII characters. [Refer to HL7 Table 0299 - Encoding]
	 * 						Valid values A,Hex,Base64.
	 */
	public void setEncoding(String strEncoding)
	{
		if(strEncoding!=null)
			setComponentType(4, strEncoding);
		else
			setEncoding((ID)null);
	}

	/**
	 *Sets the Encoding.
	 *@param objID Specifies the type of encoding used to represent successive octets of binary data 
	 * 						as displayable ASCII characters. [Refer to HL7 Table 0299 - Encoding]
	 * 						Valid values A,Hex,Base64.
	 */
	public void setEncoding(ID objID)
	{
		setComponentType(4,objID);
	}

	/**
	 * Sets the Data.
	 * @param strData Specifies displayable ASCII characters which constitute the data to be sent from source 
	 * 					application to destination application.
	 */
	public void setData(String strData)
	{
		if(strData!=null)
			setComponentType(5, strData);
		else
			setData((TX)null);
	}  
	/**
	 * Sets the Data.
	 * @param objTX Specifies displayable ASCII characters which constitute the data to be sent from source 
	 * 					application to destination application.
	 */
	public void setData(TX objTX)
	{
		setComponentType(5,objTX);
	}  
	/**
	 * Returns Source Application
	 * @return HD - data type.
	 */
	public HD getSourceApplication()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(1);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (HD)objDataCollection.elementAt(0);
		else
			return null;
	}

	/**
	 * Returns Type of Data
	 * @return ID - data type.
	 */
	public ID getTypeOfData()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(2);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (ID)objDataCollection.elementAt(0);
		else
			return null;
	}

	/**
	 * Returns Data Subtype 
	 * @return ID - data type.
	 */
	public ID getDataSubtype()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(3);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (ID)objDataCollection.elementAt(0);
		else
			return null;
	}

	/**
	 * Returns Encoding
	 * @return ID - data type.
	 */
	public ID getEncoding()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(4);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (ID)objDataCollection.elementAt(0);
		else
			return null;
	}

	/**
	 * Returns Data
	 * @return TX - data type.
	 */
	public TX getData()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(5);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (TX)objDataCollection.elementAt(0);
		else
			return null;
	}

	/**
	 * The Method validates the Input value,and if validated properly then returns true.
	 * Here the validetion is based on the compulsory fields for the Datatype. 
	 * @return boolean - true or false.
	 * @throws InvalidDataTypeException - if Invalid values is entered or Mandatory field is missing.
	 */
	public boolean validate()throws InvalidDataTypeException
	{
		super.validate();
		if(getComponentType(2)==null)
		{
			throw new MissingValueException(this.getClass().getSimpleName() + "Type of Data");
		}
		if(getComponentType(4)==null)
		{
			throw new MissingValueException(this.getClass().getSimpleName() + "Encoding");
		}
		if(getComponentType(5)==null)
		{
			throw new MissingValueException(this.getClass().getSimpleName() + "Data");
		}
		return true;
	}

	/**
	 * This method checks the equality of Object.
	 * @param objObject object of Object class.
	 * @return boolean - shows success or failure of the comparison.
	 */
	public boolean equals(Object objObject)
	{
		if((objObject == null ) || (!(objObject instanceof ED)))
			return false;

		if(this.getSourceApplication() == null)
		{
			if(((ED)objObject).getSourceApplication() != null)
				return false;
		}
		else if(false == (this.getSourceApplication().equals(((ED)objObject).getSourceApplication())))
			return false;

		if(this.getTypeOfData() == null)
		{
			if(((ED)objObject).getTypeOfData() != null)
				return false;
		}
		else if(false == (this.getTypeOfData().equals(((ED)objObject).getTypeOfData())))
			return false;

		if(this.getDataSubtype() == null)
		{
			if(((ED)objObject).getDataSubtype() != null)
				return false;
		}
		else if(false == (this.getDataSubtype().equals(((ED)objObject).getDataSubtype())))
			return false;

		if(this.getEncoding() == null)
		{
			if(((ED)objObject).getEncoding() != null)
				return false;
		}
		else if(false == (this.getEncoding().equals(((ED)objObject).getEncoding())))
			return false;

		if(this.getData() == null)
		{
			if(((ED)objObject).getData() != null)
				return false;
		}
		else if(false == (this.getData().equals(((ED)objObject).getData())))
			return false;

		return true;
	}
}
