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

/**
 * FN - Family Name<p>
 * THis data type allows full specification of the surname of a person. 
 * <p>Represents an HL7 FN data type. 
 * This data type has the following attributes:</p><p>
 *  1. Surname (ST)<b>Required</b><br>
 *  2. Own Surname Prefix (ST)<b>Optional</b><br>
 *  3. Own Surname (ST)<b>Optional</b><br>
 *  4. Surname Prefix From Partner/Spouse (ST)<b>Optional</b><br>
 *  5. Surname From Partner/Spouse (ST)<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 FN extends CompositeDataType    
{
	

	/**
	 * Default Constructor for FN
	 */
	public FN()
	{
		super(EnumDataType.HL7_DT_FN);
		 
	}

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

	/**
	 * Constructor for FN 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 FN(int iMinLength, int iMaxLength, String strCLength,int iTableNo,int iComponentType) throws InvalidComponentTypeException
	{
		super(EnumDataType.HL7_DT_FN);
		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 FN 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 FN(int iComponentType,IDataTypeMap objIDataTypeMap)
	{
		super(EnumDataType.HL7_DT_FN);
		this.objIDataTypeMap = objIDataTypeMap;
		this.iComponentType = iComponentType;
		if(!isValidComponentType())
		{
			throw new InvalidComponentTypeException(this.getClass().getSimpleName());
		}
	}
	/**
	 * Constructor for FN class which takes four 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 FN(int iComponentType,int iMinLength, int iMaxLength, String strCLength,IDataTypeMap objIDataTypeMap)
	{
		super(EnumDataType.HL7_DT_FN);
		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 FN 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 FN(int iComponentType,int iMinLength, int iMaxLength, String strCLength,int ITableNo,IDataTypeMap objIDataTypeMap)
	{
		super(EnumDataType.HL7_DT_FN);
		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 Surname.
	 *@param strSurname Specifies the person's family name.  
	 */
	public void setSurname(String strSurname)
	{
		if(strSurname!=null)
			setComponentType(1, strSurname);
		else
			setSurname((ST)null);
	}


	/**
	 *Sets the Surname.
	 *@param objST Specifies the person's family name. 
	 */
	public void setSurname(ST objST)
	{
		setComponentType(1,objST);
	}

	/**
	 *Sets the OwnSurnamePrefix.
	 *@param strOwnSurnamePrefix Specifies internationalization usage for Germanic languages.  
	 */
	public void setOwnSurnamePrefix(String strOwnSurnamePrefix)
	{
		if(strOwnSurnamePrefix!=null)
			setComponentType(2, strOwnSurnamePrefix);
		else
			setOwnSurnamePrefix((ST)null);
	}

	/**
	 *Sets the OwnSurnamePrefix.
	 *@param objST Specifies internationalization usage for Germanic languages. 
	 */
	public void setOwnSurnamePrefix(ST objST)
	{
		setComponentType(2,objST);
	}

	/**
	 *Sets the OwnSurname.
	 *@param strOwnSurname Specifies The portion of the surname (in most Western usage, the last name)
	 * 						 that is derived from the person's own surname.
	 */
	public void setOwnSurname(String strOwnSurname)
	{
		if(strOwnSurname!=null)
			setComponentType(3, strOwnSurname);
		else
			setOwnSurname((ST)null);
	}

	/**
	 *Sets the OwnSurname.
	 *@param objST Specifies The portion of the surname (in most Western usage, the last name)
	 * 						 that is derived from the person's own surname.
	 */
	public void setOwnSurname(ST objST)
	{
		setComponentType(3,objST);
	}

	/**
	 * Sets the SurnamePrefixFromPartnerSpouse.
	 * @param strSurnamePrefixFromPartnerSpouse Specifies internationalization usage for Germanic languages. An 
	 * 												example of a surname prefix is the "van" in "Ludwig van Beethoven". 
	 */
	public void setSurnamePrefixFromPartnerSpouse(String strSurnamePrefixFromPartnerSpouse)
	{
		if(strSurnamePrefixFromPartnerSpouse!=null)
			setComponentType(4, strSurnamePrefixFromPartnerSpouse);
		else
			setSurnamePrefixFromPartnerSpouse((ST)null);
	}  

	/**
	 * Sets the SurnamePrefixFromPartnerSpouse.
	 * @param objST Specifies internationalization usage for Germanic languages. An 
	 * 												example of a surname prefix is the "van" in "Ludwig van Beethoven". 
	 */
	public void setSurnamePrefixFromPartnerSpouse(ST objST)
	{
		setComponentType(4,objST);
	}

	/**
	 *Sets the SurnameFromPartnerSpouse.
	 *@param strSurnameFromPartnerSpouse Specifies the portion of the person's surname (in most Western usage,
	 * 									 the last name) that is derived from the surname of the person's partner or spouse.
	 */
	public void setSurnameFromPartnerSpouse(String strSurnameFromPartnerSpouse)
	{
		if(strSurnameFromPartnerSpouse!=null)
			setComponentType(5, strSurnameFromPartnerSpouse);
		else
			setSurnameFromPartnerSpouse((ST)null);
	}

	/**
	 *Sets the SurnameFromPartnerSpouse.
	 *@param objST Specifies the portion of the person's surname (in most Western usage,
	 * 									 the last name) that is derived from the surname of the person's partner or spouse.
	 */
	public void setSurnameFromPartnerSpouse(ST objST)
	{
		setComponentType(5,objST);
	}

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

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

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

	/**
	 * Returns Surname Prefix From Partner/Spouse
	 * @return ST - data type.
	 */
	public ST getSurnamePrefixFromPartnerSpouse()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(4);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (ST)objDataCollection.elementAt(0);
		else
			return null;
	}

	/**
	 * Returns Surname From Partner/Spouse
	 * @return ST - data type.
	 */
	public ST getSurnameFromPartnerSpouse()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(5);
		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 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)
		{
			throw new MissingValueException(this.getClass().getSimpleName() + "Surname");
		}
		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 FN)))
			return false;

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

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

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

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

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

		return true;
	}
}
