/*******************************************************************************
 *  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.exceptions.ConditionFailedException;
import cdac.medinfo.sdk.dcm30_04.exceptions.InvalidItemException;
import cdac.medinfo.sdk.dcm30_04.exceptions.InvalidSequenceException;
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 item class contains methods to set,get and validate the attributes in Request Attributes Sequence.
 */
public class RequestAttributesSequenceItem extends Item  
{
	

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

	/**
	 * This constructor creates the item and internal code sequence macro with default TransferSyntax 
	 */
	RequestAttributesSequenceItem()
	{
		super();
	}

	/**
	 * This constructor creates the item and internal code sequence macro with default TransferSyntax.
	 * @param objITransferSyntax specifies the DICOM Standard defined TransfeSyntax. 
	 */
	RequestAttributesSequenceItem(ITransferSyntax objITransferSyntax)
	{
		super(objITransferSyntax);
	}

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

	/**
	 * Sets the Requested Procedure ID in string format.
	 * @param strRequestedProcedureID String value of Requested Procedure ID.
	 */
	public void setRequestedProcedureID(String strRequestedProcedureID)
	{
		/*Accept the RequestedProcedureID in String format of type SH.
		 * 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, 0x1001, strRequestedProcedureID,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0040, 0x1001,DicomConstants.DCM_DT_SH, strRequestedProcedureID);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Requested Procedure ID.
	 * @return String - value of Requested Procedure ID.
	 */
	public String getRequestedProcedureID()
	{
		Object objValue = getValue(0x0040,0x1001);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets reason for the Requested Procedure.
	 * @param strReasonForTheRequestedProcedure String value of reason for the Requested Procedure.
	 */
	public void setReasonForTheRequestedProcedure(String strReasonForTheRequestedProcedure)
	{
		/*Accept the ReasonForTheRequestedProcedure 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, 0x1002, strReasonForTheRequestedProcedure,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0040, 0x1002,DicomConstants.DCM_DT_LO, strReasonForTheRequestedProcedure);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves reason for the Requested Procedure.
	 * @return String - value of reason for the Requested Procedure.
	 */
	public String getReasonForTheRequestedProcedure()
	{
		Object objValue = getValue(0x0040,0x1002);
		if(objValue != null)
			return objValue.toString();
		return null;
	}


	/**
	 * Retrieves pre-initialized object of Reason For Requested Procedure 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 ReasonForRequestedProcedureCodeSequence - instance of ReasonForRequestedProcedureCodeSequence. 
	 */
	public ReasonForRequestedProcedureCodeSequence getReasonForRequestedProcedureCodeSequence()
	{
		ReasonForRequestedProcedureCodeSequence objReasonForRequestedProcedureCodeSequence = null;
		IDataElement objIDataElement =  getDataForTag(0x0040, 0x100A);
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objReasonForRequestedProcedureCodeSequence = (ReasonForRequestedProcedureCodeSequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objReasonForRequestedProcedureCodeSequence = (ReasonForRequestedProcedureCodeSequence)objDicomV3Factory.createSequence(0x0040, 0x100A, objITransferSyntax, objIDictionaryReader);

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

	/**
	 * Sets Scheduled Procedure Step ID.
	 * @param strScheduledProcedureStepID String value of Scheduled Procedure Step ID.
	 */
	public void setScheduledProcedureStepID(String strScheduledProcedureStepID)
	{
		/*Accept the ScheduledProcedureStepID 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, 0x0009, strScheduledProcedureStepID,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0040, 0x0009,DicomConstants.DCM_DT_SH, strScheduledProcedureStepID);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Scheduled Procedure Step ID.
	 * @return String - value of Scheduled Procedure Step ID.
	 */
	public String getScheduledProcedureStepID()
	{
		Object objValue = getValue(0x0040,0x0009);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Scheduled Procedure Step Description.
	 * @param strScheduledProcedureStepDescription String value of Scheduled Procedure Step Description.
	 */
	public void setScheduledProcedureStepDescription(String strScheduledProcedureStepDescription)
	{
		/*Accept the ScheduledProcedureStepDescription 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, 0x0007, strScheduledProcedureStepDescription,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0040, 0x0007,DicomConstants.DCM_DT_LO, strScheduledProcedureStepDescription);
		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Scheduled Procedure Step Description.
	 * @return String - value of Scheduled Procedure Step Description.
	 */
	public String getScheduledProcedureStepDescription()
	{
		Object objValue = getValue(0x0040, 0x0007);
		if(objValue != null)
			return objValue.toString();
		return null;
	}


	/**
	 * Retrieves pre-initialized object of Scheduled Protocol 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 ScheduledProtocolCodeSequence - instance of ScheduledProtocolCodeSequence. 
	 */
	public ScheduledProtocolCodeSequence getScheduledProtocolCodeSequence()
	{
		ScheduledProtocolCodeSequence objScheduledProtocolCodeSequence = null;
		IDataElement objIDataElement =  getDataForTag(0x0040, 0x0008);
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objScheduledProtocolCodeSequence = (ScheduledProtocolCodeSequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objScheduledProtocolCodeSequence = (ScheduledProtocolCodeSequence)objDicomV3Factory.createSequence(0x0040, 0x0008, objITransferSyntax, objIDictionaryReader);

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

	/**
	 * Validates all the attributes in Request Attributes Sequence 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(0x0040,0x1001)) && (contains(0x0040,0x0009)))
				throw new ConditionFailedException("conditional Element 0040,1001 required for Tag 0040,0009 is missing") ;
			if(contains(0x0040,0x100A))
				getReasonForRequestedProcedureCodeSequence().validateForModule();
			if(contains(0x0040,0x0008))
				getScheduledProtocolCodeSequence().validateForModule();
		}
		catch( ConditionFailedException ce)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ce.getMessage());
			}
			throw new InvalidItemException(this.getClass().getSimpleName(),ce); 
		}
		catch(MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new InvalidItemException(this.getClass().getSimpleName(),me);
		}
		catch(InvalidSequenceException ise)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ise.getMessage());
			}
			throw new InvalidItemException(this.getClass().getSimpleName(),ise);
		}

		return true;
	}
}
