/*******************************************************************************
 *  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.PredecessorStructureSetSequence;
import cdac.medinfo.sdk.dcm30_04.baselibs.ReferencedFrameofReferenceSequence;
import cdac.medinfo.sdk.dcm30_04.baselibs.StructureSetROISequence;
import cdac.medinfo.sdk.dcm30_04.baselibs.Tag;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDataSet;
import cdac.medinfo.sdk.dcm30_04.commoninterface.ITag;

import cdac.medinfo.sdk.dcm30_04.exceptions.InvalidModuleException;
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;
import cdac.medinfo.sdk.dcm30_04.utils.ISingleCollection;
import cdac.medinfo.sdk.dcm30_04.utils.SingleCollection;

// TODO: Auto-generated Javadoc
/**
 * A structure set defines a set of areas of significance.
 * Each area can be associated with a Frame of Reference and zero or more images.
 */
public class StructureSetModule extends Module   
{


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


	/**
	 * Instantiates a new structure set module.
	 * 
	 * @param objIDataSet the obj i data set
	 */
	public StructureSetModule(IDataSet objIDataSet)
	{
		super(objIDataSet);
	}

	static
	{
		objSingleCollection = new SingleCollection<ITag>();
		objSingleCollection.add(new Tag(0x0020,0x0013));
		objSingleCollection.add(new Tag(0x3006,0x0002));
		objSingleCollection.add(new Tag(0x3006,0x0004));
		objSingleCollection.add(new Tag(0x3006,0x0006));
		objSingleCollection.add(new Tag(0x3006,0x0008));
		objSingleCollection.add(new Tag(0x3006,0x0009));
		objSingleCollection.add(new Tag(0x3006,0x0010));
		objSingleCollection.add(new Tag(0x3006,0x0020));
	}

	/**
	 * This method validates each Attribute in the Module according to the Type
	 * of the Attribute.Attributes are of 5 types.
	 * Type 1,Type 2 are Mandatory:
	 * Mandatory Attributes have to present in the Module.
	 * Type 1C,2C are Conditional:
	 * Conditional Attributes' presence depend on a condition.
	 * If the condition is satisfied the Attribute has to be present.
	 * Type 3 are Optional
	 * Optional Attributes can be added as and when required.
	 * 
	 * @return boolean - returns validation status as success or failure
	 * 
	 * @throws InvalidModuleException - If validation of Module fails
	 */
	public  boolean validateModule() throws InvalidModuleException
	{

		try
		{		
			if(!isPresent(0x3006,0x0002))
				throw new MandatoryMissingException("Missing Mandatory DataElement 3006,0002");
			if(!isPresent(0x3006,0x0008))
				throw new MandatoryMissingException("Missing Mandatory DataElement 3006,0008");
			if(!isPresent(0x3006,0x0009))
				throw new MandatoryMissingException("Missing Mandatory DataElement 3006,0009");
		}
		catch(MandatoryMissingException mme)
		{
			if(DicomConfig.createInstance().isLoggingOn())
				DicomLogger.getLogger().log(mme.getMessage());

			
				throw new InvalidModuleException(this.getClass().getSimpleName(),mme);

		}
		return true;
	}


	/**
	 * Sets Structure Set Label.
	 * 
	 * @param strStructureSetLabel - String value of Structure Set Label.
	 */
	public void setStructureSetLabel(String strStructureSetLabel)
	{
		/* Accept Structure Set Label 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
		 */
		addDataElement(0x3006,0x0002, DicomConstants.DCM_DT_SH, strStructureSetLabel);
	}

	/**
	 * Retrieves Structure Set Label.
	 * 
	 * @return the structure set label
	 */
	public String getStructureSetLabel()
	{Object objectValue = getValue(0x3006,0x0002);
		if(objectValue!=null)
			return objectValue.toString();
		return null;
	}

	/**
	 * Sets Structure Set Name.
	 * 
	 * @param strStructureSetName - String value of Structure Set Name.
	 */
	public void setStructureSetName(String strStructureSetName)
	{
		/* Accept Structure Set Name 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
		 */
		addDataElement(0x3006,0x0004, DicomConstants.DCM_DT_LO, strStructureSetName);
	}

	/**
	 * Retrieves Structure Set Name.
	 * 
	 * @return the structure set name
	 */
	public String getStructureSetName()
	{Object objectValue = getValue(0x3006,0x0004);
		if(objectValue!=null)
			return objectValue.toString();
		return null;
	}

	/**
	 * Sets Structure Set Description.
	 * 
	 * @param strStructureSetDescription - String value of Structure Set Description.
	 */
	public void setStructureSetDescription(String strStructureSetDescription)
	{
		/* Accept Structure Set 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
		 */
		addDataElement(0x3006,0x0006, DicomConstants.DCM_DT_ST, strStructureSetDescription);
	}

	/**
	 * Retrieves Structure Set Description.
	 * 
	 * @return the structure set description
	 */
	public String getStructureSetDescription()
	{Object objectValue = getValue(0x3006,0x0006);
		if(objectValue!=null)
			return objectValue.toString();
		return null;
	}

