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

import java.io.IOException;
import java.util.Locale;


import cdac.medinfo.sdk.dcm30_04.commoninterface.IDataSet;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDicomStream;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDictionaryReader;
import cdac.medinfo.sdk.dcm30_04.commoninterface.ITransferSyntax;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.AcquisitionContextModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.BulkMotionSynchronizationModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.CardiacSynchronizationModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.ClinicalTrialSeriesModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.ClinicalTrialSubjectModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.CommonInstanceReferenceModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.EnhancedContrastBolusModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.EnhancedGeneralEquipmentModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.FrameExtractionModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.FrameOfReferenceModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.GeneralEquipmentModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.GeneralSeriesModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.GeneralStudyModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.MRSeriesModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.MRSpectroscopyDataModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.MRSpectroscopyModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.MRSpectroscopyPulseSequenceModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.MultiFrameDimensionModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.MultiFrameFunctionalGroupsModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.NonImageIOD;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.PatientModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.PatientStudyModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.RespiratorySynchronizationModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.SOPCommonModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.SpecimenIdentificationModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.SpecimenModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.SynchronizationModule;
import cdac.medinfo.sdk.dcm30_04.enums.EnumCardiacSynchronizationTechnique;
import cdac.medinfo.sdk.dcm30_04.enums.EnumImageType;
import cdac.medinfo.sdk.dcm30_04.enums.EnumModuleNames;
import cdac.medinfo.sdk.dcm30_04.enums.EnumRespiratoryMotionCompensation;
import cdac.medinfo.sdk.dcm30_04.enums.EnumSOPClassUIDs;

import cdac.medinfo.sdk.dcm30_04.exceptions.ConditionFailedException;
import cdac.medinfo.sdk.dcm30_04.exceptions.InvalidIODException;
import cdac.medinfo.sdk.dcm30_04.exceptions.InvalidModuleException;
import cdac.medinfo.sdk.dcm30_04.exceptions.ParseException;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConfig;
import cdac.medinfo.sdk.dcm30_04.utils.DicomLogger;

import cdac.medinfo.sdk.dcm30_04.utils.ResourceBundleWrapper;

/**
 * This class represents MR Spectroscopy Information Object Definition.
 * The instance members suggest that these are the modules contained in the MR Spectroscopy IOD.
 * This class exhibits different functional behavour of the MR Spectroscopy IOD.
 */
public class MRSpectroscopyIOD extends NonImageIOD 
{

	/**
	 * The Constant MR_SPECTROSCOPY_IOD.
	 */
	public static final String MR_SPECTROSCOPY_IOD = "MR Spectroscopy IOD";

	/**
	 * The obj patient module.
	 */
	PatientModule objPatientModule;

	/**
	 * The obj general study module.
	 */
	GeneralStudyModule objGeneralStudyModule;

	/**
	 * The obj patient study module.
	 */
	PatientStudyModule objPatientStudyModule;

	/**
	 * The obj general series module.
	 */
	GeneralSeriesModule objGeneralSeriesModule;

	/**
	 * The obj clinical trial series module.
	 */
	ClinicalTrialSeriesModule objClinicalTrialSeriesModule;

	/**
	 * The obj mr series module.
	 */
	MRSeriesModule objMRSeriesModule;

	/**
	 * The obj frame of reference module.
	 */
	FrameOfReferenceModule objFrameOfReferenceModule;

	/**
	 * The obj synchronization module.
	 */
	SynchronizationModule objSynchronizationModule;

	/**
	 * The obj general equipment module.
	 */
	GeneralEquipmentModule objGeneralEquipmentModule;

	/**
	 * The obj Enhanced General Equipment Module.
	 */
	EnhancedGeneralEquipmentModule objEnhancedGeneralEquipmentModule;

	/**
	 * The obj enhanced contrast bolus module.
	 */
	EnhancedContrastBolusModule objEnhancedContrastBolusModule;

	/**
	 * The obj multi frame functional groups module.
	 */
	MultiFrameFunctionalGroupsModule objMultiFrameFunctionalGroupsModule;

	/**
	 * The obj multi frame dimension module.
	 */
	MultiFrameDimensionModule objMultiFrameDimensionModule;

	/**
	 * The obj cardiac synchronization module.
	 */
	CardiacSynchronizationModule objCardiacSynchronizationModule;

	/**
	 * The obj respiratory synchronization module.
	 */
	RespiratorySynchronizationModule objRespiratorySynchronizationModule;

	/**
	 * The obj bulk motion synchronization module.
	 */
	BulkMotionSynchronizationModule objBulkMotionSynchronizationModule;

	/**
	 * The obj acquisition context module.
	 */
	AcquisitionContextModule objAcquisitionContextModule;

	/**
	 * The obj mr spectroscopy data module.
	 */
	MRSpectroscopyDataModule objMRSpectroscopyDataModule;

	/**
	 * The obj mr spectroscopy pulse sequence module.
	 */
	MRSpectroscopyPulseSequenceModule objMRSpectroscopyPulseSequenceModule;

	/**
	 * The obj mr spectroscopy module.
	 */
	MRSpectroscopyModule objMRSpectroscopyModule;

	/**
	 * The obj sop common module.
	 */
	SOPCommonModule objSOPCommonModule;
	
