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



import cdac.medinfo.sdk.dcm30_04.commoninterface.IDataElement;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDicomValue;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDictionaryReader;
import cdac.medinfo.sdk.dcm30_04.commoninterface.ITransferSyntax;
import cdac.medinfo.sdk.dcm30_04.exceptions.InvalidItemException;
import cdac.medinfo.sdk.dcm30_04.exceptions.MandatoryMissingException;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConfig;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConstants;
import cdac.medinfo.sdk.dcm30_04.utils.DicomLogger;




/**
* This item class contains set,get and validate methods for attributes of  Radiopharmaceutical Information Sequence.
 */
public class RadiopharmaceuticalInformationSequenceItem extends Item  
{
	

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * This constructor creates the item and internal code sequence macro with default TransferSyntax 
	 */
	RadiopharmaceuticalInformationSequenceItem()
	{
		super();
	}

	/**
	 * This constructor creates the item and internal code sequence macro with default TransferSyntax 
	 * and default DictionaryReader.
	 * @param objTransferSyntax specifies the DICOM Standard defined TransfeSyntax 
	 */
	RadiopharmaceuticalInformationSequenceItem(ITransferSyntax objITransferSyntax)
	{
		super(objITransferSyntax);
	}

	/**
	 * This constructor creates RadiopharmaceuticalInformationSequenceItem with specified TransferSyntax and
	 * specified DictionaryReader
	 * @param objTransferSyntax specifies the DICOM Standard defined TransfeSyntax
	 * @param objDictionaryReader specifies the DICOM Dictionary reader to be used 
	 */
	RadiopharmaceuticalInformationSequenceItem(ITransferSyntax objTransferSyntax, IDictionaryReader objDictionaryReader)
	{
		super(objTransferSyntax,objDictionaryReader);
	}

	/**
	 * Validates all the attributes in RadiopharmaceuticalInformationSequenceItem that are present in the DataSet
	 * @return boolean - Success -true, Failure - false
	 * @throws InvalidItemException throws InvalidItemException If Mandatory or Conditional element fails.
	 */
	public boolean validateForModule()  throws InvalidItemException
	{
		try{
			if(!contains(0x0054,0x0300))
				throw new MandatoryMissingException("0054,0300");
		}
		catch(MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new InvalidItemException(this.getClass().getSimpleName(),me);
		}

		return true;
	}

	/**
	 * Retrieves pre-initialized object of Radionuclide Code Sequence.If the module contains the sequence object
	 * it returns the same otherwise it creates the sequence adds to the module with proper initialization
	 * and then returns the object of sequence
	 * @return RadionuclideCodeSequence - instance of Radionuclide Code Sequence. 
	 */
	public RadionuclideCodeSequence getRadionuclideCodeSequence()
	{
		RadionuclideCodeSequence objRadionuclideCodeSequence = null;
		IDataElement objIDataElement =  getDataForTag(0x0054,0x0300);
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objRadionuclideCodeSequence = (RadionuclideCodeSequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objRadionuclideCodeSequence = (RadionuclideCodeSequence)objDicomV3Factory.createSequence(0x0054,0x0300, objITransferSyntax, objIDictionaryReader);

			if(iVRTransferSyntax == DicomConstants.DCM_VR_IMPLICIT)
				objIDataElement = new ImplicitDE(0x0054,0x0300, objRadionuclideCodeSequence.getValue(),objIDictionaryReader);
			else
				objIDataElement = new ExplicitDE(0x0054,0x0300, DicomConstants.DCM_DT_SQ, objRadionuclideCodeSequence.getValue());
			objIDataElement.setTransferSyntax(objITransferSyntax);
			if(objIDataElement != null)
				addDataElement(objIDataElement);
		}
		return objRadionuclideCodeSequence;
	}

