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

// TODO: Auto-generated Javadoc
/**
 \* This module specifies attributes that identify a Patient as a clinical trial Subject.
 */
public class ClinicalTrialSubjectModule extends Module  
{


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


	/**
	 * Instantiates a new clinical trial subject module.
	 * 
	 * @param objIDataSet the obj i data set
	 */
	public ClinicalTrialSubjectModule(IDataSet objIDataSet)
	{
		super(objIDataSet);
	}

	static
	{
		objSingleCollection = new SingleCollection<ITag>();
		objSingleCollection.add(new Tag(0x0012,0x0010));
		objSingleCollection.add(new Tag(0x0012,0x0020));
		objSingleCollection.add(new Tag(0x0012,0x0021));
		objSingleCollection.add(new Tag(0x0012,0x0030));
		objSingleCollection.add(new Tag(0x0012,0x0031));
		objSingleCollection.add(new Tag(0x0012,0x0040));
		objSingleCollection.add(new Tag(0x0012,0x0042));
		objSingleCollection.add(new Tag(0x0012,0x0081));
		objSingleCollection.add(new Tag(0x0012,0x0082));
	}

	/**
	 * 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(0x0012,0x0010))
				throw new MandatoryMissingException("0012,0010");
			else if(!isPresent(0x0012,0x0020))
				throw new MandatoryMissingException("0012,0020");
			else if(!isPresent(0x0012,0x0021))
				throw new MandatoryMissingException("0012,0021");
			else if(!isPresent(0x0012,0x0030))
				throw new MandatoryMissingException("0012,0030");
			else if(!isPresent(0x0012,0x0031))
				throw new MandatoryMissingException("0012,0031");

			/*
			 * Validation Rules for conditional Elements
			 */
			//DataElement (0012,0040) should be present if (0012,0042) is absent.
			//DataElement (0012,0042) should be present if (0012,0040) is absent.
			else if((isPresent(0x0012,0x0040)) && (isPresent(0x0012,0x0042)))
				throw new ConditionFailedException("Condition Failed for Element 0x0012,0x0042 & 0x0012,0x0040");
			
