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

/**
 * This class defines the required fields to create response in processing of N-Create Service.
 * It provides the required methods for all mandatory and optional fields of N-Create Response.  
 */
public class NCreateRSP extends DIMSEResponse 
{
	/**
	 * Instantiates a new N-Create Response. Creates default Dicom Message for N-Create Response.
	 */
	public NCreateRSP()
	{
	}

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

	/**
	 * Sets Affected SOP Instance UID . Contains the UID of the SOP Instance stored.
	 * @param strAffectedSOPInstanceUID String value of Affected SOP Instance UID.
	 */
	public void setAffectedSOPInstanceUID(String strAffectedSOPInstanceUID)
	{
		ICommandElement objICommandElement = null;
		objICommandElement = new CommandElement(0x0000, 0x1000,strAffectedSOPInstanceUID);
		objICommandSet.addCommandElement(objICommandElement);
	}

	/**
	 * Retrieves Affected SOP Instance UID . Contains the UID of the SOP Instance stored.
	 * @return String - value of Affected SOP Instance UID .
	 */
	public String getAffectedSOPInstanceUID()
	{
		Object objValue = getValue(0x0000, 0x1000);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * 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.N_CREATE_RSP)
			throw new InvalidDICOMMessageException(ResourceBundleWrapper.getValue("exceptions","RQNotMatched",new Locale(DicomConfig.createInstance().getLocale())) + getCommandType().name());
		
		return true;
	}
}
