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

/**
 * CNN - Composite ID Number and Name Simplified<p>
 * This data type specifies a person using both an identifier and the person's name
 * <p>Represents an HL7 CNN data type. 
 * This data type has the following attributes:</p><p>
 *  1.  ID Number (ST)<b>Optional</b><br>
 *  2. Family Name (ST)<b>Optional</b><br>
 *  3. Given Name (ST)<b>Optional</b><br>
 *  4. Second and Further Given Names or Initials Thereof (ST)<b>Optional</b><br>
 *  5. Suffix (e.g., JR or III) (ST)<b>Optional</b><br>
 *  6. Prefix (e.g., DR) (ST)<b>Optional</b><br>
 *  7. Degree (e.g., MD (IS)<b>Optional</b><br>
 *  8. Source Table (IS)<b>Conditional</b><br>
 *  9. Assigning Authority - Namespace ID (IS)<b>Conditional</b><br>
 * 10. Assigning Authority - Universal ID (ST)<b>Conditional</b><br>
 * 11. Assigning Authority - 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 CNN extends CompositeDataType    
{
	


	/**
	 * Default Constructor for CNN
	 */
	public CNN()
	{
		super(EnumDataType.HL7_DT_CNN);
		 
	}

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


	/**
	 * Constructor for CNN 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 CNN(int iMinLength, int iMaxLength, String strCLength,int iTableNo,int iComponentType) throws InvalidComponentTypeException
	{
		super(EnumDataType.HL7_DT_CNN);
		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 CNN 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 CNN(int iComponentType,IDataTypeMap objIDataTypeMap)
	{
		super(EnumDataType.HL7_DT_CNN);
		this.objIDataTypeMap = objIDataTypeMap;
		this.iComponentType = iComponentType;
		if(!isValidComponentType())
		{
			throw new InvalidComponentTypeException(this.getClass().getSimpleName());
		}
	}
	/**
	 * Constructor for CNN 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 CNN(int iComponentType,int iMinLength, int iMaxLength, String strCLength,IDataTypeMap objIDataTypeMap)
	{
		super(EnumDataType.HL7_DT_CNN);
		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 CNN 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 CNN(int iComponentType,int iMinLength, int iMaxLength, String strCLength,int ITableNo,IDataTypeMap objIDataTypeMap)
	{
		super(EnumDataType.HL7_DT_CNN);
		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 coded ID according to a user-defined table. 
	 */
	public void setIdNumber(String strIdNumber)
	{
		if(strIdNumber!=null)
			setComponentType(1, strIdNumber);
		else
			setIdNumber((ST)null);
	}


	/**
	 *Sets the Id Number.
	 *@param objST Specifies coded ID according to a user-defined table.
	 */
	public void setIdNumber(ST objST)
	{
		setComponentType(1,objST);
	}

	/**
	 *Sets the Family Name.
	 *@param strFamilyName Specifies the person's family name in a string format. 
	 */
	public void setFamilyName(String strFamilyName)
	{
		if(strFamilyName!=null)
			setComponentType(2, strFamilyName);
		else
			setFamilyName((ST)null);
	}

	/**
	 *Sets the Family Name.
	 *@param objST Specifies the person's family name in a string format.
	 */
	public void setFamilyName(ST objST)
	{
		setComponentType(2,objST);
	}

	/**
	 *Sets the Given Name.
	 *@param strGivenName Specifies a first name. 
	 */
	public void setGivenName(String strGivenName)
	{
		if(strGivenName!=null)
			setComponentType(3, strGivenName);
		else
			setFamilyName((ST)null);
	}

	/**
	 *Sets the Given Name.
	 *@param objST Specifies a first name.
	 */
	public void setGivenName(ST objST)
	{
		setComponentType(3,objST);
	}

	/**
	 *Sets the Second Names Or Initials.
	 *@param strSecondNamesOrInitials Specifies second names or initials. 
	 */
	public void setSecondNamesOrInitials(String strSecondNamesOrInitials)
	{
		if(strSecondNamesOrInitials!=null)
			setComponentType(4, strSecondNamesOrInitials);
		else
			setFamilyName((ST)null);
	}

	/**
	 *Sets the Second Names Or Initials.
	 *@param objST Specifies second names or initials.
	 */
	public void setSecondNamesOrInitials(ST objST)
	{
		setComponentType(4,objST);
	}

	/**
	 *Sets the Suffix
	 *@param strSuffix Specifies a name suffix (e.g., Jr. or III). 
	 */
	public void setSuffix(String strSuffix)
	{
		if(strSuffix!=null)
			setComponentType(5, strSuffix);
		else
			setFamilyName((ST)null);
	}

	/**
	 *Sets the Suffix
	 *@param objST Specifies a name suffix (e.g., Jr. or III).
	 */
	public void setSuffix(ST objST)
	{
		setComponentType(5,objST);
	}

	/**
	 *Sets the Prefix
	 *@param strPrefix Specifies a name prefix (e.g., Dr.). 
	 */
	public void setPrefix(String strPrefix)
	{
		if(strPrefix!=null)
			setComponentType(6, strPrefix);
		else
			setFamilyName((ST)null);
	}

	/**
	 *Sets the Prefix
	 *@param objST Specifies a name prefix (e.g., Dr.).
	 */
	public void setPrefix(ST objST)
	{
		setComponentType(6,objST);
	}

	/**
	 *Sets the Degree
	 *@param strDegree Specifies an educational degree (e.g., MD).[Refer to User-defined Table 0360  Degree] 
	 */
	public void setDegree(String strDegree)
	{
		if(strDegree!=null)
			setComponentType(7, strDegree);
		else
			setDegree((IS)null);
	}

	/**
	 *Sets the Degree
	 *@param objIS Specifies an educational degree (e.g., MD).[Refer to User-defined Table 0360  Degree]
	 */
	public void setDegree(IS objIS)
	{
		setComponentType(7,objIS);
	}

	/**
	 *Sets the Source Table
	 *@param strSourceTable [Refer to User-defined Table 0297 - CN ID source] 
	 */
	public void setSourceTable(String strSourceTable)
	{
		if(strSourceTable!=null)
			setComponentType(8, strSourceTable);
		else
			setDegree((IS)null);
	}

	/**
	 *Sets the Source Table
	 *@param objString [Refer to User-defined Table 0297 - CN ID source]
	 */
	public void setSourceTable(IS objIS)
	{
		setComponentType(8,objIS);
	}

	/**
	 *Sets the Assigning Authority Namespace Id.
	 *@param strAssigningAuthorityNamespaceId Specifies a unique name of the system (or organization or agency or 
	 * 											 department) that creates the data.
	 */
	public void setAssigningAuthorityNamespaceId(String strAssigningAuthorityNamespaceId)
	{
		if(strAssigningAuthorityNamespaceId!=null)
			setComponentType(9, strAssigningAuthorityNamespaceId);
		else
			setAssigningAuthorityNamespaceId((IS)null);
	}

	/**
	 *Sets the Assigning Authority Namespace Id.
	 *@param objIS Specifies a unique name of the system (or organization or agency or 
	 * 											 department) that creates the data.
	 */
	public void setAssigningAuthorityNamespaceId(IS objIS)
	{
		setComponentType(9,objIS);
	}

	/**
	 *sets the Assigning Authority Universal Id.
	 *@param strAssigningAuthorityUniversalId If CNN.11 is valued, this component must be valued. 
	 */
	public void setAssigningAuthorityUniversalId(String strAssigningAuthorityUniversalId)
	{
		if(strAssigningAuthorityUniversalId!=null)
			setComponentType(10, strAssigningAuthorityUniversalId);
		else
			setAssigningAuthorityUniversalId((ST)null);
	}

	/**
	 *sets the Assigning Authority Universal Id.
	 *@param objST If CNN.11 is valued, this component must be valued.
	 */
	public void setAssigningAuthorityUniversalId(ST objST)
	{
		setComponentType(10,objST);
	}

	/**
	 *Sets the Assigning Authority Universal Id Type.
	 *@param strAssigningAuthorityUniversalIdType If CNN.10 is valued, this component must be valued. 
	 */
	public void setAssigningAuthorityUniversalIdType(String strAssigningAuthorityUniversalIdType)
	{
		if(strAssigningAuthorityUniversalIdType!=null)
			setComponentType(11, strAssigningAuthorityUniversalIdType);
		else
			setAssigningAuthorityUniversalIdType((ID)null);
	}

	/**
	 *Sets the Assigning Authority Universal Id Type.
	 *@param objID If CNN.10 is valued, this component must be valued.
	 */
	public void setAssigningAuthorityUniversalIdType(ID objID)
	{
		setComponentType(11,objID);
	}

	/**
	 * Returns ID Number
	 * @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 Family Name
	 * @return ST - data type.
	 */
	public ST getFamilyName()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(2);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (ST)objDataCollection.elementAt(0);
		else
			return null;
	}

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

	/**
	 * Returns Second and Further Given Names or Initials Thereof
	 * @return ST - data type.
	 */
	public ST getSecondGivenNamesInitials()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(4);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (ST)objDataCollection.elementAt(0);
		else
			return null;
	}

	/**
	 * Returns Suffix (e.g., JR or III)
	 * @return ST - data type.
	 */
	public ST getSuffix()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(5);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (ST)objDataCollection.elementAt(0);
		else
			return null;
	}

	/**
	 * Returns Prefix (e.g., DR)
	 * @return ST - data type.
	 */
	public ST getPrefix()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(6);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (ST)objDataCollection.elementAt(0);
		else
			return null;
	}

	/**
	 * Returns Degree (e.g., MD
	 * @return IS - data type.
	 */
	public IS getDegree()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(7);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (IS)objDataCollection.elementAt(0);
		else
			return null;
	}

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

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

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

	/**
	 * Returns Assigning Authority  - Universal ID Type
	 * @return ID - data type.
	 */
	public ID getAssigningAuthorityUniversalIDType()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(11);
		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(getComponentType(1)!=null)
		{
			if(getComponentType(8)!=null ||getComponentType(9)!=null || (getComponentType(10)!=null &&getComponentType(11)!=null))
			{
				return true;
			}
			else 
			{
				String strMessage = "If \"ID Number\" is present either \"Source Table\" Or \"Assigning Authority   - Namespace ID\" Or " +
				" both \"Assigning Authority  - Universal ID\" And \"Assigning Authority  - Universal ID Type\" must be valued.";
				throw new InvalidValueException(strMessage);
			}
		}
		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 CNN)))
			return false;

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

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

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

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

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

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

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

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

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

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

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

		return true;
	}
}