	/**
	 * The Specimen Module.
	 */
	SpecimenModule objSpecimenModule = null;
	
	
	/**
	 * The CommonInstanceReferenceModule Module.
	 */
	CommonInstanceReferenceModule objCommonInstanceReferenceModule = null;
	
	/**
	 * The FrameExtractionModule Module.
	 */
	FrameExtractionModule objFrameExtractionModule = null;

	/**
	 * Instantiates a new mR spectroscopy iod.
	 */
	public MRSpectroscopyIOD()
	{
		super();
	}

	/**
	 * This constructor initializes the TransferSyntax  in its superclass IOD.
	 * 
	 * @param objITransferSyntax_ - specifies the DICOM Standard defined TransfeSyntax.
	 */
	public MRSpectroscopyIOD(ITransferSyntax objITransferSyntax_)
	{
		super(objITransferSyntax_);
	}

	/**
	 * This constructor initializes the  DictionaryReader
	 * in its superclass IOD.
	 * 
	 * @param objIDictionaryReader_ - specifies the DICOM Dictionary reader to be used .
	 */
	public MRSpectroscopyIOD(IDictionaryReader objIDictionaryReader_)
	{
		super(objIDictionaryReader_);
	}

	/**
	 * This constructor accepts the DataSet from the user   in its superclass IOD.
	 * 
	 * @param objIDataSet_ - specifies the data set to be set in the IOD.
	 */
	public MRSpectroscopyIOD(IDataSet objIDataSet_)
	{
		super(objIDataSet_);
	}

	/**
	 * This constructor initializes the TransferSyntax and DictionaryReader in its super class IOD.
	 * 
	 * @param objITransferSyntax_ specifies the DICOM Standard defined TransfeSyntax.
	 * @param objIDictionaryReader_ specifies the DICOM Dictionary reader to be used .
	 */
	public MRSpectroscopyIOD(ITransferSyntax objITransferSyntax_,IDictionaryReader objIDictionaryReader_)
	{
		super(objITransferSyntax_,objIDictionaryReader_);
	}

