/*******************************************************************************
 *  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 cdac.medinfo.sdk.dcm30_04.baselibs.AnatomicStructureSpaceorRegionSequence;
import cdac.medinfo.sdk.dcm30_04.baselibs.DicomV3Factory;
import cdac.medinfo.sdk.dcm30_04.baselibs.ExposureDoseSequence;
import cdac.medinfo.sdk.dcm30_04.baselibs.Tag;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDataSet;
import cdac.medinfo.sdk.dcm30_04.commoninterface.ITag;
import cdac.medinfo.sdk.dcm30_04.exceptions.InvalidModuleException;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConstants;
import cdac.medinfo.sdk.dcm30_04.utils.ISingleCollection;
import cdac.medinfo.sdk.dcm30_04.utils.SingleCollection;

// TODO: Auto-generated Javadoc
/**
 \* This module describes the attributes in Radiation Dose Module.
 */
public class RadiationDoseModule extends Module  
{

 
 /**
  * The obj single collection.
  */
 public static ISingleCollection<ITag> objSingleCollection = null;
	 

	/**
	 * Creates Annotation List Module with default initialization.
	 * 
	 * @param objIDataSet the obj i data set
	 */
	public RadiationDoseModule(IDataSet objIDataSet)
	{
		super(objIDataSet);
	}

	static
	{
		objSingleCollection = new SingleCollection<ITag>();
		objSingleCollection.add(new Tag(0x0008,0x2229));
		objSingleCollection.add(new Tag(0x0040,0x0300));		
		objSingleCollection.add(new Tag(0x0040,0x0301));
		objSingleCollection.add(new Tag(0x0018,0x1110));
		objSingleCollection.add(new Tag(0x0040,0x0306));
		objSingleCollection.add(new Tag(0x0040,0x0302));
		objSingleCollection.add(new Tag(0x0040,0x8302));
		objSingleCollection.add(new Tag(0x0040,0x0303));
		objSingleCollection.add(new Tag(0x0018,0x115E));
		objSingleCollection.add(new Tag(0x0040,0x0310));
		objSingleCollection.add(new Tag(0x0040,0x030E));
	}

	/**
	 * This method validates each Attribute in the Module according to the Type
	 * of the Attribute.Attributes are of 5 types.
	 * Type 1,Type 2 are Mandatory:
	 * Mandatory Attributes have to present in the Module.
	 * Type 1C,2C are Conditional:
	 * Conditional Attributes' presence depend on a condition.
	 * If the condition is satisfied the Attribute has to be present.
	 * Type 3 are Optional
	 * Optional Attributes can be added as and when required.
	 * 
	 * @return - validation status as success or failure
	 * 
	 * @throws InvalidModuleException when the module is invalid
	 */
	public boolean validateModule() throws InvalidModuleException
	{
		//All Attributes are optional.
		return true;
	}
	
	/**
	 * This method returns the vector of keys representing the DataElements
	 * that belong to this module.
	 * 
	 * @return vector of Tags - returns the vector containing the collection of tags
	 */
	public ISingleCollection<ITag> getVectorOfKeys()
	{
		return objSingleCollection;
	}


	/**
	 * Retrieves pre-initialized object of AnatomicStructureSpaceorRegionSequence.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
	 * 
	 * @return AnatomicStructureSpaceorRegionSequence - instance of Anatomic Structure Space or Region Sequence
	 */
	public AnatomicStructureSpaceorRegionSequence getAnatomicStructureSpaceorRegionSequence()
	{
		AnatomicStructureSpaceorRegionSequence objAnatomicStructureSpaceorRegionSequence = (AnatomicStructureSpaceorRegionSequence)getDicomValue(0x0008,0x2229);
		if(objAnatomicStructureSpaceorRegionSequence == 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();
			objAnatomicStructureSpaceorRegionSequence = (AnatomicStructureSpaceorRegionSequence)objDicomV3Factory.createSequence(0x0008,0x2229, objITransferSyntax, objIDictionaryReader);
			
			// This will add Data Element of SQ to the DataSet.
			addDataElement(0x0008,0x2229, DicomConstants.DCM_DT_SQ, objAnatomicStructureSpaceorRegionSequence.getValue());
		}
		return objAnatomicStructureSpaceorRegionSequence;
	}

	/**
	 * Sets Total duration of X-Ray exposure during fluoroscopy
	 * in seconds (pedal time) during this Performed Procedure Step.
	 * 
	 * @param shTotalTimeofFluoroscopy short value of total duration.
	 */
	public void setTotalTimeofFluoroscopy(short shTotalTimeofFluoroscopy)
	{
		/* Accept the Total Time of Fluoroscopy of type US.
		 * Read the Transfer Syntax and check if ImplicitDE or ExplicitDE to create
		 * Set the iGroupNumber and iElementNumber to ImplicitDE object 
		 * Set the iVRType
		 * Set length from the element
		 * Add this element to the Data Set
		 */
		addDataElement(0x0040,0x0300, DicomConstants.DCM_DT_US, shTotalTimeofFluoroscopy);
	}