	/**
	 * Sets Instance Number.
	 * 
	 * @param strInstanceNumber - String value of Instance Number.
	 */
	public void setInstanceNumber(String strInstanceNumber)
	{
		/* Accept Instance Numberin String format 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
		 */
		addDataElement(0x0020, 0x0013, DicomConstants.DCM_DT_IS, strInstanceNumber);
	}

	/**
	 * Retrieves Instance Number.
	 * 
	 * @return the instance number
	 */
	public String getInstanceNumber()
	{Object objectValue = getValue(0x0020,0x0013);
		if(objectValue!=null)
			return objectValue.toString();
		return null;
	}

	/**
	 * Sets Structure Set Date.
	 * 
	 * @param strStructureSetDate - String value of Structure Set Date.
	 */
	public void setStructureSetDate(String strStructureSetDate)
	{
		/* Accept Structure Set Name in String format of type DA.
		 * 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
		 */
		addDataElement(0x3006,0x0008, DicomConstants.DCM_DT_DA, strStructureSetDate);
	}

	/**
	 * Retrieves Structure Set Date.
	 * 
	 * @return the structure set date
	 */
	public String getStructureSetDate()
	{Object objectValue = getValue(0x3006,0x0008);
		if(objectValue!=null)
			return objectValue.toString();
		return null;
	}

	/**
	 * Sets Structure Set Time.
	 * 
	 * @param strStructureSetTime - String value of Structure Set Time.
	 */
	public void setStructureSetTime(String strStructureSetTime)
	{
		/* Accept Structure Set Name in String format of type TM.
		 * 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
		 */
		addDataElement(0x3006,0x0009, DicomConstants.DCM_DT_TM, strStructureSetTime);
	}

	/**
	 * Retrieves Structure Set Time.
	 * 
	 * @return the structure set time
	 */
	public String getStructureSetTime()
	{Object objectValue = getValue(0x3006,0x0009);
		if(objectValue!=null)
			return objectValue.toString();
		return null;
	}


	/**
	 * Retrieves sequence of items describing  Frames of Reference in which the ROIs are defined.
	 * 
	 * @return - instance of {@link ReferencedFrameofReferenceSequence}.
	 */
	public ReferencedFrameofReferenceSequence getReferencedFrameofReferenceSequence()
	{
		ReferencedFrameofReferenceSequence objReferencedFrameofReferenceSequence = (ReferencedFrameofReferenceSequence)getDicomValue(0x3006,0x0010);
		if(objReferencedFrameofReferenceSequence == 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();
			objReferencedFrameofReferenceSequence = (ReferencedFrameofReferenceSequence)objDicomV3Factory.createSequence(0x3006,0x0010, objITransferSyntax, objIDictionaryReader);
			
			// This will add Data Element of SQ to the DataSet.
			addDataElement(0x3006,0x0010, DicomConstants.DCM_DT_SQ, objReferencedFrameofReferenceSequence.getValue());
		}
		return objReferencedFrameofReferenceSequence;
	}


	/**
	 * Introduces sequence of ROIs for current Structure Set.
	 * 
	 * @return - instance of {@link StructureSetROISequence}.
	 */
	public StructureSetROISequence getStructureSetROISequence()
	{
		StructureSetROISequence objStructureSetROISequence = (StructureSetROISequence)getDicomValue(0x3006,0x0020);
		if(objStructureSetROISequence == 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();
			objStructureSetROISequence = (StructureSetROISequence)objDicomV3Factory.createSequence(0x3006,0x0020, objITransferSyntax, objIDictionaryReader);
			
			// This will add Data Element of SQ to the DataSet.
			addDataElement(0x3006,0x0020, DicomConstants.DCM_DT_SQ, objStructureSetROISequence.getValue());
		}
		return objStructureSetROISequence;
	}

	/**
	 * Introduces sequence of Predecessor Structure Set 
	 * @return - instance of {@link StructureSetROISequence}.
	 */
	public PredecessorStructureSetSequence getPredecessorStructureSetSequence()
	{
		PredecessorStructureSetSequence objPredecessorStructureSetSequence = (PredecessorStructureSetSequence)getDicomValue(0x3006,0x0020);
		if(objPredecessorStructureSetSequence == 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();
			objPredecessorStructureSetSequence = (PredecessorStructureSetSequence)objDicomV3Factory.createSequence(0x3006,0x0018, objITransferSyntax, objIDictionaryReader);
			
			// This will add Data Element of SQ to the DataSet.
			addDataElement(0x3006,0x0018, DicomConstants.DCM_DT_SQ, objPredecessorStructureSetSequence.getValue());
		}
		return objPredecessorStructureSetSequence;
	}
	/**
	 * This method returns the vector of keys representing the DataElements
	 * that belong to this module.
	 * 
	 * @return vector of Tags - returns the vector containing the collection of tags.
	 */
	public ISingleCollection<ITag> getVectorOfKeys()
	{
		return objSingleCollection;
	}
}
