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

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


import cdac.medinfo.sdk.dcm30_04.baselibs.DetectorInformationSequenceItem;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDataSet;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDicomStream;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDicomValue;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDictionaryReader;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IItem;
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.CurveModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.FrameOfReferenceModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.GeneralImageModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.ImageIOD;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.MultiFrameModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.MultiFrameOverlayModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.NMDetectorModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.NMImageModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.NMImagePixelModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.NMIsotopeModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.NMMultiFrameModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.NMMultiGatedAcquisitionModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.NMOrPETPatientOrientationModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.NMPhaseModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.NMReconstructionModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.NMTOMOAcquisitionModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.OverlayPlaneModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.SOPCommonModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.SynchronizationModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.VOILUTModule;
import cdac.medinfo.sdk.dcm30_04.enums.EnumImageType;
import cdac.medinfo.sdk.dcm30_04.enums.EnumModuleNames;
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.ICollectionIterator;
import cdac.medinfo.sdk.dcm30_04.utils.ResourceBundleWrapper;

/**
 * The Nuclear Medicine (NM) Image Information Object Definition (IOD) specifies an image that
 * has been created by a nuclear medicine imaging device.
 * This class exhibits different functional behavour of the NM IOD.
 */
public class NMIOD extends ImageIOD 
{
	

	/**
	 * The Constant NM_IOD.
	 */
	public static final String NM_IOD = "NM IOD";

	/**
	 * The obj nm or pet patient orientation module.
	 */
	NMOrPETPatientOrientationModule objNMOrPETPatientOrientationModule = null;

	/**
	 * The obj frame of reference module.
	 */
	FrameOfReferenceModule objFrameOfReferenceModule = null;
	
	/**
	 * The obj Synchronization module.
	 */
	SynchronizationModule objSynchronizationModule = null;

	/**
	 * The obj general image module.
	 */
	GeneralImageModule objGeneralImageModule = null;

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

	/**
	 * The obj nm image pixel module.
	 */
	NMImagePixelModule objNMImagePixelModule = null;

	/**
	 * The obj multi frame module.
	 */
	MultiFrameModule objMultiFrameModule = null;

	/**
	 * The obj nm multi frame module.
	 */
	NMMultiFrameModule objNMMultiFrameModule = null;

	/**
	 * The obj nm image module.
	 */
	NMImageModule objNMImageModule = null;

	/**
	 * The obj nm isotope module.
	 */
	NMIsotopeModule objNMIsotopeModule = null;

	/**
	 * The obj nm detector module.
	 */
	NMDetectorModule objNMDetectorModule = null;

	/**
	 * The obj nmtomo acquisition module.
	 */
	NMTOMOAcquisitionModule objNMTOMOAcquisitionModule = null;

	/**
	 * The obj nm multi gated acquisition module.
	 */
	NMMultiGatedAcquisitionModule objNMMultiGatedAcquisitionModule = null;

	/**
	 * The obj nm phase module.
	 */
	NMPhaseModule objNMPhaseModule = null;

	/**
	 * The obj nm reconstruction module.
	 */
	NMReconstructionModule objNMReconstructionModule = null;

	/**
	 * The obj overlay plane module.
	 */
	OverlayPlaneModule objOverlayPlaneModule = null;

	/**
	 * The obj multi frame overlay module.
	 */
	MultiFrameOverlayModule objMultiFrameOverlayModule = null;

	/**
	 * The obj curve module.
	 */
	CurveModule objCurveModule = null;

	/**
	 * The obj voilut module.
	 */
	VOILUTModule objVOILUTModule = null;

	/**
	 * The obj sop common module.
	 */
	SOPCommonModule objSOPCommonModule = null;


	/**
	 * Instantiates a new nMIOD.
	 */
	public NMIOD()
	{
		super();
	}

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