	/**
	 * Retrieves Total duration of X-Ray exposure during fluoroscopy
	 * in seconds (pedal time) during this Performed Procedure Step.
	 * 
	 * @return short - short value of Total duration.
	 */
	public short getTotalTimeofFluoroscopy()
	{
		Object objValue = getValue(0x0040,0x0300);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}

	/**
	 * Sets Total number of exposures made during this  Performed
	 * Procedure Step. The number includes non-digital and digital exposures.
	 * 
	 * @param shTotalNumberofExposures short value of total number of exposures made
	 */
	public void setTotalNumberofExposures(short shTotalNumberofExposures)
	{
		/* Accept the Total Number of Exposures of type US.
		 * Read the Transfer Syntax and check if ImplicitDE or ExplicitDE to create
		 * Set the iGroupNumber and iElementNumber to ImplicitDE object 
		 * Set the iVRType
		 * Set length from the element
		 * Add this element to the Data Set
		 */
		addDataElement(0x0040,0x0301, DicomConstants.DCM_DT_US, shTotalNumberofExposures);
	}

	/**
	 * Retrieves  total number of exposures made during this  Performed
	 * Procedure Step. The number includes non-digital and digital exposures.
	 * 
	 * @return short - short value of  total number of exposures made
	 */
	public short getTotalNumberofExposures()
	{
		Object objValue = getValue(0x0040,0x0301);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}


	/**
	 * Sets attribute identifies Distance in mm from the source to detector center.
	 * 
	 * @param strDistanceSourcetoDetector - String identifies Distance in mm from the source to detector center.
	 */	
	public void setDistanceSourcetoDetector(String strDistanceSourcetoDetector)
	{
		/*Accept the Distance Source to Detector in String format of type DS.
		 * Read the Transfer Syntax and check if ImplicitDE or ExplicitDE to create
		 * 
		 * Set the iGroupNumber and iElementNumber to ImplicitDE object 
		 * Set the iVRType
		 * Set length from the element
		 * Add this element to the Data Set
		 */
		addDataElement(0x0018,0x1110, DicomConstants.DCM_DT_DS, strDistanceSourcetoDetector);
	}

	/**
	 * Return Distance in mm from the source to detector center.
	 * 
	 * @return String - String identifies the Distance in mm from the source to detector center.
	 */
	public String getDistanceSourcetoDetector()
	{
		Object objValue = getValue(0x0018,0x1110);
		if(objValue != null)
			return objValue.toString();
		return null;
	}


	/**
	 * Sets attribute identifies Distance in mm from the source to the surface of the
	 * patient closest to the source during this Performed Procedure Step.
	 * 
	 * @param StrDistanceSourcetoEntrance - String identifies the Distance in mm
	 */	
	public void setDistanceSourcetoEntrance(String StrDistanceSourcetoEntrance)
	{
		/*Accept Image identification characteristics in String format of type CS.
		 * Read the Transfer Syntax and check to create ImplicitDE or ExplicitDE .
		 * Set the iGroupNumber and iElementNumber to ImplicitDE object 
		 * Set the iVRType
		 * Set length from the element
		 * Add this element to the Hash Map
		 */
		addDataElement(0x0040,0x0306, DicomConstants.DCM_DT_DS, StrDistanceSourcetoEntrance);
	}

