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

import java.io.IOException;
import java.util.Locale;

import cdac.medinfo.sdk.dcm30_04.commoninterface.IDataSet;
import cdac.medinfo.sdk.dcm30_04.enums.EnumCommandField;
import cdac.medinfo.sdk.dcm30_04.enums.EnumSOPClassUIDs;
import cdac.medinfo.sdk.dcm30_04.exceptions.MandatoryMissingException;
import cdac.medinfo.sdk.dcm30_04.services.dicomnetcommons.EnumActionTypeID;
import cdac.medinfo.sdk.dcm30_04.services.dicomnetcommons.ICommandElement;
import cdac.medinfo.sdk.dcm30_04.services.dicomnetcommons.InvalidCommandSetException;
import cdac.medinfo.sdk.dcm30_04.services.dicomnetcommons.InvalidDICOMMessageException;
import cdac.medinfo.sdk.dcm30_04.services.dimsecommons.CommandElement;
import cdac.medinfo.sdk.dcm30_04.services.dimsecommons.DIMSERequest;
import cdac.medinfo.sdk.dcm30_04.services.dimsecommons.DicomMessage;
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.ResourceBundleWrapper;

/**
 * This class provides required methods to generate Action Request. 
 */
public class NActionRQ extends DIMSERequest
{
	/**
	 * Instantiates a new NAction RQ. Creates default Dicom Message for NAction RQ.
	 */
	public NActionRQ()
	{
	}

	/**
	 * Instantiates a new NAction RQ when Dicom Message is given by user.
	 * Retrieves Command Set and Data Set from the DicomMessage and sets in NAction Request.
	 * @param objDicomMessage Object of DicomMessage from which NAction request is initialized.
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	public NActionRQ(DicomMessage objDicomMessage) throws IOException
	{
		objICommandSet = objDicomMessage.getCommandSet();
		objIDataSet = objDicomMessage.getDataSet();
	}

	/**
	 * Sets Requested SOP Class UID . 
	 * SOP Class UID associated with the operation.
	 * @param strRequestedSOPClassUID value of Requested SOP Class UID.
	 */
	public void setRequestedSOPClassUID(String strRequestedSOPClassUID)
	{
		ICommandElement objICommandElement = null;
		objICommandElement = new CommandElement(0x0000, 0x0003,strRequestedSOPClassUID);
		objICommandSet.addCommandElement(objICommandElement);
	}

