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

/**
 * SRT - Sort order<p>
 * This data type specifies those parameters by which the response will be sorted and by what method. 
 * <p>Represents an HL7 SRT data type. 
 * This data type has the following attributes:</p><p>
 *  1. Sort-by Field (ST)<b>Required</b><br>
 *  2. Sequencing (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 SRT extends CompositeDataType    
{
	


	/**
	 * Default Constructor for SRT
	 */
	public SRT()
	{
		super(EnumDataType.HL7_DT_SRT);
		 
	}

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


	/**
	 * Constructor for SRT 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 SRT(int iMinLength, int iMaxLength, String strCLength,int iTableNo,int iComponentType) throws InvalidComponentTypeException
	{
		super(EnumDataType.HL7_DT_SRT);
		this.iMinLength = iMinLength;
		this.iMaxLength = iMaxLength;
		this.iConformanceLength = strCLength;
		this.iTableNo = iTableNo;
		this.iComponentType = iComponentType;
	}

	/**
	 * Sets the Sort by Field.
	 * @param strSortbyField Identifies the field by which the response will be sorted.
	 */
	public void setSortbyField(String strSortbyField)
	{
		if(strSortbyField!=null)
			setComponentType(1, strSortbyField);
		else
			setSortbyField((ST)null);
	}

	/**
	 * Sets the Sort by Field.
	 * @param objST Identifies the field by which the response will be sorted.
	 */
	public void setSortbyField(ST objST)
	{
		setComponentType(1,objST);
	}

	/**
	 * Sets the Sequencing
	 * @param strSequencing Identifies how the field or parameter will be sorted;
	 * and, if sorted, whether the sort will be case sensitive (the default) or not.
	 */
	public void setSequencing(String strSequencing)
	{
		if(strSequencing!=null)
			setComponentType(2, strSequencing);
		else
			setSequencing((ID)null);
	}

	/**
	 * Sets the Sequencing
	 * @param objID Identifies how the field or parameter will be sorted;
	 * and, if sorted, whether the sort will be case sensitive (the default) or not.
	 */
	public void setSequencing(ID objID)
	{
		setComponentType(2,objID);
	}
	/**
	 * Returns Sort-by Field
	 * @return ST - data type.
	 */
	public ST getSortByField()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(1);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (ST)objDataCollection.elementAt(0);
		else
			return null;
	}

	/**
	 * Returns Sequencing
	 * @return ID - data type.
	 */
	public ID getSequencing()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(2);
		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() + "Sort-by Field");
		}
		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 SRT)))
			return false;

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

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

		return true;
	}
}
