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

/**
 * PIP - Practitioner institutional privileges<p>
 * This data type specifies the institutional privileges with associated detail granted to a provider.
 * <p>Represents an HL7 PIP data type. 
 * This data type has the following attributes:</p><p>
 *  1. Privilege (CWE)<b>Required</b><br>
 *  2. Privilege Class (CWE)<b>Optional</b><br>
 *  3. Expiration Date (DT)<b>Optional</b><br>
 *  4. Activation Date (DT)<b>Optional</b><br>
 *  5. Facility (EI)<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 PIP extends CompositeDataType   
{
	


	/**
	 * Default Constructor for PIP
	 */
	public PIP()
	{
		super(EnumDataType.HL7_DT_PIP);
		 
	}

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

	/**
	 * Constructor for PIP 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 PIP(int iMinLength, int iMaxLength, String strCLength,int iTableNo,int iComponentType) throws InvalidComponentTypeException
	{
		super(EnumDataType.HL7_DT_PIP);
		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 PIP 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 PIP(int iComponentType,IDataTypeMap objIDataTypeMap)
	{
		super(EnumDataType.HL7_DT_PIP);
		this.objIDataTypeMap = objIDataTypeMap;
		this.iComponentType = iComponentType;
		if(!isValidComponentType())
		{
			throw new InvalidComponentTypeException(this.getClass().getSimpleName());
		}
	}
	/**
	 * Constructor for PIP class which takes fibe 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 PIP(int iComponentType,int iMinLength, int iMaxLength, String strCLength,IDataTypeMap objIDataTypeMap)
	{
		super(EnumDataType.HL7_DT_PIP);
		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 PIP 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 PIP(int iComponentType,int iMinLength, int iMaxLength, String strCLength,int ITableNo,IDataTypeMap objIDataTypeMap)
	{
		super(EnumDataType.HL7_DT_PIP);
		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 Privilege.
	 *@param objPrivilege Specifies the institutional privilege itself. 
	 */
	public void setPrivilege(CWE objPrivilege)
	{
		setComponentType(1,objPrivilege);
	}

	/**
	 *Sets the PrivilegeClass.
	 *@param objPrivilegeClass Specifies the class category of institutional privilege. 
	 */
	public void setPrivilegeClass(CWE objPrivilegeClass)
	{
		setComponentType(2,objPrivilegeClass);
	}

	/**
	 *Sets the ExpirationDate.
	 *@param strExpirationDate Specifies the date the institutional privilege is/was no longer valid. 
	 */
	public void setExpirationDate(String strExpirationDate)
	{
		if(strExpirationDate!=null)
			setComponentType(3, strExpirationDate);
		else
			setExpirationDate((DT)null);
	}

	/**
	 *Sets the ExpirationDate.
	 *@param objDT Specifies the date the institutional privilege is/was no longer valid.
	 */
	public void setExpirationDate(DT objDT)
	{
		setComponentType(3,objDT);
	}

	/**
	 *Sets the ActivationDate.
	 *@param strActivationDate Specifies the date the institutional privilege became/becomes valid. 
	 */
	public void setActivationDate(String strActivationDate)
	{
		if(strActivationDate!=null)
			setComponentType(4, strActivationDate);
		else
			setActivationDate((DT)null);
	}

	/**
	 *Sets the ActivationDate.
	 *@param objDT Specifies the date the institutional privilege became/becomes valid.
	 */
	public void setActivationDate(DT objDT)
	{
		setComponentType(4,objDT);
	}

	/**
	 *Sets the Facility.
	 *@param objFacility Specifies the facility in which the institutional privilege is/was valid.
	 */
	public void setFacility(EI objFacility)
	{
		setComponentType(5,objFacility);
	}

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

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

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

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

	/**
	 * Returns Facility
	 * @return EI - data type.
	 */
	public EI getFacility()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(5);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (EI)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() + "Privilege");
		}	
		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 PIP)))
			return false;

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

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

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

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

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

		return true;
	}
}
