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

/**
 * QSC - Query selection criteria<p>
 * This data type indicates the conditions that qualify the rows to be returned in the query response. 
 * <p>Represents an HL7 QSC data type. 
 * This data type has the following attributes:</p><p>
 *  1. Segment Field Name (ST)<b>Required</b><br>
 *  2. Relational Operator (ID)<b>Optional</b><br>
 *  3. Value (ST)<b>Optional</b><br>
 *  4. Relational Conjunction (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 QSC extends CompositeDataType   
{
	


	/**
	 * Default Constructor for QSC
	 */
	public QSC()
	{
		super(EnumDataType.HL7_DT_QSC);
		 
	}

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


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

		if(!isValidComponentType())
		{
			throw new InvalidComponentTypeException(this.getClass().getSimpleName());
		}
	}



	/**
	 *Sets the SegmentFieldName.
	 *@param strSegmentFieldName The name of the field that is participating as a qualifier  
	 */
	public void setSegmentFieldName(String strSegmentFieldName)
	{
		if(strSegmentFieldName!=null)
			setComponentType(1, strSegmentFieldName);
		else
			setSegmentFieldName((ST)null);
	}


	/**
	 *Sets the SegmentFieldName.
	 *@param objST The name of the field that is participating as a qualifier 
	 */
	public void setSegmentFieldName(ST objST)
	{
		setComponentType(1,objST);
	}

	/**
	 *Sets the RelationalOperator.
	 *@param strRelationalOperator Refer to HL7 Table 0209 - Relational operator for valid values. 
	 */
	public void setRelationalOperator(String strRelationalOperator)
	{
		if(strRelationalOperator!=null)
			setComponentType(2, strRelationalOperator);
		else
			setRelationalOperator((ID)null);
	}

	/**
	 *Sets the RelationalOperator.
	 *@param objString Refer to HL7 Table 0209 - Relational operator for valid values.
	 */
	public void setRelationalOperator(ID objID)
	{
		setComponentType(2,objID);
	}

	/**
	 *Sets the QSCValue.
	 *@param strQSCValue The value to which the field will be compared. 
	 */
	public void setQSCValue(String strQSCValue)
	{
		if(strQSCValue!=null)
			setComponentType(3, strQSCValue);
		else
			setQSCValue((ST)null);
	}

	/**
	 *Sets the QSCValue.
	 *@param objST The value to which the field will be compared.
	 */
	public void setQSCValue(ST objST)
	{
		setComponentType(3,objST);
	}

	/**
	 *Sets the RelationalConjunction.
	 *@param strRelationalConjunction a conjunction relates this repetition of the field to the next. 
	 */
	public void setRelationalConjunction(String strRelationalConjunction)
	{
		if(strRelationalConjunction!=null)
			setComponentType(4, strRelationalConjunction);
		else
			setRelationalConjunction((ID)null);
	}

	/**
	 *Sets the RelationalConjunction.
	 *@param objString a conjunction relates this repetition of the field to the next.
	 */
	public void setRelationalConjunction(ID objID)
	{
		setComponentType(4,objID);
	}

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

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

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

	/**
	 * Returns Relational Conjunction
	 * @return ID - data type.
	 */
	public ID getRelationalConjunction()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(4);
		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() + "Segment Field Name");
		}
		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 QSC)))
			return false;

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

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

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

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

		return true;
	}
}
