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

/**
 * RFR - Reference range<p>
 * This data type specifies a reference range and its supporting detail. 
 * <p>Represents an HL7 RFR data type. 
 * This data type has the following attributes:</p><p>
 *  1. Numeric Range (NR)<b>Required</b><br>
 *  2. Administrative Sex (CWE)<b>Optional</b><br>
 *  3. Age Range (NR)<b>Optional</b><br>
 *  4. Gestational Age Range (NR)<b>Optional</b><br>
 *  5. Species (ST)<b>Optional</b><br>
 *  6. Race/subspecies (ST)<b>Optional</b><br>
 *  7. Conditions (TX)<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 RFR extends CompositeDataType    
{
	


	/**
	 * Default Constructor for RFR
	 */
	public RFR()
	{
		super(EnumDataType.HL7_DT_RFR);
		 
	}

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


	/**
	 * Constructor for RFR 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 RFR(int iMinLength, int iMaxLength, String strCLength,int iTableNo,int iComponentType) throws InvalidComponentTypeException
	{
		super(EnumDataType.HL7_DT_RFR);
		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 RFR 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 RFR(int iComponentType,IDataTypeMap objIDataTypeMap)
	{
		super(EnumDataType.HL7_DT_RFR);
		this.objIDataTypeMap = objIDataTypeMap;
		this.iComponentType = iComponentType;
		if(!isValidComponentType())
		{
			throw new InvalidComponentTypeException(this.getClass().getSimpleName());
		}
	}
	/**
	 * Constructor for RFR 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 RFR(int iComponentType,int iMinLength, int iMaxLength, String strCLength,IDataTypeMap objIDataTypeMap)
	{
		super(EnumDataType.HL7_DT_RFR);
		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 RFR 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 RFR(int iComponentType,int iMinLength, int iMaxLength, String strCLength,int ITableNo,IDataTypeMap objIDataTypeMap)
	{
		super(EnumDataType.HL7_DT_RFR);
		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 NumericRange.
	 * @param objNumericRange This component specifies the numeric interval of the reference data. 
	 */
	public void setNumericRange(NR objNumericRange)
	{
		setComponentType(1,objNumericRange);
	}

	/**
	 *Sets the AdministrativeSex.
	 *@param strAdministrativeSex This component specifies which gender for which the reference range is valid.  
	 */
	public void setAdministrativeSex(String strAdministrativeSex)
	{
		if(strAdministrativeSex!=null)
			setComponentType(2, strAdministrativeSex);
		else
			setAdministrativeSex((CWE)null);
	}
	/**
	 *Sets the AdministrativeSex.
	 *@param objCWE This component specifies which gender for which the reference range is valid. 
	 */
	public void setAdministrativeSex(CWE objCWE)
	{
		setComponentType(2,objCWE);
	}

	/**
	 * Sets the AgeRange.
	 * @param objAgeRange This component specifies the age range for which the reference range is valid.
	 */
	public void setAgeRange(NR objAgeRange)
	{
		setComponentType(3,objAgeRange);
	}

	/**
	 * Sets the GestationalAgeRange.
	 * @param objGestationalAgeRange This component specifies the gestational age range for which the reference range is valid. 
	 */
	public void setGestationalAgeRange(NR objGestationalAgeRange)
	{
		setComponentType(4,objGestationalAgeRange);
	}

	/**
	 *Sets the Species.
	 *@param strSpecies This component specifies the species for which the reference range is valid.  
	 */
	public void setSpecies(String strSpecies)
	{
		if(strSpecies!=null)
			setComponentType(5, strSpecies);
		else
			setSpecies((ST)null);
	}

	/**
	 *Sets the Species.
	 *@param objString This component specifies the species for which the reference range is valid. 
	 */
	public void setSpecies(ST objST)
	{
		setComponentType(5,objST);
	}

	/**
	 *Sets the Racesubspecies.
	 *@param strRacesubspecies This component specifies the race or subspecies for which the reference range is valid.  
	 */
	public void setRacesubspecies(String strRacesubspecies)
	{
		if(strRacesubspecies!=null)
			setComponentType(6, strRacesubspecies);
		else
			setRacesubspecies((ST)null);
	}

	/**
	 *Sets the Racesubspecies.
	 *@param objST This component specifies the race or subspecies for which the reference range is valid. 
	 */
	public void setRacesubspecies(ST objST)
	{
		setComponentType(6,objST);
	}

	/**
	 *Sets the Conditions.
	 *@param strConditions This component specifies any arbitrary condition for which the reference range is valid. 
	 */
	public void setConditions(String strConditions)
	{
		if(strConditions!=null)
			setComponentType(7, strConditions);
		else
			setConditions((TX)null);
	}

	/**
	 *Sets the Conditions.
	 *@param objTX This component specifies any arbitrary condition for which the reference range is valid.
	 */
	public void setConditions(TX objTX)
	{
		setComponentType(7,objTX);
	}

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

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

	/**
	 * Returns Age Range
	 * @return NR - data type.
	 */
	public NR getAgeRange()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(3);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (NR)objDataCollection.elementAt(0);
		else
			return null;
	}

	/**
	 * Returns Gestational Age Range
	 * @return NR - data type.
	 */
	public NR getGestationalAgeRange()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(4);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (NR)objDataCollection.elementAt(0);
		else
			return null;
	}

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

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

	/**
	 * Returns Conditions
	 * @return TX - data type.
	 */
	public TX getConditions()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(7);
		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(1)== null)
		{
			throw new MissingValueException(this.getClass().getSimpleName() + "Numeric Range");
		}
		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 RFR)))
			return false;

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

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

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

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

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

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

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

		return true;
	}
}