	/**
	 * Retrieves Requested SOP Class UID . 
	 * SOP Class UID associated with the operation.
	 * @return String - value of Requested SOP Class UID .
	 */
	public String getRequestedSOPClassUID()
	{
		Object objValue = getValue(0x0000,0x0003);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets the UID of the SOP Instance to be retrieved.
	 * @param strRequestedSOPInstanceUID String value of Requested SOP Instance UID.
	 */
	public void setRequestedSOPInstanceUID(String strRequestedSOPInstanceUID)
	{
		ICommandElement objICommandElement = null;
		objICommandElement = new CommandElement(0x0000, 0x1001,strRequestedSOPInstanceUID);
		objICommandSet.addCommandElement(objICommandElement);
	}

	/**
	 * Retrieves the UID of the SOP Instance to be retrieved.
	 * @return String - value of Requested SOP Instance UID.
	 */
	public String getRequestedSOPInstanceUID()
	{
		Object objValue = getValue(0x0000,0x1001);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets ActionTypeID .
	 * This parameter specifies the type of to be performed.
	 * @param enumActionTypeID enum value of ActionTypeID.
	 */ 
	public void setActionTypeID(EnumActionTypeID enumActionTypeID)
	{
		ICommandElement objICommandElement = null;
		objICommandElement = new CommandElement(0x0000, 0x1008,enumActionTypeID.getValue());
		objICommandSet.addCommandElement(objICommandElement);
	}

	/**
	 * Retrieves value of ActionTypeID.
	 * @return short - value of ActionTypeID.
	 */
	public short getActionTypeID()
	{
		Object objValue = getValue(0x0000,0x1008);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}

	/**
	 * Generates Default RQ for given SOP class UID and Instance UID. This method returns null
	 * when dataset is null in case of some SOP Class UIDs.
	 * @param strSOPUID value of SOP Class UID.
	 * @param strSOPInstanceUID value of SOP Instance UID.
	 * @param IActionTypeID value of Action Type ID.
	 * @param objIDataSet instance of DataSet.
	 * @return objNActionRQ instance of objNActionRQ RQ.
	 */
	public static NActionRQ getDefaultRQ(String strSOPUID,String strSOPInstanceUID,EnumActionTypeID enumActionTypeID,IDataSet objIDataSet)
	{
		if(strSOPUID.equals(EnumSOPClassUIDs.PROCEDURALEVENTLOGGING.getValue()))
		{
			if(objIDataSet == null)
			{
				if(DicomConfig.createInstance().isLoggingOn())
				{
					DicomLogger objDicomLogger = DicomLogger.getLogger();
					objDicomLogger.log(ResourceBundleWrapper.getValue("warning","DataSetRequired",new Locale(DicomConfig.createInstance().getLocale())) + strSOPUID);
				}
				return null;
			}
		}
		NActionRQ objNActionRQ = new NActionRQ();
		objNActionRQ.setRequestedSOPClassUID(strSOPUID);
		objNActionRQ.setRequestedSOPInstanceUID(strSOPInstanceUID);
		objNActionRQ.setCommandField(EnumCommandField.N_ACTION_RQ);
		objNActionRQ.setActionTypeID(enumActionTypeID);
		if(objIDataSet != null && !objIDataSet.isEmpty())
		{
			objNActionRQ.setCommandDataSetType(DicomConstants.DCM_DATASET_PRESENT);
			objNActionRQ.setDataSet(objIDataSet);
		}
		else
			objNActionRQ.setCommandDataSetType(DicomConstants.DCM_DATASET_NOT_PRESENT);
		objNActionRQ.setMessageID(shMsgID++);
		objNActionRQ.setCommandGroupLength(objNActionRQ.getCommandSet().getCommandSetLength());

		//Validating RQ
		objNActionRQ.validate();
		return objNActionRQ;
	}
	
	/**
	 * Validates the request for their mandatory attributes only.
	 * @return true, if validation is successful.
	 * @throws InvalidDICOMMessageException If validation of mandatory fields fails.
	 */
	public boolean validate() throws InvalidDICOMMessageException
	{
		super.validate();
		try
		{		
			//Checks for service type.
			if(getCommandType() != EnumCommandField.N_ACTION_RQ)
				throw new InvalidDICOMMessageException(ResourceBundleWrapper.getValue("exceptions","RQNotMatched",new Locale(DicomConfig.createInstance().getLocale())) + getCommandType().name());
			
			if(!objICommandSet.isPresent(0x0000,0x0003))			// Requested SOP Class UID
				throw new MandatoryMissingException("0000,0003");
			else
			{
				if(objICommandSet.getValue(0x0000,0x0003) == null)
					throw new InvalidDICOMMessageException(ResourceBundleWrapper.getValue("exceptions", "NullValue",new Locale(DicomConfig.createInstance().getLocale())) + 0x0000 + "," + 0x0003);
			}

			if(!objICommandSet.isPresent(0x0000,0x1001))		// Requested SOP Instance UID
				throw new MandatoryMissingException("0000,1001");
			else
			{
				if(objICommandSet.getValue(0x0000,0x1001) == null)
					throw new InvalidDICOMMessageException(ResourceBundleWrapper.getValue("exceptions", "NullValue",new Locale(DicomConfig.createInstance().getLocale())) + 0x0000 + "," + 0x1001);
			}
			
			if(!objICommandSet.isPresent(0x0000,0x1008))		// Action Type ID
				throw new MandatoryMissingException("0000,1008");
			else
			{
				if(objICommandSet.getValue(0x0000,0x1008) == null)
					throw new InvalidDICOMMessageException(ResourceBundleWrapper.getValue("exceptions", "NullValue",new Locale(DicomConfig.createInstance().getLocale())) + 0x0000 + "," + 0x1008);
			}
		}
		catch(MandatoryMissingException mme)
		{
			InvalidCommandSetException objCommandSetException = new InvalidCommandSetException(mme);
			if(DicomConfig.createInstance().isLoggingOn())
				DicomLogger.getLogger().log(mme.getMessage());

			throw new InvalidDICOMMessageException(this.getClass().getSimpleName(),objCommandSetException);
		}	
		return true;
	}
}