	/**
	 * Creates MRSpectroscopyIOD IOD from a DICOM file containing MRSpectroscopyIOD DICOM data.
	 * @param strFileName DICOM File with absolute path.
	 * @throws IOException Exception caused due to wrong I/O operations. 
	 * @throws ParseException signals if some parse exceptions are occurred.
	 * @throws InvalidIODException signals if some IOD exceptions are occurred.
	 */
	public MRSpectroscopyIOD(String strFileName)throws IOException,InvalidIODException
	{
		super(strFileName);
		try
		{
			if(objIDataSet != null)
			{
				String strSOPClassUID = objIDataSet.getSOPClassUID();
				if(strSOPClassUID != null)
				{
					if(!strSOPClassUID.equals(EnumSOPClassUIDs.MRSPECTROSCOPYIOD.getValue()))
					{
						throw new InvalidIODException(ResourceBundleWrapper.getValue("exceptions", "InvalidIOD.FileNotMatched", new Locale(DicomConfig.createInstance().getLocale())) + MR_SPECTROSCOPY_IOD);
					}
				}
				strSOPClassUID = null;
			}
		}
		catch(InvalidIODException iie)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(iie.getMessage());
			}
			throw iie;
		}
	}

	/**
	 * Retrieves pre-initialized instance of AcquisitionContextModule. Creates a new instance
	 * if no pre-existing module instance is available.If previously created instance is
	 * present then returns the same module instance.
	 * 
	 * @return objAcquisitionContextModule - instance of {@link AcquisitionContextModule}.
	 */
	public AcquisitionContextModule getAcquisitionContextModule() 
	{
		if(objAcquisitionContextModule == null)
		{
			objAcquisitionContextModule = new AcquisitionContextModule(objIDataSet);
			return objAcquisitionContextModule;
		}
		return objAcquisitionContextModule;
	}

	/**
	 * Retrieves pre-initialized instance of BulkMotionSynchronizationModule. Creates a new instance
	 * if no pre-existing module instance is available.If previously created instance is
	 * present then returns the same module instance.
	 * 
	 * @return objBulkMotionSynchronizationModule - instance of {@link BulkMotionSynchronizationModule}.
	 */
	public BulkMotionSynchronizationModule getBulkMotionSynchronizationModule()
	{
		if(objBulkMotionSynchronizationModule == null)
		{
			objBulkMotionSynchronizationModule = new BulkMotionSynchronizationModule(objIDataSet);
			return objBulkMotionSynchronizationModule;
		}
		return objBulkMotionSynchronizationModule;
	}

	/**
	 * Retrieves pre-initialized instance of objCardiacModuleSynchronizationModule. Creates a new instance
	 * if no pre-existing module instance is available.If previously created instance is
	 * present then returns the same module instance.
	 * 
	 * @return objCardiacModuleSynchronizationModule - instance of {@link objCardiacModuleSynchronizationModule}.
	 */
	public CardiacSynchronizationModule getCardiacSynchronizationModule()
	{
		if(objCardiacSynchronizationModule == null)
		{
			objCardiacSynchronizationModule = new CardiacSynchronizationModule(objIDataSet);
			return objCardiacSynchronizationModule;
		}
		return objCardiacSynchronizationModule;
	}

	/**
	 * Retrieves pre-initialized instance of objClinicalTrialSeriesModule. Creates a new instance
	 * if no pre-existing module instance is available.If previously created instance is
	 * present then returns the same module instance.
	 * 
	 * @return objClinicalTrialSeriesModule - instance of {@link objClinicalTrialSeriesModule}.
	 */
	public ClinicalTrialSeriesModule getClinicalTrialSeriesModule() 
	{
		if(objClinicalTrialSeriesModule == null)
		{
			objClinicalTrialSeriesModule = new ClinicalTrialSeriesModule(objIDataSet);
			return objClinicalTrialSeriesModule;
		}
		return objClinicalTrialSeriesModule;
	}

	/**
	 * Retrieves pre-initialized instance of EnhancedContrastBolusModule. Creates a new instance
	 * if no pre-existing module instance is available.If previously created instance is
	 * present then returns the same module instance.
	 * 
	 * @return objEnhancedContrastBolusModule - instance of {@link EnhancedContrastBolusModule}.
	 */
	public EnhancedContrastBolusModule getEnhancedContrastBolusModule() 
	{
		if(objEnhancedContrastBolusModule == null)
		{
			objEnhancedContrastBolusModule = new EnhancedContrastBolusModule(objIDataSet);
			return objEnhancedContrastBolusModule;
		}
		return objEnhancedContrastBolusModule;
	}

	/**
	 * Retrieves pre-initialized instance of FrameOfReferenceModule. Creates a new instance
	 * if no pre-existing module instance is available.If previously created instance is
	 * present then returns the same module instance.
	 * 
	 * @return objFrameOfReferenceModule - instance of {@link FrameOfReferenceModule}.
	 */
	public FrameOfReferenceModule getFrameOfReferenceModule()
	{
		if(objFrameOfReferenceModule == null)
		{
			objFrameOfReferenceModule = new FrameOfReferenceModule(objIDataSet);
			return objFrameOfReferenceModule;
		}
		return objFrameOfReferenceModule;
	}

	/**
	 * Retrieves pre-initialized instance of GeneralEquipmentModule. Creates a new instance
	 * if no pre-existing module instance is available.If previously created instance is
	 * present then returns the same module instance.
	 * 
	 * @return objGeneralEquipmentModule - instance of {@link objGeneralEquipmentModule}.
	 */
	public GeneralEquipmentModule getGeneralEquipmentModule()
	{
		if(objGeneralEquipmentModule == null)
		{
			objGeneralEquipmentModule = new GeneralEquipmentModule(objIDataSet);
			return objGeneralEquipmentModule;
		}
		return objGeneralEquipmentModule;
	}

	/**
	 * Retrieves pre-initialized instance of GeneralSeriesModule. Creates a new instance
	 * if no pre-existing module instance is available.If previously created instance is
	 * present then returns the same module instance.
	 * 
	 * @return objGeneralSeriesModule - instance of {@link GeneralSeriesModule}.
	 */
	public GeneralSeriesModule getGeneralSeriesModule()
	{
		if(objGeneralSeriesModule == null)
		{
			objGeneralSeriesModule = new GeneralSeriesModule(objIDataSet);
			return objGeneralSeriesModule;
		}
		return objGeneralSeriesModule;
	}

	/**
	 * Retrieves pre-initialized instance of GeneralStudyModule. Creates a new instance
	 * if no pre-existing module instance is available.If previously created instance is
	 * present then returns the same module instance.
	 * 
	 * @return objGeneralStudyModule - instance of {@link GeneralStudyModule}.
	 */
	public GeneralStudyModule getGeneralStudyModule()
	{
		if(objGeneralStudyModule == null)
		{
			objGeneralStudyModule = new GeneralStudyModule(objIDataSet);
			return objGeneralStudyModule;
		}
		return objGeneralStudyModule;
	}

	/**
	 * Retrieves pre-initialized instance of MRSeriesModule. Creates a new instance
	 * if no pre-existing module instance is available.If previously created instance is
	 * present then returns the same module instance.
	 * 
	 * @return objMRSeriesModule - instance of {@link MRSeriesModule}.
	 */
	public MRSeriesModule getMRSeriesModule()
	{
		if(objMRSeriesModule == null)
		{
			objMRSeriesModule = new MRSeriesModule(objIDataSet);
			return objMRSeriesModule;
		}
		return objMRSeriesModule;
	}

	/**
	 * Retrieves pre-initialized instance of MRSpectroscopyDataModule. Creates a new instance
	 * if no pre-existing module instance is available.If previously created instance is
	 * present then returns the same module instance.
	 * 
	 * @return objMRSpectroscopyDataModule - instance of {@link MRSpectroscopyDataModule}.
	 */
	public MRSpectroscopyDataModule getMRSpectroscopyDataModule()
	{
		if(objMRSpectroscopyDataModule == null)
		{
			objMRSpectroscopyDataModule = new MRSpectroscopyDataModule(objIDataSet);
			return objMRSpectroscopyDataModule;
		}
		return objMRSpectroscopyDataModule;
	}

	/**
	 * Retrieves pre-initialized instance of MRSpectroscopyModule. Creates a new instance
	 * if no pre-existing module instance is available.If previously created instance is
	 * present then returns the same module instance.
	 * 
	 * @return objMRSpectroscopyModule - instance of {@link MRSpectroscopyModule}.
	 */
	public MRSpectroscopyModule getMRSpectroscopyModule()
	{
		if(objMRSpectroscopyModule == null)
		{
			objMRSpectroscopyModule = new MRSpectroscopyModule(objIDataSet);
			return objMRSpectroscopyModule;
		}
		return objMRSpectroscopyModule;
	}

	/**
	 * Retrieves pre-initialized instance of MRSpectroscopyModulePulseSequenceModule. Creates a new instance
	 * if no pre-existing module instance is available.If previously created instance is
	 * present then returns the same module instance.
	 * 
	 * @return objMRSpectroscopyModulePulseSequenceModule - instance of {@link MRSpectroscopyModulePulseSequenceModule}.
	 */
	public MRSpectroscopyPulseSequenceModule getMRSpectroscopyModulePulseSequenceModule()
	{
		if(objMRSpectroscopyPulseSequenceModule == null)
		{
			objMRSpectroscopyPulseSequenceModule = new MRSpectroscopyPulseSequenceModule(objIDataSet);
			return objMRSpectroscopyPulseSequenceModule;
		}
		return objMRSpectroscopyPulseSequenceModule;
	}

	/**
	 * Retrieves pre-initialized instance of MultiFrameDimensionModule. Creates a new instance
	 * if no pre-existing module instance is available.If previously created instance is
	 * present then returns the same module instance.
	 * 
	 * @return objMultiFrameDimensionModule - instance of {@link MultiFrameDimensionModule}.
	 */
	public MultiFrameDimensionModule getMultiFrameDimensionModule()
	{
		if(objMultiFrameDimensionModule == null)
		{
			objMultiFrameDimensionModule = new MultiFrameDimensionModule(objIDataSet);
			return objMultiFrameDimensionModule;
		}
		return objMultiFrameDimensionModule;
	}

	/**
	 * Retrieves pre-initialized instance of MultiFrameFunctionalGroupsModule. Creates a new instance
	 * if no pre-existing module instance is available.If previously created instance is
	 * present then returns the same module instance.
	 * 
	 * @return objMultiFrameFunctionalGroupsModule - instance of {@link MultiFrameFunctionalGroupsModule}.
	 */
	public MultiFrameFunctionalGroupsModule getMultiFrameFunctionalGroupsModule()
	{
		if(objMultiFrameFunctionalGroupsModule == null)
		{
			objMultiFrameFunctionalGroupsModule = new MultiFrameFunctionalGroupsModule(objIDataSet);
			return objMultiFrameFunctionalGroupsModule;
		}
		return objMultiFrameFunctionalGroupsModule;
	}

	/**
	 * Retrieves pre-initialized instance of PatientModule. Creates a new instance
	 * if no pre-existing module instance is available.If previously created instance is
	 * present then returns the same module instance.
	 * 
	 * @return objPatientModule - instance of {@link PatientModule}.
	 */
	public PatientModule getPatientModule()
	{
		if(objPatientModule == null)
		{
			objPatientModule = new PatientModule(objIDataSet);
			return objPatientModule;
		}
		return objPatientModule;
	}

	/**
	 * Retrieves pre-initialized instance of PatientStudyModule. Creates a new instance
	 * if no pre-existing module instance is available.If previously created instance is
	 * present then returns the same module instance.
	 * 
	 * @return objPatientStudyModule - instance of {@link PatientStudyModule}.
	 */
	public PatientStudyModule getPatientStudyModule() 
	{
		if(objPatientStudyModule == null)
		{
			objPatientStudyModule = new PatientStudyModule(objIDataSet);
			return objPatientStudyModule;
		}
		return objPatientStudyModule;
	}

	/**
	 * Retrieves pre-initialized instance of RespiratorySynchronizationModule. Creates a new instance
	 * if no pre-existing module instance is available.If previously created instance is
	 * present then returns the same module instance.
	 * 
	 * @return objRespiratorySynchronizationModule - instance of {@link RespiratorySynchronizationModule}.
	 */
	public RespiratorySynchronizationModule getRespiratorySynchronizationModule() 
	{
		if(objRespiratorySynchronizationModule == null)
		{
			objRespiratorySynchronizationModule = new RespiratorySynchronizationModule(objIDataSet);
			return objRespiratorySynchronizationModule;
		}
		return objRespiratorySynchronizationModule;
	}

	/**
	 * Retrieves pre-initialized instance of SOPCommonModule. Creates a new instance
	 * if no pre-existing module instance is available.If previously created instance is
	 * present then returns the same module instance.
	 * 
	 * @return objSOPCommonModule - instance of {@link SOPCommonModule}.
	 */
	public SOPCommonModule getSOPCommonModule()
	{
		if(objSOPCommonModule == null)
		{
			objSOPCommonModule = new SOPCommonModule(objIDataSet);
			return objSOPCommonModule;
		}
		return objSOPCommonModule;
	}


	/**
	 * Retrieves pre-initialized instance of SynchronizationModule. Creates a new instance
	 * if no pre-existing module instance is available.If previously created instance is
	 * present then returns the same module instance.
	 * 
	 * @return objSynchronizationModule - instance of {@link SynchronizationModule}.
	 */
	public SynchronizationModule getSynchronizationModule() 
	{
		if(objSynchronizationModule == null)
		{
			objSynchronizationModule = new SynchronizationModule(objIDataSet);
			return objSynchronizationModule;
		}
		return objSynchronizationModule;
	}

	
	 
	/**
	 * Retrieves pre-initialized instance of SynchronizationModule. Creates a new instance
	 * if no pre-existing module instance is available.If previously created instance is
	 * present then returns the same module instance.
	 * 
	 * @return objSynchronizationModule - instance of {@link SynchronizationModule}.
	 */
	public EnhancedGeneralEquipmentModule getEnhancedGeneralEquipmentModule() 
	{
		if(objEnhancedGeneralEquipmentModule == null)
		{
			objEnhancedGeneralEquipmentModule = new EnhancedGeneralEquipmentModule(objIDataSet);
			return objEnhancedGeneralEquipmentModule;
		}
		return objEnhancedGeneralEquipmentModule;
	}

	/**
	 * Retrieves pre-initialized instance of Specimen Module. Creates a new instance
	 * if no pre-existing module instance is available.If previously created instance is
	 * present then returns the same module instance.
	 * 
	 * @return SpecimenModule - instance of {@link SpecimenModule}.
	 */
	public SpecimenModule getSpecimenModule()
	{
		if(objSpecimenModule == null)
		{
			objSpecimenModule = new SpecimenModule(objIDataSet);
			return objSpecimenModule;
		}
		return objSpecimenModule;
	}
	
	/**
	 * Retrieves pre-initialized instance of objCommon Instance Reference Module. Creates a new instance
	 * if no pre-existing module instance is available.If previously created instance is
	 * present then returns the same module instance.
	 * 
	 * @return CommonInstanceReferenceModule - instance of {@link CommonInstanceReferenceModule}.
	 */
	public CommonInstanceReferenceModule getCommonInstanceReferenceModule()
	{
		if(objCommonInstanceReferenceModule == null)
		{
			objCommonInstanceReferenceModule = new CommonInstanceReferenceModule(objIDataSet);
			return objCommonInstanceReferenceModule;
		}
		return objCommonInstanceReferenceModule;
	}
	
	/**
	 * Retrieves pre-initialized instance of Frame Extraction Module. Creates a new instance
	 * if no pre-existing module instance is available.If previously created instance is
	 * present then returns the same module instance.
	 * 
	 * @return FrameExtractionModule - instance of {@link FrameExtractionModule}.
	 */
	public FrameExtractionModule getFrameExtractionModule()
	{
		if(objFrameExtractionModule == null)
		{
			objFrameExtractionModule = new FrameExtractionModule(objIDataSet);
			return objFrameExtractionModule;
		}
		return objFrameExtractionModule;
	}

	
	/**
	 * This method validates Non-Image IOD MR Spectroscopy IOD according to the Module Table given in The DICOM Standard.
	 * The validate method fails if one of the mandatory modules are absent or any of the
	 * modules are invalid. The validate method also checks for conditional modules.
	 * 
	 * @return boolean - status of validation as success or failure
	 * 
	 * @throws InvalidIODException  throws when the IOD is invalid.
	 */
	public boolean validate() throws InvalidIODException
	{
		try
		{
			super.validate();

			/*Validate the mandatory elements*/
			getPatientModule().validateModule();
			getGeneralStudyModule().validateModule();
			getGeneralSeriesModule().validateModule();
			getMRSeriesModule().validateModule();
			getFrameOfReferenceModule().validateModule();
			getGeneralEquipmentModule().validateModule();
			getMultiFrameDimensionModule().validateModule();
			getMultiFrameFunctionalGroupsModule().validateModule();
			getAcquisitionContextModule().validateModule();
			getMRSpectroscopyModule().validateModule();
			getMRSpectroscopyDataModule().validateModule();
			getSOPCommonModule().validateModule();
			getEnhancedContrastBolusModule().validateModule();
			if(isModulePresent(PatientStudyModule.objSingleCollection, objIDataSet))
				getPatientStudyModule().validateModule();
			if(isModulePresent(SpecimenModule.objSingleCollection, objIDataSet))
				getSpecimenModule().validateModule();
			/*
			 * Module level validation at IOD level for RespiratorySynchronizationModule and CardiacSynchronizationModule.
			 * Attirbutes in these modules depends upon ImageType and module containing ImageType and these modules are not always together in all IODs.
			 */
			String[] arrStrImageType = getMRSpectroscopyModule().getImageType();
			if(arrStrImageType != null)
			{
				CardiacSynchronizationModule objCardiacSynchronizationModule = getCardiacSynchronizationModule();
				String strCardiacSynchronizationTechnique = objCardiacSynchronizationModule.getCardiacSynchronizationTechnique();

				RespiratorySynchronizationModule objRespiratorySynchronizationModule = getRespiratorySynchronizationModule();
				String strRespiratoryMotionCompensationTechnique = objRespiratorySynchronizationModule.getRespiratoryMotionCompensationTechnique();
				//Validation for RespiratorySynchronizationModule
				if((EnumImageType.Value1.ORIGINAL.getValue().equalsIgnoreCase(arrStrImageType[0]))||(EnumImageType.Value1.MIXED.getValue().equalsIgnoreCase(arrStrImageType[0]))
						&& !objRespiratorySynchronizationModule.isPresent(0x0018,0x9170))
					throw new ConditionFailedException("element 0018,9170 should be present if Image Type(0008,0008) value is ORIGINAL or MIXED");
				else if((EnumImageType.Value1.ORIGINAL.getValue().equalsIgnoreCase(arrStrImageType[0]))||(EnumImageType.Value1.MIXED.getValue().equalsIgnoreCase(arrStrImageType[0]))||(EnumImageType.Value1.DERIVED.getValue().equalsIgnoreCase(arrStrImageType[0]))
						&& !EnumRespiratoryMotionCompensation.NONE.getValue().equalsIgnoreCase(strRespiratoryMotionCompensationTechnique) && !objRespiratorySynchronizationModule.isPresent(0x0018,0x9171))
					throw new ConditionFailedException("element 0018,9171 should be present if Image Type(0008,0008) value is ORIGINAL or MIXED or DERIVED and Respiratory Motion Compensation Technique(0018,9170) value is other than NONE");
				//Validation for CardiacSynchronizationModule
				//Cardiac Synchronization Technique, Required if Image Type (0008,0008) Value 1 is ORIGINAL or MIXED.
				else if(!((EnumImageType.Value1.ORIGINAL.getValue().equalsIgnoreCase(arrStrImageType[0])) || (EnumImageType.Value1.MIXED.getValue().equalsIgnoreCase(arrStrImageType[0]))) &&  !objCardiacSynchronizationModule.isPresent(0x0018, 0x9037))
					throw new ConditionFailedException("Cardiac Synchronization Technique (0018,9037) should be present if Image Type(0008,0008) is ORIGINAL or MIXED");
				//Required if Image Type (0008,0008) Value 1 is ORIGINAL or MIXED and Cardiac Synchronization Technique
				//(0018,9037) equals other than NONE.
				else if(objCardiacSynchronizationModule.isPresent(0x0018, 0x9037))
				{
					if(((EnumImageType.Value1.ORIGINAL.getValue().equalsIgnoreCase(arrStrImageType[0])) || (EnumImageType.Value1.MIXED.getValue().equalsIgnoreCase(arrStrImageType[0]))))
					{
						if(!EnumCardiacSynchronizationTechnique.NONE.getValue().equalsIgnoreCase(strCardiacSynchronizationTechnique))
						{
							if(!objCardiacSynchronizationModule.isPresent(0x0018, 0x9085))
								throw new ConditionFailedException("Cardiac Signal Source(0018,9085) should be present if Image Type(0008,0008) is ORIGINAL or MIXED or DERIVED and Cardiac Synchronization Technique value is other than NONE");
							else if(!objCardiacSynchronizationModule.isPresent(0x0018, 0x9070))
								throw new ConditionFailedException("Cardiac RR Interval Specified(0018,9070) should be present if Image Type(0008,0008) is ORIGINAL or MIXED or DERIVED and Cardiac Synchronization Technique value is other than NONE");
							else if(!objCardiacSynchronizationModule.isPresent(0x0018, 0x1083))
								throw new ConditionFailedException("Intervals Acquired(0018,1083) should be present if Image Type(0008,0008) is ORIGINAL or MIXED or DERIVED and Cardiac Synchronization Technique value is other than NONE");
							else if(!objCardiacSynchronizationModule.isPresent(0x0018, 0x1084))
								throw new ConditionFailedException("Intervals Rejected(0018,1084) should be present if Image Type(0008,0008) is ORIGINAL or MIXED or DERIVED and Cardiac Synchronization Technique value is other than NONE");
						}
						else if(EnumCardiacSynchronizationTechnique.PROSPECTIVE.getValue().equalsIgnoreCase(strCardiacSynchronizationTechnique) || EnumCardiacSynchronizationTechnique.RETROSPECTIVE.getValue().equalsIgnoreCase(strCardiacSynchronizationTechnique))
						{
							if(!objCardiacSynchronizationModule.isPresent(0x0018, 0x9169))
								throw new ConditionFailedException("Cardiac Beat Rejection Technique(0018,9169) should be present if Image Type(0008,0008) is ORIGINAL or MIXED or DERIVED and Cardiac Synchronization Technique value is equals PROSPECTIVE or RETROSPECTIVE.");
							else if(!objCardiacSynchronizationModule.isPresent(0x0018, 0x1081))
								throw new ConditionFailedException("Low R-R Value(0018,1081) should be present if Image Type(0008,0008) is ORIGINAL or MIXED or DERIVED and Cardiac Synchronization Technique value is equals PROSPECTIVE or RETROSPECTIVE.");
							else if(!objCardiacSynchronizationModule.isPresent(0x0018, 0x1082))
								throw new ConditionFailedException("High R-R Value(0018,1082) should be present if Image Type(0008,0008) is ORIGINAL or MIXED or DERIVED and Cardiac Synchronization Technique value is equals PROSPECTIVE or RETROSPECTIVE.");
						}
					}
				}
			}

			/*
			 * Required if Image Type (0008,0008) Value 1 is ORIGINAL
			 */
			String [] arrStrImagetype = getMRSpectroscopyModule().getImageType();
			if(EnumImageType.Value1.ORIGINAL.getValue().equals(arrStrImagetype[0]) && !isModulePresent(MRSpectroscopyPulseSequenceModule.objSingleCollection,objIDataSet))
				throw new ConditionFailedException("MRSpectroscopyPulseSequenceModule is required if Image Type (0008,0008) Value 1 is ORIGINAL");
			else
				getMRSpectroscopyModulePulseSequenceModule().validateModule();

			/*
			 * Condition for validating following conditional modules is not clear
			 * Hence not validated.
			 * Condition :- Required if time synchronization was applied.
			 * Condition :- Required if cardiac synchronization was applied.
			 * Condition :- Required if Respiratory Synchronization was applied.
			 * Condition :- Required if Bulk Motion Synchronization was applied.
			 * Condition :- Required if time synchronization was applied.
			 * Condition :- Required if time synchronization was applied.
			 * Refer p.d.f Chapter 3
			 * Page No :- 186
			 */

			if(isModulePresent(SynchronizationModule.objSingleCollection, objIDataSet))
				getSynchronizationModule().validateModule();

			if(isModulePresent(EnhancedContrastBolusModule.objSingleCollection, objIDataSet))
				getEnhancedContrastBolusModule().validateModule();

			if(isModulePresent(CardiacSynchronizationModule.objSingleCollection, objIDataSet))
				getCardiacSynchronizationModule().validateModule();

			if(isModulePresent(RespiratorySynchronizationModule.objSingleCollection, objIDataSet))
				getRespiratorySynchronizationModule().validateModule();

			if(isModulePresent(BulkMotionSynchronizationModule.objSingleCollection, objIDataSet))
				getBulkMotionSynchronizationModule().validateModule();
			
			if(isModulePresent(CommonInstanceReferenceModule.objSingleCollection, objIDataSet))
				getCommonInstanceReferenceModule().validateModule();

		}
		catch(ConditionFailedException cfe)
		{
			if(DicomConfig.createInstance().isLoggingOn())
				DicomLogger.getLogger().log(cfe.getMessage());
			throw new InvalidIODException(MR_SPECTROSCOPY_IOD,cfe);
		}
		catch(InvalidModuleException ime)
		{
			if(DicomConfig.createInstance().isLoggingOn())
				DicomLogger.getLogger().log(ime.getMessage());
			
				throw new InvalidIODException(MR_SPECTROSCOPY_IOD,ime);
		}
		return true;
	}
	/**
	 *  Dumps the Name of IOD,Module and corresponding Attributs of Module.
	 *  @param objIDicomStream - the Stream on which the Data is to be written.
	 *  @throws IOException - Exception is thrown indicating runtime failure of writing operation.
	 */
	public void dumpDICOMInfo(IDicomStream objIDicomStream) throws IOException
	{
		objIDicomStream.writeString(MR_SPECTROSCOPY_IOD+ "\n");
		super.dumpDICOMInfo(objIDicomStream);

		objIDicomStream.writeString("\t" + getPatientModule().getClass().getSimpleName() + "\n");
		getPatientModule().dumpDICOMInfo(objIDicomStream);

		objIDicomStream.writeString("\t" + getClinicalTrialSubjectModule().getClass().getSimpleName() + "\n");
		getClinicalTrialSubjectModule().dumpDICOMInfo(objIDicomStream);

		objIDicomStream.writeString("\t" + getGeneralStudyModule().getClass().getSimpleName() + "\n");
		getGeneralStudyModule().dumpDICOMInfo(objIDicomStream);

		objIDicomStream.writeString("\t" + getPatientStudyModule().getClass().getSimpleName() + "\n");
		getPatientStudyModule().dumpDICOMInfo(objIDicomStream);


		objIDicomStream.writeString("\t" + getGeneralSeriesModule().getClass().getSimpleName() + "\n");
		getGeneralSeriesModule().dumpDICOMInfo(objIDicomStream);

		objIDicomStream.writeString("\t" + getClinicalTrialSeriesModule().getClass().getSimpleName() + "\n");
		getClinicalTrialSeriesModule().dumpDICOMInfo(objIDicomStream);

		objIDicomStream.writeString("\t" + getMRSeriesModule().getClass().getSimpleName() + "\n");
		getMRSeriesModule().dumpDICOMInfo(objIDicomStream);

		objIDicomStream.writeString("\t" + getFrameOfReferenceModule().getClass().getSimpleName() + "\n");
		getFrameOfReferenceModule().dumpDICOMInfo(objIDicomStream);

		objIDicomStream.writeString("\t" + getSynchronizationModule().getClass().getSimpleName() + "\n");
		getSynchronizationModule().dumpDICOMInfo(objIDicomStream);

		objIDicomStream.writeString("\t" + getGeneralEquipmentModule().getClass().getSimpleName() + "\n");
		getGeneralEquipmentModule().dumpDICOMInfo(objIDicomStream);

		objIDicomStream.writeString("\t" + getEnhancedGeneralEquipmentModule().getClass().getSimpleName() + "\n");
		getEnhancedGeneralEquipmentModule().dumpDICOMInfo(objIDicomStream);

		
		objIDicomStream.writeString("\t" + getEnhancedContrastBolusModule().getClass().getSimpleName() + "\n");
		getEnhancedContrastBolusModule().dumpDICOMInfo(objIDicomStream);

		objIDicomStream.writeString("\t" + getMultiFrameFunctionalGroupsModule().getClass().getSimpleName() + "\n");
		getMultiFrameFunctionalGroupsModule().dumpDICOMInfo(objIDicomStream);

		objIDicomStream.writeString("\t" + getMultiFrameDimensionModule().getClass().getSimpleName() + "\n");
		getMultiFrameDimensionModule().dumpDICOMInfo(objIDicomStream);

		objIDicomStream.writeString("\t" + getCardiacSynchronizationModule().getClass().getSimpleName() + "\n");
		getCardiacSynchronizationModule().dumpDICOMInfo(objIDicomStream);

		objIDicomStream.writeString("\t" + getRespiratorySynchronizationModule().getClass().getSimpleName() + "\n");
		getRespiratorySynchronizationModule().dumpDICOMInfo(objIDicomStream);

		objIDicomStream.writeString("\t" + getBulkMotionSynchronizationModule().getClass().getSimpleName() + "\n");
		getBulkMotionSynchronizationModule().dumpDICOMInfo(objIDicomStream);

		objIDicomStream.writeString("\t" + getAcquisitionContextModule().getClass().getSimpleName() + "\n");
		getAcquisitionContextModule().dumpDICOMInfo(objIDicomStream);

		objIDicomStream.writeString("\t" + getMRSpectroscopyDataModule().getClass().getSimpleName() + "\n");
		getMRSpectroscopyDataModule().dumpDICOMInfo(objIDicomStream);

		objIDicomStream.writeString("\t" + getMRSpectroscopyModulePulseSequenceModule().getClass().getSimpleName() + "\n");
		getMRSpectroscopyModulePulseSequenceModule().dumpDICOMInfo(objIDicomStream);

		objIDicomStream.writeString("\t" + getMRSpectroscopyModule().getClass().getSimpleName() + "\n");
		getMRSpectroscopyModule().dumpDICOMInfo(objIDicomStream);

		objIDicomStream.writeString("\t" + getSOPCommonModule().getClass().getSimpleName() + "\n");
		getSOPCommonModule().dumpDICOMInfo(objIDicomStream);
		
		objIDicomStream.writeString("\t" + getSpecimenModule().getClass().getSimpleName() + "\n");
		getSpecimenModule().dumpDICOMInfo(objIDicomStream);
	}

	/**
	 * Checks whether the Module is present in the IOD or not.
	 * @param strModuleName String value of module name 
	 * @return boolean - true if the module is present in the IOD, else false is returned.
	 */
	public boolean isModulePresent(String strModuleName)
	{
		if(!super.isModulePresent(strModuleName))
		{
			if(strModuleName == null || strModuleName.equals(""))
				return false;
			if(strModuleName.equalsIgnoreCase(EnumModuleNames.PATIENT_MODULE.getValue()) && isModulePresent(PatientModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.SPECIMEN_IDENTIFICATION_MODULE.getValue()) && isModulePresent(SpecimenIdentificationModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.CLINICAL_TRIAL_SUBJECT_MODULE.getValue()) && isModulePresent(ClinicalTrialSubjectModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.GENERAL_STUDY_MODULE.getValue()) && isModulePresent(GeneralStudyModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.PATIENT_STUDY_MODULE.getValue()) && isModulePresent(PatientStudyModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.CLINICAL_TRIAL_SERIES_MODULE.getValue()) && isModulePresent(ClinicalTrialSeriesModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.GENERAL_SERIES_MODULE.getValue()) && isModulePresent(GeneralSeriesModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.MR_SERIES_MODULE.getValue()) && isModulePresent(MRSeriesModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.FRAME_OF_REFERENCE_MODULE.getValue()) && isModulePresent(FrameOfReferenceModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.SYNCHRONIZATION_MODULE.getValue()) && isModulePresent(SynchronizationModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.GENERAL_EQUIPMENT_MODULE.getValue()) && isModulePresent(GeneralEquipmentModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.ENHANCED_GENERAL_EQUIPMENT_MODULE.getValue()) && isModulePresent(EnhancedGeneralEquipmentModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.ENHANCED_CONTRAST_BOLUS_MODULE.getValue()) && isModulePresent(EnhancedContrastBolusModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.MULTI_FRAME_FUNCTIONAL_GROUPS_MODULE.getValue()) && isModulePresent(MultiFrameFunctionalGroupsModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.MULTI_FRAME_DIMENSION_MODULE.getValue()) && isModulePresent(MultiFrameDimensionModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.CARDIAC_SYNCHRONIZATION_MODULE.getValue()) && isModulePresent(CardiacSynchronizationModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.RESPIRATORY_SYNCHRONIZATION_MODULE.getValue()) && isModulePresent(RespiratorySynchronizationModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.BULK_MOTION_SYNCHRONIZATION_MODULE.getValue()) && isModulePresent(BulkMotionSynchronizationModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.ACQUISITION_CONTEXT_MODULE.getValue()) && isModulePresent(AcquisitionContextModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.MR_SPECTROSCOPY_DATA_MODULE.getValue()) && isModulePresent(MRSpectroscopyDataModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.MR_SPECTROSCOPY_PULSE_SEQUENCE_MODULE.getValue()) && isModulePresent(MRSpectroscopyPulseSequenceModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.MR_SPECTROSCOPY_MODULE.getValue()) && isModulePresent(MRSpectroscopyModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.SOP_COMMON_MODULE.getValue()) && isModulePresent(SOPCommonModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.SPECIMEN_MODULE.getValue()) && isModulePresent(SpecimenModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.COMMON_INSTANCE_REFERENCE_MODULE.getValue()) && isModulePresent(CommonInstanceReferenceModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.FRAME_EXTRACTION_MODULE.getValue()) && isModulePresent(FrameExtractionModule.objSingleCollection))
				return true;
			else
			{
				if(DicomConfig.createInstance().isLoggingOn())
				{
					DicomLogger objDicomLogger = DicomLogger.getLogger();
					objDicomLogger.log(this.getClass().getName() + " : " +ResourceBundleWrapper.getValue("status","ModuleNotPresent",new Locale(DicomConfig.createInstance().getLocale())) + strModuleName);
				}
				return false;
			}
		}
		return true;
	}
}