	/**
	 * Sets Route of administration..
	 * @param strRadiopharmaceuticalRoute  String value of  Radiopharmaceutical Route.
	 */
	public void setRadiopharmaceuticalRoute(String strRadiopharmaceuticalRoute)
	{
		/*Accept the RadiopharmaceuticalRoute in String format of type LO.
		 * 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
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0018, 0x1070, strRadiopharmaceuticalRoute,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0018, 0x1070,DicomConstants.DCM_DT_LO, strRadiopharmaceuticalRoute);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Route of administration.
	 * @return String - value of  Radiopharmaceutical Route.
	 */
	public String getRadiopharmaceuticalRoute()
	{
		Object objValue = getValue(0x0018, 0x1070);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Retrieves Sequence that identifies the administration route for the radiopharmaceutical.
	 * @return - instance of AdministrationRouteCodeSequence.
	 */
	public AdministrationRouteCodeSequence getAdministrationRouteCodeSequence()
	{
		AdministrationRouteCodeSequence objAdministrationRouteCodeSequence = null;
		IDataElement objIDataElement =  getDataForTag(0x0054,0x0302);
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objAdministrationRouteCodeSequence = (AdministrationRouteCodeSequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objAdministrationRouteCodeSequence = (AdministrationRouteCodeSequence)objDicomV3Factory.createSequence(0x0054,0x0302, objITransferSyntax, objIDictionaryReader);

			if(iVRTransferSyntax == DicomConstants.DCM_VR_IMPLICIT)
				objIDataElement = new ImplicitDE(0x0054,0x0302, objAdministrationRouteCodeSequence.getValue(),objIDictionaryReader);
			else
				objIDataElement = new ExplicitDE(0x0054,0x0302, DicomConstants.DCM_DT_SQ, objAdministrationRouteCodeSequence.getValue());
			objIDataElement.setTransferSyntax(objITransferSyntax);
			if(objIDataElement != null)
				addDataElement(objIDataElement);
		}
		return objAdministrationRouteCodeSequence;
	}

	/**
	 * Sets Volume of administered radiopharmaceutical in cubic cm.
	 * @param strRadiopharmaceuticalVolume String value of Radiopharmaceutical Volume.
	 */
	public void setRadiopharmaceuticalVolume(String strRadiopharmaceuticalVolume)
	{
		/* Accept the Radiopharmaceutical Volume 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
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)	
			objIDataElement = new ImplicitDE(0x0018, 0x1071, strRadiopharmaceuticalVolume,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0018, 0x1071,DicomConstants.DCM_DT_DS, strRadiopharmaceuticalVolume);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves String value of Radiopharmaceutical Volume.
	 * @return String - value of Radiopharmaceutical Volume.
	 */
	public String getRadiopharmaceuticalVolume()
	{
		Object objValue = getValue(0x0018, 0x1071);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Time of start of administration..
	 * @param strRadiopharmaceuticalStartTime String value of Radiopharmaceutical Start Time.
	 */
	public void setRadiopharmaceuticalStartTime(String strRadiopharmaceuticalStartTime)
	{
		/*Accept the Radiopharmaceutical Start Time in String format of type TM.
		 * 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
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0018, 0x1072, strRadiopharmaceuticalStartTime,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0018, 0x1072,DicomConstants.DCM_DT_TM, strRadiopharmaceuticalStartTime);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Time of start of administration.
	 * @return String - value of Radiopharmaceutical Start Time.
	 */
	public String getRadiopharmaceuticalStartTime()
	{
		Object objValue = getValue(0x0018, 0x1072);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Time of end of administration.
	 * @param strRadiopharmaceuticalStopTime  String value of  Radiopharmaceutical Stop Time.
	 */
	public void setRadiopharmaceuticalStopTime(String strRadiopharmaceuticalStopTime)
	{
		/* Accept the Radiopharmaceutical Stop Time in String format of type TM.
		 * 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
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0018, 0x1073, strRadiopharmaceuticalStopTime,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0018, 0x1073,DicomConstants.DCM_DT_TM, strRadiopharmaceuticalStopTime);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Time of end of administration.
	 * @return - String - value of  Radiopharmaceutical Stop Time.
	 */
	public String getRadiopharmaceuticalStopTime()
	{
		Object objValue = getValue(0x0018, 0x1073);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Total amount of radionuclide injected.
	 * @param strRadionuclideTotalDose String value of Radionuclide Total Dose
	 */
	public void setRadionuclideTotalDose(String strRadionuclideTotalDose)
	{
		/* Accept the Radionuclide Total Dose 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
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0018, 0x1074, strRadionuclideTotalDose,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0018, 0x1074,DicomConstants.DCM_DT_DS, strRadionuclideTotalDose);
		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Total amount of radionuclide injected.
	 * @return String - value of Radionuclide Total Dose
	 */
	public String getRadionuclideTotalDose()
	{
		Object objValue = getValue(0x0018, 0x1074);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Retrieves sequence that contains calibration data..
	 * @return - instance of CalibrationDataSequence.
	 */
	public CalibrationDataSequence getCalibrationDataSequence()
	{
		CalibrationDataSequence objCalibrationDataSequence = null;
		IDataElement objIDataElement =  getDataForTag(0x0054,0x0306);
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objCalibrationDataSequence = (CalibrationDataSequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objCalibrationDataSequence = (CalibrationDataSequence)objDicomV3Factory.createSequence(0x0054,0x0306, objITransferSyntax, objIDictionaryReader);

			if(iVRTransferSyntax == DicomConstants.DCM_VR_IMPLICIT)
				objIDataElement = new ImplicitDE(0x0054,0x0306, objCalibrationDataSequence.getValue(),objIDictionaryReader);
			else
				objIDataElement = new ExplicitDE(0x0054,0x0306, DicomConstants.DCM_DT_SQ, objCalibrationDataSequence.getValue());
			objIDataElement.setTransferSyntax(objITransferSyntax);
			if(objIDataElement != null)
				addDataElement(objIDataElement);
		}
		return objCalibrationDataSequence;
	}

	/**
	 * Sets Name of the radiopharmaceutical.
	 * @param strRadiopharmaceutical String value of Radiopharmaceutical.
	 */
	public void setRadiopharmaceutical(String strRadiopharmaceutical)
	{
		/* Accept the Radiopharmaceutical 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
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0018, 0x0031, strRadiopharmaceutical,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0018, 0x0031,DicomConstants.DCM_DT_DS, strRadiopharmaceutical);
		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Name of the radiopharmaceutical.
	 * @return strRadiopharmaceutical - String value of Radiopharmaceutical.
	 */
	public String getRadiopharmaceutical()
	{
		Object objValue = getValue(0x0018, 0x0031);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets a UID that can be used to uniquely reference this signature.
	 * @param strRadiopharmaceuticalAdministrationEventUID String value of Digital Signature UID.
	 */
	public void setRadiopharmaceuticalAdministrationEventUID(String strRadiopharmaceuticalAdministrationEventUID)
	{
		/*Accept the Digital Signature UID in String format of type UI.
		 * 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
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0008, 0x3012, strRadiopharmaceuticalAdministrationEventUID,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0008, 0x3012,DicomConstants.DCM_DT_UI, strRadiopharmaceuticalAdministrationEventUID);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves a UID that can be used to uniquely reference this signature.
	 * @return String - value of Digital Signature UID.
	 */
	public String getRadiopharmaceuticalAdministrationEventUID()
	{
		Object objValue = getValue(0x0008, 0x3012);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	/**
	 * Sets The radionuclide half life, in seconds, that was used in the correction of this image.
	 * @param strRadionuclideHalfLife String value of Radionuclide Half Life.
	 */
	public void setRadionuclideHalfLife(String strRadionuclideHalfLife)
	{
		/* Accept the RadionuclideHalfLife 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
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)	
			objIDataElement = new ImplicitDE(0x0018, 0x1075, strRadionuclideHalfLife,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0018, 0x1075,DicomConstants.DCM_DT_DS, strRadionuclideHalfLife);
		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves The radionuclide half life, in seconds, that was used in the correction of this image.
	 * @return strRadiopharmaceutical - String value of Radionuclide Half Life.
	 */
	public String getRadionuclideHalfLife()
	{
		Object objValue = getValue(0x0018, 0x1075);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets The radionuclide positron fraction that was used in the correction of this image.
	 * @param strRadionuclidePositronFraction String value of Radionuclide Positron Fraction.
	 */
	public void setRadionuclidePositronFraction(String strRadionuclidePositronFraction)
	{
		/* Accept the Radionuclide Positron Fraction 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
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0018, 0x1076, strRadionuclidePositronFraction,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0018, 0x1076,DicomConstants.DCM_DT_DS, strRadionuclidePositronFraction);
		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves The radionuclide positron fraction that was used in the correction of this image.
	 * @return strRadiopharmaceutical - String value of Radionuclide Positron Fraction.
	 */
	public String getRadionuclidePositronFraction()
	{
		Object objValue = getValue(0x0018, 0x1076);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets The activity per unit mass of the radiopharmaceutical, in Bq/micromole, at the Radiopharmaceutical Start Time 
	 * @param strRadiopharmaceuticalSpecificActivity String value of Radiopharmaceutical Specific Activity.
	 */
	public void setRadiopharmaceuticalSpecificActivity(String strRadiopharmaceuticalSpecificActivity)
	{
		/* Accept the Radiopharmaceutical Specific Activity 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
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0018, 0x1077, strRadiopharmaceuticalSpecificActivity,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0018, 0x1077,DicomConstants.DCM_DT_DS, strRadiopharmaceuticalSpecificActivity);
		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Radiopharmaceutical Specific Activity.
	 * @return strRadiopharmaceutical - String value of Radiopharmaceutical Specific Activity.
	 */
	public String getRadiopharmaceuticalSpecificActivity()
	{
		Object objValue = getValue(0x0018, 0x1077);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Retrieves pre-initialized object of Radiopharmaceutical Code Sequence. If the module contains the sequence object
	 * it returns the same otherwise it creates the sequence adds to the module with proper initialization
	 * and then returns the object of sequence
	 * @return BeamLimitingDeviceSequence - instance of Radiopharmaceutical Code Sequence. 
	 */
	public RadiopharmaceuticalCodeSequence getRadiopharmaceuticalCodeSequence()
	{
		RadiopharmaceuticalCodeSequence objRadiopharmaceuticalCodeSequence = null;
		IDataElement objIDataElement =  getDataForTag(0x0054,0x0304);
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objRadiopharmaceuticalCodeSequence = (RadiopharmaceuticalCodeSequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objRadiopharmaceuticalCodeSequence = (RadiopharmaceuticalCodeSequence)objDicomV3Factory.createSequence(0x0054,0x0304, objITransferSyntax, objIDictionaryReader);

			if(iVRTransferSyntax == DicomConstants.DCM_VR_IMPLICIT)
				objIDataElement = new ImplicitDE(0x0054,0x0304, objRadiopharmaceuticalCodeSequence.getValue(),objIDictionaryReader);
			else
				objIDataElement = new ExplicitDE(0x0054,0x0304, DicomConstants.DCM_DT_SQ, objRadiopharmaceuticalCodeSequence.getValue());
			objIDataElement.setTransferSyntax(objITransferSyntax);
			if(objIDataElement != null)
				addDataElement(objIDataElement);
		}
		return objRadiopharmaceuticalCodeSequence;
	}
}

