/*******************************************************************************
 *  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.exceptions.ConditionFailedException;
import cdac.medinfo.sdk.dcm30_04.exceptions.InvalidItemException;
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 set and get methods for all the attributes in the Coding Scheme Identification Sequence 
 */
public class CodingSchemeIdentificationSequenceItem extends Item  
{
	

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

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

	/**
	 * This constructor creates the item and internal code sequence macro with default TransferSyntax 
	 * @param objTransferSyntax specifies the DICOM Standard defined TransfeSyntax
	 */
	CodingSchemeIdentificationSequenceItem(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 
	 */
	CodingSchemeIdentificationSequenceItem(ITransferSyntax objTransferSyntax, IDictionaryReader objDictionaryReader)
	{
		super(objTransferSyntax,objDictionaryReader);
	}

	/**
	 * Sets the value of a Coding Scheme Designator, used in this SOP Instance, which is being mapped.
	 * @param strCodingSchemeDesignator value of a Coding Scheme Designator
	 */
	public void setCodingSchemeDesignator(String strCodingSchemeDesignator)
	{
		/*Accept the Coding Scheme Designator 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(0x0008, 0x0102, strCodingSchemeDesignator,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0008, 0x0102,DicomConstants.DCM_DT_SH, strCodingSchemeDesignator);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves value of a Coding Scheme Designator
	 * @return String value of a Coding Scheme Designator
	 */
	public String getCodingSchemeDesignator()
	{
		Object objValue = getValue(0x0008, 0x0102);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets the name of the external registry where further definition of the identified coding
	 * scheme may be obtained.
	 * @param strCodingSchemeRegistry name of the external registry
	 */
	public void setCodingSchemeRegistry(String strCodingSchemeRegistry)
	{
		/*Accept the Coding Scheme Registry 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(0x0008, 0x0112, strCodingSchemeRegistry,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0008, 0x0112,DicomConstants.DCM_DT_LO, strCodingSchemeRegistry);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Coding Scheme Registry.
	 * @return String - value of name of the external registry
	 */
	public String getCodingSchemeRegistry()
	{
		Object objValue = getValue(0x0008, 0x0112);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets the coding scheme UID identifier. Required if coding scheme is identified by an ISO 8824 object identifier compatible
	 * with the UI VR.
	 * @param strCodingSchemeUID coding scheme UID identifier
	 */
	public void setCodingSchemeUID(String strCodingSchemeUID)
	{
		/*Accept the Coding Scheme UID in String format of type UI.
		 * 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, 0x010C, strCodingSchemeUID,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0008, 0x010C,DicomConstants.DCM_DT_UI, strCodingSchemeUID);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves coding scheme UID identifier.
	 * @return String - value of coding scheme UID identifier.
	 */
	public String getCodingSchemeUID()
	{
		Object objValue = getValue(0x0008, 0x010C);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets the coding scheme identifier as defined in an external registry.
	 * @param strCodingSchemeExternalID coding scheme identifier.
	 */
	public void setCodingSchemeExternalID(String strCodingSchemeExternalID)
	{
		/*Accept the Coding Scheme External ID 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(0x0008, 0x0114, strCodingSchemeExternalID,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0008, 0x0114,DicomConstants.DCM_DT_ST, strCodingSchemeExternalID);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves the coding scheme identifier as defined in an external registry.
	 * @return String value of the coding scheme identifier
	 */
	public String getCodingSchemeExternalID()
	{
		Object objValue = getValue(0x0008, 0x0114);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets the coding scheme full common name.
	 * @param strCodingSchemeName coding scheme full common name
	 */
	public void setCodingSchemeName(String strCodingSchemeName)
	{
		/*Accept the Coding Scheme Name 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(0x0008, 0x0115, strCodingSchemeName,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0008, 0x0115,DicomConstants.DCM_DT_ST, strCodingSchemeName);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves the coding scheme full common name.
	 * @return String value of coding scheme full common name.
	 */
	public String getCodingSchemeName()
	{
		Object objValue = getValue(0x0008, 0x0115);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets the coding scheme version associated with the Coding Scheme Designator (0008,0102).
	 * @param strCodingSchemeVersion- String value of coding scheme version
	 */
	public void setCodingSchemeVersion(String strCodingSchemeVersion)
	{
		/*Accept the Coding Scheme Version 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(0x0008, 0x0103, strCodingSchemeVersion,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0008, 0x0103,DicomConstants.DCM_DT_SH, strCodingSchemeVersion);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves coding scheme version associated with the Coding Scheme Designator (0008,0102).
	 * @return String - value of coding scheme version.
	 */
	public String getCodingSchemeVersion()
	{
		Object objValue = getValue(0x0008, 0x0103);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets name of the organization responsible for the Coding Scheme. May include organizational contact information.
	 * @param strResponsibleOrganization -String value of Name of the organization responsible for the Coding Scheme.
	 */
	public void setResponsibleOrganization(String strResponsibleOrganization)
	{
		/*Accept the Responsible Organization 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(0x0008, 0x0116, strResponsibleOrganization,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0008, 0x0116,DicomConstants.DCM_DT_ST, strResponsibleOrganization);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves name of the organization responsible for the Coding Scheme.
	 * @return String - value of Name of the organization responsible for the Coding Scheme. 
	 */
	public String getResponsibleOrganization()
	{
		Object objValue = getValue(0x0008, 0x0116);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Validates all the attributes in AcquisitionContextSequenceItem 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
	{
		/*
		 * Condition for (0008,0112),(0008,010C) not understood. PDF 3, Pg. No. 727,728.
		 */
		try{
			if(!contains(0x0008,0x0102))
				throw new MandatoryMissingException("0008,0102");
			else if(contains(0x0008,0x0114) && contains(0x0008,0x010C))
				throw new ConditionFailedException("conditional Element 0040,010C not required for Tag 0008,0114") ;
		}
		catch(ConditionFailedException ce)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ce.getMessage());
			}
			throw new InvalidItemException(this.getClass().getSimpleName(),ce); 
		}
		catch( MandatoryMissingException mme)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(mme.getMessage());
			}
			throw new InvalidItemException(this.getClass().getSimpleName(),mme);
		}

		return true;
	}

}
