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

import cdac.medinfo.sdk.hl7282.baselibs.CWE;
import cdac.medinfo.sdk.hl7282.baselibs.NM;
import cdac.medinfo.sdk.hl7282.baselibs.ST;
import cdac.medinfo.sdk.hl7282.commoninterface.IDataType;
import cdac.medinfo.sdk.hl7282.commoninterface.ISingleCollection;
import cdac.medinfo.sdk.hl7282.exceptions.FieldMissingException;
import cdac.medinfo.sdk.hl7282.exceptions.InvalidSegmentException;
import cdac.medinfo.sdk.hl7282.utils.EnumSegments;
import cdac.medinfo.sdk.hl7282.utils.SingleCollection;

/***
 * OM5 - Observation Batteries (Sets) Segment<p>
 * The Technical Steward for the OM5 segment is ORDERS.
 * This segment contains the information about batteries and supersets.
 * <p>Represents an HL7 OM5 message segment. 
 * This segment has the following fields:</p><p>
 * OM5-1: Sequence Number - Test/Observation Master File (NM)<b>Optional</b><br> 
 * OM5-2: Test/Observations Included within an Ordered Test Battery (CWE)<b>Optional</b><br> 
 * OM5-3: Observation ID Suffixes (ST)<b>Optional</b><br><p>
 * This class provides mechanism to validate message against HL7 v 2.8.2 standard.
 * The class provides accessors & mutators for the elements mentioned above.
 * The class has validate method which validates Message against HL7 v 2.8.2 standard.
 *
 * <p>User can also create Segment through {@link SegmentFactory}
 */
 public class OM5 extends Segment   
{
	

	/**
	 * Constructs the OM5 segment.
	 */
	public OM5()
	{super(EnumSegments.HL7_SEG_OM5);
	}

	//Integer values used in getter/setter is unique item id defined 
	//for each attribute.

	/**
	 * Retrieves the Observation ID Suffixes.
	 * This field contains the tests or procedures that produce a type which uses observation ID.
	 * @return ST - observationIDSuffixes
	 */
	public ST getObservationIDSuffixes() 
	{
		ISingleCollection<IDataType> objDataCollection =getDataType(656);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (ST)objDataCollection.elementAt(0);
		else
			return null;
	}

	/**
	 * Retrieves the Sequence Number Test Observation Master File.
	 * This field contains the same value as the sequence number of the associated OM1 segment.
	 * @return NM - sequenceNumberTestObservationMasterFile
	 */
	public NM getSequenceNumberTestObservationMasterFile() 
	{
		ISingleCollection<IDataType> objDataCollection =getDataType(586);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (NM)objDataCollection.elementAt(0);
		else
			return null;
	}

	/**
	 * Retrieves the Test Observations Included within an Ordered Test Battery.
	 * This field contains the codes and names of all tests/observations included within a single battery.
	 * @return ISingleCollection<CWE> - testObservationsIncludedwithinanOrderedTestBattery
	 */
	public ISingleCollection<CWE> getTestObservationsIncludedwithinanOrderedTestBattery() 
	{
		ISingleCollection<IDataType> objDataCollection =getDataType(655);
		ISingleCollection<CWE> objDataList = new SingleCollection<CWE>(); 
		if(objDataCollection != null && objDataCollection.size() != 0)
		{
			for(IDataType objDataType : objDataCollection)
			{
				CWE objCWE = (CWE)objDataType;
				objDataList.add(objCWE);
			}
		}
		return objDataList;
	}

	/**
	 * Set the Observation ID Suffixes.
	 * This field contains the tests or procedures that produce a type which uses observation ID.
	 * @param strObservationIDSuffixes Use as ObservationIDSuffixes.
	 */ 
	public void setObservationIDSuffixes (String strObservationIDSuffixes) 
	{
		ST observationIDSuffixes  = (ST)getDataElement(656);
		observationIDSuffixes.setValue(strObservationIDSuffixes); 
		setObservationIDSuffixes(observationIDSuffixes);

	} 

	/** 
	 * Set the Observation ID Suffixes.
	 * This field contains the tests or procedures that produce a type which uses observation ID.
	 * @param objobservationIDSuffixes Object of Observation ID Suffixes.
	 */ 
	public void setObservationIDSuffixes(ST objobservationIDSuffixes)
	{
		setDataType(656,objobservationIDSuffixes);

	}

