/*******************************************************************************
 *  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.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 Specimen Description Sequence .
 */

public class SpecimenDescriptionSequenceItem extends PrimaryAnatomicStructureMacroDependentItem
{
	


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

	/**
	 * This constructor creates the item with default TransferSyntax 
	 */
	SpecimenDescriptionSequenceItem()
	{
		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
	 */
	SpecimenDescriptionSequenceItem(ITransferSyntax objITransferSyntax)
	{
		super(objITransferSyntax);
	}

	/**
	 * This constructor creates SpecimenDescriptionSequenceItem with specified TransferSyntax and
	 * specified DictionaryReader.
	 * @param objTransferSyntax specifies the DICOM Standard defined TransfeSyntax.
	 * @param objDictionaryReader specifies the DICOM Dictionary reader to be used.
	 */
	SpecimenDescriptionSequenceItem(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 SpecimenDescriptionSequenceItem 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,0x0551))
				throw new MandatoryMissingException("0040,0551");
			if(!contains(0x0040,0x0562))
				throw new MandatoryMissingException("0040,0562");
			if(!contains(0x0040,0x0554))
				throw new MandatoryMissingException("0040,0554");
			if(!contains(0x0040,0x0610))
				throw new MandatoryMissingException("0040,0610");
		}
		catch(MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new ValidationException(this.getClass().getSimpleName(),me);
		}
		return true;
	}
	
	/**
	 * Sets Specimen Identifier.
	 * @param strSpecimenIdentifier String value of Specimen Identifier.
	 */
	public void setSpecimenIdentifier(String strSpecimenIdentifier)
	{
		/* Accept the Specimen Identifier 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(0x0040,0x0551, strSpecimenIdentifier,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0040,0x0551,DicomConstants.DCM_DT_LO, strSpecimenIdentifier);

		addDataElement(objIDataElement);
	}

	/**
	 * Return Specimen Identifier.
	 * @return String - value of Specimen Identifier.
	 */
	public String getSpecimenIdentifier()
	{
		Object objValue = getValue(0x0040,0x0551);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Retrieves pre-initialized object of Issuer of the Specimen Identifier 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 IssueroftheSpecimenIdentifierSequence - instance of IssueroftheSpecimenIdentifierSequence. 
	 */
	public IssueroftheSpecimenIdentifierSequence getIssueroftheSpecimenIdentifierSequence()
	{
		IssueroftheSpecimenIdentifierSequence objIssueroftheSpecimenIdentifierSequence = null;
		IDataElement objIDataElement =  getDataForTag(0x0040, 0x0562);
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objIssueroftheSpecimenIdentifierSequence = (IssueroftheSpecimenIdentifierSequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objIssueroftheSpecimenIdentifierSequence = (IssueroftheSpecimenIdentifierSequence)objDicomV3Factory.createSequence(0x0040, 0x0562, objITransferSyntax, objIDictionaryReader);

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


	
	/**
	 * Sets a Specimen UID.
	 * @param strSpecimenUID String value of Specimen UID.
	 */
	public void setSpecimenUID(String strSpecimenUID)
	{
		/*Accept the Specimen 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(0x0400, 0x0554, strSpecimenUID,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0400, 0x0554,DicomConstants.DCM_DT_UI, strSpecimenUID);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves a Specimen UID.
	 * @return String - value of Specimen UID.
	 */
	public String getSpecimenUID()
	{
		Object objValue = getValue(0x0400, 0x0554);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Retrieves pre-initialized object of Specimen Type 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 SpecimenTypeCodeSequence - instance of SpecimenTypeCodeSequence. 
	 */
	public SpecimenTypeCodeSequence getSpecimenTypeCodeSequence()
	{
		SpecimenTypeCodeSequence objSpecimenTypeCodeSequence = null;
		IDataElement objIDataElement =  getDataForTag(0x0400, 0x059A);
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objSpecimenTypeCodeSequence = (SpecimenTypeCodeSequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objSpecimenTypeCodeSequence = (SpecimenTypeCodeSequence)objDicomV3Factory.createSequence(0x0400, 0x059A, objITransferSyntax, objIDictionaryReader);

			if(iVRTransferSyntax == DicomConstants.DCM_VR_IMPLICIT)
				objIDataElement = new ImplicitDE(0x0400, 0x059A, objSpecimenTypeCodeSequence.getValue(),objIDictionaryReader);
			else
				objIDataElement = new ExplicitDE(0x0400, 0x059A, DicomConstants.DCM_DT_SQ, objSpecimenTypeCodeSequence.getValue());
			objIDataElement.setTransferSyntax(objITransferSyntax);
			if(objIDataElement != null)
				addDataElement(objIDataElement);
		}
		return objSpecimenTypeCodeSequence;
	}
	
	/**
	 * Sets Specimen Short Description.
	 * @param strSpecimenShortDescription String value of Specimen Short Description.
	 */
	public void setSpecimenShortDescription(String strSpecimenShortDescription)
	{
		/* Accept the Specimen Short Description 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(0x0400,0x0600, strSpecimenShortDescription,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0400,0x0600,DicomConstants.DCM_DT_LO, strSpecimenShortDescription);

		addDataElement(objIDataElement);
	}

	/**
	 * Return Specimen Short Description.
	 * @return String - value of Specimen Short Description.
	 */
	public String getSpecimenShortDescription()
	{
		Object objValue = getValue(0x0400,0x0600);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Sets the Specimen Detailed Description.
	 * @param strSpecimenDetailedDescription String value of Specimen Detailed Description.
	 */
	public void setSpecimenDetailedDescription(String strSpecimenDetailedDescription)
	{
		/* Accept the Specimen Detailed Description in String format of type UT.
		 * 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, 0x0602, strSpecimenDetailedDescription,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0040, 0x0602,DicomConstants.DCM_DT_UT, strSpecimenDetailedDescription);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves the Specimen Detailed Description.
	 * @return String - value of the Specimen Detailed Description.
	 */
	public String getSpecimenDetailedDescription()
	{
		Object objValue = getValue(0x0040, 0x0602);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Retrieves pre-initialized object of Specimen Preparation 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 SpecimenPreparationSequence - instance of SpecimenPreparationSequence. 
	 */
	public SpecimenPreparationSequence getSpecimenPreparationSequence()
	{
		SpecimenPreparationSequence objSpecimenPreparationSequence = null;
		IDataElement objIDataElement =  getDataForTag(0x0040, 0x0610);
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objSpecimenPreparationSequence = (SpecimenPreparationSequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objSpecimenPreparationSequence = (SpecimenPreparationSequence)objDicomV3Factory.createSequence(0x0040, 0x0610, objITransferSyntax, objIDictionaryReader);

			if(iVRTransferSyntax == DicomConstants.DCM_VR_IMPLICIT)
				objIDataElement = new ImplicitDE(0x0040, 0x0610, objSpecimenPreparationSequence.getValue(),objIDictionaryReader);
			else
				objIDataElement = new ExplicitDE(0x0040, 0x0610, DicomConstants.DCM_DT_SQ, objSpecimenPreparationSequence.getValue());
			objIDataElement.setTransferSyntax(objITransferSyntax);
			if(objIDataElement != null)
				addDataElement(objIDataElement);
		}
		return objSpecimenPreparationSequence;
	}
	
	/**
	 * Retrieves pre-initialized object of Specimen Localization Content Item 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 SpecimenLocalizationContentItemSequence - instance of SpecimenLocalizationContentItemSequence. 
	 */
	public SpecimenLocalizationContentItemSequence getSpecimenLocalizationContentItemSequence()
	{
		SpecimenLocalizationContentItemSequence objSpecimenLocalizationContentItemSequence = null;
		IDataElement objIDataElement =  getDataForTag(0x0040, 0x0620); 
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objSpecimenLocalizationContentItemSequence = (SpecimenLocalizationContentItemSequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objSpecimenLocalizationContentItemSequence = (SpecimenLocalizationContentItemSequence)objDicomV3Factory.createSequence(0x0040, 0x0620, objITransferSyntax, objIDictionaryReader);

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