	/**
	 * Return attribute identifies Distance in mm from the source to the surface of the
	 * patient closest to the source during this Performed Procedure Step.
	 * 
	 * @return String - String identifies the Distance in mm
	 */
	public String getDistanceSourcetoEntrance()
	{
		Object objValue = getValue(0x0040,0x0306);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Average entrance dose value measured in dGy at the surface of the patient
	 * during this Performed Procedure Step.
	 * 
	 * @param shEntranceDose short value of Average entrance dose.
	 */
	public void setEntranceDose(short shEntranceDose)
	{
		/* Accept the Entrance Dose of type US.
		 * Read the Transfer Syntax and check if ImplicitDE or ExplicitDE to create
		 * Set the iGroupNumber and iElementNumber to ImplicitDE object 
		 * Set the iVRType
		 * Set length from the element
		 * Add this element to the Data Set
		 */
		addDataElement(0x0040,0x0302, DicomConstants.DCM_DT_US, shEntranceDose);
	}

	/**
	 * Retrieves Average entrance dose value measured in dGy at the surface of the patient
	 * during this Performed Procedure Step.
	 * 
	 * @return short - short value of Average entrance dose.
	 */
	public short getEntranceDose()
	{
		Object objValue = getValue(0x0040,0x0302);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}


	/**
	 * Sets attribute identifies Average entrance dose value measured in mGy at
	 * the surface of the patient during this Performed Procedure Step.
	 * 
	 * @param StrEntranceDoseinmGy - String identifies Average entrance dose value
	 */	
	public void setEntranceDoseinmGy(String StrEntranceDoseinmGy)
	{
		/*Accept Entrance Dose in mGy in String format of type DS.
		 * Read the Transfer Syntax and check to create ImplicitDE or ExplicitDE .
		 * Set the iGroupNumber and iElementNumber to ImplicitDE object 
		 * Set the iVRType
		 * Set length from the element
		 * Add this element to the Hash Map
		 */
		addDataElement(0x0040,0x8302, DicomConstants.DCM_DT_DS, StrEntranceDoseinmGy);
	}

	/**
	 * Return attribute identifies Average entrance dose value measured in mGy at
	 * the surface of the patient during this Performed Procedure Step.
	 * 
	 * @return String - String identifies the Average entrance dose value
	 */
	public String getEntranceDoseinmGy()
	{
		Object objValue = getValue(0x0040,0x8302);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets attribute identifies Typical dimension of the exposed area at the
	 * detector plane. If Rectangular: row dimension followed by column;
	 * 
	 * @param arrShExposedArea - Short array of typical dimension of the exposed area
	 */	
	public void setExposedArea(Short[] arrShExposedArea)
	{
		/*Accept Image identification characteristics in String Array format of type CS.
		 * Read the Transfer Syntax and check to create ImplicitDE or ExplicitDE .
		 * Set the iGroupNumber and iElementNumber to ImplicitDE object 
		 * Set the iVRType
		 * Set length from the element
		 * Add this element to the Hash Map
		 */
		addDataElement(0x0040,0x0303, DicomConstants.DCM_DT_US, arrShExposedArea);
	}

	/**
	 * Return attribute identifies Typical dimension of the exposed area at the
	 * detector plane. If Rectangular: row dimension followed by column;
	 * 
	 * @return Short[]- Short array of typical dimension of the exposed area
	 */
	public Short[] getExposedArea()
	{
		Object objValue = getValue(0x0040,0x0303);
		if(objValue != null)
			return (Short[])objValue;
		return null;
	}


	/**
	 * Sets attribute identifies Total area-dose-product to which the patient was exposed,
	 * accumulated over the complete Performed Procedure Step and measured in dGy*cm*cm,
	 * including fluoroscopy.
	 * 
	 * @param StrImageAreaDoseProduct - String identifies Total area-dose-product
	 */	
	public void setImageAreaDoseProduct(String StrImageAreaDoseProduct)
	{
		/*Accept Image identification characteristics in String format of type CS.
		 * Read the Transfer Syntax and check to create ImplicitDE or ExplicitDE .
		 * Set the iGroupNumber and iElementNumber to ImplicitDE object 
		 * Set the iVRType
		 * Set length from the element
		 * Add this element to the Hash Map
		 */
		addDataElement(0x0018,0x115E, DicomConstants.DCM_DT_DS, StrImageAreaDoseProduct);
	}

	/**
	 * Return identifies Total area-dose-product to which the patient was exposed,
	 * accumulated over the complete Performed Procedure Step and measured in dGy*cm*cm,
	 * including fluoroscopy.
	 * 
	 * @return String - String identifies Total area-dose-product
	 */
	public String getImageAreaDoseProduct()
	{
		Object objValue = getValue(0x0018,0x115E);
		if(objValue != null)
			return objValue.toString();
		return null;
	}


	/**
	 * Sets attribute identifies User-defined comments on any special conditions
	 * related to radiation dose encountered during this Performed Procedure Step.
	 * 
	 * @param StrCommentsonRadiationDose - String identifies User-defined comments
	 */	
	public void setCommentsonRadiationDose(String StrCommentsonRadiationDose)
	{
		/*Accept Image identification characteristics in String format of type CS.
		 * Read the Transfer Syntax and check to create ImplicitDE or ExplicitDE .
		 * Set the iGroupNumber and iElementNumber to ImplicitDE object 
		 * Set the iVRType
		 * Set length from the element
		 * Add this element to the Hash Map
		 */
		addDataElement(0x0040,0x0310, DicomConstants.DCM_DT_ST, StrCommentsonRadiationDose);
	}

	/**
	 * Return attribute identifies User-defined comments on any special conditions
	 * related to radiation dose encountered during this Performed Procedure Step.
	 * 
	 * @return String - String identifies User-defined comments
	 */
	public String getCommentsonRadiationDose()
	{
		Object objValue = getValue(0x0040,0x0310);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Retrieves pre-initialized object of ExposureDoseSequence.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
	 * 
	 * @return ExposureDoseSequence - instance of Exposure Dose Sequence
	 */
	public ExposureDoseSequence getExposureDoseSequence()
	{
		ExposureDoseSequence objExposureDoseSequence = (ExposureDoseSequence)getDicomValue(0x0040,0x030E);
		if(objExposureDoseSequence == 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();
			objExposureDoseSequence = (ExposureDoseSequence)objDicomV3Factory.createSequence(0x0040,0x030E, objITransferSyntax, objIDictionaryReader);
			
			// This will add Data Element of SQ to the DataSet.
			addDataElement(0x0040,0x030E, DicomConstants.DCM_DT_SQ, objExposureDoseSequence.getValue());
		}
		return objExposureDoseSequence;
	}
}