	/** 
	 * Set the Sequence Number Test Observation Master File.
	 * This field contains the same value as the sequence number of the associated OM1 segment.
	 * @param strSequenceNumberTestObservationMasterFile Specifies the maximum number of days that the insurance policy will cover.
	 */ 
	public void setSequenceNumberTestObservationMasterFile (String strSequenceNumberTestObservationMasterFile) 
	{
		NM sequenceNumberTestObservationMasterFile  = (NM)getDataElement(586);
		sequenceNumberTestObservationMasterFile.setValue(strSequenceNumberTestObservationMasterFile); 
		setSequenceNumberTestObservationMasterFile(sequenceNumberTestObservationMasterFile);

	} 

	/** Set the Sequence Number Test Observation Master File.
	 * This field contains the same value as the sequence number of the associated OM1 segment.
	 * @param strSequenceNumberTestObservationMasterFile Specifies the maximum number of days that the insurance policy will cover.
	 */ 
	public void setSequenceNumberTestObservationMasterFile(NM objsequenceNumberTestObservationMasterFile)
	{
		setDataType(586,objsequenceNumberTestObservationMasterFile);

	}

	/**
	 * Add the Test Observations Included within an Ordered Test Battery.
	 * This field contains the codes and names of all tests/observations included within a single battery.
	 * @param strIdentifier Specifies of characters (the code) that uniquely identifies the item being referenced. 
	 * @param strText Specifies The descriptive or textual name of the identifier, e.g., myocardial infarction. 
	 * @param strNameOfCodingSystem Identifies the coding scheme being used in the identifier component.
	 * @param strAlternateIdentifier Specifies an alternate sequence of characters (the code) that uniquely identifies the item being referenced.
	 * @param strAlternateText Specifies the descriptive or textual name of the alternate identifier. See usage note in section introduction.
	 * @param strNameOfAlternateCodingSystem Identifies the coding scheme being used in the alternate identifier component. 
	 * @param strCodingSystemVersionID A sequence of characters that uniquely identifies a second alternate code.
	 * @param strAlternateCodingSystemVersionId This component carries the version for the coding system identified by components 4-6.
	 * @param strOriginalText The text as seen and/or selected by the user who entered the data.
	 * @param strSecondAlternateIdentifier A sequence of characters that uniquely identifies a second alternate code.
	 * @param strSecondAlternateText The descriptive or textual name of the alternate identifier.
	 * @param strNameofSecondAlternateCodingSystem Identifies the coding scheme being used in the alternate identifier component.
	 * @param strSecondAlternateCodingSystemVersionID This component carries the version for the coding system identified by components 10-12.
	 * @param strCodingSystemOID This component contains the ISO Object Identifier (OID) for the coding system or value set named in CWE.3.
	 * @param strValueSetOID This component contains the ISO Object Identifier (OID) to allow identification of the value set from which the value in CWE.1 is obtained.
	 * @param strValueSetVersionID This component carries the version for the value set identified by CWE.15. The version is a date. The date is the date/time that the value set being used was published.
	 * @param strAlternateCodingSystemOID This component contains the ISO Object Identifier (OID) for the coding system or value set named in CWE.6.
	 * @param strAlternateValueSetOID This component contains the ISO Object Identifier (OID) to allow identification of the value set from which the value in CWE.4 is obtained.
	 * @param strAlternateValueSetVersionID This component carries the version for the value set identified by CWE.18. The version is a date.
	 * @param strSecondAlternateCodingSystemOID This component contains the ISO Object Identifier (OID) for the coding system or value set named in CWE.12.
	 * @param strSecondAlternateValueSetOID This component contains the ISO Object Identifier (OID) to allow identification of the value set from which the value in CWE.10 is obtained.
	 * @param strSecondAlternateValueSetVersionID This component carries the version for the value set identified by CWE.21. The version is a date.
	 */
	public void addTestObservationsIncludedwithinanOrderedTestBattery (String strIdentifier,String strText,String strNameOfCodingSystem,
			String strAlternateIdentifier,String strAlternateText,String strNameOfAlternateCodingSystem, String strCodingSystemVersionID, String 				strAlternateCodingSystemVersionId,
			String strOriginalText, String  strSecondAlternateIdentifier, String strSecondAlternateText, 
			String strNameofSecondAlternateCodingSystem, String strSecondAlternateCodingSystemVersionID, String strCodingSystemOID, String strValueSetOID,
			String strValueSetVersionID, String strAlternateCodingSystemOID, String strAlternateValueSetOID, String strAlternateValueSetVersionID,
			String  strSecondAlternateCodingSystemOID, String strSecondAlternateValueSetOID, String strSecondAlternateValueSetVersionID)
	{
		CWE cwe  = (CWE)getDataElement(655);
		cwe.setIdentifier(strIdentifier);
		cwe.setText(strText);
		cwe.setNameOfCodingSystem(strNameOfCodingSystem);
		cwe.setAlternateIdentifier(strAlternateIdentifier);
		cwe.setAlternateText(strAlternateText);
		cwe.setNameOfAlternateCodingSystem(strNameOfAlternateCodingSystem); 
		cwe.setCodingSystemVersionID(strCodingSystemVersionID);
		cwe.setAlternateCodingSystemVersionId(strAlternateCodingSystemVersionId);
		cwe.setOriginalText(strOriginalText);
		cwe.setSecondAlternateIdentifier(strSecondAlternateIdentifier);
		cwe.setSecondAlternateText(strSecondAlternateText);
		cwe.setNameofSecondAlternateCodingSystem(strNameofSecondAlternateCodingSystem);
		cwe.setSecondAlternateCodingSystemVersionID(strSecondAlternateCodingSystemVersionID);
		cwe.setCodingSystemOID(strCodingSystemOID);
		cwe.setValueSetOID(strValueSetOID);
		cwe.setValueSetVersionID(strValueSetVersionID);
		cwe.setAlternateCodingSystemOID(strAlternateCodingSystemOID);
		cwe.setAlternateValueSetOID(strAlternateValueSetOID);
		cwe.setAlternateValueSetVersionID(strAlternateValueSetVersionID);
		cwe.setSecondAlternateCodingSystemOID(strSecondAlternateCodingSystemOID);
		cwe.setSecondAlternateValueSetOID(strSecondAlternateValueSetOID);
		cwe.setSecondAlternateValueSetVersionID(strSecondAlternateValueSetVersionID);
		addTestObservationsIncludedwithinanOrderedTestBattery(cwe);
	}

