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

/**
 * OSP - Occurrence span code and date<p>
 * This data type defines A code and the 
 * related dates that identify an event that relates to the payment of the
 *  claim. For example, Prior Stay Dates which is the from/through dates 
 *  given by the patient of any hospital stay that ended within 60 days of 
 *  this hospital or SNF admission.
 *  <p>Represents an HL7 PIP data type. 
 * This data type has the following attributes:</p><p>
 *  1. Occurrence Span Code (CNE)<b>Required</b><br>
 *  2. Occurrence Span Start Date (DT)<b>Conditional</b><br>
 *  3. Occurrence Span Stop Date (DT)<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 OSP extends CompositeDataType    
{
	


	/**
	 * Default Constructor for OSP
	 */
	public OSP()
	{
		super(EnumDataType.HL7_DT_OSP);
		 
	}

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

	/**
	 * Constructor for OSP 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 OSP(int iMinLength, int iMaxLength, String strCLength,int iTableNo,int iComponentType) throws InvalidComponentTypeException
	{
		super(EnumDataType.HL7_DT_OSP);
		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 OSP 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 OSP(int iComponentType,IDataTypeMap objIDataTypeMap)
	{
		super(EnumDataType.HL7_DT_OSP);
		this.objIDataTypeMap = objIDataTypeMap;
		this.iComponentType = iComponentType;
		if(!isValidComponentType())
		{
			throw new InvalidComponentTypeException(this.getClass().getSimpleName());
		}
	}
	/**
	 * Constructor for OSP 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 OSP(int iComponentType,int iMinLength, int iMaxLength, String strCLength,IDataTypeMap objIDataTypeMap)
	{
		super(EnumDataType.HL7_DT_OSP);
		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 OSP 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 OSP(int iComponentType,int iMinLength, int iMaxLength, String strCLength,int ITableNo,IDataTypeMap objIDataTypeMap)
	{
		super(EnumDataType.HL7_DT_OSP);
		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 OccurrenceSpanCode.
	 *@param objOccurrenceSpanCode Specifies the NUBC code itself that identifies an event that relates to the payment of a claim.
	 */
	public void setOccurrenceSpanCode(CNE objOccurrenceSpanCode)
	{
		setComponentType(1,objOccurrenceSpanCode);
	}

	/**
	 *Sets the OccurrenceSpanStartDate.
	 *@param strOccurrenceSpanStartDate Specifies date an event started that relates to the payment of a claim. 
	 */
	public void setOccurrenceSpanStartDate(String strOccurrenceSpanStartDate)
	{
		if(strOccurrenceSpanStartDate!=null)
			setComponentType(2, strOccurrenceSpanStartDate);
		else
			setOccurrenceSpanStartDate((DT)null);
	}

	/**
	 *Sets the OccurrenceSpanStartDate.
	 *@param objDT Specifies date an event started that relates to the payment of a claim.
	 */
	public void setOccurrenceSpanStartDate(DT objDT)
	{
		setComponentType(2,objDT);
	}

	/**
	 *Sets the OccurrenceSpanStopDate.
	 *@param strOccurrenceSpanStopDate Specifies date an event ended that relates to the payment of a claim. 
	 */
	public void setOccurrenceSpanStopDate(String strOccurrenceSpanStopDate)
	{
		if(strOccurrenceSpanStopDate!=null)
			setComponentType(3, strOccurrenceSpanStopDate);
		else
			setOccurrenceSpanStopDate((DT)null);
	}

	/**
	 *Sets the OccurrenceSpanStopDate.
	 *@param objDT Specifies date an event ended that relates to the payment of a claim.
	 */
	public void setOccurrenceSpanStopDate(DT objDT)
	{
		setComponentType(3,objDT);
	}

	/**
	 * Returns Occurrence Span Code
	 * @return CNE - data type.
	 */
	public CNE getOccurrenceSpanCode()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(1);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (CNE)objDataCollection.elementAt(0);
		else
			return null;
	}

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

	/**
	 * Returns Occurrence Span Stop Date
	 * @return DT - data type.
	 */
	public DT getOccurrenceSpanStopDate()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(3);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (DT)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() + "Occurrence Span Code");
		}	
		if(getComponentType(2)==null &getComponentType(3)== null)
		{
			throw new MissingValueException(this.getClass().getSimpleName() + "At least one of 'Occurrence Span Start Date','Occurrence Span Stop Date' both should be present");
		}	
		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 OSP)))
			return false;

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

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

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

		return true;
	}
}
