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

/**
 * ERL - Error Location<p>
 * This data type identifies the segment and its constituent where an error has occurred.
 * <p>Represents an HL7 ERL data type. 
 * This data type has the following attributes:</p><p>
 *  1. Segment ID (ST)<b>Required</b><br>
 *  2. Segment Sequence (NM)<b>Required</b><br>
 *  3. Field Position (NM)<b>Optional</b><br>
 *  4. Field Repetition (NM)<b>Optional</b><br>
 *  5. Component Number (NM)<b>Optional</b><br>
 *  6. Sub-Component Number (NM)<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 ERL extends CompositeDataType    
{
	

	/**
	 * Default Constructor for ERL
	 */
	public ERL()
	{
		super(EnumDataType.HL7_DT_ERL);
		 
	}

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

	/**
	 * Constructor for ERL 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 ERL(int iMinLength, int iMaxLength, String strCLength,int iTableNo,int iComponentType) throws InvalidComponentTypeException
	{
		super(EnumDataType.HL7_DT_ERL);
		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 ERL 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 ERL(int iComponentType,IDataTypeMap objIDataTypeMap)
	{
		super(EnumDataType.HL7_DT_ERL);
		this.objIDataTypeMap = objIDataTypeMap;
		this.iComponentType = iComponentType;
		if(!isValidComponentType())
		{
			throw new InvalidComponentTypeException(this.getClass().getSimpleName());
		}
	}
	/**
	 * Constructor for ERL 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 ERL(int iComponentType,int iMinLength, int iMaxLength, String strCLength,IDataTypeMap objIDataTypeMap)
	{
		super(EnumDataType.HL7_DT_ERL);
		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 ERL 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 ERL(int iComponentType,int iMinLength, int iMaxLength, String strCLength,int ITableNo,IDataTypeMap objIDataTypeMap)
	{
		super(EnumDataType.HL7_DT_ERL);
		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 SegmentId.
	 *@param strSegmentId Specifies the 3-letter name for the segment.  
	 */
	public void setSegmentId(String strSegmentId)
	{
		if(strSegmentId!=null)
			setComponentType(1, strSegmentId);
		else
			setSegmentId((ST)null);
	}


	/**
	 *Sets the SegmentId.
	 *@param objST Specifies the 3-letter name for the segment. 
	 */
	public void setSegmentId(ST objST)
	{
		setComponentType(1,objST);
	}

	/**
	 *Sets the SegmentSequence.
	 *@param strSegmentSequence Specifies the segment occurrence within the message. 
	 */
	public void setSegmentSequence(String strSegmentSequence)
	{
		if(strSegmentSequence!=null)
			setComponentType(2, strSegmentSequence);
		else
			setSegmentSequence((NM)null);
	}

	/**
	 *Sets the SegmentSequence.
	 *@param objNM Specifies the segment occurrence within the message.
	 */
	public void setSegmentSequence(NM objNM)
	{
		setComponentType(2,objNM);
	}

	/**
	 *Sets the FieldPosition.
	 *@param strFieldPosition Specifies the number of the field within the segment.  
	 */
	public void setFieldPosition(String strFieldPosition)
	{
		if(strFieldPosition!=null)
			setComponentType(3, strFieldPosition);
		else
			setFieldPosition((NM)null);
	}

	/**
	 *Sets the FieldPosition.
	 *@param objNM Specifies the number of the field within the segment. 
	 */
	public void setFieldPosition(NM objNM)
	{
		setComponentType(3,objNM);
	}

	/**
	 *Sets the FieldRepetition.
	 *@param strFieldRepetition Specifies the repetition number of the field.  
	 */
	public void setFieldRepetition(String strFieldRepetition)
	{
		if(strFieldRepetition!=null)
			setComponentType(4, strFieldRepetition);
		else
			setFieldRepetition((NM)null);
	}

	/**
	 *Sets the FieldRepetition.
	 *@param objNM Specifies the repetition number of the field. 
	 */
	public void setFieldRepetition(NM objNM)
	{
		setComponentType(4,objNM);
	}

	/**
	 *Sets the ComponentNumber.
	 *@param strComponentNumber Specifies the number of the component within the field.  
	 */
	public void setComponentNumber(String strComponentNumber)
	{
		if(strComponentNumber!=null)
			setComponentType(5, strComponentNumber);
		else
			setComponentNumber((NM)null);
	}

	/**
	 *Sets the ComponentNumber.
	 *@param objNM Specifies the number of the component within the field. 
	 */
	public void setComponentNumber(NM objNM)
	{
		setComponentType(5,objNM);
	}

	/**
	 *Sets the SubComponentNumber.
	 *@param strSubComponentNumber Specifies the number of the sub-component within the component. 
	 */
	public void setSubComponentNumber(String strSubComponentNumber)
	{
		if(strSubComponentNumber!=null)
			setComponentType(6, strSubComponentNumber);
		else
			setSubComponentNumber((NM)null);
	}

	/**
	 *Sets the SubComponentNumber.
	 *@param objNM Specifies the number of the sub-component within the component.
	 */
	public void setSubComponentNumber(NM objNM)
	{
		setComponentType(6,objNM);
	}

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

	/**
	 * Returns Segment Sequence
	 * @return NM - data type.
	 */
	public NM getSegmentSequence()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(2);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (NM)objDataCollection.elementAt(0);
		else
			return null;
	}

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

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

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

	/**
	 * Returns Sub-Component Number
	 * @return NM - data type.
	 */
	public NM getSubComponentNumber()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(6);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (NM)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() + "Segment ID");
		}
		if(getComponentType(2)==null)
		{
			throw new MissingValueException(this.getClass().getSimpleName() + "Segment Sequence");
		}

		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 ERL)))
			return false;

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

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

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

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

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

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

		return true;
	}
}