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


/**
* This item class contains the methods to set, get and validate the attribute present in Override Sequence .
 */

import cdac.medinfo.sdk.dcm30_04.commoninterface.IDataElement;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDictionaryReader;
import cdac.medinfo.sdk.dcm30_04.commoninterface.ITag;
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.utils.DicomConfig;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConstants;
import cdac.medinfo.sdk.dcm30_04.utils.DicomLogger;




/**
 * This item introduces sequence of parameters which were overridden during the
 * administration of the beam segment immediately prior to the current control point. 
 * The sequence may contain one or more items.
 */
public class OverrideSequenceItem extends Item  
{
	

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

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

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

	/**
	 * Sets the Data Element Tag of the attribute which was overridden.
	 * @param objITag Tag object of Override Parameter Pointer.
	 */
	public void setOverrideParameterPointer(ITag objITag)
	{
		/*Accept the Value Type in String format of type AT.
		 * 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(0x3008,0x0062, objITag,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x3008,0x0062,DicomConstants.DCM_DT_AT, objITag);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves  Data Element Tag of the attribute which was overridden.
	 * @return String - Tag object of Override Parameter Pointer.
	 */
	public ITag getOverrideParameterPointer()
	{
		Object objValue = getValue(0x3008,0x0062);
		if(objValue != null)
			return (ITag)objValue;
		return null;
	}

	/**
	 * Sets the Data Element Tag of the attribute which was overridden.
	 * @param objITag Tag object of Override Parameter Pointer.
	 */
	public void setParameterSequencePointer(ITag objITag)
	{
		/* Accept the Value Type in String format of type AT.
		 * Contains the Data Element Tag of the parent sequence containing the attribute that was overridden.
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x3008,0x0061, objITag,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x3008,0x0061,DicomConstants.DCM_DT_AT, objITag);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves  Data Element Tag of the attribute which was overridden.
	 * @return String - Tag object of Override Parameter Pointer.
	 */
	public ITag getParameterSequencePointer()
	{
		Object objValue = getValue(0x3008,0x0061);
		if(objValue != null)
			return (ITag)objValue;
		return null;
	}
	
	/**
	 * Sets Parameter Item Index
	 * @param strParameterItemIndex String value of Parameter Item Index.
	 */
	public void setParameterItemIndex(String strParameterItemIndex )
	{
		/* Accept the Parameter Item Index  in String format of type IS.
		 * Contains the sequence item index (monotonically increasing from 1) of the overridden attributes within its parent sequence. 
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x3008,0x0063, strParameterItemIndex ,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x3008,0x0063,DicomConstants.DCM_DT_IS, strParameterItemIndex );

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves ParameterItemIndex
	 * Contains the sequence item index (monotonically increasing from 1) of the overridden attributes within its parent sequence. 
	 * @return String - value of Parameter Item Index.
	 */
	public String getParameterItemIndex()
	{
		Object objValue = getValue(0x3008,0x0063);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	
	/**
	 * Sets the Name of operator who authorized override.
	 * @param arrStrOperatorsName String array  of Operators Name .
	 */
	public void setOperatorsName(String[] arrStrOperatorsName)
	{
		/*Accept the Value Type 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(0x0008,0x1070, arrStrOperatorsName,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0008,0x1070,DicomConstants.DCM_DT_PN, arrStrOperatorsName);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves  Name of operator who authorized override.
	 * @return String - array  of Operators Name.
	 */
	public String[]  getOperatorsName()
	{
		Object objValue = getValue(0x0008,0x1070);
		if(objValue != null)
			return (String[])objValue;
		return null;
	}
	/**
	 * Sets User-defined description of reason for override of parameter specified by Override Parameter Pointer (3008,0062).
	 * @param strOverrideReason String value of Override Reason .
	 */
	public void setOverrideReason(String strOverrideReason)
	{
		/*Accept the Value Type in String format of type ST.
		 * 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(0x3008,0x0066, strOverrideReason,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x3008,0x0066,DicomConstants.DCM_DT_ST, strOverrideReason);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves  Override Reason.
	 * @return String - value of Override Reason.
	 */
	public String  getOverrideReason()
	{
		Object objValue = getValue(0x3008,0x0066);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Validates all the attributes in OverrideSequenceItem  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(0x3008,0x0062))
				throw new ConditionFailedException("value of conditional Element 3008,0062 required if 3008,0060 is present") ;
			if(!contains(0x0008,0x1070))
				throw new ConditionFailedException("value of conditional Element 0008,1070 required if 3008,0060 is present") ;
		}
		catch( ConditionFailedException ce)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ce.getMessage());
			}
			throw new InvalidItemException(this.getClass().getSimpleName(),ce); 
		}
		return true;
	}
}
