/*******************************************************************************
 *  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.exceptions.InvalidMacroException;
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 Relationship Macro.
 */
public class DocumentRelationshipMacro extends Macro  
{
	

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

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

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

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

	/**
	 * Sets The date and time on which this Content Item was completed. For the purpose of recording measurements or logging events,
	 * completion time is defined as the time of data acquisition of the measurement, or the time of occurrence of the event.
	 * @param strObservationDateTime String value of Observation Date Time.
	 */
	public void setObservationDateTime(String strObservationDateTime)
	{
		/* Accept the Observation DateTime 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, 0xA032, strObservationDateTime,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0040, 0xA032,DicomConstants.DCM_DT_DT, strObservationDateTime);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves The date and time on which this Content Item was completed.
	 * @return String - value of Observation DateTime.
	 */
	public String getObservationDateTime()
	{
		Object objValue = getValue(0x0040, 0xA032);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Retrieves pre-initialized object of Content Template 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 ContentTemplateSequence - instance of Content Template Sequence.
	 */
	public ContentTemplateSequence getContentTemplateSequence()
	{
		ContentTemplateSequence objContentTemplateSequence = null;
		IDataElement objIDataElement =  getDataElement(0x0040,0xA504);
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objContentTemplateSequence = (ContentTemplateSequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objContentTemplateSequence = (ContentTemplateSequence)objDicomV3Factory.createSequence(0x0040,0xA504, objITransferSyntax, objIDictionaryReader);

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

	/**
	 * Retrieves pre-initialized object of Content 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 ContentSequence - instance of Content Sequence.
	 */
	public ContentSequence getContentSequence()
	{
		ContentSequence objContentSequence = null;
		IDataElement objIDataElement =  getDataElement(0x0040,0xA730);
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objContentSequence = (ContentSequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objContentSequence = (ContentSequence)objDicomV3Factory.createSequence(0x0040,0xA730, objITransferSyntax, objIDictionaryReader);

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

	/**
	 * Validates all the attributes in Document Relationship 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 Relationship 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 static boolean validate(IDataSet objIDataSet) throws InvalidMacroException
	{
		try
		{
			/*
			 * Observation DateTime 0040,A032
			 * please refer p.d.f chapter 3 page no 783
			 * Required if the date and time are different from the Content Date (0008,0023) and 
			 * Content Time (0008,0033) or the Observation DateTime (0040,A032) defined in higher items.
			 */

			/*
			 * Content Template Sequence 0040,A504
			 * please refer p.d.f chapter 3 page no 783
			 * Required if a template was used to define the content of this Item, and the template 
			 * consists of a single CONTAINER with nested content, and it is the outermost 
			 * invocation of a set of nested templates that start with the same CONTAINER
			 */

			/*
			 * Content Sequence 0040,A730
			 * please refer p.d.f chapter 3 page no 784
			 * Required if the enclosing Content Item has relationships.
			 */

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

	/**
	 * Validates all the attributes in Document Relationship 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
		{
			/*
			 * Observation DateTime 0040,A032
			 * please refer p.d.f chapter 3 page no 783
			 * Required if the date and time are different from the Content Date (0008,0023) and 
			 * Content Time (0008,0033) or the Observation DateTime (0040,A032) defined in higher items.
			 */

			/*
			 * Content Template Sequence 0040,A504
			 * please refer p.d.f chapter 3 page no 783
			 * Required if a template was used to define the content of this Item, and the template 
			 * consists of a single CONTAINER with nested content, and it is the outermost 
			 * invocation of a set of nested templates that start with the same CONTAINER
			 */

			/*
			 * Content Sequence 0040,A730
			 * please refer p.d.f chapter 3 page no 784
			 * Required if the enclosing Content Item has relationships.
			 */
		}
		catch( MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new InvalidMacroException("Document Relationship Macro",me);
		}
		return true;
	}
}