	/**
	 * Add the Test Observations Included within an Ordered Test Battery.
	 * This field contains the codes and names of all tests/observations included within a single battery.
	 * @param objtestObservationsIncludedwithinanOrderedTestBattery Object of Test Observations Included within an Ordered Test Battery.
	 */
	public void addTestObservationsIncludedwithinanOrderedTestBattery(CWE objtestObservationsIncludedwithinanOrderedTestBattery)
	{
		addDataType(655,objtestObservationsIncludedwithinanOrderedTestBattery);
	}

	/**
	 * Set the Test Observations Included within an Ordered Test Battery.
	 * This field contains the codes and names of all tests/observations included within a single battery.
	 * @param objtestObservationsIncludedwithinanOrderedTestBatteryList List of Test Observations Included within an Ordered Test Battery.
	 */
	@SuppressWarnings("unchecked")
	public void setTestObservationsIncludedwithinanOrderedTestBattery(ISingleCollection<CWE> objtestObservationsIncludedwithinanOrderedTestBatteryList)
	{
		ISingleCollection<IDataType> objCollection = null;
		ISingleCollection<?> objUnknownCollection = (ISingleCollection<?>)objtestObservationsIncludedwithinanOrderedTestBatteryList;
		objCollection = (ISingleCollection<IDataType>)objUnknownCollection;
		setDataType(655,objCollection);
	}

	/**
	 * Validates HL7 Message objects .
	 * Validate method checks for validation each and every segments in side the Message object against specified message Structure. 
	 * Validation checks include optionality of segment or group.
	 * It also verifies the number of times segment or group is repeated in message.
	 * e.g 
	 * {PSH} while parsing PSH segment validate method will check for its mandatory presence.
	 * [PSH] in this case validate will throw exception if one tries to enter more than one PSH segment.
	 * @return boolean - true or false.
	 * @throws InvalidSegmentException Throws when Segment Validation fails.
	 */	
	@Override
	public boolean validate() throws InvalidSegmentException 
	{
		try
		{
			/*
			 * All Fields are optional
			 */
			super.validate();

			return true;
		}
		catch(FieldMissingException ex)
		{
			throw new InvalidSegmentException(enumSegments.getValue(),ex);
		}
	}
}