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





/**
 * This item Introduces sequence describing treatment machine used for treatment delivery.
 *  Only a single Item shall be permitted in this Sequence.
 */

public class TreatmentMachineSequenceItem extends Item  
{
	


	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

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


	/**
	 * This constructor creates the item and internal code sequence macro with default TransferSyntax 
	 * and default DictionaryReader
	 * @param objITransferSyntax specifies the DICOM Standard defined TransfeSyntax
	 */
	TreatmentMachineSequenceItem(ITransferSyntax objITransferSyntax)
	{
		super(objITransferSyntax);
	}

	/**
	 * This constructor creates TreatmentMachineSequenceItem with specified TransferSyntax and
	 * specified DictionaryReader
	 * @param objTransferSyntax specifies the DICOM Standard defined TransfeSyntax
	 * @param objDictionaryReader specifies the DICOM Dictionary reader to be used 
	 */
	TreatmentMachineSequenceItem(ITransferSyntax objTransferSyntax, IDictionaryReader objDictionaryReader)
	{
		super(objTransferSyntax,objDictionaryReader);
	}


	/**
	 * Sets User-defined name identifying treatment machine used for treatment delivery.
	 * @param strTreatmentMachineName String value of Treatment Machine Name.
	 */
	public void setTreatmentMachineName(String strTreatmentMachineName)
	{
		/*Accept the TreatmentMachineName 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(0x300A,0x00B2, strTreatmentMachineName,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x300A,0x00B2,DicomConstants.DCM_DT_SH, strTreatmentMachineName);

		addDataElement(objIDataElement);

	}
	/**
	 * Retrieves Treatment Machine Name.
	 * @return String - value of Treatment Machine Name
	 */
	public String getTreatmentMachineName()
	{
		Object objValue = getValue(0x300A,0x00B2);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets the Manufacturer of the equipment used for treatment delivery..
	 * @param strManufacturer String value of Manufacturer .
	 */
	public void setManufacturer(String strManufacturer)
	{
		/*Accept the Manufacturer 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(0x0008,0x0070, strManufacturer,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0008,0x0070,DicomConstants.DCM_DT_LO, strManufacturer);

		addDataElement(objIDataElement);


	}
	/**
	 * Retrieves Manufacturer of the equipment used for treatment delivery.
	 * @return String - value of Manufacturer.
	 */
	public String getManufacturer()
	{
		Object objValue = getValue(0x0008,0x0070);
		if(objValue != null)
			return objValue.toString();
		return null;
	}


	/**
	 * Sets Institution where the equipment is located that was used for treatment delivery.
	 * @param strInstitutionName String value of Institution Name .
	 */
	public void setInstitutionName(String strInstitutionName)
	{
		/*Accept the InstitutionName 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(0x0008,0x0080, strInstitutionName,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0008,0x0080,DicomConstants.DCM_DT_LO, strInstitutionName);

		addDataElement(objIDataElement);


	}
	/**
	 * Retrieves Institution where the equipment is located that was used for treatment delivery.
	 * @return String - value of Institution Name.
	 */
	public String getInstitutionName()
	{
		Object objValue = getValue(0x0008,0x0080);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Mailing address of the institution where the equipment is located that was used for treatment delivery.
	 * @param strInstitutionAddress String value of Institution Address .
	 */
	public void setInstitutionAddress(String strInstitutionAddress)
	{
		/*Accept the InstitutionAddress 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
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0008,0x0081, strInstitutionAddress,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0008,0x0081,DicomConstants.DCM_DT_ST, strInstitutionAddress);

		addDataElement(objIDataElement);


	}
	/**
	 * Retrieves Mailing address of the institution where the equipment is located that was used for treatment delivery.
	 * @return String - value of Institution Address.
	 */
	public String getInstitutionAddress()
	{
		Object objValue = getValue(0x0008,0x0081);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Institution where the equipment is located that was used for treatment delivery.
	 * @param strInstitutionalDepartmentName String value of Institution Name .
	 */
	public void setInstitutionalDepartmentName(String strInstitutionalDepartmentName)
	{
		/*Accept the InstitutionalDepartmentName 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(0x0008,0x1040, strInstitutionalDepartmentName,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0008,0x1040,DicomConstants.DCM_DT_LO, strInstitutionalDepartmentName);

		addDataElement(objIDataElement);


	}
	/**
	 * Retrieves Institution where the equipment is located that was used for treatment delivery.
	 * @return String - value of Institution Name.
	 */
	public String getInstitutionalDepartmentName()
	{
		Object objValue = getValue(0x0008,0x1040);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Manufacturer's model name of the equipment used for treatment delivery.
	 * @param strManufacturersModelName String value of Manufacturers Model Name .
	 */
	public void setManufacturersModelName(String strManufacturersModelName)
	{
		/*Accept the ManufacturersModelName 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(0x0008,0x1090, strManufacturersModelName,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0008,0x1090,DicomConstants.DCM_DT_LO, strManufacturersModelName);

		addDataElement(objIDataElement);


	}
	/**
	 * Retrieves Manufacturer's model name of the equipment used for treatment delivery.
	 * @return String - value of Manufacturers Model Name.
	 */
	public String getManufacturersModelName()
	{
		Object objValue = getValue(0x0008,0x1090);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Manufacturer's serial number of the equipment used for treatment delivery.
	 * @param strDeviceSerialNumber  String value of Device Serial Number.
	 */
	public void setDeviceSerialNumber(String strDeviceSerialNumber)
	{
		/*Accept the DeviceSerialNumber 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,0x1000, strDeviceSerialNumber,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x1000,DicomConstants.DCM_DT_LO, strDeviceSerialNumber);

		addDataElement(objIDataElement);


	}
	/**
	 * Retrieves Manufacturer's serial number of the equipment used for treatment delivery.
	 * @return String - value of Device Serial Number.
	 */
	public String getDeviceSerialNumber()
	{
		Object objValue = getValue(0x0018,0x1000);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Validates all the attributes in  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
		{
			if(!contains(0x300A,0x00B2))
				throw new MandatoryMissingException("300A,00B2");
		}
		catch( MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new InvalidItemException(this.getClass().getSimpleName(),me);
		}
		return true;
	}


}