	/**
	 * This constructor initializes the  DictionaryReader
	 * in its superclass IOD.
	 * 
	 * @param objIDictionaryReader_ - specifies the DICOM Dictionary reader to be used .
	 */
	public NMIOD(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 NMIOD(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 NMIOD(ITransferSyntax objITransferSyntax_,IDictionaryReader objIDictionaryReader_)
	{
		super(objITransferSyntax_,objIDictionaryReader_);
	}

	/**
	 * Creates NMIOD IOD from a DICOM file containing NMIOD 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 NMIOD(String strFileName)throws IOException,InvalidIODException
	{
		super(strFileName);
		try
		{
			if(objIDataSet != null)
			{
				String strSOPClassUID = objIDataSet.getSOPClassUID();
				if(strSOPClassUID != null)
				{
					if(!strSOPClassUID.equals(EnumSOPClassUIDs.NMIOD.getValue()))
					{
						throw new InvalidIODException(ResourceBundleWrapper.getValue("exceptions", "InvalidIOD.FileNotMatched", new Locale(DicomConfig.createInstance().getLocale())) + NM_IOD);
					}
				}
				strSOPClassUID = null;
			}
		}
		catch(InvalidIODException iie)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(iie.getMessage());
			}
			throw iie;
		}
	}

	/**
	 * Retrieves pre-initialized instance of NM Or PET Patient Orientation 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 NMOrPETPatientOrientationModule - instance of {@link NMOrPETPatientOrientationModule}.
	 */
	public NMOrPETPatientOrientationModule getNMOrPETPatientOrientationModule()
	{
		if(objNMOrPETPatientOrientationModule == null)
		{
			objNMOrPETPatientOrientationModule = new NMOrPETPatientOrientationModule(objIDataSet);
			return objNMOrPETPatientOrientationModule;
		}
		return objNMOrPETPatientOrientationModule;
	}

	/**
	 * Retrieves pre-initialized instance of General Image 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 GeneralImageModule - instance of {@link GeneralImageModule}.
	 */
	public GeneralImageModule getGeneralImageModule()
	{
		if(objGeneralImageModule == null)
		{
			objGeneralImageModule = new GeneralImageModule(objIDataSet);
			return objGeneralImageModule;
		}
		return objGeneralImageModule;
	}

	/**
	 * Retrieves pre-initialized instance of Synchronization 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 SynchronizationModule - instance of {@link SynchronizationModule}.
	 */
	public SynchronizationModule getSynchronizationModule() 
	{
		if(objSynchronizationModule == null)
		{
			objSynchronizationModule = new SynchronizationModule(objIDataSet);
			return objSynchronizationModule;
		}
		return objSynchronizationModule;
	}
	
	/**
	 * Retrieves pre-initialized instance of Frame Of 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 FrameOfReferenceModule - instance of {@link FrameOfReferenceModule}.
	 */
	public FrameOfReferenceModule getFrameOfReferenceModule() 
	{
		if(objFrameOfReferenceModule == null)
		{
			objFrameOfReferenceModule = new FrameOfReferenceModule(objIDataSet);
			return objFrameOfReferenceModule;
		}
		return objFrameOfReferenceModule;
	}

	/**
	 * Retrieves pre-initialized instance of Overlay Plane 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 OverlayPlaneModule - instance of {@link OverlayPlaneModule}.
	 */
	public OverlayPlaneModule getOverlayPlaneModule()
	{
		if(objOverlayPlaneModule == null)
		{
			objOverlayPlaneModule = new OverlayPlaneModule(objIDataSet);
			return objOverlayPlaneModule;
		}
		return objOverlayPlaneModule;
	}

	/**
	 * Retrieves pre-initialized instance of Acquisition Context 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 AcquisitionContextModule - instance of {@link AcquisitionContextModule}.
	 */
	public AcquisitionContextModule getAcquisitionContextModule()
	{
		if(objAcquisitionContextModule == null)
		{
			objAcquisitionContextModule = new AcquisitionContextModule(objIDataSet);
			return objAcquisitionContextModule;
		}
		return objAcquisitionContextModule;
	}

	/**
	 * Retrieves pre-initialized instance of NM Image Pixel 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 NMImagePixelModule - instance of {@link NMImagePixelModule}.
	 */
	public NMImagePixelModule getNMImagePixelModule()
	{
		if(objNMImagePixelModule == null)
		{
			objNMImagePixelModule = new NMImagePixelModule(objIDataSet);
			return objNMImagePixelModule;
		}
		return objNMImagePixelModule;
	}

	/**
	 * Retrieves pre-initialized instance of Multi Frame 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 MultiFrameModule - instance of {@link MultiFrameModule}.
	 */
	public MultiFrameModule getMultiFrameModule()
	{
		if(objMultiFrameModule == null)
		{
			objMultiFrameModule = new MultiFrameModule(objIDataSet);
			return objMultiFrameModule;
		}
		return objMultiFrameModule;
	}

	/**
	 * Retrieves pre-initialized instance of NM Multi Frame 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 NMMultiFrameModule - instance of {@link NMMultiFrameModule}.
	 */
	public NMMultiFrameModule getNMMultiFrameModule()
	{
		if(objNMMultiFrameModule == null)
		{
			objNMMultiFrameModule = new NMMultiFrameModule(objIDataSet);
			return objNMMultiFrameModule;
		}
		return objNMMultiFrameModule;
	}

	/**
	 * Retrieves pre-initialized instance of NM Image 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 NMImageModule - instance of {@link NMImageModule}.
	 */
	public NMImageModule getNMImageModule()
	{
		if(objNMImageModule == null)
		{
			objNMImageModule = new NMImageModule(objIDataSet);
			return objNMImageModule;
		}
		return objNMImageModule;
	}

	/**
	 * Retrieves pre-initialized instance of NM Isotope 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 NMIsotopeModule - instance of {@link NMIsotopeModule}.
	 */
	public NMIsotopeModule getNMIsotopeModule()
	{
		if(objNMIsotopeModule == null)
		{
			objNMIsotopeModule = new NMIsotopeModule(objIDataSet);
			return objNMIsotopeModule;
		}
		return objNMIsotopeModule;
	}

	/**
	 * Retrieves pre-initialized instance of NM Detector 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 NMDetectorModule - instance of {@link NMDetectorModule}.
	 */
	public NMDetectorModule getNMDetectorModule()
	{
		if(objNMDetectorModule == null)
		{
			objNMDetectorModule = new NMDetectorModule(objIDataSet);
			return objNMDetectorModule;
		}
		return objNMDetectorModule;
	}

	/**
	 * Retrieves pre-initialized instance of NM TO MO Acquisition 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 NMTOMOAcquisitionModule - instance of {@link NMTOMOAcquisitionModule}.
	 */
	public NMTOMOAcquisitionModule getNMTOMOAcquisitionModule()
	{
		if(objNMTOMOAcquisitionModule == null)
		{
			objNMTOMOAcquisitionModule = new NMTOMOAcquisitionModule(objIDataSet);
			return objNMTOMOAcquisitionModule;
		}
		return objNMTOMOAcquisitionModule;
	}

	/**
	 * Retrieves pre-initialized instance of NM Multi Gated Acquisition 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 NMMultiGatedAcquisitionModule - instance of {@link NMMultiGatedAcquisitionModule}.
	 */
	public NMMultiGatedAcquisitionModule getNMMultiGatedAcquisitionModule()
	{
		if(objNMMultiGatedAcquisitionModule == null)
		{
			objNMMultiGatedAcquisitionModule = new NMMultiGatedAcquisitionModule(objIDataSet);
			return objNMMultiGatedAcquisitionModule;
		}
		return objNMMultiGatedAcquisitionModule;
	}

	/**
	 * Retrieves pre-initialized instance of NM Phase 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 NMPhaseModule - instance of {@link NMPhaseModule}.
	 */
	public NMPhaseModule getNMPhaseModule()
	{
		if(objNMPhaseModule == null)
		{
			objNMPhaseModule = new NMPhaseModule(objIDataSet);
			return objNMPhaseModule;
		}
		return objNMPhaseModule;
	}

	/**
	 * Retrieves pre-initialized instance of NM Reconstruction 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 NMReconstructionModule - instance of {@link NMReconstructionModule}.
	 */
	public NMReconstructionModule getNMReconstructionModule()
	{
		if(objNMReconstructionModule == null)
		{
			objNMReconstructionModule = new NMReconstructionModule(objIDataSet);
			return objNMReconstructionModule;
		}
		return objNMReconstructionModule;
	}

	/**
	 * Retrieves pre-initialized instance of Multi Frame Overlay 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 MultiFrameOverlayModule - instance of {@link MultiFrameOverlayModule}.
	 */
	public MultiFrameOverlayModule getMultiFrameOverlayModule()
	{
		if(objMultiFrameOverlayModule == null)
		{
			objMultiFrameOverlayModule = new MultiFrameOverlayModule(objIDataSet);
			return objMultiFrameOverlayModule;
		}
		return objMultiFrameOverlayModule;
	}

	/**
	 * Retrieves pre-initialized instance of Curve 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 CurveModule - instance of {@link CurveModule}.
	 */
	public CurveModule getCurveModule()
	{
		if(objCurveModule == null)
		{
			objCurveModule = new CurveModule(objIDataSet);
			return objCurveModule;
		}
		return objCurveModule;
	}

	/**
	 * Retrieves pre-initialized instance of SOP Common 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 SOPCommonModule - instance of {@link SOPCommonModule}.
	 */
	public SOPCommonModule getSOPCommonModule()
	{
		if(objSOPCommonModule == null)
		{
			objSOPCommonModule = new SOPCommonModule(objIDataSet);
			return objSOPCommonModule;
		}
		return objSOPCommonModule;
	}

	/**
	 * Retrieves pre-initialized instance of VOI LUT 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 VOILUTModule - instance of {@link VOILUTModule}.
	 */
	public VOILUTModule getVOILUTModule()
	{
		if(objVOILUTModule == null)
		{
			objVOILUTModule = new VOILUTModule(objIDataSet);
			return objVOILUTModule;
		}
		return objVOILUTModule;
	}

	/**
	 * This method validates NM 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 - function status indicating success or failure.
	 * 
	 * @throws InvalidIODException -  throws when the IOD is invalid.
	 */
	public boolean validate() throws InvalidIODException
	{
		/*
		 * This condition calls ImageIOD class's validation which checks for common modules validation
		 */
		super.validate();
		try
		{
			/*
			 * This condition checks for NM IOD's mandatory modules validation
			 */
			getGeneralEquipmentModule().validateModule();
			getNMOrPETPatientOrientationModule().validateModule();
			getNMImagePixelModule().validateModule();
			getMultiFrameModule().validateModule();
			getNMMultiFrameModule().validateModule();
			getNMImageModule().validateModule();
			getGeneralImageModule().validateModule();
			getNMIsotopeModule().validateModule();
			getNMDetectorModule().validateModule();
			getSOPCommonModule().validateModule();

			/*
			 * Validation for attribute PatientOrientation(0020,0020) of GeneralImageModule is implemented at IOD level.
			 * condition :- Required if image does not require Image Orientation (Patient)(0020,0037) and Image Position (Patient)
			 * 				(0020,0032). See C.7.6.1.1.1 for further explanation.
			 * Right now we are taking it as if IOD contains any module which has (0020,0037) and (0020,0032)
			 * and if that module and attribute are compulsory then (0020,0020) is not mandatory.
			 * If no module is there in IOD then (0020,0020) is mandatory.
			 * and if attribute or module is optional or conditional then 0020,0020 is also conditional.
			 * THIS IOD CONTAINS NM DETECTOR MODULE WHICH HAS BOTH THE ATTRIBUTES AS MANDATORY. 
			 */

			/*
			 * Validation for Detector Information Sequence which comes in NM DETECTOR MODULE.
			 * Refer pdf 3 page 371.
			 * In this attribute 0018,1110 depends upon image type and NM DETECTOR MODULE does not contain image type.
			 * So it is module level dependency which is checked at IOD level.
			 */
			String []arrstrImageType = getGeneralImageModule().getImageType();
			if(arrstrImageType == null)
				arrstrImageType = getNMImageModule().getImageType();
			if(arrstrImageType != null && arrstrImageType.length == 4)
			{
				if((EnumImageType.Value4.TRANSMISSION.getValue().equalsIgnoreCase(arrstrImageType[3])) && !(EnumImageType.Value3.TOMO.getValue().equalsIgnoreCase(arrstrImageType[2])))
				{
					DetectorInformationSequenceItem objDetectorInformationSequenceItem = null;
					ICollectionIterator<IItem> objDetectorInformationSequenceIterator = getNMDetectorModule().getDetectorInformationSequence().iterator();
					while(objDetectorInformationSequenceIterator.hasNext())
					{
						objDetectorInformationSequenceItem = (DetectorInformationSequenceItem)objDetectorInformationSequenceIterator.next();
						if(!objDetectorInformationSequenceItem.contains(0x0018,0x1110))
						{
							throw new ConditionFailedException("0018,1110 Required if ImageType Value 4 is TRANSMISSION and Value 3 is not TOMO");
						}
					}
				}
			}

			/*
			 * This condition checks for NM IOD's User Optional modules validation
			 */
			if(isModulePresent(FrameOfReferenceModule.objSingleCollection, objIDataSet))
			{
				getFrameOfReferenceModule().validateModule();
			}
			if(isModulePresent(SynchronizationModule.objSingleCollection, objIDataSet))
			{
				getSynchronizationModule().validateModule();
			}
			if(isModulePresent(AcquisitionContextModule.objSingleCollection, objIDataSet))
			{
				getAcquisitionContextModule().validateModule();
			}
			if(isModulePresent(OverlayPlaneModule.objSingleCollection, objIDataSet))
			{
				getOverlayPlaneModule().validateModule();
			}
			if(isModulePresent(MultiFrameOverlayModule.objSingleCollection, objIDataSet))
			{
				getMultiFrameOverlayModule().validateModule();
			}
			if(isModulePresent(CurveModule.objSingleCollection, objIDataSet))
			{
				getCurveModule().validateModule();
			}
			if(isModulePresent(VOILUTModule.objSingleCollection, objIDataSet))
			{
				getVOILUTModule().validateModule();
			}

			/*
			 * This condition checks for NM IOD's Conditional modules validation
			 */
			if(objIDataSet.isPresent(0x0008, 0x0008))
			{
				IDicomValue objIDicomValue =  objIDataSet.getDataForTag(0x0008, 0x0008).getDicomValue();
				if(objIDicomValue!=null)
				{
					String[] arrImageType = null;
					if(objIDicomValue.getValue()!=null)
						arrImageType = (String[])objIDicomValue.getValue();
					if(arrImageType!=null)
					{
						if(arrImageType.length > 2) 
						{
							if(arrImageType[2].equals(EnumImageType.Value3.TOMO.getValue()) ||
									arrImageType[2].equals(EnumImageType.Value3.GATED_TOMO.getValue()) ||
									arrImageType[2].equals(EnumImageType.Value3.RECON_TOMO.getValue()) ||
									arrImageType[2].equals(EnumImageType.Value3.RECON_GATED_TOMO.getValue())
							)
							{
								if(getNMTOMOAcquisitionModule().validateModule())
									return false;
							}

							if(arrImageType[2].equals(EnumImageType.Value3.GATED.getValue()) ||
									arrImageType[2].equals(EnumImageType.Value3.GATED_TOMO.getValue()) ||
									arrImageType[2].equals(EnumImageType.Value3.RECON_GATED_TOMO.getValue())
							)
							{
								if(getNMMultiGatedAcquisitionModule().validateModule())
									return false;
							}
							if( arrImageType[2].equals(EnumImageType.Value3.DYNAMIC.getValue()))
							{
								if(getNMPhaseModule().validateModule())
									return false;
							}
							if(arrImageType[2].equals(EnumImageType.Value3.RECON_TOMO.getValue()) ||
									arrImageType[2].equals(EnumImageType.Value3.RECON_GATED_TOMO.getValue())
							)
							{
								if(getNMReconstructionModule().validateModule())
									return false;
							}
						}
					}
				}
			}

			// checking for presence of Image data
			if(!isPresent(0x7FE0,0x0010))
			{
				InvalidIODException iie= new InvalidIODException(ResourceBundleWrapper.getValue("exceptions", "ImageData", new Locale(DicomConfig.createInstance().getLocale())));
				if(DicomConfig.createInstance().isLoggingOn())
					DicomLogger.getLogger().log(iie.getMessage());
				throw iie;
			}
		}
		catch(InvalidModuleException ime)
		{
			if(DicomConfig.createInstance().isLoggingOn())
				DicomLogger.getLogger().log(ime.getMessage());
			
				throw new InvalidIODException(NM_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(NM_IOD+ "\n");
		super.dumpDICOMInfo(objIDicomStream);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

		objIDicomStream.writeString("\t" + getSOPCommonModule().getClass().getSimpleName() + "\n");
		getSOPCommonModule().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.NM_PET_PATIENT_ORIENTATION_MODULE.getValue()) && isModulePresent(NMOrPETPatientOrientationModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.SYNCHRONIZATION_MODULE.getValue()) && isModulePresent(FrameOfReferenceModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.FRAME_OF_REFERENCE_MODULE.getValue()) && isModulePresent(FrameOfReferenceModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.GENERAL_IMAGE_MODULE.getValue()) && isModulePresent(GeneralImageModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.ACQUISITION_CONTEXT_MODULE.getValue()) && isModulePresent(AcquisitionContextModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.NM_IMAGE_PIXEL_MODULE.getValue()) && isModulePresent(NMImagePixelModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.MULTI_FRAME_MODULE.getValue()) && isModulePresent(MultiFrameModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.NM_MULTI_FRAME_MODULE.getValue()) && isModulePresent(NMMultiFrameModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.NM_IMAGE_MODULE.getValue()) && isModulePresent(NMImageModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.NM_ISOTOPE_MODULE.getValue()) && isModulePresent(NMIsotopeModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.NM_DETECTOR_MODULE.getValue()) && isModulePresent(NMDetectorModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.NM_TOMO_ACQUISITION_MODULE.getValue()) && isModulePresent(NMTOMOAcquisitionModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.NM_MULTI_GATED_ACQUISITION_MODULE.getValue()) && isModulePresent(NMMultiGatedAcquisitionModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.NM_PHASE_MODULE.getValue()) && isModulePresent(NMPhaseModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.NM_RECONSTRUCTION_MODULE.getValue()) && isModulePresent(NMReconstructionModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.OVERLAY_PLANE_MODULE.getValue()) && isModulePresent(OverlayPlaneModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.MULTI_FRAME_OVERLAY_MODULE.getValue()) && isModulePresent(MultiFrameOverlayModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.CURVE_MODULE.getValue()) && isModulePresent(CurveModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.VOI_LUT_MODULE.getValue()) && isModulePresent(VOILUTModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.SOP_COMMON_MODULE.getValue()) && isModulePresent(SOPCommonModule.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;
	}
}
