/*******************************************************************************
 *  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.NM;
import cdac.medinfo.sdk.hl7282.baselibs.TX;
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;

/***
 * OM6 - Observations that are Calculated from Other Observations Segments.<p>
 * The Technical Steward for the OM6 segment is ORDERS.
 * This segment contains the information about quantities that are derived from one or more
 * other quantities or direct observations by mathematical or logical means
 * <p>Represents an HL7 OM6 message segment. 
 * This segment has the following fields:</p><p>
 * OM6-1: Sequence Number - Test/Observation Master File (NM)<b>Optional</b><br> 
 * OM6-2: Derivation Rule (TX)<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 OM6 extends Segment   
{
	

	/**
	 * Constructs the OM6 segment.
	 */
	public OM6()
	{super(EnumSegments.HL7_SEG_OM6);
	}

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

	/**
	 * Retrieves the Derivation Rule.
	 * This field used when there are patient variables that are derived from one or more other patient variables.
	 * @return TX - derivationRule
	 */
	public TX getDerivationRule() 
	{
		ISingleCollection<IDataType> objDataCollection =getDataType(657);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (TX)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;
	}

	/**
	 * Set the Derivation Rule.
	 * This field used when there are patient variables that are derived from one or more other patient variables.
	 * @param strDerivationRule Use as DerivationRule.
	 */ 
	public void setDerivationRule(String strDerivationRule)
	{
		TX derivationRule  = (TX)getDataElement(657); 
		derivationRule.setValue(strDerivationRule); 
		setDerivationRule(derivationRule);
	} 

	/** 
	 * Set the Derivation Rule.
	 * This field used when there are patient variables that are derived from one or more other patient variables.
	 * @param objderivationRule Object of Derivation Rule.
	 */ 
	public void setDerivationRule(TX objderivationRule)
	{
		setDataType(657,objderivationRule);
	}

	/** 
	 * 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 objsequenceNumberTestObservationMasterFile Object of Sequence Number Test Observation Master File.
	 */ 
	public void setSequenceNumberTestObservationMasterFile(NM objsequenceNumberTestObservationMasterFile)
	{
		setDataType(586,objsequenceNumberTestObservationMasterFile);
	}

	/**
	 * Validates HL7 Message objects .
	 * Validate method checks for validation each and every segments in side the Message object against specified message Structure. 
	 * Validation chcecks 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);
		}
	}
}