/*******************************************************************************
 *  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.IDataSet;
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.enums.EnumValueType;
import cdac.medinfo.sdk.dcm30_04.exceptions.ConditionFailedException;
import cdac.medinfo.sdk.dcm30_04.exceptions.InvalidMacroException;
import cdac.medinfo.sdk.dcm30_04.exceptions.ItemLimitException;
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;





/**
 * A content item is a flexible means of encoding attribute identifiers and attribute values using the Code
 * Sequence Macro for coded terminology defined by a coding scheme.
 */
public class ContentItemMacro extends Macro   
{

	private static final long serialVersionUID = 1L;

	/**
	 * This constructor constructs a Macro with specified DataSet.
	 * @param objDataSet Collection of DataElements 
	 */
	public ContentItemMacro(IDataSet objDataSet)
	{
		super(objDataSet);
	}

	/**
	 * This constructor constructs a Macro with specified DataSet and DictionaryReader
	 * @param objDataSet specifies the DataSet
	 * @param objDictionaryReader specifies the DICOM Dictionary reader to be used 
	 */
	public ContentItemMacro(IDataSet objDataSet,IDictionaryReader objDictionaryReader)
	{
		super(objDataSet,objDictionaryReader);
	}

	/**
	 * This constructor constructs a Macro with the specified Item
	 * @param objItem specifies instance of Item class
	 */
	public ContentItemMacro(IItem objItem)
	{
		super(objItem);
	}

	/**
	 * This constructor constructs a Macro with the specified Item and DictionaryReader
	 * @param objItem specifies instance of Item class
	 * @param objDictionaryReader specifies the DICOM Dictionary reader to be used 
	 */
	public ContentItemMacro(IItem objItem,IDictionaryReader objDictionaryReader)
	{
		super(objItem, objDictionaryReader);
	}

