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

/**
 * CP - Composite Price<p>
 * This data type is often used to define a repeating field within a given segment.
 * <p>Represents an HL7 CP data type. 
 * This data type has the following attributes:</p><p>
 *  1.  Price (MO)<b>Required</b><br>
 *  2. Price Type (ID)<b>Optional</b><br>
 *  3. From Value (NM)<b>Optional</b><br>
 *  4. To Value (NM)<b>Optional</b><br>
 *  5. Range Units (CWE)<b>Conditional</b><br>
 *  6. Range Type (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 CP extends CompositeDataType    
{
	


	/**
	 * Default Constructor for CP
	 */
	public CP()
	{
		super(EnumDataType.HL7_DT_CP);
		 
	}

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


	/**
	 * Constructor for CP 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 CP(int iMinLength, int iMaxLength, String strCLength,int iTableNo,int iComponentType) throws InvalidComponentTypeException
	{
		super(EnumDataType.HL7_DT_CP);
		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 CP 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 CP(int iComponentType,IDataTypeMap objIDataTypeMap)
	{
		super(EnumDataType.HL7_DT_CP);
		this.objIDataTypeMap = objIDataTypeMap;
		this.iComponentType = iComponentType;
		if(!isValidComponentType())
		{
			throw new InvalidComponentTypeException(this.getClass().getSimpleName());
		}
	}
	/**
	 * Constructor for CP 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 CP(int iComponentType,int iMinLength, int iMaxLength, String strCLength,IDataTypeMap objIDataTypeMap)
	{
		super(EnumDataType.HL7_DT_CP);
		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 CP 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 CP(int iComponentType,int iMinLength, int iMaxLength, String strCLength,int ITableNo,IDataTypeMap objIDataTypeMap)
	{
		super(EnumDataType.HL7_DT_CP);
		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 Price
	 * @param objPrice Specifies an amount of money and the denomination in which it is expressed.
	 */
	public void setPrice(MO objPrice)
	{
		setComponentType(1,objPrice);
	}

	/**
	 *Sets the Price Type.
	 *@param strPriceType Specifies a coded value, data type ID.[Refer to HL7 Table 0205]
	 * 						  Suggested Values AP,DC,IC,PF,TF,TP,UP.
	 */
	public void setPriceType(String strPriceType)
	{
		if(strPriceType!=null)
			setComponentType(2, strPriceType);
		else
			setPriceType((ID)null);
	}

	/**
	 *Sets the Price Type.
	 *@param objID Specifies a coded value, data type ID.[Refer to HL7 Table 0205]
	 * 						  Suggested Values AP,DC,IC,PF,TF,TP,UP.
	 */
	public void setPriceType(ID objID)
	{
		setComponentType(2,objID);
	}

	/**
	 *Sets the From Value.
	 *@param strFromValue Spefies the "range" alongwith strToValue. 
	 */
	public void setFromValue(String strFromValue)
	{
		if(strFromValue!=null)
			setComponentType(3, strFromValue);
		else
			setFromValue((NM)null);
	}

	/**
	 *Sets the From Value.
	 *@param objNM Spefies the "range" alongwith strToValue.
	 */
	public void setFromValue(NM objNM)
	{
		setComponentType(3,objNM);
	}

	/**
	 *Sets the To Value.
	 *@param strToValue Spefies the "range" alongwith strFromValue. 
	 */
	public void setToValue(String strToValue)
	{
		if(strToValue!=null)
			setComponentType(4, strToValue);
		else
			setToValue((NM)null);
	}

	/**
	 *Sets the To Value.
	 *@param objNM Spefies the "range" alongwith strFromValue.
	 */
	public void setToValue(NM objNM)
	{
		setComponentType(4,objNM);
	}

	/**
	 *Sets the Range Units.
	 *@param objRangeUnits Specifies This describes the units associated with the range, e.g., seconds, minutes
	 */
	public void setRangeUnits(CWE objRangeUnits)
	{
		setComponentType(5,objRangeUnits);
	}

	/**
	 *Sets the Range Type.
	 *@param strRangeType Refer to HL7 Table 0298 - CP range type for valid values.(P,F) 
	 */
	public void setRangeType(String strRangeType)
	{
		if(strRangeType!=null)
			setComponentType(6, strRangeType);
		else
			setRangeType((ID)null);
	}

	/**
	 *Sets the Range Type.
	 *@param objID Refer to HL7 Table 0298 - CP range type for valid values.(P,F)
	 */
	public void setRangeType(ID objID)
	{
		setComponentType(6,objID);
	}

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

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

	/**
	 * Returns From Value
	 * @return NM - data type.
	 */
	public NM getFromValue()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(3);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (NM)objDataCollection.elementAt(0);
		else
			return null;
	}

	/**
	 * Returns To Value
	 * @return NM - data type.
	 */
	public NM getToValue()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(4);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (NM)objDataCollection.elementAt(0);
		else
			return null;
	}

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

	/**
	 * Returns Range Type
	 * @return ID - data type.
	 */
	public ID getRangeType()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(6);
		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)
		{
			throw new MissingValueException(this.getClass().getSimpleName() + "Price");	
		}
		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 CP)))
			return false;

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

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

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

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

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

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

		return true;
	}
}
