/*******************************************************************************
 *  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.IDictionaryReader;
import cdac.medinfo.sdk.dcm30_04.commoninterface.ITransferSyntax;
import cdac.medinfo.sdk.dcm30_04.enums.EnumDetectorType;
import cdac.medinfo.sdk.dcm30_04.enums.EnumPlaneIdentification;
import cdac.medinfo.sdk.dcm30_04.exceptions.ConditionFailedException;
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 the methods to set, get and validate the attribute present in Contributing Sources Sequence .
 */

public class ContributingSourcesSequenceItem extends Item  
{

	private static final long serialVersionUID = 1L;

	/**
	 * This constructor creates the item and internal code sequence macro with default TransferSyntax 
	 */
	ContributingSourcesSequenceItem()
	{
		super();
	}

	GeneralContributingSourcesMacro objGeneralContributingSourcesMacro =null ;
	ContributingImageSourcesMacro  objContributingImageSourcesMacro = null ;
	/**
	 * This constructor creates the item and internal code sequence macro with default TransferSyntax 
	 * and default DictionaryReader
	 * @param objITransferSyntax specifies the DICOM Standard defined TransfeSyntax
	 */
	ContributingSourcesSequenceItem(ITransferSyntax objITransferSyntax)
	{
		super(objITransferSyntax);
	}

