/*******************************************************************************
 *  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.utils.EnumDataType;
import cdac.medinfo.sdk.hl7282.utils.IDataTypeMap;

/**
 * OG - Observation Grouper<p>
 * This data type can be used to define the structure of the observation/result segments (OBX) within an observation report segment (OBR).  
 * <p>Represents an HL7 LA2 data type.
 * This data type has the following attributes:</p><p>
 * 1.	Original Sub-Identifier (ST)<b>Optional</b><br>
 * 2.	Group (NM)<b>Optional</b><br>
 * 3.	Sequence (NM)<b>Optional</b><br>
 * 4.	Identifier (ST)<b>Optional</b><br>
 * 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 OG extends CompositeDataType
{

	
	/**
	 * Default Constructor for OG
	 */
	public OG()
	{
		super(EnumDataType.HL7_DT_OG);
		 
	}
	
	/**
	 * Constructor  for OG 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 OG(int iMinLength, int iMaxLength, String strCLength,int iComponentType) throws InvalidComponentTypeException
	{
		super(EnumDataType.HL7_DT_OG);
		this.iMinLength = iMinLength;
		this.iMaxLength = iMaxLength;
		this.iConformanceLength = strCLength;
		this.iComponentType = iComponentType;
		if(!isValidComponentType())
		{
			throw new InvalidComponentTypeException(this.getClass().getSimpleName());
		}
	}
	
	/**
	 * Constructor for OG 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 OG(int iMinLength, int iMaxLength, String strCLength,int iTableNo,int iComponentType) throws InvalidComponentTypeException
	{
		super(EnumDataType.HL7_DT_OG);
		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 OG 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 OG(int iComponentType,IDataTypeMap objIDataTypeMap)
	{
		super(EnumDataType.HL7_DT_OG);
		this.objIDataTypeMap = objIDataTypeMap;
		this.iComponentType = iComponentType;
		if(!isValidComponentType())
		{
			throw new InvalidComponentTypeException(this.getClass().getSimpleName());
		}
	}
	
	/**
	 * Constructor for OG 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 OG(int iComponentType,int iMinLength, int iMaxLength, String strCLength,IDataTypeMap objIDataTypeMap)
	{
		super(EnumDataType.HL7_DT_OG);
		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 OG 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 OG(int iComponentType,int iMinLength, int iMaxLength, String strCLength,int ITableNo,IDataTypeMap objIDataTypeMap)
	{
		super(EnumDataType.HL7_DT_OG);
		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 Original Sub-identifier.
	 * @param strOriginalSubIdentifier Specifies the street or mailing address of a person or institution.
	 */
	public void setOriginalSubIdentifier(String strOriginalSubIdentifier)
	{
		if(strOriginalSubIdentifier!=null)
			setComponentType(1, strOriginalSubIdentifier);
		else
			setOriginalSubIdentifier((ST)null);
	}
	
	/**
	 * Sets the Original Sub-identifier
	 * @param objST represents the original data type used in OBX-4 to enable backwards compatibility.
	 */
	public void setOriginalSubIdentifier(ST objST) 
	{
		setComponentType(1, objST);
	}
	/**
	 *Sets the Group.
	 *@param strGroup Specifies group and its sequence within the OBR segment. 
	 */

	public void setGroup(String strGroup)

	{
		if(strGroup!=null)
			setComponentType(2, strGroup);
		else
			setGroup((NM)null);
	}

	/**
	 *Sets the Group
	 *@param objGroup Specifies group and its sequence within the OBR segment.
	 */
	public void setGroup(NM objGroup) 
	{
		setComponentType(2, objGroup);
	}
	/**
	 *Sets the Sequence.
	 *@param strSequence Specifies sequence within the group. 
	 */

	public void setSequence(String strSequence)

	{
		if(strSequence!=null)
			setComponentType(3, strSequence);
		else
			setGroup((NM)null);
	}
	
	/**
	 *Sets the Sequence
	 *@param objSequence Specifies sequence within the group.
	 */
	public void setSequence(NM objSequence) 
	{
		setComponentType(3, objSequence);
	}
	/**
	 * Sets the Identifier.
	 * @param strIdentifier Specifies sequence within the group.
	 */
	public void setIdentifier(String strIdentifier)
	{
		if(strIdentifier!=null)
			setComponentType(4, strIdentifier);
		else
			setIdentifier((ST)null);
	}
	/**
	 *Sets the Sequence
	 *@param objSequence Specifies sequence within the group.
	 */
	
	public void setIdentifier(ST objIdentifier) 
	{
		setComponentType(4, objIdentifier);
	}
	
	/**
	 * Returns Original Sub-Identifier. 
	 * @return ST - data type.
	 */
	public ST getOriginalSubIdentifier()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(1);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (ST)objDataCollection.elementAt(0);
		else
			return null;
	}
	/**
	 * Returns Group
	 * @return NM - data type.
	 */
	public NM getGroup()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(2);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (NM)objDataCollection.elementAt(0);
		else
			return null;
	}
	/**
	 * Returns Sequence
	 * @return NM - data type.
	 */
	public NM getSequence()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(3);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (NM)objDataCollection.elementAt(0);
		else
			return null;
	}
	/**
	 * Returns Identifier. 
	 * @return ST - data type.
	 */
	public ST getIdentifier()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(4);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (ST)objDataCollection.elementAt(0);
		else
			return null;
	}
	
	/**
	 * 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 OG)))
			return false;

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

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

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

		if(this.getIdentifier() == null)
		{
			if(((OG)objObject).getIdentifier() != null)
				return false;
		}
		else if(false == (this.getIdentifier().equals(((OG)objObject).getIdentifier())))
			return false;
		
		return true;
	}
}