/*******************************************************************************
 *  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.DTM;
import cdac.medinfo.sdk.hl7282.baselibs.EI;
import cdac.medinfo.sdk.hl7282.baselibs.FN;
import cdac.medinfo.sdk.hl7282.baselibs.HD;
import cdac.medinfo.sdk.hl7282.baselibs.ST;
import cdac.medinfo.sdk.hl7282.baselibs.XCN;
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;

/**
 * VAR - Variance Segment<p>
 * Variance segment contains the data necessary to describe differences 
 * that may have occurred at the time when a healthcare event was documented.
 * <p>Represents an HL7 VAR message segment. 
 * This segment has the following fields:</p><p>
 * VAR-1: Variance Instance ID (EI)<b>Required</b><br> 
 * VAR-2: Documented Date/Time (DTM)<b>Required</b><br> 
 * VAR-3: Stated Variance Date/Time (DTM)<b>Optional</b><br> 
 * VAR-4: Variance Originator (XCN)<b>Optional</b><br> 
 * VAR-5: Variance Classification (CWE)<b>Optional</b><br> 
 * VAR-6: Variance Description (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 VAR extends Segment    
{
	


	/**
	 * Constucts the VAR segment.
	 */
	public VAR()
	{super(EnumSegments.HL7_SEG_VAR);
	}

	/**
	 * Retrieves the Variance Instance ID.
	 * @return EI - varianceInstanceID
	 */
	public EI getVarianceInstanceID() 
	{
		ISingleCollection<IDataType> objDataCollection =getDataType(1212);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (EI)objDataCollection.elementAt(0);
		else
			return null;
	}

	/**
	 * Retrieves the Documented Date Time.
	 * This field specify the time stamp that identifies the timed occurrence of the variance documentation.
	 * @return DTM - documentedDateTime
	 */
	public DTM getDocumentedDateTime() 
	{
		ISingleCollection<IDataType> objDataCollection =getDataType(1213);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (DTM)objDataCollection.elementAt(0);
		else
			return null;
	}

	/**
	 * Retrieves the Stated Variance Date Time.
	 * This field specify the time stamp that identifies a stated time of the variance which may be 
	 * different than the time it is documented.
	 * @return DTM - statedVarianceDateTime
	 */
	public DTM getStatedVarianceDateTime() 
	{
		ISingleCollection<IDataType> objDataCollection =getDataType(1214);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (DTM)objDataCollection.elementAt(0);
		else
			return null;
	}

	/**
	 * Retrieves the Variance Originator.
	 * @return ISingleCollection<XCN> - varianceOriginator
	 */
	public ISingleCollection<XCN> getVarianceOriginator() 
	{
		ISingleCollection<IDataType> objDataCollection =getDataType(1215);
		ISingleCollection<XCN> objDataList = new SingleCollection<XCN>(); 
		if(objDataCollection != null && objDataCollection.size() != 0)
		{
			for(IDataType objDataType : objDataCollection)
			{
				XCN objXCN = (XCN)objDataType;
				objDataList.add(objXCN);
			}
		}
		return objDataList;
	}	
	/**
	 * Retrieves the Variance Classification.  
	 * @return CWE - varianceClassification
	 */
	public CWE getVarianceClassification() 
	{
		ISingleCollection<IDataType> objDataCollection =getDataType(1216);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (CWE)objDataCollection.elementAt(0);
		else
			return null;
	}

	/**
	 * Retrieves the Variance Description.
	 * @return ISingleCollection<ST> - varianceDescription
	 */
	public ISingleCollection<ST> getVarianceDescription() 
	{
		ISingleCollection<IDataType> objDataCollection =getDataType(1217);
		ISingleCollection<ST> objDataList = new SingleCollection<ST>(); 
		if(objDataCollection != null && objDataCollection.size() != 0)
		{
			for(IDataType objDataType : objDataCollection)
			{
				ST objST = (ST)objDataType;
				objDataList.add(objST);
			}
		}
		return objDataList;
	}	
	/**
	 * Set the Documented Date Time.
	 * This field specify the time stamp that identifies the timed occurrence of the variance documentation.
	 * @param strTime Specifies a point in time using a 24-hour clock notation. 
	 */ 
	public void setDocumentedDateTime (String strTime) 
	{
		DTM documentedDateTime  = (DTM)getDataElement(1213);
		documentedDateTime.setValue(strTime);		
		setDocumentedDateTime(documentedDateTime);

	} 

	/** Set the Documented Date Time.
	 * This field specify the time stamp that identifies the timed occurrence of the variance documentation.
	 * @param objdocumentedDateTime
	 */ 
	public void setDocumentedDateTime(DTM objdocumentedDateTime)
	{
		setDataType(1213,objdocumentedDateTime);

	}


	/**
	 * Set the Stated Variance Date Time.
	 * This field specify the time stamp that identifies a stated time of the variance which may be 
	 * different than the time it is documented. 
	 * @param strTime Specifies a point in time using a 24-hour clock notation. 
	 */ 
	public void setStatedVarianceDateTime (String strTime)
	{
		DTM statedVarianceDateTime  = (DTM)getDataElement(1214);
		statedVarianceDateTime.setValue(strTime);		 
		setStatedVarianceDateTime(statedVarianceDateTime);

	} 

	/** Set the Stated Variance Date Time.
	 * This field specify the time stamp that identifies a stated time of the variance which may be 
	 * different than the time it is documented. 
	 * @param objstatedVarianceDateTime
	 */ 
	public void setStatedVarianceDateTime(DTM objstatedVarianceDateTime)
	{
		setDataType(1214,objstatedVarianceDateTime);

	}

	/**
	 * Set the Variance Classification.
	 * This field identifies a categorical set of variances.  Classification may be used by applications for presentation and processing functions.
	 * @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 setVarianceClassification (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(1216);
		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);
		setVarianceClassification(cwe);

	} 

	/**
	 * Set the Variance Classification.
	 * This field identifies a categorical set of variances.  Classification may be used by applications for presentation and processing functions.
	 * @param objvarianceClassification.
	 */ 
	public void setVarianceClassification(CWE objvarianceClassification)
	{
		setDataType(1216,objvarianceClassification);

	}


	/**
	 * Set the Variance Description.
	 * @param strVarianceDescription String which to set as Application Error Parameter 
	 */ 
	public void addVarianceDescription (String strVarianceDescription) 
	{
		ST tempVarianceDescription  = (ST)getDataElement(1217);
		tempVarianceDescription.setValue(strVarianceDescription); 
		addVarianceDescription(tempVarianceDescription);

	} 

	/** Set the Variance Description.
	 * @param objtempVarianceDescription
	 */ 
	public void addVarianceDescription(ST objtempVarianceDescription)
	{
		addDataType(1217,objtempVarianceDescription);

	}

	/**
	 * Set the VarianceDescription list.
	 * @param objVarianceDescriptionList List of VarianceDescription
	 */	
	@SuppressWarnings("unchecked")
	public void setVarianceDescription(ISingleCollection<ST> objVarianceDescriptionList)
	{
		ISingleCollection<IDataType> objCollection = null;
		ISingleCollection<?> objUnknownCollection = (ISingleCollection<?>)objVarianceDescriptionList;
		objCollection = (ISingleCollection<IDataType>)objUnknownCollection;
		setDataType(1217,objCollection);
	}

	/**
	 * Set the Variance Instance ID.
	 * @param strEntityIdentifier Specifies defined by a hierarchic designator, represented by components 2 through 4.
	 * 								[Reference Section 2.A.33, "HD - hierarchic designator".] 
	 * @param strNamespaceId Specifies  the HL7 identifier for the user-defined table of values for this component.
	 * 						   [Refer to User-defined Table 0363 - Assigning authority] 	 
	 * @param strUniversalId Specifies string formatted according to the scheme defined by the component, 
	 * 							<universal ID type> (UID type). 
	 * @param strUniversalIdType Specifies  the interpretation of the second component of the HD. 
	 */ 
	public void setVarianceInstanceID (String strEntityIdentifier,String strNamespaceId,
			String strUniversalId,String strUniversalIdType)
	{
		EI varianceInstanceID  = (EI)getDataElement(1212);
		varianceInstanceID.setEntityIdentifier(strEntityIdentifier);
		varianceInstanceID.setNamespaceId(strNamespaceId);
		varianceInstanceID.setUniversalId(strUniversalId);
		varianceInstanceID.setUniversalIdType(strUniversalIdType); 
		setVarianceInstanceID(varianceInstanceID);

	} 

	/** Set the Variance Instance ID.
	 * @param objvarianceInstanceID
	 */ 
	public void setVarianceInstanceID(EI objvarianceInstanceID)
	{
		setDataType(1212,objvarianceInstanceID);

	}



	/**
	 * 
	 * Set the Variance Originator.
	 * This field contains the originator (person or system) documenting the variance.
	 * @param strIDNumber Refers to the coded ID according to a user-defined table
	 * @param objFamilyName Specifies the surname of a person.
	 * @param strGivenName Specifies First name.
	 * @param strSecondAndFurtherGivenNamesOrInitialsThereof Specifies multiple middle names that may be included by 
	 * 														   separating them with spaces.
	 * @param strSuffix Used to specify a name suffix (e.g., Jr. or III).
	 * @param strPrefix Used to specify a name prefix (e.g., Dr.).
	 * @param strSourceTable Used as the HL7 identifier for the user-defined table of values for this component. [Refer User-defined Table 0297 - CN ID.]
	 * @param objAssigningAuthority Specifies a unique identifier of the system (or organization or agency of department)
	 * 								  that creates the data. [Refer User-defined Table 0363 - Assigning authority]
	 * @param strNameTypeCode Specifies a code that represents the type of name. 
	 * @param strIdentifierCheckDigit Specifies part of the identifying number used in the sending application. 
	 * @param strCheckDigitScheme Specifies the code identifying the check digit scheme employed.[Refer HL7 Table 0061 - Check digit scheme].
	 * @param strIdentifierTypeCode Specifies A code corresponding to the type of identifier.[Refer HL7 Table 0203 - Identifier type].
	 * @param objAssigningFacility Specifies the place or location identifier where the identifier was first assigned to the person
	 * @param strNameRepresentationCode Provides an indication of the representation provided by the data item.  
	 * @param objNameContext Designates the context in which a name is used. [Refer User-defined Table 0448 ]
	 * @param strNameAssemblyOrder Specifies a code that represents the preferred display order of the components of this person name. Suggested values are G,F[Refer table 0444.]
	 * @param strEffectiveDate Specifies the first date, if known, on which the address is valid and active.
	 * @param strExpirationDate Specifies the last date, if known, on which the address is valid and active.
	 * @param strProfessionalSuffix Used to specify an abbreviation, or a string of abbreviations, denoting qualifications that support the person's profession, (e.g., licenses).
	 * @param objAssigningJurisdiction Specifies the geo-political body that assigned the identifier.
	 * @param objAssigningAgencyOrDepratment Specifies the agency or department that assigned the identifier
	 * @param strSecurityCheck Specifies the version code that may be assigned to the person identifier
	 * @param strSecurityCheckScheme Specifies the check digit scheme.
	 */  
	public void addVarianceOriginator (String strIDNumber,FN objFamilyName,String strGivenName,String strSecondAndFurtherGivenNamesOrInitialsThereof,
			String 	strSuffix,String strPrefix,CWE objSourceTable,HD objAssigningAuthority,
			String strNameTypeCode,String strIdentifierCheckDigit,String strCheckDigitScheme,String strIdentifierTypeCode,
			HD objAssigningFacility,String strNameRepresentationCode,CWE objNameContext,
			String strNameAssemblyOrder, String strEffectiveDate,String strExpirationDate,String strProfessionalSuffix,
			CWE objAssigningJurisdiction,CWE objAssigningAgencyOrDepratment, String strSecurityCheck, String strSecurityCheckScheme)
	{
		XCN xcn  = (XCN)getDataElement(1215);
		xcn.setIdNumber(strIDNumber);
		xcn.setFamilyName(objFamilyName);
		xcn.setGivenName(strGivenName);
		xcn.setSecondNameOrInitials(strSecondAndFurtherGivenNamesOrInitialsThereof);
		xcn.setSuffix(strSuffix);
		xcn.setPrefix(strPrefix);
 
		xcn.setSourceTable(objSourceTable);
		xcn.setAssigningAuthority(objAssigningAuthority);
		xcn.setNameTypeCode(strNameTypeCode);
		xcn.setIdentifierCheckDigit(strIdentifierCheckDigit);
		xcn.setCheckDigitScheme(strCheckDigitScheme);
		xcn.setIdentifierTypeCode(strIdentifierTypeCode);
		xcn.setAssigningFacility(objAssigningFacility);
		xcn.setNameRepresentationCode(strNameRepresentationCode);
		xcn.setNameContext(objNameContext);
 
		xcn.setNameAssemblyOrder(strNameAssemblyOrder);
		xcn.setEffectiveDate(strEffectiveDate);
		xcn.setExpirationDate(strExpirationDate);
		xcn.setProfessionalSuffix(strProfessionalSuffix);
		xcn.setAssigningJurisdiction(objAssigningJurisdiction);
		xcn.setAssigningAgencyOrDepartment(objAssigningAgencyOrDepratment); 
		xcn.setSecurityCheck(strSecurityCheck);
		xcn.setSecurityCheckScheme(strSecurityCheckScheme);
		addVarianceOriginator(xcn);

	} 

	/**
	 * 
	 * Set the Variance Originator.
	 * This field contains the originator (person or system) documenting the variance.
	 * @param objtempvarianceOriginator
	 */ 
	public void addVarianceOriginator(XCN objtempvarianceOriginator)
	{
		addDataType(1215,objtempvarianceOriginator);

	}

	/**
	 * Set the VarianceOriginator list.
	 * @param objVarianceOriginatorList List of VarianceOriginator
	 */	
	@SuppressWarnings("unchecked")
	public void setVarianceOriginator(ISingleCollection<XCN> objVarianceOriginatorList)
	{
		ISingleCollection<IDataType> objCollection = null;
		ISingleCollection<?> objUnknownCollection = (ISingleCollection<?>)objVarianceOriginatorList;
		objCollection = (ISingleCollection<IDataType>)objUnknownCollection;
		setDataType(1215,objCollection);
	}

	/**
	 * 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
		{
			/*
			 * Variance Instance ID, Documented Date Time  are required fields.
			 */
			super.validate();
			return true;
		}
		catch(FieldMissingException ex)
		{
			throw new InvalidSegmentException(enumSegments.getValue(),ex);
		}
	}
}