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




/**
 * This Macro contains attributes of Document Content Macro.
 */
public class DocumentContentMacro extends Macro  
{
	

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	NumericMeasurementMacro objNumericMeasurementMacro = null; 
	CodeMacro objCodeMacro = null;
	CompositeObjectReferenceMacro objCompositeObjectReferenceMacro = null;
	ImageReferenceMacro objImageReferenceMacro = null;
	WaveformReferenceMacro objWaveformReferenceMacro = null;
	SpatialCoordinatesMacro objSpatialCoordinatesMacro = null;
	TemporalCoordinatesMacro objTemporalCoordinatesMacro = null;

	/**
	 * This constructor constructs a Macro with specified DataSet.
	 * @param objDataSet Collection of DataElements 
	 */
	public DocumentContentMacro(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 DocumentContentMacro(IDataSet objDataSet,IDictionaryReader objDictionaryReader)
	{
		super(objDataSet,objDictionaryReader);
	}

	/**
	 * This constructor constructs a Macro with the specified Item
	 * @param objItem specifies instance of Item class
	 */
	public DocumentContentMacro(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 DocumentContentMacro(IItem objItem,IDictionaryReader objDictionaryReader)
	{
		super(objItem, objDictionaryReader);
	}

	/**
	 * Sets The type of the value encoded in this Content Item.
	 * Defined Terms: TEXT, NUM, CODE, DATETIME, DATE, TIME, UIDREF, PNAME, COMPOSITE, IMAGE, WAVEFORM, SCOORD, TCOORD, CONTAINER
	 * @param enumValueType Enum value of  type of the value encoded in this Content Item.
	 */
	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);
	}

