/*******************************************************************************
 *  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.enums.EnumBlockIdentifyingInformationStatus;
import cdac.medinfo.sdk.dcm30_04.exceptions.ConditionFailedException;
import cdac.medinfo.sdk.dcm30_04.exceptions.InvalidItemException;
import cdac.medinfo.sdk.dcm30_04.exceptions.MandatoryMissingException;
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 item class contains methods to validate, set and get the attributes of Private Data Element Characteristics Sequence.
 */


public class PrivateDataElementCharacteristicsSequenceItem extends Item
{
	


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

	/**
	 * This constructor creates the item with default TransferSyntax 
	 */
	PrivateDataElementCharacteristicsSequenceItem()
	{
		super();
	}

	/*
	 * This constructor calls the respective superclass constructor
	 * This constructor should throw exception if null TS is tried to be set
	 */
	/**
	 * This constructor creates the item with specified TransferSyntax 
	 * and default DictionaryReader
	 * @param objITransferSyntax specifies the DICOM Standard defined TransfeSyntax
	 */
	PrivateDataElementCharacteristicsSequenceItem(ITransferSyntax objITransferSyntax)
	{
		super(objITransferSyntax);
	}

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

	/*
	 * (non-Javadoc) This method validates the contents of the Item
	 * @see cdac.medinfo.sdk.dcm30_04.commoninterface.IItem#validate()
	 */
	/**
	 * Validates all the attributes in PrivateDataElementCharacteristicsSequenceItem 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(0x0008,0x0301))
				throw new MandatoryMissingException("0008,0301");
			if(!contains(0x0008,0x0302))
				throw new MandatoryMissingException("0008,0302");
			if(!contains(0x0008,0x0303))
				throw new MandatoryMissingException("0008,0303");
			if(contains(0x0008,0x0303) && getDataForTag(0x0008, 0x0303).getValue().equals(EnumBlockIdentifyingInformationStatus.MIXED))
				if(!contains(0x0008,0x0304))
					throw new ConditionFailedException("0x0008,0x0304");
		}
		catch(MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new ValidationException(this.getClass().getSimpleName(),me);
		}
		catch(ConditionFailedException ce)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ce.getMessage());
			}
			throw new ValidationException(this.getClass().getSimpleName(),ce); 
		}
		return true;
	}
	
	/**
	 * Sets Private Group Reference.
	 * Odd group number within which the Private Data Element block is reserved.
	 * @param shPrivateGroupReference short value of Private Group Reference
	 */
	public void setPrivateGroupReference(short shPrivateGroupReference)
	{
		/* Accept the Private Group Reference 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(0x0008,0x0301,shPrivateGroupReference,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0008,0x0301,DicomConstants.DCM_DT_US, shPrivateGroupReference);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Private Group Reference.
	 * Odd group number within which the Private Data Element block is reserved.
	 * @return short - short value of Private Group Reference
	 */
	public short getPrivateGroupReference()
	{
		Object objValue = getValue(0x0008,0x0301);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	
	/**
	 * Sets Private Creator Reference.
	 * @param strPrivateCreatorReference String value of Private Creator Reference.
	 */
	public void setPrivateCreatorReference(String strPrivateCreatorReference)
	{
		/* Accept the Private Creator Reference 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(0x0008,0x0302, strPrivateCreatorReference,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0008,0x0302,DicomConstants.DCM_DT_LO, strPrivateCreatorReference);

		addDataElement(objIDataElement);
	}

	/**
	 * Return Private Creator Reference.
	 * @return String - value of Private Creator Reference.
	 */
	public String getPrivateCreatorReference()
	{
		Object objValue = getValue(0x0008,0x0302);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Sets type of Block Identifying Information Status.
	 * Defined Terms:
	 * 1) SAFE.
	 * 2) UNSAFE.
	 * 3) MIXED.
	 * @param enumBlockIdentifyingInformationStatus enum value of Block Identifying Information Status.
	 */
	public void setBlockIdentifyingInformationStatus(EnumBlockIdentifyingInformationStatus enumBlockIdentifyingInformationStatus)
	{
		/* Accept the Block Identifying Information Status 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(0x0008,0x0303, enumBlockIdentifyingInformationStatus.getValue(),getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0008,0x0303,DicomConstants.DCM_DT_CS, enumBlockIdentifyingInformationStatus.getValue());

		addDataElement(objIDataElement);


	}

	/**
	 * Return type of Block Identifying Information Status. 
	 * @return String - value of Block Identifying Information Status.
	 */
	public String getBlockIdentifyingInformationStatus()
	{
		Object objValue = getValue(0x0008,0x0303);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Non identifying Private Elements.
	 * List of Private Data Elements in block that do not contain identifying information.
	 * @param arrShNonidentifyingPrivateElements
	 */
	public void setNonidentifyingPrivateElements(short[] arrShNonidentifyingPrivateElements)
	{
		/*Accept the Non identifying Private Elements 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(0x0008, 0x0304, arrShNonidentifyingPrivateElements,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0008, 0x0304,DicomConstants.DCM_DT_US, arrShNonidentifyingPrivateElements);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Non identifying Private Elements.
	 * List of Private Data Elements in block that do not contain identifying information.
	 * @return short Array - Non identifying Private Elements.
	 */
	public short[] getNonidentifyingPrivateElements()
	{
		Object objValue = getValue(0x0008, 0x0304);
		if(objValue != null)
			return (short[])objValue;
		return null;
	}
	
	/**
	 * Retrieves pre-initialized object of Deidentification Action 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 DeidentificationActionSequence - instance of DeidentificationActionSequence. 
	 */
	public DeidentificationActionSequence getDeidentificationActionSequence()
	{
		DeidentificationActionSequence objDeidentificationActionSequence = null;
		IDataElement objIDataElement =  getDataForTag(0x0008, 0x0305);
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objDeidentificationActionSequence = (DeidentificationActionSequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objDeidentificationActionSequence = (DeidentificationActionSequence)objDicomV3Factory.createSequence(0x0008, 0x0305, objITransferSyntax, objIDictionaryReader);

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

	
}
