/*******************************************************************************
 *  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.ImplantTemplateGroupMembersSequence;
import cdac.medinfo.sdk.dcm30_04.baselibs.ImplantTemplateGroupTargetAnatomySequence;
import cdac.medinfo.sdk.dcm30_04.baselibs.ImplantTemplateGroupVariationDimensionSequence;
import cdac.medinfo.sdk.dcm30_04.baselibs.ReplacedImplantTemplateGroupSequence;
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.ConditionFailedException;
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;
/**
 \* This module describes attributes in Implant Template Group Module.
 */
public class ImplantTemplateGroupModule extends Module
{
	/**
	 * The obj single collection.
	 */
	public static ISingleCollection<ITag> objSingleCollection = null;


	/**
	 * Instantiates a Implant Template Group Module.
	 * 
	 * @param objIDataSet the obj i data set
	 */
	public ImplantTemplateGroupModule(IDataSet objIDataSet)
	{
		super(objIDataSet);
	}

	static
	{
		objSingleCollection = new SingleCollection<ITag>();
		objSingleCollection.add(new Tag(0x0068, 0x6226));
		objSingleCollection.add(new Tag(0x0078, 0x0001));
		objSingleCollection.add(new Tag(0x0078, 0x0010));
		objSingleCollection.add(new Tag(0x0078, 0x0020));
		objSingleCollection.add(new Tag(0x0078, 0x0024));
		objSingleCollection.add(new Tag(0x0078, 0x0026));
		objSingleCollection.add(new Tag(0x0078, 0x0028));
		objSingleCollection.add(new Tag(0x0078, 0x002A));
		objSingleCollection.add(new Tag(0x0078, 0x00B0));


	}

	/**
	 * 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 - status indicating success or failure
	 * 
	 * @throws InvalidModuleException - If validation of Module fails
	 */
	public  boolean validateModule() throws InvalidModuleException
	{

		try
		{		
		/*
		 * Validation Rules for Mandatory Elements
		 */
			
			if(!isPresent(0x0068, 0x6226))
				throw new MandatoryMissingException("0068,6226");
			if(!isPresent(0x0078, 0x0001))
				throw new MandatoryMissingException("0078,0001");
			if(!isPresent(0x0078, 0x0020))
				throw new MandatoryMissingException("0078,0020");
			if(!isPresent(0x0078, 0x0024))
				throw new MandatoryMissingException("0078,0024");
			if(!isPresent(0x0078, 0x0026))
				throw new MandatoryMissingException("0078,0026");
			if(!isPresent(0x0078, 0x002A))
				throw new MandatoryMissingException("0078,002A");
			if(!isPresent(0x0078, 0x00B0))
				throw new MandatoryMissingException("0078,002B0");
	
		}
		catch(MandatoryMissingException mme)
		{
			if(DicomConfig.createInstance().isLoggingOn())
				DicomLogger.getLogger().log(mme.getMessage());

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

		}
		catch (ConditionFailedException cfe)
		{
			if(DicomConfig.createInstance().isLoggingOn())
				DicomLogger.getLogger().log(cfe.getMessage());

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

		}
		return true;
	}
	
