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



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.enums.EnumModuleNames;

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;




// TODO: Auto-generated Javadoc
/**
 * This class represents the Composite IODs which may consists of a collection
 * of different types of modules. This category contains Image IODs as well as Non-Image IODs.
 * 
 * 
 */
public abstract class CompositeIOD extends IOD 
{


	/**
	 * The obj clinical trial subject module.
	 */
	ClinicalTrialSubjectModule objClinicalTrialSubjectModule = null;

	/**
	 * The obj clinical trial study module.
	 */
	ClinicalTrialStudyModule objClinicalTrialStudyModule = null;

	/**
	 * Creates Composite IOD with default initialization.
	 */
	public CompositeIOD()
	{
		super();
	}

	/**
	 * This constructor initializes the TransferSyntax in its superclass IOD.
	 * 
	 * @param objITransferSyntax_  Transfer syntax of IOD.
	 */
	public CompositeIOD(ITransferSyntax objITransferSyntax_)
	{
		super(objITransferSyntax_);
	}

	/**
	 * This constructor initializes the DataSet and DictionaryReader in its superclass IOD.
	 * 
	 * @param objIDictionaryReader_ the obj i dictionary reader_
	 */
	public CompositeIOD(IDictionaryReader objIDictionaryReader_)
	{
		super(objIDictionaryReader_);
	}

	/**
	 * This constructor accepts the DataSet from the user in its superclass IOD.
	 * 
	 * @param objIDataSet_ the obj i data set_
	 */
	public CompositeIOD(IDataSet objIDataSet_)
	{
		super(objIDataSet_);
	}

	/**
	 * This constructor initializes the transfer syntax and DictionaryReader in its superclass IOD.
	 * 
	 * @param objITransferSyntax_ Transfer syntax of IOD.
	 * @param objIDictionaryReader_ Dictionary Reader used by IOD.
	 */
	public CompositeIOD(ITransferSyntax objITransferSyntax_,IDictionaryReader objIDictionaryReader_)
	{
		super(objITransferSyntax_,objIDictionaryReader_);
	}

	/**
	 * Creates IOD from a DICOM file containing corresponding DICOM data.
	 * @param strFileName DICOM File with absolute path.
	 * @throws IOException signals Exception caused due to wrong I/O operations. 
	 * @throws ParseException signals if some parse exceptions occurred.
	 */
	public CompositeIOD(String strFileName)throws IOException, InvalidIODException
	{
		super(strFileName);
	}

	/**
	 * This method returns the ClinicalTrialSubjectModule with preinitializing it.
	 * 
	 * @return the clinical trial subject module
	 */
	public ClinicalTrialSubjectModule getClinicalTrialSubjectModule()
	{
		if(objClinicalTrialSubjectModule == null)
		{
			objClinicalTrialSubjectModule = new ClinicalTrialSubjectModule(objIDataSet);
			return objClinicalTrialSubjectModule;
		}
		return objClinicalTrialSubjectModule;
	}

	/**
	 * This method returns the ClinicalTrialStudyModule with preinitializing it.
	 * 
	 * @return the clinical trial study module
	 */
	public ClinicalTrialStudyModule getClinicalTrialStudyModule()
	{
		if(objClinicalTrialStudyModule == null)
		{
			objClinicalTrialStudyModule = new ClinicalTrialStudyModule(objIDataSet);
			return objClinicalTrialStudyModule;
		}
		return objClinicalTrialStudyModule;
	}

	/**
	 * This method validates Composite IOD according to the Module Table given in The DICOM Standard.
	 * The validate method fails if one of the modules are present but are invalid because both the
	 * modules in Composite IOD are user optional modules
	 * 
	 * @return - function status indicating success or failure
	 * 
	 * @throws InvalidIODException the invalid iod exception
	 */
	public boolean validate() throws InvalidIODException
	{
		super.validate();

		/*
		 * This condition checks for Composite IOD's User Optional modules validation
		 */
		try
		{
			{
				if(!objIDataSet.isPresent(0x0008, 0x0060))
					throw new InvalidIODException(ResourceBundleWrapper.getValue("exceptions", "SOPclassUIDorModality", new Locale(DicomConfig.createInstance().getLocale())));
			}

			if(isModulePresent(ClinicalTrialSubjectModule.objSingleCollection, objIDataSet))
			{
				getClinicalTrialSubjectModule().validateModule();
			}
			if(isModulePresent(ClinicalTrialStudyModule.objSingleCollection, objIDataSet))
			{
				getClinicalTrialStudyModule().validateModule();
			}
		}
		catch(InvalidModuleException ime)
		{
			if(DicomConfig.createInstance().isLoggingOn())
				DicomLogger.getLogger().log(ime.getMessage());
			
				throw new InvalidIODException(ime.getMessage(),ime);
		}
		return true;
	}


	/**
	 *  Dumps the Name of Module and corresponding Attributs of IOD.
	 *  @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("\t" + getClinicalTrialSubjectModule().getClass().getSimpleName() + "\n");
		getClinicalTrialSubjectModule().dumpDICOMInfo(objIDicomStream);

		objIDicomStream.writeString("\t" + getClinicalTrialStudyModule().getClass().getSimpleName() + "\n");
		getClinicalTrialStudyModule().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.CLINICAL_TRIAL_SUBJECT_MODULE.getValue()) && isModulePresent(ClinicalTrialSubjectModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.CLINICAL_TRIAL_STUDY_MODULE.getValue()) && isModulePresent(ClinicalTrialStudyModule.objSingleCollection))
				return true;
			else
				return false;
		}
		return true;
	}

}
