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

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

import cdac.medinfo.sdk.dcm30_04.enums.EnumCommandField;
import cdac.medinfo.sdk.dcm30_04.services.dicomnetcommons.ICommandElement;
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.DIMSEResponse;
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.ResourceBundleWrapper;

// TODO: Auto-generated Javadoc
/**
 * This class contains attributes of  C-Move response.
 * This response is created when C-Move operation needs to be performed.
 * The C-MOVE service is invoked by a DIMSE-service-user to move the information for one or
 * more Composite SOP Instances from a peer DIMSE-service-user, to a third party DIMSE
 * service-user, based upon the Attributes supplied by the invoking DIMSE-service-user.
 * The C-Move service returns dataset for each match.
 */
public class CMoveRSP extends DIMSEResponse 
{

	/**
	 * Instantiates a new C-Move-RSP.Creates Dicom Message which contain data set and command set for C-Move RQ.
	 */
	public CMoveRSP()
	{
		
	}

	/**
	 * Instantiates a new C-Move-RSP.
	 * Sets the Dicom Message in C-Move response as provided by user.
	 * @param objDicomMessge object of dicom message to be set in C-Move response.
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	public CMoveRSP(DicomMessage objDicomMessge) throws IOException
	{
		objICommandSet = objDicomMessge.getCommandSet();
		objIDataSet = objDicomMessge.getDataSet();
	}

	/**
	 * Sets The number of remaining sub-operations to be invoked for this C-MOVE operation.
	 * @param shNumberOfRemainingSubOperations short value of number of remaining sub-operations to be invoked for this C-MOVE operation.
	 */
	public void setNumberOfRemainingSubOperations(short shNumberOfRemainingSubOperations)
	{
		ICommandElement objICommandElement = null;
		objICommandElement = new CommandElement(0x0000, 0x1020,shNumberOfRemainingSubOperations);
		objICommandSet.addCommandElement(objICommandElement);
	}

	/**
	 * Retrieves The number of remaining sub-operations to be invoked for this C-MOVE operation.
	 * @return short - value of number of remaining sub-operations to be invoked for this C-MOVE operation.
	 */
	public short getNumberOfRemainingSubOperations()
	{
		Object objValue = getValue(0x0000,0x1020);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}

	/**
	 * Sets The number of remaining sub-operations to be invoked for this C-MOVE operation.
	 * @param shNumberOfCompleteSubOperations short value of number of remaining sub-operations to be invoked for this C-MOVE operation.
	 */
	public void setNumberOfCompleteSubOperations(short shNumberOfCompleteSubOperations)
	{
		ICommandElement objICommandElement = null;
		objICommandElement = new CommandElement(0x0000, 0x1021,shNumberOfCompleteSubOperations);
		objICommandSet.addCommandElement(objICommandElement);
	}

	/**
	 * Retrieves The number of remaining sub-operations to be invoked for this C-MOVE operation.
	 * @return short - value of number of remaining sub-operations to be invoked for this C-MOVE operation.
	 */
	public short getNumberOfCompleteSubOperations()
	{
		Object objValue = getValue(0x0000,0x1021);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}

	/**
	 * Sets The number of failed sub-operations for this C-MOVE operation.
	 * @param shNumberOfFailedSubOperations short value of number of failed sub-operations for this C-MOVE operation.
	 */
	public void setNumberOfFailedSubOperations(short shNumberOfFailedSubOperations)
	{
		ICommandElement objICommandElement = null;
		objICommandElement = new CommandElement(0x0000, 0x1022,shNumberOfFailedSubOperations);
		objICommandSet.addCommandElement(objICommandElement);
	}

	/**
	 * Retrieves The number of failed sub-operations for this C-MOVE operation.
	 * @return short - value of number of failed sub-operations  for this C-MOVE operation.
	 */
	public short getNumberOfFailedSubOperations()
	{
		Object objValue = getValue(0x0000,0x1022);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}

	/**
	 * Sets The number of warning sub-operations  for this C-MOVE operation.
	 * @param shNumberOfWarningSubOperations short value of number of warning sub-operations for this C-MOVE operation.
	 */
	public void setNumberOfWarningSubOperations(short shNumberOfWarningSubOperations)
	{
		ICommandElement objICommandElement = null;
		objICommandElement = new CommandElement(0x0000, 0x1023,shNumberOfWarningSubOperations);
		objICommandSet.addCommandElement(objICommandElement);
	}

	/**
	 * Retrieves The number of warning sub-operations for this C-MOVE operation.
	 * @return short - value of number of warning sub-operations for this C-MOVE operation.
	 */
	public short getNumberOfWarningSubOperations()
	{
		Object objValue = getValue(0x0000,0x1023);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	
	/**
	 * Validates the response 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();
		//Checks for service type.
		if(getCommandType() != EnumCommandField.C_MOVE_RSP)
			throw new InvalidDICOMMessageException(ResourceBundleWrapper.getValue("exceptions","RQNotMatched",new Locale(DicomConfig.createInstance().getLocale())) + getCommandType().name());
		
		return true;
	}
}