	/**
	 * Sets Effective DateTime
	 * @param strEffectiveDateTime String value of Effective DateTime
	 */
	public void setEffectiveDateTime(String strEffectiveDateTime)
	{
		/* Accept the Effective DateTime of type DT.
		 * 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(0x0068, 0x6226,DicomConstants.DCM_DT_DT,strEffectiveDateTime);
	}

	/**
	 * Retrieves Effective DateTime
	 * @return String - value of Effective DateTime
	 */
	public String getEffectiveDateTime()
	{
		Object objValue = getValue(0x0068, 0x6226);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	/**
	 * Sets Implant Template Group Name.
	 * @param strImplantTemplateGroupName String value of Implant Template Group Name.
	 */
	public void setImplantTemplateGroupName(String strImplantTemplateGroupName)
	{
		/* Accept the Implant Template Group 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(0x0078, 0x0001,DicomConstants.DCM_DT_LO,strImplantTemplateGroupName);
	}

	/**
	 * Return Implant Template Group Name
	 * @return String - value of Implant Template Group Name
	 */
	public String getImplantTemplateGroupName()
	{
		Object objValue = getValue(0x0078, 0x0001);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	/**
	 * Sets Implant Template Group Description
	 * @param strImplantTemplateGroupDescription String value of Implant Template Group Description
	 */
	public void setImplantTemplateGroupDescription(String strImplantTemplateGroupDescription)
	{
		/*Accept the ImplantTemplateGroupDescription 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(0x0078, 0x0010,DicomConstants.DCM_DT_ST,strImplantTemplateGroupDescription);
	}

	/**
	 * Retrieves Implant Template Group Description
	 * @return String value of Implant Template Group Description
	 */
	public String getImplantTemplateGroupDescription()
	{
		Object objValue = getValue(0x0078, 0x0010);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	/**
	 * Sets Implant Template Group Issuer
	 * @param strImplantTemplateGroupIssuer String value of Implant Template Group Issuer
	 */
	public void setImplantTemplateGroupIssuer(String strImplantTemplateGroupIssuer)
	{
		/* Accept the Implant Template Group Issuer 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(0x0078, 0x0020,DicomConstants.DCM_DT_LO,strImplantTemplateGroupIssuer);
	}

	/**
	 * Return Implant Template Group Issuer
	 * @return String - value of Implant Template Group Issuer
	 */
	public String getImplantTemplateGroupIssuer()
	{
		Object objValue = getValue(0x0078, 0x0020);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Sets Implant Template Group Version
	 * @param strImplantTemplateGroupVersion String value of Implant Template Group Version
	 */
	public void setImplantTemplateGroupVersion(String strImplantTemplateGroupVersion)
	{
		/* Accept the Implant Template Group Issuer 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(0x0078, 0x0024,DicomConstants.DCM_DT_LO,strImplantTemplateGroupVersion);
	}

	/**
	 * Return Implant Template Group Version
	 * @return String - value of Implant Template Group Version
	 */
	public String getImplantTemplateGroupVersion()
	{
		Object objValue = getValue(0x0078, 0x0024);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Retrieves pre-initialized object of Replaced Implant Template GroupSequence. 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 ReplacedImplantTemplateGroupSequence - Replaced Implant Template GroupSequence.
	 */
	public ReplacedImplantTemplateGroupSequence getReplacedImplantTemplateGroupSequence()
	{
		ReplacedImplantTemplateGroupSequence objReplacedImplantTemplateGroupSequence = (ReplacedImplantTemplateGroupSequence)getDicomValue(0x0078, 0x0026);
		if(objReplacedImplantTemplateGroupSequence == 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();
			objReplacedImplantTemplateGroupSequence = (ReplacedImplantTemplateGroupSequence)objDicomV3Factory.createSequence(0x0078, 0x0026, objITransferSyntax, objIDictionaryReader);
			
			// This will add Data Element of SQ to the DataSet.
			addDataElement(0x0078, 0x0026, DicomConstants.DCM_DT_SQ, objReplacedImplantTemplateGroupSequence.getValue());
		}
		return objReplacedImplantTemplateGroupSequence;
	}
	/**
	 * Retrieves pre-initialized object of Implant Template Group TargetAnatomy 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 ImplantTemplateGroupTargetAnatomySequence - Implant Template Group TargetAnatomy Sequence.
	 */
	public ImplantTemplateGroupTargetAnatomySequence getImplantTemplateGroupTargetAnatomySequence()
	{
		ImplantTemplateGroupTargetAnatomySequence objImplantTemplateGroupTargetAnatomySequence = (ImplantTemplateGroupTargetAnatomySequence)getDicomValue(0x0078, 0x0028);
		if(objImplantTemplateGroupTargetAnatomySequence == 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();
			objImplantTemplateGroupTargetAnatomySequence = (ImplantTemplateGroupTargetAnatomySequence)objDicomV3Factory.createSequence(0x0078, 0x0028, objITransferSyntax, objIDictionaryReader);
			
			// This will add Data Element of SQ to the DataSet.
			addDataElement(0x0078, 0x0028, DicomConstants.DCM_DT_SQ, objImplantTemplateGroupTargetAnatomySequence.getValue());
		}
		return objImplantTemplateGroupTargetAnatomySequence;
	}
	
	/**
	 * Retrieves pre-initialized object of Implant Template Group MembersSequence. 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 ImplantTemplateGroupMembersSequence - Implant Template Group MembersSequence.
	 */
	public ImplantTemplateGroupMembersSequence getImplantTemplateGroupMembersSequence()
	{
		ImplantTemplateGroupMembersSequence objImplantTemplateGroupMembersSequence = (ImplantTemplateGroupMembersSequence)getDicomValue(0x0078, 0x002A);
		if(objImplantTemplateGroupMembersSequence == 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();
			objImplantTemplateGroupMembersSequence = (ImplantTemplateGroupMembersSequence)objDicomV3Factory.createSequence(0x0078, 0x002A, objITransferSyntax, objIDictionaryReader);
			
			// This will add Data Element of SQ to the DataSet.
			addDataElement(0x0078, 0x002A, DicomConstants.DCM_DT_SQ, objImplantTemplateGroupMembersSequence.getValue());
		}
		return objImplantTemplateGroupMembersSequence;
	}
	
	
	/**
	 * Retrieves pre-initialized object of Implant Template Group VariationDimension 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 ImplantTemplateGroupVariationDimensionSequence - Implant Template Group VariationDimension Sequence.
	 */
	public ImplantTemplateGroupVariationDimensionSequence getImplantTemplateGroupVariationDimensionSequence()
	{
		ImplantTemplateGroupVariationDimensionSequence objImplantTemplateGroupVariationDimensionSequence = (ImplantTemplateGroupVariationDimensionSequence)getDicomValue(0x0078, 0x00B0);
		if(objImplantTemplateGroupVariationDimensionSequence == 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();
			objImplantTemplateGroupVariationDimensionSequence = (ImplantTemplateGroupVariationDimensionSequence)objDicomV3Factory.createSequence(0x0078, 0x00B0, objITransferSyntax, objIDictionaryReader);
			
			// This will add Data Element of SQ to the DataSet.
			addDataElement(0x0078, 0x00B0, DicomConstants.DCM_DT_SQ, objImplantTemplateGroupVariationDimensionSequence.getValue());
		}
		return objImplantTemplateGroupVariationDimensionSequence;
	}

	
	/* (non-Javadoc)
	 * @see cdac.medinfo.sdk.dcm30_04.dicommodel.modules.Module#getVectorOfKeys()
	 */
	public ISingleCollection<ITag> getVectorOfKeys()
	{
		return objSingleCollection;
	}
}
