/*******************************************************************************
 *  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.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 Printer Characteristics Sequence Item.
 */
public class PrinterCharacteristicsSequenceItem extends Item  
{
	

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

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

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

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

	/**
	 * Sets Date of print job creation.
	 * @param strCreationDate String value of Date of print job creation.
	 */
	public void setCreationDate(String strCreationDate)
	{
		/* 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(0x2100,0x0040,strCreationDate,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x2100,0x0040,DicomConstants.DCM_DT_DA, strCreationDate);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Date of print job creation.
	 * @return String - value of Date of print job creation.
	 */
	public String getCreationDate()
	{
		Object objValue = getValue(0x2100,0x0040);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Time of print job creation.
	 * @param strCreationTime String value of Time of print job creation.
	 */
	public void setCreationTime(String strCreationTime)
	{
		/* Accept the Creation Time 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(0x2100,0x0050,strCreationTime,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x2100,0x0050,DicomConstants.DCM_DT_TM, strCreationTime);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Time of print job creation.
	 * @return String - value of Time of print job creation.
	 */
	public String getCreationTime()
	{
		Object objValue = getValue(0x2100,0x0050);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Application Entity Title that issued the print operation.
	 * @param strOriginator String value of Application Entity Title that issued the print operation.
	 */
	public void setOriginator(String strOriginator)
	{
		/* Accept the Originator of type AE.
		 * 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(0x2100,0x0070,strOriginator,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x2100,0x0070,DicomConstants.DCM_DT_AE, strOriginator);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Application Entity Title that issued the print operation.
	 * @return String - value of Application Entity Title that issued the print operation.
	 */
	public String getOriginator()
	{
		Object objValue = getValue(0x2100,0x0070);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Application Entity Title that performed the print operation.
	 * @param strDestinationAE String value of Application Entity Title that performed the print operation.
	 */
	public void setDestinationAE(String strDestinationAE)
	{
		/* Accept the Destination AE of type AE.
		 * 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(0x2100,0x0140,strDestinationAE,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x2100,0x0140,DicomConstants.DCM_DT_AE, strDestinationAE);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Application Entity Title that performed the print operation.
	 * @return String - value of Application Entity Title that performed the print operation.
	 */
	public String getDestinationAE()
	{
		Object objValue = getValue(0x2100,0x0140);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets User defined name identifying the printer.
	 * @param strPrinterName String value of User defined name identifying the printer.
	 */
	public void setPrinterName(String strPrinterName)
	{
		/* Accept the Printer Name 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(0x2110,0x0030,strPrinterName,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x2110,0x0030,DicomConstants.DCM_DT_LO, strPrinterName);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves User defined name identifying the printer.
	 * @return String - value of User defined name identifying the printer.
	 */
	public String getPrinterName()
	{
		Object objValue = getValue(0x2110,0x0030);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Manufacturer of the printer.
	 * @param strManufacturer String value of Manufacturer of the printer..
	 */
	public void setManufacturer(String strManufacturer)
	{
		/* Accept the Manufacturer 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 printer.
	 * @return String - value of Manufacturer of the printer.
	 */
	public String getManufacturer()
	{
		Object objValue = getValue(0x0008,0x0070);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Manufacturer's Model Name of the printer.
	 * @param strManufacturerModelName String value of Manufacturer's Model Name of the printer.
	 */
	public void setManufacturerModelName(String strManufacturerModelName)
	{
		/* Accept the Manufacturer Model Name 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,strManufacturerModelName,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0008,0x1090,DicomConstants.DCM_DT_LO, strManufacturerModelName);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Manufacturer Model Name.
	 * @return String - value of Manufacturer Model Name.
	 */
	public String getManufacturerModelName()
	{
		Object objValue = getValue(0x0008,0x1090);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

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

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Manufacturer�s serial number of the printer.
	 * @return String - value of Manufacturer�s serial number of the printer.
	 */
	public String getDeviceSerialNumber()
	{
		Object objValue = getValue(0x0018,0x1000);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Manufacturer�s designation of software version of the printer.
	 * @param strArrSoftwareVersion String array of Manufacturer�s designation of Software Version of the printer.
	 */
	public void setSoftwareVersion(String[] strArrSoftwareVersion)
	{
		/* Accept Software Version 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,0x1020,strArrSoftwareVersion,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x1020,DicomConstants.DCM_DT_LO, strArrSoftwareVersion);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Manufacturer�s designation of software version of the printer.
	 * @return String[] - Manufacturer�s designation of Software Version of the printer.
	 */
	public String[] getSoftwareVersion()
	{
		Object objValue = getValue(0x0018,0x1020);
		if(objValue != null)
			return (String[])objValue;
		return null;
	}

	/**
	 * Sets Date when the printer was last calibrated.
	 * @param strDateOfLastCalibration String array of Date when the printer was last calibrated.
	 */
	public void setDateOfLastCalibration(String[] strDateOfLastCalibration)
	{
		/* Accept Date of Last Calibration 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,0x1200,strDateOfLastCalibration,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x1200,DicomConstants.DCM_DT_DA,strDateOfLastCalibration);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Date when the printer was last calibrated.
	 * @return String - value of Date when the printer was last calibrated.
	 */
	public String[] getDateOfLastCalibration()
	{
		Object objValue = getValue(0x0018,0x1200);
		if(objValue != null)
			return (String[])objValue;
		return null;
	}

	/**
	 * Sets Time when the printer was last calibrated.
	 * @param strTimeOfLastCalibration String array of Time when the printer was last calibrated.
	 */
	public void setTimeOfLastCalibration(String[] strTimeOfLastCalibration)
	{
		/* 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,0x1201,strTimeOfLastCalibration,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x1201,DicomConstants.DCM_DT_TM,strTimeOfLastCalibration);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Time when the printer was last calibrated.
	 * @return String - value of Time when the printer was last calibrated.
	 */
	public String[] getTimeOfLastCalibration()
	{
		Object objValue = getValue(0x0018,0x1201);
		if(objValue != null)
			return (String[])objValue;
		return null;
	}

	/**
	 * Validates all the attributes in {@link PrinterCharacteristicsSequenceItem} 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(0x2100,0x0070))
				throw new MandatoryMissingException("2100,0070");
			if(!contains(0x2100,0x0140))
				throw new MandatoryMissingException("2100,0140");
		}
		catch(MandatoryMissingException mme)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(mme.getMessage());
			}
			throw new ValidationException(this.getClass().getSimpleName(),mme);
		}
		return true;
	}
}
