/*******************************************************************************
 *  Copyright 2017-2018 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.dcm30_04.services.mwl.mwlsourceimpl;

import cdac.medinfo.sdk.dcm30_04.baselibs.DicomV3Factory;
import cdac.medinfo.sdk.dcm30_04.baselibs.ReferencedPatientAliasSequence;
import cdac.medinfo.sdk.dcm30_04.baselibs.ReferencedStudySequence;
import cdac.medinfo.sdk.dcm30_04.baselibs.ReferencedVisitSequence;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDataSet;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConstants;

/**
 *  This class contains the Attributes required for Query Model. Also contains methods to populate and fill data set of Query Model.
 */
public class PatientRelationshipAttributes extends Attributes
{
	/**
	 * This method provides the pre-initialized object of Referenced Study Sequence.
	 * 
	 * @return - object of Referenced Study Sequence. If the module contains the sequence object
	 * it returns the same otherwise it creates the sequence adds to the module with proper initialization
	 * and then returns the object of sequence
	 */
	public ReferencedStudySequence getReferencedStudySequence()
	{
		ReferencedStudySequence objReferencedStudySequence = (ReferencedStudySequence)getDicomValue(0x0008, 0x1110);
		if(objReferencedStudySequence == null)
		{
			/*
			 * If the sequence object is not present in the DataSet, a new sequence object is created and
			 * the TransferSyntax and DictionaryReader is set into it as it is set in the Module. This ensures that 
			 * the DataElements getting added in the DataSet follow the same TransferSyntax and use the same
			 * DictionaryReader object as that of the module.
			 */
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			objReferencedStudySequence = (ReferencedStudySequence)objDicomV3Factory.createSequence(0x0008, 0x1110, getDataSet().getTransferSyntax(), getDataSet().getDictionaryReader());
			
			// This will add Data Element of SQ to the DataSet.
			addDataElement(0x0008, 0x1110, DicomConstants.DCM_DT_SQ, objReferencedStudySequence.getValue());
		}
		return objReferencedStudySequence;
	}

	/**
	 * This method provides the pre-initialized object of Referenced Visit Sequence.
	 * 
	 * @return - object of Referenced Visit Sequence. If the module contains the sequence object
	 * it returns the same otherwise it creates the sequence adds to the module with proper initialization
	 * and then returns the object of sequence
	 */
	public ReferencedVisitSequence getReferencedVisitSequence()
	{
		ReferencedVisitSequence objReferencedVisitSequence = (ReferencedVisitSequence)getDicomValue(0x0008, 0x1125);
		if(objReferencedVisitSequence == null)
		{
			/*
			 * If the sequence object is not present in the DataSet, a new sequence object is created and
			 * the TransferSyntax and DictionaryReader is set into it as it is set in the Module. This ensures that 
			 * the DataElements getting added in the DataSet follow the same TransferSyntax and use the same
			 * DictionaryReader object as that of the module.
			 */
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			objReferencedVisitSequence = (ReferencedVisitSequence)objDicomV3Factory.createSequence(0x0008, 0x1125, getDataSet().getTransferSyntax(), getDataSet().getDictionaryReader());
			
			// This will add Data Element of SQ to the DataSet.
			addDataElement(0x0008, 0x1125, DicomConstants.DCM_DT_SQ, objReferencedVisitSequence.getValue());
		}
		return objReferencedVisitSequence;
	}

	/**
	 * This method provides the pre-initialized object of Referenced Patient Alias Sequence.
	 * 
	 * @return - object of Referenced Patient Alias Sequence. If the module contains the sequence object
	 * it returns the same otherwise it creates the sequence adds to the module with proper initialization
	 * and then returns the object of sequence
	 */
	public ReferencedPatientAliasSequence getReferencedPatientAliasSequence()
	{
		ReferencedPatientAliasSequence objReferencedPatientAliasSequence = (ReferencedPatientAliasSequence)getDicomValue(0x0038, 0x0004);
		if(objReferencedPatientAliasSequence == null)
		{
			/*
			 * If the sequence object is not present in the DataSet, a new sequence object is created and
			 * the TransferSyntax and DictionaryReader is set into it as it is set in the Module. This ensures that 
			 * the DataElements getting added in the DataSet follow the same TransferSyntax and use the same
			 * DictionaryReader object as that of the module.
			 */
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			objReferencedPatientAliasSequence = (ReferencedPatientAliasSequence)objDicomV3Factory.createSequence(0x0038, 0x0004, getDataSet().getTransferSyntax(), getDataSet().getDictionaryReader());
			
			// This will add Data Element of SQ to the DataSet.
			addDataElement(0x0038, 0x0004, DicomConstants.DCM_DT_SQ, objReferencedPatientAliasSequence.getValue());
		}
		return objReferencedPatientAliasSequence;
	}
	
	/**
	 * Populates the query Model using filled data set.
	 */
	public void populate(IDataSet objIDataSet)
	{
		super.populate(objIDataSet);
	}	
	
	/**
	 * Fills the data set using already populated Query Model.
	 */
	public void fill(IDataSet objIDataSet)
	{
		super.fill(objIDataSet);
	}
}
