/*******************************************************************************
 *  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.InvalidValueException;
import cdac.medinfo.sdk.hl7282.exceptions.MissingValueException;
import cdac.medinfo.sdk.hl7282.utils.EnumDataType;
import cdac.medinfo.sdk.hl7282.utils.IDataTypeMap;

/**
 * EI - Entity Identifier<p>
 * The EI entity identifier defines a given entity within a specified series of identifiers.
 * <p>Represents an HL7 EI data type. 
 * This data type has the following attributes:</p><p>
 *  1. Entity Identifier (ST)<b>Optional</b><br>
 *  2. Namespace ID (IS)<b>Optional</b><br>
 *  3. Universal ID (ST)<b>Conditional</b><br>
 *  4. Universal ID Type (ID)<b>Conditional</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 EI extends CompositeDataType    
{
	

	/**
	 * Default Constructor for EI
	 */
	public EI()
	{
		super(EnumDataType.HL7_DT_EI);
		 
	}

	/**
	 * Constructor  for CQ 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 EI(int iMinLength, int iMaxLength, String strCLength,int iComponentType) throws InvalidComponentTypeException
	{
		super(EnumDataType.HL7_DT_EI);
		this.iMinLength = iMinLength;
		this.iMaxLength = iMaxLength;
		this.iConformanceLength = strCLength;
		this.iComponentType = iComponentType;
		if(!isValidComponentType())
		{
			throw new InvalidComponentTypeException(this.getClass().getSimpleName());
		}
	}

	/**
	 * Constructor for EI 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 EI(int iMinLength, int iMaxLength, String strCLength,int iTableNo,int iComponentType) throws InvalidComponentTypeException
	{
		super(EnumDataType.HL7_DT_EI);
		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 EI 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 EI(int iComponentType,IDataTypeMap objIDataTypeMap)
	{
		super(EnumDataType.HL7_DT_EI);
		this.objIDataTypeMap = objIDataTypeMap;
		this.iComponentType = iComponentType;
		if(!isValidComponentType())
		{
			throw new InvalidComponentTypeException(this.getClass().getSimpleName());
		}
	}
	/**
	 * Constructor for EI 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 EI(int iComponentType,int iMinLength, int iMaxLength, String strCLength,IDataTypeMap objIDataTypeMap)
	{
		super(EnumDataType.HL7_DT_EI);
		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 EI 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 EI(int iComponentType,int iMinLength, int iMaxLength, String strCLength,int ITableNo,IDataTypeMap objIDataTypeMap)
	{
		super(EnumDataType.HL7_DT_EI);
		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 EntityIdentifier.
	 *@param strEntityIdentifier Specifies to be unique within the series of identifiers created by the 
	 * 								assigning authority, defined by a hierarchic designator
	 */
	public void setEntityIdentifier(String strEntityIdentifier)
	{
		if(strEntityIdentifier!=null)
			setComponentType(1, strEntityIdentifier);
		else
			setEntityIdentifier((ST)null);
	}


	/**
	 *Sets the EntityIdentifier.
	 *@param objST Specifies to be unique within the series of identifiers created by the 
	 * 								assigning authority, defined by a hierarchic designator
	 */
	public void setEntityIdentifier(ST objST)
	{
		setComponentType(1,objST);
	}

	/**
	 * Sets the NamespaceId.
	 * @param strNamespaceId Specifies the assigning authority is a unique identifier of the system 
	 * 							(or organization or agency or department) that creates the data. 
	 */
	public void setNamespaceId(String strNamespaceId)
	{
		if(strNamespaceId!=null)
			setComponentType(2, strNamespaceId);
		else
			setNamespaceId((IS)null);
	}  

	/**
	 * Sets the NamespaceId.
	 * @param objIS Specifies the assigning authority is a unique identifier of the system 
	 * 							(or organization or agency or department) that creates the data. 
	 */
	public void setNamespaceId(IS objIS)
	{
		setComponentType(2,objIS);
	}  

	/**
	 *Sets the UniversalId.
	 *@param strUniversalId Specifies a string formatted according to the scheme defined by the third component universal ID type.
	 * 							It is intended to be unique over time within the UID type.
	 */
	public void setUniversalId(String strUniversalId)
	{
		if(strUniversalId!=null)
			setComponentType(3, strUniversalId);
		else
			setUniversalId((ST)null);
	}

	/**
	 *Sets the UniversalId.
	 *@param objST Specifies a string formatted according to the scheme defined by the third component universal ID type.
	 * 							It is intended to be unique over time within the UID type.
	 */
	public void setUniversalId(ST objST)
	{
		setComponentType(3,objST);
	}

	/**
	 *Sets the UniversalIdType.
	 *@param strUniversalIdType Governs the interpretation of the Universal ID. 
	 */
	public void setUniversalIdType(String strUniversalIdType)
	{
		if(strUniversalIdType!=null)
			setComponentType(4, strUniversalIdType);
		else
			setUniversalIdType((ID)null);
	}

	/**
	 *Sets the UniversalIdType.
	 *@param objID Governs the interpretation of the Universal ID.
	 */
	public void setUniversalIdType(ID objID)
	{
		setComponentType(4,objID);
	}

	/**
	 * Returns Entity Identifier.
	 * @return ST - data type.
	 */
	public ST getEntityIdentifier()
	{
		ISingleCollection<IDataType> objDataCollection = getComponentType(1);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (ST)objDataCollection.elementAt(0);
		else
			return null;
	}

	/**
	 * Returns Namespace Id.
	 * @return IS - data type.
	 */
	public IS getNamespaceId()
	{
		ISingleCollection<IDataType> objDataCollection = getComponentType(2);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (IS)objDataCollection.elementAt(0);
		else
			return null;
	}

	/**
	 * Returns Universal Id.
	 * @return ST - data type.
	 */
	public ST getUniversalId()
	{
		ISingleCollection<IDataType> objDataCollection = getComponentType(3);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (ST)objDataCollection.elementAt(0);
		else
			return null;
	}

	/**
	 * Returns Universal Id Type.
	 * @return ID - data type.
	 */
	public ID getUniversalIdType()
	{
		ISingleCollection<IDataType> objDataCollection = getComponentType(4);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (ID)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 the second component is present the The remaioning two are optional.
		 * if third is present then forth is required and vice a versa.
		 * if all three are present then return true.
		 * if all not present return false.
		 */
		if(getComponentType(2)!=null)
		{
			if(getComponentType(3)!=null &&getComponentType(4)!=null)
				return true;
			if(getComponentType(3)==null &&getComponentType(4)==null)
				return true;
			else 
			{
				String strMessage = "'UniversalID','UniversalID Type' both should be present or both should not be present.";
				throw new InvalidValueException(strMessage);
			}
		}
		else if(getComponentType(3)!=null &&getComponentType(4)!=null)
			return true;
		else
		{
			throw new MissingValueException(this.getClass().getSimpleName() + "");
		}
	}


	/**
	 * The method checks for the equality with other EI DataType. 
	 * @param  objDataType conatains object of another DataType to be checked for equality.
	 * @return boolean [true/false]
	 */
	public boolean equals(IDataType objDataType)
	{
		if((objDataType instanceof EI) && (objDataType != null))
		{
			if(
					(this.getEntityIdentifier().equals(((EI)objDataType).getEntityIdentifier()))
					&& (this.getNamespaceId().equals(((EI)objDataType).getNamespaceId()))
					&& (this.getUniversalId().equals(((EI)objDataType).getUniversalId()))
					&& (this.getUniversalIdType().equals(((EI)objDataType).getUniversalIdType()))
			)
				return true;
		}
		return false;

	}
	/**
	 * 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 EI)))
			return false;

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

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

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

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

		return true;
	}
}