	/**
	 * Retrieves The type of the value encoded in this Content Item.
	 * @return String - value of type of the value encoded in this Content Item.
	 */
	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 Concept Name Code Sequence. 
	 */
	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;
	}

	/**
	 * This flag specifies for a CONTAINER whether or not its contained Content Items are logically linked in a continuous textual
	 * flow, or are separate items.
	 * Enumerated Values: SEPARATE, CONTINUOUS
	 * @param enumContinuityOfContent Enum value of Continuity of Content.
	 */
	public void setContinuityOfContent(EnumContinuityOfContent enumContinuityOfContent)
	{
		/* Accept the Continuity of Content 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, 0xA050, enumContinuityOfContent.getValue(),objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0040, 0xA050,DicomConstants.DCM_DT_CS, enumContinuityOfContent.getValue());

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves whether or not its contained Content Items are logically linked in a continuous textual flow, or are separate items.
	 * @return String - value of Continuity of Content.
	 */
	public String getContinuityOfContent()
	{
		Object objValue = getValue(0x0040, 0xA050);
		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;
	}

	/**
	 * This is the value of the Content Item.
	 * @param strDateTime String value of Date Time.
	 */
	public void setDateTime(String strDateTime)
	{
		/* Accept the Date Time in String format 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 the value of the Content Item.
	 * @return String - value of the value of Date Time.
	 */
	public String getDateTime()
	{
		Object objValue = getValue(0x0040, 0xA120);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * This is the value of the Content Item.
	 * @param strDate String value of Date.
	 */
	public void setDate(String strDate)
	{
		/* Accept the Date in String format 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 the value of the Content Item.
	 * @return String - value of the value of Date.
	 */
	public String getDate()
	{
		Object objValue = getValue(0x0040, 0xA121);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * This is the value of the Content Item.
	 * @param strDate String value of Time.
	 */
	public void setTime(String strDate)
	{
		/* Accept the 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(0x0040, 0xA122, strDate,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0040, 0xA122,DicomConstants.DCM_DT_TM, strDate);

		addDataElement(objIDataElement);
	}

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

	/**
	 * This is the value of the Content Item.
	 * @param strPersonName String value of Person Name.
	 */
	public void setPersonName(String strPersonName)
	{
		/* Accept the Person Name in String format of type PN.
		 * 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, 0xA123, strPersonName,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0040, 0xA123,DicomConstants.DCM_DT_PN, strPersonName);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves the value of the Content Item.
	 * @return String - value of Person Name.
	 */
	public String getPersonName()
	{
		Object objValue = getValue(0x0040, 0xA123);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * This is the value of 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(0x0040, 0xA124, strUID,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0040, 0xA124,DicomConstants.DCM_DT_PN, strUID);

		addDataElement(objIDataElement);
	}

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

	/**
	 * Retrieves the instance of Numeric Measurement Macro. If the instance is not already present a new instance 
	 * is created and returned. It is used to set the attribtues of Numeric Measurement Macro.
	 * @return - instance of NumericMeasurementMacro.
	 */
	public NumericMeasurementMacro getNumericMeasurementMacro()
	{
		if(getValueType() != null)
		{
			if(getValueType().equalsIgnoreCase(EnumValueType.NUM.toString()))
			{
				if(objNumericMeasurementMacro == null)
				{
					if(iMacroLevel == DicomConstants.DCM_MODULE_LEVEL_MACRO)
						objNumericMeasurementMacro = new NumericMeasurementMacro(objIDataSet);
					else 
						objNumericMeasurementMacro = new NumericMeasurementMacro(objIItem);

					return objNumericMeasurementMacro;
				}
				return objNumericMeasurementMacro;
			}
			else
				return null;
		}
		else
			return null;
	}

	/**
	 * Retrieves the instance of Code Macro. If the instance is not already present a new instance 
	 * is created and returned. It is used to set the attribtues of Code Macro.
	 * @return - instance of CodeMacro.
	 */
	public CodeMacro getCodeMacro()
	{
		if(getValueType() != null)
		{
			if(getValueType().equalsIgnoreCase(EnumValueType.CODE.toString()))
			{
				if(objCodeMacro == null)
				{
					if(iMacroLevel == DicomConstants.DCM_MODULE_LEVEL_MACRO)
						objCodeMacro = new CodeMacro(objIDataSet);
					else 
						objCodeMacro = new CodeMacro(objIItem);

					return objCodeMacro;
				}
				return objCodeMacro;
			}
			else
				return null;
		}
		else
			return null;
	}

	/**
	 * Retrieves the instance of Composite Object Reference Macro. If the instance is not already present a new instance 
	 * is created and returned. It is used to set the attribtues of Composite Object Reference Macro.
	 * @return - instance of CompositeObjectReferenceMacro.
	 */
	public CompositeObjectReferenceMacro getCompositeObjectReferenceMacro()
	{
		if(getValueType() != null)
		{
			if(getValueType().equalsIgnoreCase(EnumValueType.COMPOSITE.toString()))
			{
				if(objCompositeObjectReferenceMacro == null)
				{
					if(iMacroLevel == DicomConstants.DCM_MODULE_LEVEL_MACRO)
						objCompositeObjectReferenceMacro = new CompositeObjectReferenceMacro(objIDataSet);
					else 
						objCompositeObjectReferenceMacro = new CompositeObjectReferenceMacro(objIItem);

					return objCompositeObjectReferenceMacro;
				}
				return objCompositeObjectReferenceMacro;
			}
			else
				return null;
		}
		else
			return null;
	}

	/**
	 * Retrieves the instance of Image Reference Macro. If the instance is not already present a new instance 
	 * is created and returned. It is used to set the attribtues of Image Reference Macro.
	 * @return - instance of ImageReferenceMacro.
	 */
	public ImageReferenceMacro getImageReferenceMacro()
	{
		if(getValueType() != null)
		{
			if(getValueType().equalsIgnoreCase(EnumValueType.IMAGE.toString()))
			{
				if(objImageReferenceMacro == null)
				{
					if(iMacroLevel == DicomConstants.DCM_MODULE_LEVEL_MACRO)
						objImageReferenceMacro = new ImageReferenceMacro(objIDataSet);
					else 
						objImageReferenceMacro = new ImageReferenceMacro(objIItem);

					return objImageReferenceMacro;
				}
				return objImageReferenceMacro;
			}
			else
				return null;
		}
		else
			return null;
	}

	/**
	 * Retrieves the instance of Waveform Reference Macro. If the instance is not already present a new instance 
	 * is created and returned. It is used to set the attribtues of Waveform Reference Macro.
	 * @return - instance of WaveformReferenceMacro.
	 */
	public WaveformReferenceMacro getWaveformReferenceMacro()
	{
		if(getValueType() != null)
		{
			if(getValueType().equalsIgnoreCase(EnumValueType.WAVEFORM.toString()))
			{
				if(objWaveformReferenceMacro == null)
				{
					if(iMacroLevel == DicomConstants.DCM_MODULE_LEVEL_MACRO)
						objWaveformReferenceMacro = new WaveformReferenceMacro(objIDataSet);
					else 
						objWaveformReferenceMacro = new WaveformReferenceMacro(objIItem);

					return objWaveformReferenceMacro;
				}
				return objWaveformReferenceMacro;
			}
			else
				return null;
		}
		else
			return null;
	}

	/**
	 * Retrieves the instance of Spatial Coordinates Macro. If the instance is not already present a new instance 
	 * is created and returned. It is used to set the attribtues of Spatial Coordinates Macro.
	 * @return - instance of SpatialCoordinatesMacro.
	 */
	public SpatialCoordinatesMacro getSpatialCoordinatesMacro()
	{
		if(getValueType() != null)
		{
			if(getValueType().equalsIgnoreCase(EnumValueType.SCOORD.toString()))
			{
				if(objSpatialCoordinatesMacro == null)
				{
					if(iMacroLevel == DicomConstants.DCM_MODULE_LEVEL_MACRO)
						objSpatialCoordinatesMacro = new SpatialCoordinatesMacro(objIDataSet);
					else 
						objSpatialCoordinatesMacro = new SpatialCoordinatesMacro(objIItem);

					return objSpatialCoordinatesMacro;
				}
				return objSpatialCoordinatesMacro;
			}
			else
				return null;
		}
		else
			return null;
	}

	/**
	 * Retrieves the instance of Temporal Coordinates Macro. If the instance is not already present a new instance 
	 * is created and returned. It is used to set the attribtues of Temporal Coordinates Macro.
	 * @return - instance of TemporalCoordinatesMacro.
	 */
	public TemporalCoordinatesMacro getTemporalCoordinatesMacro()
	{
		if(getValueType() != null)
		{
			if(getValueType().equalsIgnoreCase(EnumValueType.TCOORD.toString()))
			{
				if(objTemporalCoordinatesMacro == null)
				{
					if(iMacroLevel == DicomConstants.DCM_MODULE_LEVEL_MACRO)
						objTemporalCoordinatesMacro = new TemporalCoordinatesMacro(objIDataSet);
					else 
						objTemporalCoordinatesMacro = new TemporalCoordinatesMacro(objIItem);

					return objTemporalCoordinatesMacro;
				}
				return objTemporalCoordinatesMacro;
			}
			else
				return null;
		}
		else
			return null;
	}

	/**
	 * Validates all the attributes in Document Content Macro
	 * @return boolean - Success -true, Failure -false.
	 * @throws InvalidMacroException throws InvalidMacroException If Mandatory or Conditional element fails.
	 */
	public boolean validate() throws InvalidMacroException
	{
		if(iMacroLevel == DicomConstants.DCM_MODULE_LEVEL_MACRO)
			return validate(objIDataSet);
		else if(iMacroLevel == DicomConstants.DCM_ITEM_LEVEL_MACRO)
			return validate(objIItem);
		return false;
	}

	/**
	 * Validates all the attributes in Document 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.CONTAINER.getValue().equalsIgnoreCase(strValueType) && !objIDataSet.isPresent(0x0040,0xA050))
					throw new ConditionFailedException("0040,A050 Required if Value Type (0040,A040) is " + EnumValueType.CONTAINER.getValue());
				if(EnumValueType.TEXT.getValue().equalsIgnoreCase(strValueType) && !objIDataSet.isPresent(0x0040,0xA160))
					throw new ConditionFailedException("0040,A160 Required if Value Type (0040,A160) is " + EnumValueType.TEXT.getValue());
				if(EnumValueType.DATETIME.getValue().equalsIgnoreCase(strValueType) && !objIDataSet.isPresent(0x0040,0xA120))
					throw new ConditionFailedException("0040,A120 Required if Value Type (0040,A160) is " + EnumValueType.DATETIME.getValue());
				if(EnumValueType.DATE.getValue().equalsIgnoreCase(strValueType) && !objIDataSet.isPresent(0x0040,0xA121))
					throw new ConditionFailedException("0040,A121 Required if Value Type (0040,A160) is " + EnumValueType.DATE.getValue());
				if(EnumValueType.TIME.getValue().equalsIgnoreCase(strValueType) && !objIDataSet.isPresent(0x0040,0xA122))
					throw new ConditionFailedException("0040,A122 Required if Value Type (0040,A160) is " + EnumValueType.TIME.getValue());
				if(EnumValueType.PNAME.getValue().equalsIgnoreCase(strValueType) && !objIDataSet.isPresent(0x0040,0xA123))
					throw new ConditionFailedException("0040,A123 Required if Value Type (0040,A160) is " + EnumValueType.PNAME.getValue());
				if(EnumValueType.UIDREF.getValue().equalsIgnoreCase(strValueType) && !objIDataSet.isPresent(0x0040,0xA124))
					throw new ConditionFailedException("0040,A124 Required if Value Type (0040,A160) is " + EnumValueType.UIDREF.getValue());
				if((EnumValueType.COMPOSITE.getValue().equalsIgnoreCase(strValueType)|| EnumValueType.IMAGE.getValue().equalsIgnoreCase(strValueType) || EnumValueType.WAVEFORM.getValue().equalsIgnoreCase(strValueType) || EnumValueType.SCOORD.getValue().equalsIgnoreCase(strValueType) || EnumValueType.TCOORD.getValue().equalsIgnoreCase(strValueType)) && !objIDataSet.isPresent(0x0040,0xA043))
					throw new ConditionFailedException("0040,A043 Required if Value Type (0040,A160) is " + EnumValueType.COMPOSITE.getValue() +"or "+EnumValueType.IMAGE.getValue()+"or "+EnumValueType.WAVEFORM.getValue()+"or "+EnumValueType.SCOORD.getValue()+"or "+EnumValueType.TCOORD.getValue());
				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();
				}

				/*
				 * These conditions check presence of macro depending on the value of Value Type.
				 */
				if(objIDataSet.isPresent(0x0040,0xA300))
				{
					getNumericMeasurementMacro().validate();
					if(!(EnumValueType.NUM.getValue().equalsIgnoreCase(strValueType)))
						throw new ConditionFailedException("Include Numeric Measurement Macro if and only if Value Type (0040,A040) is" + EnumValueType.NUM.getValue());
				}

				if(objIDataSet.isPresent(0x0040,0xA168))
				{
					getCodeMacro().validate();
					if(!(EnumValueType.CODE.getValue().equalsIgnoreCase(strValueType)))
						throw new ConditionFailedException("Include Code Macro if and only if Value Type (0040,A040) is " +  EnumValueType.CODE.getValue());
				}
				if((objIDataSet.isPresent(0x0008,0x1160) || objIDataSet.isPresent(0x0008,0x1199) || objIDataSet.isPresent(0x0088,0x0200)))
				{
					getImageReferenceMacro().validate();
					if(!EnumValueType.IMAGE.getValue().equalsIgnoreCase(strValueType))
						throw new ConditionFailedException("Include Image Reference Macro if and only if Value Type (0040,A040) is " + EnumValueType.IMAGE.getValue());
				}
				if(objIDataSet.isPresent(0x0008,0x1199))
				{
					getCompositeObjectReferenceMacro().validate();
					if(!(EnumValueType.COMPOSITE.getValue().equalsIgnoreCase(strValueType)))
						throw new ConditionFailedException("Include Composite Object Reference Macro  if and only if Value Type (0040,A040) is " + EnumValueType.COMPOSITE.getValue());
				}
				if(objIDataSet.isPresent(0x0040,0xA0B0))
				{
					getWaveformReferenceMacro().validate();
					if(!(EnumValueType.WAVEFORM.getValue().equalsIgnoreCase(strValueType)))
						throw new ConditionFailedException("Include Waveform Reference Macro if and only if Value Type (0040,A040) is " + EnumValueType.WAVEFORM.getValue());
				}
				if(objIDataSet.isPresent(0x0070,0x0022) || objIDataSet.isPresent(0x0070,0x0023))
				{
					getSpatialCoordinatesMacro().validate();
					if(!EnumValueType.SCOORD.getValue().equalsIgnoreCase(strValueType))
						throw new ConditionFailedException("Include Spatial Coordinates Macro if and only if Value Type (0040,A040) is " + EnumValueType.SCOORD.getValue());
				}
				if(objIDataSet.isPresent(0x0040,0xA130))
				{
					getTemporalCoordinatesMacro().validate();
					if(!EnumValueType.TCOORD.getValue().equalsIgnoreCase(strValueType))
						throw new ConditionFailedException("Include Temporal Coordinates Macro if and only if Value Type (0040,A040) is " + EnumValueType.TCOORD.getValue());
				}
			}
		}
		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 Document Content 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.CONTAINER.getValue().equalsIgnoreCase(strValueType) && !objIItem.contains(0x0040,0xA050))
					throw new ConditionFailedException("0040,A050 Required if Value Type (0040,A040) is " + EnumValueType.CONTAINER.getValue());
				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());

				/*
				 * These conditions check presence of module depending on the value of Value Type.
				 */
				DocumentContentMacro objDocumentContentMacro = new DocumentContentMacro(objIItem);
				if(objIItem.contains(0x0040,0xA300))
				{
					objDocumentContentMacro.getNumericMeasurementMacro().validate();
					if(!(EnumValueType.NUM.getValue().equalsIgnoreCase(strValueType)))
						throw new ConditionFailedException("Include Numeric Measurement Macro if and only if Value Type (0040,A040) is" + EnumValueType.NUM.getValue());
				}

				if(objIItem.contains(0x0040,0xA168))
				{
					objDocumentContentMacro.getCodeMacro().validate();
					if(!(EnumValueType.CODE.getValue().equalsIgnoreCase(strValueType)))
						throw new ConditionFailedException("Include Code Macro if and only if Value Type (0040,A040) is " +  EnumValueType.CODE.getValue());
				}
				if((objIItem.contains(0x0008,0x1160) || objIItem.contains(0x0008,0x1199) || objIItem.contains(0x0088,0x0200)))
				{
					objDocumentContentMacro.getImageReferenceMacro().validate();
					if(!EnumValueType.IMAGE.getValue().equalsIgnoreCase(strValueType))
						throw new ConditionFailedException("Include Image Reference Macro if and only if Value Type (0040,A040) is " + EnumValueType.IMAGE.getValue());
				}
				if(objIItem.contains(0x0008,0x1199))
				{
					objDocumentContentMacro.getCompositeObjectReferenceMacro().validate();
					if(!(EnumValueType.COMPOSITE.getValue().equalsIgnoreCase(strValueType)))
						throw new ConditionFailedException("Include Composite Object Reference Macro  if and only if Value Type (0040,A040) is " + EnumValueType.COMPOSITE.getValue());
				}
				if(objIItem.contains(0x0040,0xA0B0))
				{
					objDocumentContentMacro.getWaveformReferenceMacro().validate();
					if(!(EnumValueType.WAVEFORM.getValue().equalsIgnoreCase(strValueType)))
						throw new ConditionFailedException("Include Waveform Reference Macro if and only if Value Type (0040,A040) is " + EnumValueType.WAVEFORM.getValue());
				}
				if(objIItem.contains(0x0070,0x0022) || objIItem.contains(0x0070,0x0023))
				{
					objDocumentContentMacro.getSpatialCoordinatesMacro().validate();
					if(!EnumValueType.SCOORD.getValue().equalsIgnoreCase(strValueType))
						throw new ConditionFailedException("Include Spatial Coordinates Macro if and only if Value Type (0040,A040) is " + EnumValueType.SCOORD.getValue());
				}
				if(objIItem.contains(0x0040,0xA130))
				{
					objDocumentContentMacro.getTemporalCoordinatesMacro().validate();
					if(!EnumValueType.TCOORD.getValue().equalsIgnoreCase(strValueType))
						throw new ConditionFailedException("Include Temporal Coordinates Macro if and only if Value Type (0040,A040) is " + EnumValueType.TCOORD.getValue());
				}
			}
		}
		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;
	}
}