			if(isPresent(0x0012,0x0082))
				if(!isPresent(0x0012,0x0081))
					throw new ConditionFailedException("0x0012,0x0081");
		}
		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;
	}

	/**
	 * The method is used to set The name of the clinical trial sponsor.
	 * 
	 * @param strClinicalTrialSponsorName : The name of the clinical trial sponsor.
	 */
	public void setClinicalTrialSponsorName(String strClinicalTrialSponsorName)
	{
		/*Accept the name of the clinical trial sponsor 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 Hash Map
		 */
		addDataElement(0x0012, 0x0010, DicomConstants.DCM_DT_LO, strClinicalTrialSponsorName);
	}

	/**
	 * The method is used to set The Identifier for the noted protocol.
	 * 
	 * @param strClinicalTrialProtocolID the str clinical trial protocol id
	 */
	public void setClinicalTrialProtocolID(String strClinicalTrialProtocolID)
	{
		/* Accept The Identifier for the noted protocol 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 Hash Map
		 */
		addDataElement(0x0012, 0x0020, DicomConstants.DCM_DT_LO, strClinicalTrialProtocolID);
	}

	/**
	 * The method is used to set The name of the clinical trial protocol.
	 * 
	 * @param strClinicalTrialProtocolName the str clinical trial protocol name
	 */
	public void setClinicalTrialProtocolName(String strClinicalTrialProtocolName)
	{
		/*Accept The name of the clinical trial protocol. 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 Hash Map
		 */
		addDataElement(0x0012, 0x0021, DicomConstants.DCM_DT_LO, strClinicalTrialProtocolName);
	}

	/**
	 * The method is used to set The identifier of the site responsible for
	 * submitting clinical trial data.
	 * 
	 * @param strClinicalTrialSiteID :The ID of the site responsible for submitting clinical trial data..
	 */
	public void setClinicalTrialSiteID(String strClinicalTrialSiteID)
	{
		/* Accept The identifier of the site responsible for
	       submitting clinical trial data 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 Hash Map
		 */
		addDataElement(0x0012, 0x0030, DicomConstants.DCM_DT_LO, strClinicalTrialSiteID);
	}

	/**
	 * The method is used to set The name of the site responsible for
	 * submitting clinical trial data.
	 * 
	 * @param strClinicalTrialSiteName :The name of the site responsible for submitting clinical trial data..
	 */
	public void setClinicalTrialSiteName(String strClinicalTrialSiteName)
	{
		/* Accept The name of the site responsible for
	       submitting clinical trial data 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 Hash Map
		 */
		addDataElement(0x0012, 0x0031, DicomConstants.DCM_DT_LO, strClinicalTrialSiteName);
	}

	/**
	 * The method is used to set identifier for the clinical trial subject.
	 * submitting clinical trial data.
	 * 
	 * @param strClinicalTrialSubjectID :The identifier for the clinical trial subject.
	 */
	public void setClinicalTrialSubjectID(String strClinicalTrialSubjectID)
	{
		/* Accept The ID  for the clinical trial subject.
	       submitting clinical trial data 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 Hash Map
		 */
		addDataElement(0x0012, 0x0040, DicomConstants.DCM_DT_LO, strClinicalTrialSubjectID);
	}

	/**
	 * The method is used to set identifier for subject for blinded evaluations.
	 * submitting clinical trial data.
	 * 
	 * @param strClinicalTrialSubjectReadingID :The identifier for subject for blinded evaluations
	 */
	public void setClinicalTrialSubjectReadingID(String strClinicalTrialSubjectReadingID)
	{
		/*Accept The ID for subject for blinded evaluations.
	       submitting clinical trial data 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 Hash Map
		 */
		addDataElement(0x0012, 0x0042, DicomConstants.DCM_DT_LO, strClinicalTrialSubjectReadingID);
	}
	
	/**
	 * The method is used to set Clinical Trial Protocol Ethics Committee Name.
	 * 
	 * @param strClinicalTrialProtocolEthicsCommitteeName value Clinical Trial Protocol Ethics Committee Name
	 */
	public void setClinicalTrialProtocolEthicsCommitteeName(String strClinicalTrialProtocolEthicsCommitteeName)
	{
		/*Accept The Clinical Trial Protocol Ethics Committee 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 Hash Map
		 */
		addDataElement(0x0012, 0x0081, DicomConstants.DCM_DT_LO, strClinicalTrialProtocolEthicsCommitteeName);
	}
	
	/**
	 * The method is used to set Clinical Trial Protocol Ethics Committee Approval Number.
	 * 
	 * @param strClinicalTrialProtocolEthicsCommitteeApprovalNumber value Clinical Trial Protocol Ethics Committee Approval Number
	 */
	public void setClinicalTrialProtocolEthicsCommitteeApprovalNumber(String strClinicalTrialProtocolEthicsCommitteeApprovalNumber)
	{
		/*Accept The Clinical Trial Protocol Ethics Committee Approval Number 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 Hash Map
		 */
		addDataElement(0x0012, 0x0082, DicomConstants.DCM_DT_LO, strClinicalTrialProtocolEthicsCommitteeApprovalNumber);
	}

	/**
	 * Gets the clinical trial sponsor name.
	 * 
	 * @return the clinical trial sponsor name
	 */
	public String getClinicalTrialSponsorName()
	{
		Object objValue = getValue(0x0012,0x0010);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Gets the clinical trial protocol name.
	 * 
	 * @return the clinical trial protocol name
	 */
	public String getClinicalTrialProtocolName()
	{
		Object objValue = getValue(0x0012,0x0021);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Gets the clinical trial protocol id.
	 * 
	 * @return the clinical trial protocol id
	 */
	public String getClinicalTrialProtocolID()
	{
		Object objValue = getValue(0x0012,0x0020);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Gets the clinical trial site id.
	 * 
	 * @return the clinical trial site id
	 */
	public String getClinicalTrialSiteID()
	{
		Object objValue = getValue(0x0012,0x0030);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Gets the clinical trial site name.
	 * 
	 * @return the clinical trial site name
	 */
	public String  getClinicalTrialSiteName()
	{
		Object objValue = getValue(0x0012,0x0031);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Gets the clinical trial subject id.
	 * 
	 * @return the clinical trial subject id
	 */
	public String getClinicalTrialSubjectID()
	{
		Object objValue = getValue(0x0012,0x0040);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Gets the clinical trial subject reading id.
	 * 
	 * @return the clinical trial subject reading id
	 */
	public String getClinicalTrialSubjectReadingID()
	{
		Object objValue = getValue(0x0012,0x0042);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Gets the Clinical Trial Protocol Ethics Committee Name.
	 * 
	 * @return the Clinical Trial Protocol Ethics Committee Name
	 */
	public String getClinicalTrialProtocolEthicsCommitteeName()
	{
		Object objValue = getValue(0x0012,0x0081);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Gets the Clinical Trial Protocol Ethics Committee Approval Number.
	 * 
	 * @return the Clinical Trial Protocol Ethics Committee Approval Number
	 */
	public String getClinicalTrialProtocolEthicsCommitteeApprovalNumber()
	{
		Object objValue = getValue(0x0012,0x0082);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

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


}
