/*******************************************************************************
 *  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;

/**
 * CX - Extended Composite ID with Check Digit<p>
 * This data type type is used for specifying an identifier 
 * with its associated administrative detail.
 * <p>Represents an HL7 CX data type. 
 * This data type has the following attributes:</p><p>
 *  1.  ID Number (ST)<b>Required</b><br>
 *  2. Check Digit (ST)<b>Optional</b><br>
 *  3. Check Digit Scheme (ID)<b>Optional</b><br>
 *  4. Assigning Authority (HD)<b>Conditional</b><br>
 *  5. Identifier Type Code (ID)<b>Required</b><br>
 *  6. Assigning Facility (HD)<b>Optional</b><br>
 *  7. Effective Date (DT)<b>Optional</b><br>
 *  8. Expiration Date (DT)<b>Optional</b><br>
 *  9. Assigning Jurisdiction (CWE)<b>Conditional</b><br>
 * 10. Assigning Agency or Department (CWE)<b>Conditional</b><br><p>
 * 11. Security Check or Department (ST)<b>Optional</b><br><p>
 * 12. Security Check Scheme (ID)<b>Optional</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 CX extends CompositeDataType    
{
	


	/**
	 * Default Constructor for CX
	 */
	public CX()
	{
		super(EnumDataType.HL7_DT_CX);
		 
	}

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



	/**
	 * Constructor  for CX 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 CX(int iMinLength, int iMaxLength, String strCLength,int iComponentType) throws InvalidComponentTypeException
	{
		super(EnumDataType.HL7_DT_CX);
		this.iMinLength = iMinLength;
		this.iMaxLength = iMaxLength;
		this.iConformanceLength = strCLength;
		this.iComponentType = iComponentType;
		if(!isValidComponentType())
		{
			throw new InvalidComponentTypeException(this.getClass().getSimpleName());
		}
	}
	/**
	 * Constructor for CX 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 CX(int iComponentType,IDataTypeMap objIDataTypeMap)
	{
		super(EnumDataType.HL7_DT_CX);
		this.objIDataTypeMap = objIDataTypeMap;
		this.iComponentType = iComponentType;
		if(!isValidComponentType())
		{
			throw new InvalidComponentTypeException(this.getClass().getSimpleName());
		}
	}
	/**
	 * Constructor for CX 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 CX(int iComponentType,int iMinLength, int iMaxLength, String strCLength,IDataTypeMap objIDataTypeMap)
	{
		super(EnumDataType.HL7_DT_CX);
		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 CX 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 CX(int iComponentType,int iMinLength, int iMaxLength, String strCLength,int ITableNo,IDataTypeMap objIDataTypeMap)
	{
		super(EnumDataType.HL7_DT_CX);
		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 Id Number.
	 *@param strIdNumber Specifies the value of the identifier itself. 
	 */
	public void setIdNumber(String strIdNumber)
	{
		if(strIdNumber!=null)
			setComponentType(1, strIdNumber);
		else
			setIdNumber((ST)null);
	}


	/**
	 *Sets the Id Number.
	 *@param objST Specifies the value of the identifier itself.
	 */
	public void setIdNumber(ST objST)
	{
		setComponentType(1,objST);
	}

	/**
	 *Sets the Check Digit.
	 *@param strCheckDigit It is part of the identifying number used in the sending application. 
	 */
	public void setCheckDigit(String strCheckDigit)
	{
		if(strCheckDigit!=null)
			setComponentType(2, strCheckDigit);
		else
			setCheckDigit((ST)null);
	}

	/**
	 *Sets the Check Digit.
	 *@param objST It is part of the identifying number used in the sending application.
	 */
	public void setCheckDigit(ST objST)
	{
		setComponentType(2,objST);
	}

	/**
	 *Sets the Check Digit Scheme.
	 *@param strCheckDigitScheme  Contains the code identifying the check digit scheme employed.
	 * 								Suggested Values NP,ISO,M10,M11.[Refer table No.0061].
	 */
	public void setCheckDigitScheme(String strCheckDigitScheme)
	{
		if(strCheckDigitScheme!=null)
			setComponentType(3, strCheckDigitScheme);
		else
			setCheckDigitScheme((ID)null);
	}

	/**
	 *Sets the Check Digit Scheme.
	 *@param objID  Contains the code identifying the check digit scheme employed.
	 * 								Suggested Values NP,ISO,M10,M11.[Refer table No.0061].
	 */
	public void setCheckDigitScheme(ID objID)
	{
		setComponentType(3,objID);
	}

	/**
	 * Sets the Assigning Authority.
	 * @param objAssigningAuthority The assigning authority is a unique name of the system (or organization or agency 
	 * 									or department) that creates the data.
	 */
	public void setAssigningAuthority(HD objAssigningAuthority)
	{
		setComponentType(4,objAssigningAuthority);
	}

	/**
	 *Sets the Identifier Type Code.
	 *@param strIdentifierTypeCode Specifies a code corresponding to the type of identifier.In some cases, this code may be
	 * 								 used as a qualifier to the "Assigning authority" component.[Refer table No 0203]
	 */
	public void setIdentifierTypeCode(String strIdentifierTypeCode)
	{
		if(strIdentifierTypeCode!=null)
			setComponentType(5, strIdentifierTypeCode);
		else
			setIdentifierTypeCode((ID)null);
	}

	/**
	 *Sets the Identifier Type Code.
	 *@param objID Specifies a code corresponding to the type of identifier.In some cases, this code may be
	 * 								 used as a qualifier to the "Assigning authority" component.[Refer table No 0203]
	 */
	public void setIdentifierTypeCode(ID objID)
	{
		setComponentType(5,objID);
	}

	/**
	 * Sets the Assigning Facility.
	 * @param objAssigningFacility Specifies the place or location identifier where the identifier was first assigned 
	 * 									to the patient.
	 */
	public void setAssigningFacility(HD objAssigningFacility)
	{
		setComponentType(6,objAssigningFacility);
	}

	/**
	 *Sets the Effective Date.
	 *@param strEffectiveDate Specifies the first date, if known, on which the address is valid and active. 
	 */
	public void setEffectiveDate(String strEffectiveDate)
	{
		if(strEffectiveDate!=null)
			setComponentType(7, strEffectiveDate);
		else
			setEffectiveDate((DT)null);
	}

	/**
	 *Sets the Effective Date.
	 *@param objDT Specifies the first date, if known, on which the address is valid and active.
	 */
	public void setEffectiveDate(DT objDT)
	{
		setComponentType(7,objDT);
	}

	/**
	 *Sets the Expiration Date.
	 *@param strExpirationDate Specifies the last date, if known, on which the address is valid and active. 
	 */
	public void setExpirationDate(String strExpirationDate)
	{
		if(strExpirationDate!=null)
			setComponentType(8, strExpirationDate);
		else
			setExpirationDate((DT)null);
	}

	/**
	 *Sets the Expiration Date.
	 *@param objString Specifies the last date, if known, on which the address is valid and active.
	 */
	public void setExpirationDate(DT objDT)
	{
		setComponentType(8,objDT);
	}

	/**
	 *Sets the Assigning Jurisdiction.
	 *@param objAssigningJurisdiction Specifies the geo-political body that assigned the identifier.
	 */
	public void setAssigningJurisdiction(CWE objAssigningJurisdiction)
	{
		setComponentType(9,objAssigningJurisdiction);
	}

	/**
	 *Sets the Assigning Agency Or Department.
	 *@param objAssigningAgencyOrDepartment Specifies agency or department that assigned the identifier.
	 */
	public void setAssigningAgencyOrDepartment(CWE objAssigningAgencyOrDepartment)
	{
		setComponentType(10,objAssigningAgencyOrDepartment);
	}
	
	/**
	 *Sets the Security Check.
	 *@param objST Specifies a version code.
	 */
	public void setSecurityCheck(ST objST)
	{
		setComponentType(11,objST);
	}

	/**
	 *Sets the Security Check.
	 *@param strSecurityCheck communicate a version code that may be assigned to the value given in CX.1 ID. 
	 */
	public void setSecurityCheck(String strSecurityCheck)
	{
		if(strSecurityCheck!=null)
			setComponentType(11, strSecurityCheck);
		else
			setSecurityCheck((ST)null);
	}
	
	/**
	 *Sets the Security Check Scheme.
	 *@param objST Specifies the value of Supplied Identifier.
	 */
	public void setSecurityCheckScheme(ST objST)
	{
		setComponentType(12,objST);
	}

	/**
	 *Sets the Security Check Scheme.
	 *@param strSecurityCheckScheme is used to transmit information intended to validate the veracity of the supplied identifier 
	 *				or the presenter of the identifier.
	 */
	public void setSecurityCheckScheme(String strSecurityCheckScheme)
	{
		if(strSecurityCheckScheme!=null)
			setComponentType(12, strSecurityCheckScheme);
		else
			setSecurityCheckScheme((ST)null);
	}

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

	/**
	 * Returns Check Digit
	 * @return ST - data type.
	 */
	public ST getCheckDigit()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(2);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (ST)objDataCollection.elementAt(0);
		else
			return null;
	}

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

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

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

	/**
	 * Returns Assigning Facility
	 * @return HD - data type.
	 */
	public HD getAssigningFacility()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(6);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (HD)objDataCollection.elementAt(0);
		else
			return null;
	}

	/**
	 * Returns Effective Date
	 * @return DT - data type.
	 */
	public DT getEffectiveDate()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(7);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (DT)objDataCollection.elementAt(0);
		else
			return null;
	}

	/**
	 * Returns Expiration Date
	 * @return DT - data type.
	 */
	public DT getExpirationDate()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(8);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (DT)objDataCollection.elementAt(0);
		else
			return null;
	}

	/**
	 * Returns Assigning Jurisdiction
	 * @return CWE - data type.
	 */
	public CWE getAssigningJurisdiction()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(9);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (CWE)objDataCollection.elementAt(0);
		else
			return null;
	}

	/**
	 * Returns Assigning Agency or Department
	 * @return CWE - data type.
	 */
	public CWE getAssigningAgencyOrDepartment()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(10);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (CWE)objDataCollection.elementAt(0);
		else
			return null;
	}
	
	/**
	 * Returns Security Check
	 * @return ST - data type.
	 */
	public ST getSecurityCheck()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(11);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (ST)objDataCollection.elementAt(0);
		else
			return null;
	}
	
	/**
	 * Returns Assigning Agency or Department
	 * @return ST - data type.
	 */
	public ST getSecurityCheckScheme()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(12);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (ST)objDataCollection.elementAt(0);
		else
			return null;
	}

	/**
	 * The Method validates the Input value,and if validated properly then returns true.
	 * Here the validation 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(1)== null)
			{
				throw new MissingValueException(this.getClass().getSimpleName() + "ID Number");
			}
		
			if(getComponentType(5)== null)
			{
				throw new MissingValueException(this.getClass().getSimpleName() + "Identifier Type Code");
			}
			else
			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 CX)))
			return false;

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

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

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

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

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

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

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

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

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

		if(this.getAssigningAgencyOrDepartment() == null)
		{
			if(((CX)objObject).getAssigningAgencyOrDepartment() != null)
				return false;
		}
		else if(false == (this.getAssigningAgencyOrDepartment().equals(((CX)objObject).getAssigningAgencyOrDepartment())))
			return false;
		
		if(this.getSecurityCheck() == null)
		{
			if(((CX)objObject).getSecurityCheck() != null)
				return false;
		}
		else if(false == (this.getSecurityCheck().equals(((CX)objObject).getSecurityCheck())))
			return false;
		
		if(this.getSecurityCheckScheme() == null)
		{
			if(((CX)objObject).getSecurityCheckScheme() != null)
				return false;
		}
		else if(false == (this.getSecurityCheckScheme().equals(((CX)objObject).getSecurityCheckScheme())))
			return false;

		return true;
	}
}