	/**
	 * This constructor creates ControlPointDeliverySequenceItem with specified TransferSyntax and
	 * specified DictionaryReader
	 * @param objTransferSyntax specifies the DICOM Standard defined TransfeSyntax
	 * @param objDictionaryReader specifies the DICOM Dictionary reader to be used 
	 */
	ContributingSourcesSequenceItem(ITransferSyntax objTransferSyntax, IDictionaryReader objDictionaryReader)
	{
		super(objTransferSyntax,objDictionaryReader);
	}
	/**
	 * Validates all the attributes in Contributing Sources Sequence Item 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
		{
			//validation of mandatory elements
			if(!contains(0x0018,0x1400))
				throw new ConditionFailedException("(0018,1400");
			if(!contains(0x0018,0x1401))
				throw new ConditionFailedException("0018,1401");
			if(!contains(0x0018,0x9457))
				throw new ConditionFailedException("0018,9457");
			if(!contains(0x0018,0x1164))
				throw new ConditionFailedException("0018,1164");
			if(!contains(0x0018,0x7004))
				throw new ConditionFailedException("(0018,7004");
			if(!contains(0x0018,0x700A))
				throw new ConditionFailedException("0018,700A");
			if(!contains(0x0018,0x700C))
				throw new ConditionFailedException("0018,700C");
			if(!contains(0x0018,0x700E))
				throw new ConditionFailedException("0018,700E");
			if(!contains(0x0018,0x7022))
				throw new ConditionFailedException("0018,7022");
		}
		catch( MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new ValidationException(this.getClass().getSimpleName(),me);
		}
		catch(ConditionFailedException ce)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ce.getMessage());
			}
			throw new ValidationException(this.getClass().getSimpleName(),ce); 
		}
		return true;
	}
	/**
	 * This method returns the Numeric Value Macro with proper initialization.
	 * @return - instance of {@link NumericValueMacro} 
	 */
	public GeneralContributingSourcesMacro getGeneralContributingSourcesMacro()
	{
		if(objGeneralContributingSourcesMacro == null)
		{
			objGeneralContributingSourcesMacro = new GeneralContributingSourcesMacro(this);
			return objGeneralContributingSourcesMacro;
		}
		return objGeneralContributingSourcesMacro;
	}
	/**
	 * This method returns the Contributing Image Sources Macro with proper initialization.
	 * @return - instance of {@link NumericValueMacro} 
	 */
	public ContributingImageSourcesMacro getContributingImageSourcesMacro()
	{
		if(objContributingImageSourcesMacro == null)
		{
			objContributingImageSourcesMacro = new ContributingImageSourcesMacro(this);
			return objContributingImageSourcesMacro;
		}
		return objContributingImageSourcesMacro;
	}
	/**
	 * Sets Physical distance measured at the receptor plane of the detector between thecenters of each pixel specified by a numeric pair - row spacing value (delimiter)column spacing value in mm.
	 * @param strImagerPixelSpacing String value of Imager Pixel Spacing.
	 */
	public void setImagerPixelSpacing(String[] strImagerPixelSpacing)
	{
		/*Accept the Imager Pixel Spacing in String format of type DS.
		 * 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(0x0018,0x1164, strImagerPixelSpacing,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x1164,DicomConstants.DCM_DT_DS, strImagerPixelSpacing);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Physical distance measured at the receptor plane of the detector between thecenters of each pixel specified by a numeric pair - row spacing value (delimiter)column spacing value in mm.
	 * @return String - value of Imager Pixel Spacing.
	 */
	public String[] getImagerPixelSpacing()
	{
		Object objValue = getValue(0x0018,0x1164);
		if(objValue != null)
			return (String[])objValue;
		return null;
	}
	/**
	 * Sets Acquisition Device ProcessingCode.
	 * @param strAcquisitionDeviceProcessingCode String value of Acquisition Device ProcessingCode.
	 */
	public void setAcquisitionDeviceProcessingCode(String strAcquisitionDeviceProcessingCode)
	{
		/* Accept the Acquisition Device ProcessingCode 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(0x0018,0x1401, strAcquisitionDeviceProcessingCode,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x1401,DicomConstants.DCM_DT_LO, strAcquisitionDeviceProcessingCode);

		addDataElement(objIDataElement);
	}

	/**
	 * Return Acquisition Device ProcessingCode.
	 * @return String - value of Acquisition Device ProcessingCode.
	 */
	public String getAcquisitionDeviceProcessingCode()
	{
		Object objValue = getValue(0x0018,0x1401);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	/**
	 * Sets Acquisition Device ProcessingDescription.
	 * @param strAcquisitionDeviceProcessingDescription String value of Acquisition Device ProcessingDescription.
	 */
	public void setAcquisitionDeviceProcessingDescription(String strAcquisitionDeviceProcessingDescription)
	{
		/* Accept the Acquisition Device ProcessingDescription 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(0x0018,0x1400, strAcquisitionDeviceProcessingDescription,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x1400,DicomConstants.DCM_DT_LO, strAcquisitionDeviceProcessingDescription);

		addDataElement(objIDataElement);
	}

	/**
	 * Return Acquisition Device ProcessingDescription.
	 * @return String - value of Acquisition Device ProcessingDescription.
	 */
	public String getAcquisitionDeviceProcessingDescription()
	{
		Object objValue = getValue(0x0018,0x1400);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	/**
	 * Sets Identification of the plane used to acquire this image.
	 * Sequence (300A,01B4) is sent.
	 * Defined Terms:
	 * 1) MONOPLANE.
	 * 2)  PLANE A
	 * 3) PLANE B
	 * @param enumPlaneIdentification enum value of PlaneIdentification.
	 */
	public void setPlaneIdentification(EnumPlaneIdentification enumPlaneIdentification)
	{
		/* Accept the PlaneIdentification in String format of type CS.
		 * 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(0x0018,0x9457, enumPlaneIdentification.getValue(),getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x9457,DicomConstants.DCM_DT_CS, enumPlaneIdentification.getValue());

		addDataElement(objIDataElement);
	}
	/**
	 * Return PlaneIdentification. 
	 * @return String - value of PlaneIdentification.
	 */
	public String getPlaneIdentification()
	{
		Object objValue = getValue(0x0018,0x9457);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets the Detector Type Defined in Terms of TEXT,NUMERIC,CODE,DATE,TIME,PNAME
	 * @param enumDetectorType Specifies type of the value encoded in this Item
	 */
	public void setDetectorType(EnumDetectorType enumDetectorType)
	{
		/* Accept the Detector Type in String format of type CS.
		 * 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(0x0018, 0x7004, enumDetectorType,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0018, 0x7004,DicomConstants.DCM_DT_CS, enumDetectorType);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves type of the value encoded in this Item
	 * @return String - Type of the value encoded in this Item
	 */
	public String getDetectorType()
	{
		Object objValue = getValue(0x0018, 0x7004);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	/**
	 * Sets the DetectorID.
	 * @param strDetectorID
	 */
	public void setDetectorID(String strDetectorID)
	{
		/* Accept the DetectorID 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
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0018, 0x700A, strDetectorID,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0018, 0x700A,DicomConstants.DCM_DT_SH, strDetectorID);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves The ID or serial number of the detector used to acquire this image
	 * @return String - value of Energy Window Name.
	 */
	public String getDetectorID()
	{
		Object objValue = getValue(0x0018, 0x700A);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	/**
	 * Sets Date of print Last Detector Calibration.
	 * @param strDateofLastDetectorCalibration String value of Date of print job creation.
	 */
	public void setDateofLastDetectorCalibration(String strDateofLastDetectorCalibration)
	{
		/* Accept the Creation Date 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
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0018,0x700C,strDateofLastDetectorCalibration,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0018,0x700C,DicomConstants.DCM_DT_DA, strDateofLastDetectorCalibration);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Date of print job creation.
	 * @return String - value of Date of print job creation.
	 */
	public String getDateofLastDetectorCalibration()
	{
		Object objValue = getValue(0x0018,0x700C);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Sets Time when the printer was last calibrated.
	 * @param strTimeofLastDetectorCalibration String array of Time when the printer was last calibrated.
	 */
	public void setTimeofLastDetectorCalibration(String[] strTimeofLastDetectorCalibration)
	{
		/* Accept Time of Last Calibration 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
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0018,0x700E,strTimeofLastDetectorCalibration,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0018,0x700E,DicomConstants.DCM_DT_TM,strTimeofLastDetectorCalibration);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Time when the printer was last calibrated.
	 * @return String - value of Time when the printer was last calibrated.
	 */
	public String[] getTimeofLastDetectorCalibration()
	{
		Object objValue = getValue(0x0018,0x700E);
		if(objValue != null)
			return (String[])objValue;
		return null;
	}
	/**
	 * Sets DetectorElementSpacing.Uniquely identifies DetectorElementSpacing.
	 * @param strDetectorElementSpacing String value of Detector Temperature.
	 */
	public void setDetectorElementSpacing(String strDetectorElementSpacing)
	{
		/* Accept the DetectorElementSpacing in String format of type DS.
		 * 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(0x0018,0x7022, strDetectorElementSpacing,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0018,0x7022,DicomConstants.DCM_DT_DS, strDetectorElementSpacing);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Detector Detector Element Spacing.
	 * @return String - value of Detector Detector Element Spacing.
	 */
	public String getDetectorElementSpacing()
	{
		Object objValue = getValue(0x0018,0x7022);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
}
