/*******************************************************************************
 *  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.IDictionaryReader;
import cdac.medinfo.sdk.dcm30_04.commoninterface.ITransferSyntax;
import cdac.medinfo.sdk.dcm30_04.enums.EnumSourceApplicatorType;
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 the methods to set, get and validate the attribute present in Recorded Source Applicator Sequence.
 */
public class RecordedSourceApplicatorSequenceItem extends Item  
{
	

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

	/**
	 * This constructor creates the item and internal code sequence macro with default TransferSyntax 
	 */
	RecordedSourceApplicatorSequenceItem()
	{
		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
	 */
	RecordedSourceApplicatorSequenceItem(ITransferSyntax objITransferSyntax)
	{
		super(objITransferSyntax);
	}

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

	/**
	 * Sets Identification number of the Source Applicator. The value of Source Applicator Number (300A,0290) shall be unique within the Channel in which it is created.
	 * @param strReferencedSourceApplicatorNumber String value of Referenced Source Applicator Number.
	 */
	public void setReferencedSourceApplicatorNumber(String strReferencedSourceApplicatorNumber)
	{
		/* Accept the Referenced Source Applicator Number of type IS.
		 * 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,0x0142,strReferencedSourceApplicatorNumber,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x3008,0x0142,DicomConstants.DCM_DT_IS, strReferencedSourceApplicatorNumber);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Identification number of the Source Applicator.
	 * @return String - value of Identification number of the Source Applicator.
	 */
	public String getReferencedSourceApplicatorNumber()
	{
		Object objValue = getValue(0x3008,0x0142);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets User or machine supplied identifier for Source Applicator.
	 * @param strSourceApplicatorID String value of Source Applicator ID.
	 */
	public void setSourceApplicatorID(String strSourceApplicatorID)
	{
		/* Accept the Source Applicator ID 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(0x300A,0x0291,strSourceApplicatorID,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x300A,0x0291,DicomConstants.DCM_DT_SH, strSourceApplicatorID);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves User or machine supplied identifier for Source Applicator.
	 * @return String - value of User or machine supplied identifier for Source Applicator.
	 */
	public String getSourceApplicatorID()
	{
		Object objValue = getValue(0x300A,0x0291);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Type of Source Applicator.
	 * @param enumSourceApplicatorType Enum value of Type of Source Applicator.
	 */
	public void setSourceApplicatorType(EnumSourceApplicatorType enumSourceApplicatorType)
	{
		/* Accept the Source Applicator Type 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(0x300A,0x0292,enumSourceApplicatorType.getValue(),getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x300A,0x0292,DicomConstants.DCM_DT_CS, enumSourceApplicatorType.getValue());

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Type of Source Applicator.
	 * @return String - value of Type of Source Applicator.
	 */
	public String getSourceApplicatorType()
	{
		Object objValue = getValue(0x300A,0x0292);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets User-defined name for Source Applicator.
	 * @param strSourceApplicatorName String value of Source Applicator Name.
	 */
	public void setSourceApplicatorName(String strSourceApplicatorName)
	{
		/* Accept the Source Applicator Name 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(0x300A,0x0294,strSourceApplicatorName,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x300A,0x0294,DicomConstants.DCM_DT_LO, strSourceApplicatorName);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves User-defined name for Source Applicator.
	 * @return String - value of Source Applicator Name.
	 */
	public String getSourceApplicatorName()
	{
		Object objValue = getValue(0x300A,0x0294);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Length of Source Applicator (mm), defined as the distance between the connector of 
	 * the applicator and the distal-most position of the source.
	 * @param strSourceApplicatorLength String value of Source Applicator Length.
	 */
	public void setSourceApplicatorLength(String strSourceApplicatorLength)
	{
		/* Accept the Source Applicator Length of type DS.
		 * 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(0x300A,0x0296,strSourceApplicatorLength,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x300A,0x0296,DicomConstants.DCM_DT_DS, strSourceApplicatorLength);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Length of Source Applicator (mm),
	 * @return String - value of Length of Source Applicator (mm).
	 */
	public String getSourceApplicatorLength()
	{
		Object objValue = getValue(0x300A,0x0296);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Manufacturer of Source Applicator.
	 * @param strSourceApplicatorManufacturer String value of Source Applicator Manufacturer.
	 */
	public void setSourceApplicatorManufacturer(String strSourceApplicatorManufacturer)
	{
		/* Accept the Source Applicator Manufacturer 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(0x300A,0x0298,strSourceApplicatorManufacturer,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x300A,0x0298,DicomConstants.DCM_DT_LO, strSourceApplicatorManufacturer);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Manufacturer of Source Applicator.
	 * @return String - value of Manufacturer of Source Applicator.
	 */
	public String getSourceApplicatorManufacturer()
	{
		Object objValue = getValue(0x300A,0x0298);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Distance of path along channel (mm) between adjacent (potential) dwell positions.
	 * @param strSourceApplicatorStepSize String value of Source Applicator Step Size.
	 */
	public void setSourceApplicatorStepSize(String strSourceApplicatorStepSize)
	{
		/* Accept the Source Applicator Step Size of type DS.
		 * 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(0x300A,0x02A0,strSourceApplicatorStepSize,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x300A,0x02A0,DicomConstants.DCM_DT_DS, strSourceApplicatorStepSize);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Distance of path along channel (mm) between adjacent (potential) dwell positions.
	 * @return String - value of Manufacturer of  Source Applicator Step Size.
	 */
	public String getSourceApplicatorStepSize()
	{
		Object objValue = getValue(0x300A,0x02A0);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Validates all the attributes in {@link RecordedSourceApplicatorSequenceItem} 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,0x0142))
				throw new MandatoryMissingException("3008,0142");
			if(!contains(0x300A,0x0291))
				throw new ConditionFailedException("300A,0291");
			if(!contains(0x300A,0x0292))
				throw new ConditionFailedException("300A,0292");
			if(!contains(0x300A,0x0296))
				throw new ConditionFailedException("300A,0296");
			/*
			 * This condition is validated in Parent sequence item as it depeneds on attribute of RecordedChannelSequenceItem.
			 * Please refer Chapter 3 page no 528.
			 * Source Applicator Step Size (300A,02A0) Required if Source Movement Type (300A,0288) is STEPWISE.
			 */
		}
		catch(MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new ValidationException("Recorded Source Applicator Sequence Item",me);
		}
		catch( ConditionFailedException cfe)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(cfe.getMessage());
			}
			throw new ValidationException("Recorded Source Applicator Sequence Item",cfe);
		}
		return true;
	}
}
