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

import java.io.IOException;
import java.util.Locale;
import cdac.medinfo.sdk.dcm30_04.enums.EnumCommandField;
import cdac.medinfo.sdk.dcm30_04.exceptions.MandatoryMissingException;
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.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 contains attributes of  CCancelFind request.
 * This request is created when C-Find operation needs to be canceled.
 */
public class CCancelRQ extends DicomMessage 
{
	/**
	 * Instantiates a new C-Cancel-Find request.
	 */
	public CCancelRQ()
	{
		
	}

	/**
	 * Instantiates a new C-Cancel-Find request.
	 * Sets the Dicom Message in C-Cancel-Find request as provided by user.
	 * 
	 * @param objDicomMessge object of dicom message to be set in C-Cancel-Find request.
	 * 
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	public CCancelRQ(DicomMessage objDicomMessge) throws IOException
	{
		objICommandSet = objDicomMessge.getCommandSet();
		objIDataSet = objDicomMessge.getDataSet();
	}
	
	/**
	 * Sets Group Length i.e The even number of bytes from the end of the value
	 * field to the beginning of the next group
	 * 
	 * @param lGroupLength integer value of group length
	 */
	public void setCommandGroupLength(int lCommandGroupLength)
	{
		ICommandElement objICommandElement = null;
		objICommandElement = new CommandElement(0x0000, 0x0000,lCommandGroupLength);
		objICommandSet.addCommandElement(objICommandElement);
	}

	/**
	 * Retrieves the Group Length . The even number of bytes from the end of the
	 * value field to the beginning of the next group
	 * 
	 * @return int - value of Group Length
	 */
	public int getCommandGroupLength()
	{
		Object objValue = getValue(0x0000,0x0000);
		if(objValue != null)
			return Integer.parseInt(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}

	/**
	 * Sets Command Field. This field distinguishes the DIMSE-C operation
	 * conveyed by this Message. The value of this field shall be set to 0FFFH
	 * for the C-CANCEL-FIND-RQ Message.
	 * 
	 * @param enumCommandField  the enum command field
	 */ 
	public void setCommandField(EnumCommandField enumCommandField)
	{
		ICommandElement objICommandElement = null;
		objICommandElement = new CommandElement(0x0000, 0x0100,enumCommandField.getValue());
		objICommandSet.addCommandElement(objICommandElement);
	}

	/**
	 * Retrieves Command Field. This field distinguishes the DIMSE-C operation conveyed by this Message.
	 * 
	 * @return short - value of command field
	 */
	public short getCommandField()
	{
		Object objValue = getValue(0x0000,0x0100);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}

	/**
	 * This field indicates that no Data Set is present in the Message and shall
	 * be set to a value of 0101H.
	 * 
	 * @param shDataSetType short value of DataSet type.
	 */
	public void setCommandDataSetType(short shCommandDataSetType)
	{
		ICommandElement objICommandElement = null;
		objICommandElement = new CommandElement(0x0000, 0x0800,shCommandDataSetType);
		objICommandSet.addCommandElement(objICommandElement);
	}

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

	/**
	 * Sets Message ID Being Responded To. Shall be set to the value of the
	 * Message ID (0000,0110) field used in associated C-ECHO RQ Message.
	 * 
	 * @param shMessageIDBeingRespondedTo short value of Message ID Being Responded To.
	 */
	public void setMessageIDBeingRespondedTo(short shMessageIDBeingRespondedTo)
	{
		ICommandElement objICommandElement = null;
		objICommandElement = new CommandElement(0x0000, 0x0120,shMessageIDBeingRespondedTo);
		objICommandSet.addCommandElement(objICommandElement);
	}

	/**
	 * Retrieves Message ID Being Responded To. Shall be set to the value of the
	 * Message ID (0000,0110) field used in associated C-ECHO RQ Message.
	 * 
	 * @return short - value of Message ID Being Responded To.
	 */
	public short getMessageIDBeingRespondedTo()
	{
		Object objValue = getValue(0x0000,0x0120);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	
	/**
	 * Validates the CancelFind 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
	{
		try
		{	
			//Checks for service type.
			if(getCommandType() != EnumCommandField.C_CANCEL_RQ)
				throw new InvalidDICOMMessageException(ResourceBundleWrapper.getValue("exceptions","RQNotMatched",new Locale(DicomConfig.createInstance().getLocale())) + getCommandType().name());
			
			if(!objICommandSet.isPresent(0x0000,0x0120))			// Message ID Being Responded
				throw new MandatoryMissingException("0000,0120");
			else
			{
				if(objICommandSet.getValue(0x0000,0x0120) == null)
					throw new InvalidDICOMMessageException(ResourceBundleWrapper.getValue("exceptions", "NullValue",new Locale(DicomConfig.createInstance().getLocale())) + 0x0000 + "," + 0x0120);
			}
		}
		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;
	}
}
