/*******************************************************************************
 *  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.dicommodel.modules;


import cdac.medinfo.sdk.dcm30_04.baselibs.DicomV3Factory;
import cdac.medinfo.sdk.dcm30_04.baselibs.ReferencedPerformedProcedureStepSequence;
import cdac.medinfo.sdk.dcm30_04.baselibs.ReferencedSeriesSequence;
import cdac.medinfo.sdk.dcm30_04.baselibs.Tag;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDataElement;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDataSet;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDicomValue;
import cdac.medinfo.sdk.dcm30_04.commoninterface.ITag;
import cdac.medinfo.sdk.dcm30_04.exceptions.InvalidModuleException;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConstants;
import cdac.medinfo.sdk.dcm30_04.utils.ISingleCollection;
import cdac.medinfo.sdk.dcm30_04.utils.SingleCollection;

// TODO: Auto-generated Javadoc
/**
 \* This module defines the Attributes of Instance Availability Notification Module.
 */
public class InstanceAvailabilityNotificationModule extends Module  
{

 
 /**
  * The obj single collection.
  */
 public static ISingleCollection<ITag> objSingleCollection = null;
	 

	/**
	 * Instantiates a new instance availability notification module.
	 * 
	 * @param objIDataSet the obj i data set
	 */
	public InstanceAvailabilityNotificationModule(IDataSet objIDataSet)
	{
		super(objIDataSet);
	}

	static
	{
		objSingleCollection = new SingleCollection<ITag>();
		objSingleCollection.add(new Tag(0x0008,0x1111));
		objSingleCollection.add(new Tag(0x0020,0x000D));
		objSingleCollection.add(new Tag(0x0008,0x1115));

	}


	/**
	 * Retrieves pre-initialized object of ReferencedPerformedProcedureStepSequence.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 ReferencedPerformedProcedureStepSequence - instance of Referenced Performed Procedure Step Sequence
	 */
	public ReferencedPerformedProcedureStepSequence getReferencedPerformedProcedureStepSequence()
	{
		ReferencedPerformedProcedureStepSequence objReferencedPerformedProcedureStepSequence = (ReferencedPerformedProcedureStepSequence)getDicomValue(0x0008,0x1111);
		if(objReferencedPerformedProcedureStepSequence == null)
		{
			/*
			 * If the sequence object is not present in the DataSet, a new sequence object is created and
			 * the TransferSyntax and DictionaryReader is set into it as it is set in the Module. This ensures that 
			 * the DataElements getting added in the DataSet follow the same TransferSyntax and use the same
			 * DictionaryReader object as that of the module.
			 */
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			objReferencedPerformedProcedureStepSequence = (ReferencedPerformedProcedureStepSequence)objDicomV3Factory.createSequence(0x0008,0x1111, objITransferSyntax, objIDictionaryReader);
			
			// This will add Data Element of SQ to the DataSet.
			addDataElement(0x0008,0x1111, DicomConstants.DCM_DT_SQ, objReferencedPerformedProcedureStepSequence.getValue());
		}
		return objReferencedPerformedProcedureStepSequence;
	}


	/**
	 * Sets Unique identifier for the Study of which all the Instances referenced in this notification are part.
	 * 
	 * @param strStudyInstanceUID - String Unique identifier
	 */	
	public void setStudyInstanceUID(String strStudyInstanceUID)
	{
		/* Accept Unique identifier for the Study 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 Hash Map
		 */
		addDataElement(0x0020, 0x000D, DicomConstants.DCM_DT_UI, strStudyInstanceUID);
	}

	/**
	 * Return Unique identifier for the Study of which all the Instances referenced in this notification are part.
	 * 
	 * @return String - String Unique identifier
	 */
	public String getStudyInstanceUID()
	{
		IDataElement objIDataElement =objIDataSet.getDataForTag(0x0020,0x000D);
		if(objIDataElement == null)
			return null;
		IDicomValue objIDicomValue = objIDataElement.getDicomValue();
		if(objIDicomValue == null)
			return null;
		if(objIDicomValue.getValue()==null)
			return null;
		return objIDicomValue.getValue().toString();
	}


	/**
	 * Retrieves pre-initialized object of ReferencedSeriesSequence.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 ReferencedSeriesSequence - instance of Referenced Series Sequence
	 */
	public ReferencedSeriesSequence getReferencedSeriesSequence()
	{
		ReferencedSeriesSequence objReferencedSeriesSequence = (ReferencedSeriesSequence)getDicomValue(0x0008,0x1115);
		if(objReferencedSeriesSequence == null)
		{
			/*
			 * If the sequence object is not present in the DataSet, a new sequence object is created and
			 * the TransferSyntax and DictionaryReader is set into it as it is set in the Module. This ensures that 
			 * the DataElements getting added in the DataSet follow the same TransferSyntax and use the same
			 * DictionaryReader object as that of the module.
			 */
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			objReferencedSeriesSequence = (ReferencedSeriesSequence)objDicomV3Factory.createSequence(0x0008,0x1115, objITransferSyntax, objIDictionaryReader);
			
			// This will add Data Element of SQ to the DataSet.
			addDataElement(0x0008,0x1115, DicomConstants.DCM_DT_SQ, objReferencedSeriesSequence.getValue());
		}
		return objReferencedSeriesSequence;
	}


	/* 
	 * For normalized IODs, the type(Mandatory/Optional) of a Module depends upon the
	 * Requirement Type which defines if this module is required by a SCP class or an
	 * SCP class. Hence,the validation of Modules in a Normalized IOD totally depends 
	 * upon the decision that the is it an SCU/SCP that needs the module.Also, Type of the Modules
	 * describes the type of the Attributes inside the Module. Ex: Mandatory Modules contain Attributes that are
	   included in all SOP Instances employing this IOD.
	 * Decision on implementation of this kind of validation is yet to be done. Hence,
	 * i am putting a empty validate method.	 
	 */
	/* (non-Javadoc)
	 * @see cdac.medinfo.sdk.dcm30_04.dicommodel.modules.Module#validateModule()
	 */
	public boolean validateModule()throws InvalidModuleException
	{
		return true;
	}

	/* 
	 * This method is used by the Module Iterator to iterate through the DataSet and return only
	 * those elements that belong to the respective Module class whose this method is called.
	 */
	/**
	 * This method returns the vector of keys representing the DataElements
	 * that belong to this module.
	 * 
	 * @return collection of Tags - returns the vector containing the collection of tags
	 */
	public ISingleCollection<ITag> getVectorOfKeys()
	{
		return objSingleCollection;
	}
}
