/*******************************************************************************
 *  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.InvalidDataTypeException;
import cdac.medinfo.sdk.dcm30_04.exceptions.InvalidMacroException;
import cdac.medinfo.sdk.dcm30_04.exceptions.ValidationException;
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 describes the attributes for the coding of the principal structure
 */
public class PrimaryAnatomicStructureMacro extends Macro  
{
	

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

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

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

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

	/**
	 * Retrieves pre-initialized object of Primary Anatomic Structure 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 PrimaryAnatomicStructureSequence - instance of PrimaryAnatomicStructureSequence. 
	 */
	public PrimaryAnatomicStructureSequence getPrimaryAnatomicStructureSequence()
	{
		PrimaryAnatomicStructureSequence objPrimaryAnatomicStructureSequence = null;
		IDataElement objIDataElement =  getDataElement(0x0008,0x2228);
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objPrimaryAnatomicStructureSequence = (PrimaryAnatomicStructureSequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objPrimaryAnatomicStructureSequence = (PrimaryAnatomicStructureSequence)objDicomV3Factory.createSequence(0x0008,0x2228, objITransferSyntax, objIDictionaryReader);

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

	/**
	 * This method checks for the DataElements of the Item according to the Primary Anatomic Structure Macro
	 * @param objIItem object of Item for which the the validation is to be done according to the
	 * 						rules of Digital Signatures Macro
	 * @return boolean - status of the function as success or failure
	 * @throws InvalidMacroException Signals Macro Validation is failed.
	 */
	public boolean validate(IItem objIItem) throws InvalidMacroException
	{
		try
		{
			if(objIItem.contains(0x0008,0x2228))
				getPrimaryAnatomicStructureSequence().validate();
		}
		catch( InvalidDataTypeException dte)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(dte.getMessage());
			}
			throw new InvalidMacroException(this.getClass().getSimpleName(),dte);
		}
		catch( ValidationException ve)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ve.getMessage());
			}
			throw new InvalidMacroException(this.getClass().getSimpleName(),ve);
		}
		return true;
	}

	/**
	 * This method checks if any of the DataElements belonging to this Macro is present in the DataSet.
	 * If such is the case, then the Macro is present in the DataSet.
	 * @param objIDataSet object of DataSet in which the Macro is to be searched 
	 * @return status indicating success or failure
	 */
	public static boolean isMacroPresent(IDataSet objIDataSet) 
	{
		if(objIDataSet.isPresent(0x0008, 0x2228))
			return true;
		else 
			return false;
	}

	/**
	 * This method checks if any of the DataElements belonging to this Macro is present in the Item.
	 * If such is the case, then the Macro is present in the Item.
	 * @param objIItem object of Item in which the Macro is to be searched 
	 * @return status indicating success or failure
	 */
	public static boolean isMacroPresent(IItem objIItem)
	{
		if(objIItem.contains(0x0008, 0x2228))
			return true;
		else 
			return false;
	}

	/**
	 * Validates all the attributes in Primary Anatomic Structure 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;
	}

	/**
	 * This method checks for the DataElements of the DataSet according to the Digital Signatures Macro
	 * @param objIDataSet object of DataSet for which the the validation is to be done according to the
	 * 						rules of Digital Signatures Macro
	 * @return boolean - status of the function as success or failure
	 * @throws InvalidMacroException If Validation of Macro fails. 
	 */
	public boolean validate(IDataSet objIDataSet) throws InvalidMacroException
	{
		try
		{
			if(objIDataSet.isPresent(0x0008,0x2228))
				getPrimaryAnatomicStructureSequence().validate();
		}
		catch( InvalidDataTypeException dte)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(dte.getMessage());
			}
			throw new InvalidMacroException("Primary Anatomic Structure Macro",dte);
		}
		catch( ValidationException ve)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ve.getMessage());
			}
			throw new InvalidMacroException("Primary Anatomic Structure Macro",ve);
		}
		return true;

	}
}