	/**
	 * Validates all the attributes in CodeSequenceMacro that are present in the DataSet
	 * @return int - Success -true, Failure - false
	 * @throws InvalidMacroException throws InvalidMacroException If Mandatory or Conditional element fails.
	 */
	public boolean validate() throws InvalidMacroException
	{
		return true;
	}

	
	/**
	 * Validates all the attributes in Content Macro that are present in the DataSet
	 * @param objIDataSet collection of DataElements
	 * @return boolean - Success -true, Failure - false
	 * @throws InvalidMacroException throws InvalidMacroException If Mandatory or Conditional element fails.
	 */
	public boolean validate(IDataSet objIDataSet) throws InvalidMacroException
	{
		try
		{
			/*
			 * Validation Rules for Mandatory Elements
			 */
			if(!objIDataSet.isPresent(0x0040,0xA040))
				throw new MandatoryMissingException("0040,A040)");
			String strValueType = null;
			if(objIDataSet.getDataForTag(0x0040, 0xA040).getValue()!=null)
			{
				strValueType = objIDataSet.getDataForTag(0x0040, 0xA040).getValue().toString();

				if(EnumValueType.TEXT.getValue().equalsIgnoreCase(strValueType) && !objIDataSet.isPresent(0x0040,0xA160))
					throw new ConditionFailedException("0040,A160 Required if Value Type (0040,A040) is " + EnumValueType.TEXT.getValue());
				if(EnumValueType.DATETIME.getValue().equalsIgnoreCase(strValueType) && !objIDataSet.isPresent(0x0040,0xA120))
					throw new ConditionFailedException("0040,A120 Required if Value Type (0040,A040) is " + EnumValueType.DATETIME.getValue());
				if(EnumValueType.DATE.getValue().equalsIgnoreCase(strValueType) && !objIDataSet.isPresent(0x0040,0xA121))
					throw new ConditionFailedException("0040,A121 Required if Value Type (0040,A040) is " + EnumValueType.DATE.getValue());
				if(EnumValueType.TIME.getValue().equalsIgnoreCase(strValueType) && !objIDataSet.isPresent(0x0040,0xA122))
					throw new ConditionFailedException("0040,A122 Required if Value Type (0040,A040) is " + EnumValueType.TIME.getValue());
				if(EnumValueType.PNAME.getValue().equalsIgnoreCase(strValueType) && !objIDataSet.isPresent(0x0040,0xA123))
					throw new ConditionFailedException("0040,A123 Required if Value Type (0040,A040) is " + EnumValueType.PNAME.getValue());
				if(EnumValueType.UIDREF.getValue().equalsIgnoreCase(strValueType) && !objIDataSet.isPresent(0x0040,0xA124))
					throw new ConditionFailedException("0040,A124 Required if Value Type (0040,A040) is " + EnumValueType.UIDREF.getValue());
				if(EnumValueType.NUMERIC.getValue().equalsIgnoreCase(strValueType) && !objIDataSet.isPresent(0x0040,0xA30A))
					throw new ConditionFailedException("0040,A30A Required if Value Type (0040,A040) is " + EnumValueType.NUMERIC.getValue());
				if(EnumValueType.NUMERIC.getValue().equalsIgnoreCase(strValueType) && !objIDataSet.isPresent(0x0040,0x08EA))
					throw new ConditionFailedException("0040,08EA Required if Value Type (0040,A040) is " + EnumValueType.NUMERIC.getValue());
				if(objIDataSet.isPresent(0x0040,0xA162))
					if(!objIDataSet.isPresent(0x0040,0xA163))
						throw new ConditionFailedException("0x0040,0xA163");
				if(objIDataSet.isPresent(0x0040,0xA043))
				{
					int iNoOfItems = getConceptNameCodeSequence().noOfItems();
					if(iNoOfItems > 1)
						throw new ItemLimitException("Sequence 0040,A043 shall contain single item for this macro");
					getConceptNameCodeSequence().validate();
				}

			}
		}
		catch( MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new InvalidMacroException(this.getClass().getSimpleName(), me);
		}
		catch( ConditionFailedException cfe)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(cfe.getMessage());
			}
			throw new ConditionFailedException(this.getClass().getSimpleName(), cfe);
		}
		return true;
	}

	/**
	 * Validates all the attributes in Content Item Macro that are present in the DataSet
	 * @param objIItem collection of DataElements in a sequence
	 * @return boolean Success -true, Failure - false
	 * @throws InvalidMacroException throws InvalidMacroException If Mandatory or Conditional element fails.
	 */
	public static boolean validate(IItem objIItem) throws InvalidMacroException
	{
		try
		{
			if(!objIItem.contains(0x0040,0xA040))
				throw new MandatoryMissingException("0040,A040)");
			String strValueType = null;
			if(objIItem.getDataForTag(0x0040, 0xA050).getValue()!=null)
			{
				strValueType = objIItem.getDataForTag(0x0040, 0xA050).getValue().toString();

				if(EnumValueType.TEXT.getValue().equalsIgnoreCase(strValueType) && !objIItem.contains(0x0040,0xA160))
					throw new ConditionFailedException("0040,A160 Required if Value Type (0040,A160) is " + EnumValueType.TEXT.getValue());
				if(EnumValueType.DATETIME.getValue().equalsIgnoreCase(strValueType) && !objIItem.contains(0x0040,0xA120))
					throw new ConditionFailedException("0040,A120 Required if Value Type (0040,A160) is " + EnumValueType.DATETIME.getValue());
				if(EnumValueType.DATE.getValue().equalsIgnoreCase(strValueType) && !objIItem.contains(0x0040,0xA121))
					throw new ConditionFailedException("0040,A121 Required if Value Type (0040,A160) is " + EnumValueType.DATE.getValue());
				if(EnumValueType.TIME.getValue().equalsIgnoreCase(strValueType) && !objIItem.contains(0x0040,0xA122))
					throw new ConditionFailedException("0040,A122 Required if Value Type (0040,A160) is " + EnumValueType.TIME.getValue());
				if(EnumValueType.PNAME.getValue().equalsIgnoreCase(strValueType) && !objIItem.contains(0x0040,0xA123))
					throw new ConditionFailedException("0040,A123 Required if Value Type (0040,A160) is " + EnumValueType.PNAME.getValue());
				if(EnumValueType.UIDREF.getValue().equalsIgnoreCase(strValueType) && !objIItem.contains(0x0040,0xA124))
					throw new ConditionFailedException("0040,A124 Required if Value Type (0040,A160) is " + EnumValueType.UIDREF.getValue());
				if(EnumValueType.NUMERIC.getValue().equalsIgnoreCase(strValueType) && !objIItem.contains(0x0040,0xA30A))
					throw new ConditionFailedException("0040,A30A Required if Value Type (0040,A040) is " + EnumValueType.NUMERIC.getValue());
				if(EnumValueType.NUMERIC.getValue().equalsIgnoreCase(strValueType) && !objIItem.contains(0x0040,0x08EA))
					throw new ConditionFailedException("0040,08EA Required if Value Type (0040,A040) is " + EnumValueType.NUMERIC.getValue());
				if(objIItem.contains(0x0040,0xA162))
					if(!objIItem.contains(0x0040,0xA163))
						throw new ConditionFailedException("0x0040,0xA163");

			}
		}
		catch( MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new InvalidMacroException("Document Content Macro",me);
		}
		catch( ConditionFailedException cfe)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(cfe.getMessage());
			}
			throw new InvalidMacroException("Document Content Macro",cfe);
		}
		return true;
	}
	
	/**
	 * Sets type of Value.
	 * The type of the value encoded in the Content Item.
	 * Defined Terms:
	 * 1) DATE.
	 * 2) TIME.
	 * 3) DATETIME.
	 * 4) PNAME.
	 * 5) UIDREF.
	 * 6) TEXT.
	 * 7) CODE.
	 * 8) NUMERIC.
	 * 9) COMPOSITE.
	 * 10) IMAGE.
	 * @param enumValueType enum value of ValueType.
	 */
	public void setValueType(EnumValueType enumValueType)
	{
		/* Accept the Value Type in String format of type CS.
		 * 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(0x0040,0xA040, enumValueType.getValue(),objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0040,0xA040,DicomConstants.DCM_DT_CS, enumValueType.getValue());

		addDataElement(objIDataElement);


	}

	/**
	 * Return Value Type. 
	 * The type of the value encoded in the Content Item.
	 * @return String - value of ValueType.
	 */
	public String getValueType()
	{
		Object objValue = getValue(0x0040,0xA040);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Retrieves pre-initialized object of Concept Name 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 ConceptNameCodeSequence - instance of ConceptNameCodeSequence. 
	 */
	public ConceptNameCodeSequence getConceptNameCodeSequence()
	{
		ConceptNameCodeSequence objConceptNameCodeSequence = null;
		IDataElement objIDataElement =  getDataElement(0x0040, 0xA043);
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objConceptNameCodeSequence = (ConceptNameCodeSequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objConceptNameCodeSequence = (ConceptNameCodeSequence)objDicomV3Factory.createSequence(0x0040, 0xA043, objITransferSyntax, objIDictionaryReader);

			if(iVRTransferSyntax == DicomConstants.DCM_VR_IMPLICIT)
				objIDataElement = new ImplicitDE(0x0040, 0xA043, objConceptNameCodeSequence.getValue(),objIDictionaryReader);
			else
				objIDataElement = new ExplicitDE(0x0040, 0xA043, DicomConstants.DCM_DT_SQ, objConceptNameCodeSequence.getValue());
			objIDataElement.setTransferSyntax(objITransferSyntax);
			if(objIDataElement != null)
				addDataElement(objIDataElement);
		}
		return objConceptNameCodeSequence;
	}
	
	/**
	 * Sets Date and Time.
	 * DateTime value for the Content Item.
	 * @param strDateTime String value of DateTime
	 */
	public void setDateTime(String strDateTime)
	{
		/* Accept the DateTime of type DT.
		 * 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(0x0040,0xA120,strDateTime,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0040,0xA120,DicomConstants.DCM_DT_DT, strDateTime);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves DateTime.
	 * DateTime value for the Content Item.
	 * @return String - value of DateTime
	 */
	public String getDateTime()
	{
		Object objValue = getValue(0x0040,0xA120);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Sets Date.
	 * Date value for the Content Item.
	 * @param strDate String value of Date.
	 */
	public void setDate(String strDate)
	{
		/* Accept the Date of type DA.
		 * 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(0x0040,0xA121,strDate,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0040,0xA121,DicomConstants.DCM_DT_DA, strDate);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Date.
	 * Date value for the Content Item.
	 * @return String - value of Date.
	 */
	public String getDate()
	{
		Object objValue = getValue(0x0040,0xA121);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Sets Time.
	 * Time value for the Content Item.
	 * @param strTime String array of Time.
	 */
	public void setTime(String strTime)
	{
		/* Accept Time 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(0x0040,0xA122,strTime,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0040,0xA122,DicomConstants.DCM_DT_TM,strTime);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Time.
	 * Time value for the Content Item.
	 * @return String - value of Time.
	 */
	public String getTime()
	{
		Object objValue = getValue(0x0040,0xA122);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Person Name.
	 * Person name value for the Content Item.
	 * @param strPersonName String Person Name.
	 */	
	public void setPersonName(String strPersonName)
	{
		/*Accept Person Name in String format of type PN.
		 * 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
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0040,0xA123,strPersonName,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0040,0xA123, DicomConstants.DCM_DT_PN,strPersonName);
		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Person Name.
	 * Person name value for the Content Item.
	 * @return String - Name of the Person.
	 */
	public String getPersonName()
	{
		Object objValue = getValue(0x0040,0xA123);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Sets a UID.
	 * UID value for the Content Item.
	 * @param strUID String value of UID.
	 */
	public void setUID(String strUID)
	{
		/*Accept the 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(0x0400, 0xA124, strUID,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0400, 0xA124,DicomConstants.DCM_DT_UI, strUID);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves a UID.
	 * UID value for the Content Item.
	 * @return String - value of UID.
	 */
	public String getUID()
	{
		Object objValue = getValue(0x0400, 0xA124);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Sets the value of the Content Item.
	 * @param strTextValue String value of Content Item.
	 */
	public void setTextValue(String strTextValue)
	{
		/* Accept the Text Value in String format of type UT.
		 * 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(0x0040, 0xA160, strTextValue,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0040, 0xA160,DicomConstants.DCM_DT_UT, strTextValue);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves the value of the Content Item.
	 * @return String - value of the value of the Content Item.
	 */
	public String getTextValue()
	{
		Object objValue = getValue(0x0040, 0xA160);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Retrieves pre-initialized object of Concept 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 PrimaryFluenceModeSequence - instance of PrimaryFluenceModeSequence. 
	 */
	public ConceptCodeSequence getConceptCodeSequence()
	{
		ConceptCodeSequence objConceptCodeSequence = null;
		IDataElement objIDataElement =  getDataElement(0x0040, 0xA168);
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objConceptCodeSequence = (ConceptCodeSequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objConceptCodeSequence = (ConceptCodeSequence)objDicomV3Factory.createSequence(0x0040, 0xA168, objITransferSyntax, objIDictionaryReader);

			if(iVRTransferSyntax == DicomConstants.DCM_VR_IMPLICIT)
				objIDataElement = new ImplicitDE(0x0040, 0xA168, objConceptCodeSequence.getValue(),objIDictionaryReader);
			else
				objIDataElement = new ExplicitDE(0x0040, 0xA168, DicomConstants.DCM_DT_SQ, objConceptCodeSequence.getValue());
			objIDataElement.setTransferSyntax(objITransferSyntax);
			if(objIDataElement != null)
				addDataElement(objIDataElement);
		}
		return objConceptCodeSequence;
	}
	
	/**
	 * Sets Numeric Value.
	 * Numeric value for the Content Item.
	 * @param strSliceThickness String value of Numeric Value.
	 */
	public void setNumericValue(String[] strSliceThickness)
	{
		/*Accept the NumericValue 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(0x0040,0xA30A, strSliceThickness,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0040,0xA30A,DicomConstants.DCM_DT_DS, strSliceThickness);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Numeric Value.
	 * Numeric value for the Content Item.
	 * @return String - value of Numeric Value.
	 */
	public String[] getNumericValue()
	{
		Object objValue = getValue(0x0040,0xA30A);
		if(objValue != null)
			return (String[])objValue;
		return null;
	}

	/**
	 * Sets the Floating Point Value.
	 * @param dblArrFloatingPointValue Double value of FloatingPointValue
	 */
	public void setFloatingPointValue(double[] dblArrFloatingPointValue)
	{
		/*Accept the Floating Point Value in double format of type FD.
		 * 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(0x0040,0xA161, dblArrFloatingPointValue,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0040,0xA161,DicomConstants.DCM_DT_FD, dblArrFloatingPointValue);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves the Floating Point Value.
	 * @return double - double value of Floating Point Value
	 */
	public double[] getFloatingPointValue()
	{
		Object objValue = getValue(0x0040,0xA161);
		if(objValue != null)
			return (double[])objValue;
		return null;
	}
	
	/**
	 * Sets Rational Numerator Value.
	 * @param lArrRationalNumeratorValue Long array of Rational Numerator Value.
	 */
	public void setRationalNumeratorValue(Long[] lArrRationalNumeratorValue)
	{
		/* 
		 * Accept the Rational Numerator Value in String format of type SL.
		 * 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(0x0040,0xA162, lArrRationalNumeratorValue,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0040,0xA162,DicomConstants.DCM_DT_SL, lArrRationalNumeratorValue);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves the Rational Numerator Value.
	 * @return Long[] - Long array of Rational Numerator Value.
	 */
	public Long[] getRationalNumeratorValue()
	{
		Object objValue = getValue(0x0040,0xA162);
		if(objValue != null)
			return (Long[])objValue;
		return null;
	}
	
	/**
	 * Sets Rational Denominator Value.
	 * @param lArrRationalDenominatorValue Long value of Referenced Content Item Identifier
	 */
	public void setRationalDenominatorValue(long[] lArrRationalDenominatorValue)
	{
		/* Accept the Referenced Content Item Identifier in long format of type UL
		 * 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(0x0040,0xA163, lArrRationalDenominatorValue,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0040,0xA163,DicomConstants.DCM_DT_UL, lArrRationalDenominatorValue);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Referenced Content Item Identifier.
	 * @return long[] - long value of Referenced Content Item Identifier.
	 */
	public long[] getRationalDenominatorValue()
	{
		Object objValue = getValue(0x0040,0xA163);
		if(objValue != null)
			return (long[])objValue;
		return null;
	}
	
	/**
	 * Retrieves pre-initialized object of Measurement Units 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 MeasurementUnitsCodeSequence - instance of MeasurementUnitsCodeSequence. 
	 */
	public MeasurementUnitsCodeSequence getMeasurementUnitsCodeSequence()
	{
		MeasurementUnitsCodeSequence objMeasurementUnitsCodeSequence = null;
		IDataElement objIDataElement =  getDataElement(0x0040, 0x08EA);
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objMeasurementUnitsCodeSequence = (MeasurementUnitsCodeSequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objMeasurementUnitsCodeSequence = (MeasurementUnitsCodeSequence)objDicomV3Factory.createSequence(0x0040, 0x08EA, objITransferSyntax, objIDictionaryReader);

			if(iVRTransferSyntax == DicomConstants.DCM_VR_IMPLICIT)
				objIDataElement = new ImplicitDE(0x0040, 0x08EA, objMeasurementUnitsCodeSequence.getValue(),objIDictionaryReader);
			else
				objIDataElement = new ExplicitDE(0x0040, 0x08EA, DicomConstants.DCM_DT_SQ, objMeasurementUnitsCodeSequence.getValue());
			objIDataElement.setTransferSyntax(objITransferSyntax);
			if(objIDataElement != null)
				addDataElement(objIDataElement);
		}
		return objMeasurementUnitsCodeSequence;
	}
	
	/**
	 * Retrieves pre-initialized object of Referenced SOP 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 ReferencedSOPSequence - instance of ReferencedSOPSequence. 
	 */
	public ReferencedSOPSequence getReferencedSOPSequence()
	{
		ReferencedSOPSequence objReferencedSOPSequence = null;
		IDataElement objIDataElement =  getDataElement(0x0040, 0x1199);
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objReferencedSOPSequence = (ReferencedSOPSequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objReferencedSOPSequence = (ReferencedSOPSequence)objDicomV3Factory.createSequence(0x0040, 0x1199, objITransferSyntax, objIDictionaryReader);

			if(iVRTransferSyntax == DicomConstants.DCM_VR_IMPLICIT)
				objIDataElement = new ImplicitDE(0x0040, 0x1199, objReferencedSOPSequence.getValue(),objIDictionaryReader);
			else
				objIDataElement = new ExplicitDE(0x0040, 0x1199, DicomConstants.DCM_DT_SQ, objReferencedSOPSequence.getValue());
			objIDataElement.setTransferSyntax(objITransferSyntax);
			if(objIDataElement != null)
				addDataElement(objIDataElement);
		}
		return objReferencedSOPSequence;
	}
	
	/**
	 * Sets Referenced Frame Number.
	 * @param arrShReferencedFrameNumber
	 */
	public void setReferencedFrameNumber(short[] arrShReferencedFrameNumber)
	{
		/*Accept the Referenced Frame Number in Array of short format 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
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)	
			objIDataElement = new ImplicitDE(0x0040, 0x1160, arrShReferencedFrameNumber,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0040, 0x1160,DicomConstants.DCM_DT_US, arrShReferencedFrameNumber);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Referenced Frame Number.
	 * @return short Array - Referenced Frame Number.
	 */
	public short[] getReferencedFrameNumber()
	{
		Object objValue = getValue(0x0040, 0x1160);
		if(objValue != null)
			return (short[])objValue;
		return null;
	}
	
	/**
	 * Sets Referenced Segment Number.
	 * @param arrShReferencedSegmentNumber - Referenced Segment Number
	 */
	public void setReferencedSegmentNumber(short[] arrShReferencedSegmentNumber)
	{
		/*Accept the Referenced Segment Number in Array of short format 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
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)	
			objIDataElement = new ImplicitDE(0x0040, 0x000B, arrShReferencedSegmentNumber,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0040, 0x000B,DicomConstants.DCM_DT_US, arrShReferencedSegmentNumber);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Referenced Segment Number.
	 * @return short Array - Referenced Segment Number.
	 */
	public short[] getReferencedSegmentNumber()
	{
		Object objValue = getValue(0x0040, 0x000B);
		if(objValue != null)
			return (short[])objValue;
		return null;
	}
}
