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

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

import cdac.medinfo.sdk.dcm30_04.commoninterface.IDataSet;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IItem;
import cdac.medinfo.sdk.dcm30_04.enums.EnumCommandField;
import cdac.medinfo.sdk.dcm30_04.enums.EnumSOPClassUIDs;
import cdac.medinfo.sdk.dcm30_04.services.dicomnetcommons.EnumImplOpsStatus;
import cdac.medinfo.sdk.dcm30_04.services.dimsecommons.DicomMessage;
import cdac.medinfo.sdk.dcm30_04.services.print.dbhelper.Print_DBAgent;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConfig;
import cdac.medinfo.sdk.dcm30_04.utils.DicomLogger;
import cdac.medinfo.sdk.dcm30_04.utils.ISingleCollection;
import cdac.medinfo.sdk.dcm30_04.utils.ResourceBundleWrapper;

/**
 * This class provides methods to proces N-Set Request of Basic Grayscale ImageBox SOP class and 
 * Basic Color Image box SOP class.
 */
class ImageBoxDataSinkImpl 
{
	/**
	 * This function process the Dicom Request of Updating the SOP instance UID.
	 * It create the SOP instance of the strValue and the DataSet provided in the DicomMessage.
	 */
	@SuppressWarnings("unchecked")
	short storeBasicGrayScale(Print_DBAgent objPrintDBAgent, DicomMessage objDicomMessage, String strValue) throws IOException, SQLException
	{
		EnumCommandField enumRequestType = objDicomMessage.getCommandType();
		switch(enumRequestType)
		{
		case N_SET_RQ :
		{
			String strSOPInstance = objDicomMessage.getCommandSet().getValue(0x0000,0x1001).toString();
			IDataSet objIDataSet = objDicomMessage.getDataSet();
			if(!objPrintDBAgent.isSOPInstanceExist(EnumSOPClassUIDs.BASICGRAYSCALEIMAGEBOX,strSOPInstance))
			{
				if(DicomConfig.createInstance().isLoggingOn())
				{
					DicomLogger objDicomLogger =DicomLogger.getLogger();
					objDicomLogger.log(ResourceBundleWrapper.getValue("exceptions","NoInstanceCreated", new Locale(DicomConfig.createInstance().getLocale())) + strSOPInstance);
				}
				return EnumImplOpsStatus.FAILURE.getValue();
			}
			else
			{
				if(objPrintDBAgent.updateSOPInstance(EnumSOPClassUIDs.BASICGRAYSCALEIMAGEBOX,objDicomMessage.getDataSet(),strSOPInstance))
				{
					if(objPrintDBAgent.updateSOPInstance(EnumSOPClassUIDs.BASICGRAYSCALEIMAGEBOX,objDicomMessage.getDataSet(),strSOPInstance))
					{
						ISingleCollection<IItem>objItemCollection = (ISingleCollection<IItem>)objIDataSet.getValue(0x2020,0x0130);
						if(objItemCollection != null && objItemCollection.size() !=0)
						{
							IItem itemImageOverlay = objItemCollection.get(0);
							String strImageOverlaySOPInstanceUID = (String)itemImageOverlay.getValue(0x0008, 0x1155);
							if(!objPrintDBAgent.updateRelationShipTable(EnumSOPClassUIDs.BASICGRAYSCALEIMAGEBOX,EnumSOPClassUIDs.BASICPRINTIMAGEOVERLAYBOXIOD, strSOPInstance, strImageOverlaySOPInstanceUID))
							{
								if(DicomConfig.createInstance().isLoggingOn())
								{
									DicomLogger objDicomLogger =DicomLogger.getLogger();
									objDicomLogger.log(ResourceBundleWrapper.getValue("exceptions","InstanceUpdationFailed", new Locale(DicomConfig.createInstance().getLocale())) + strSOPInstance);
								}
								return EnumImplOpsStatus.FAILURE.getValue();
							}
						}
						objItemCollection = (ISingleCollection<IItem>)objIDataSet.getValue(0x2050,0x0500);
						if(objItemCollection != null && objItemCollection.size() !=0)
						{
							IItem itemPresentationLUT = objItemCollection.get(0);
							String strPresentationLUTSOPInstanceUID = (String)itemPresentationLUT.getValue(0x0008, 0x1155);
							if(!objPrintDBAgent.updateRelationShipTable(EnumSOPClassUIDs.BASICGRAYSCALEIMAGEBOX,EnumSOPClassUIDs.PRESENTATIONLUTIOD, strSOPInstance, strPresentationLUTSOPInstanceUID))
							{
								if(DicomConfig.createInstance().isLoggingOn())
								{
									DicomLogger objDicomLogger =DicomLogger.getLogger();
									objDicomLogger.log(ResourceBundleWrapper.getValue("exceptions","InstanceUpdationFailed", new Locale(DicomConfig.createInstance().getLocale())) + strSOPInstance);
								}
								return EnumImplOpsStatus.FAILURE.getValue();
							}
						}
						if(DicomConfig.createInstance().isLoggingOn())
						{
							DicomLogger objDicomLogger = DicomLogger.getLogger();
							objDicomLogger.log(ResourceBundleWrapper.getValue("status", "Print.ImageBoxInstanceUpdated", new Locale(DicomConfig.createInstance().getLocale())) + strSOPInstance);
						}
						return EnumImplOpsStatus.SUCCESS.getValue();
					}
					else
					{
						if(DicomConfig.createInstance().isLoggingOn())
						{
							DicomLogger objDicomLogger =DicomLogger.getLogger();
							objDicomLogger.log(ResourceBundleWrapper.getValue("exceptions","InstanceUpdationFailed", new Locale(DicomConfig.createInstance().getLocale())) + strSOPInstance);
						}
						return EnumImplOpsStatus.FAILURE.getValue();
					}
				}
			}
		}
		default : 
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger objDicomLogger =DicomLogger.getLogger();
				objDicomLogger.log(ResourceBundleWrapper.getValue("exceptions","ServiceRequestNotSupported", new Locale(DicomConfig.createInstance().getLocale())) + enumRequestType);
			}
			return EnumImplOpsStatus.FAILURE.getValue();
		}
		}
	}

	/**
	 * This function process the Dicom Request of Updating the SOP instance UID.
	 * It create the SOP instance of the strValue and the DataSet provided in the DicomMessage.
	 */
	@SuppressWarnings("unchecked")
	short storeBasicColorImage(Print_DBAgent objPrintDBAgent, DicomMessage objDicomMessage, String strValue) throws IOException, SQLException
	{
		EnumCommandField enumRequestType = objDicomMessage.getCommandType();
		switch(enumRequestType)
		{
		case N_SET_RQ :
		{
			String strSOPInstance = objDicomMessage.getCommandSet().getValue(0x0000,0x1001).toString();
			IDataSet objIDataSet = objDicomMessage.getDataSet();
			if(!objPrintDBAgent.isSOPInstanceExist(EnumSOPClassUIDs.BASICCOLORIMAGEBOX,strSOPInstance))
			{
				if(DicomConfig.createInstance().isLoggingOn())
				{
					DicomLogger objDicomLogger =DicomLogger.getLogger();
					objDicomLogger.log(ResourceBundleWrapper.getValue("exceptions","NoInstanceCreated", new Locale(DicomConfig.createInstance().getLocale())) + strSOPInstance);
				}
				return EnumImplOpsStatus.FAILURE.getValue();
			}
			else
			{
				if(objPrintDBAgent.updateSOPInstance(EnumSOPClassUIDs.BASICCOLORIMAGEBOX,objDicomMessage.getDataSet(),strSOPInstance))
				{
					ISingleCollection<IItem>objItemCollection = (ISingleCollection<IItem>)objIDataSet.getValue(0x2020,0x0130);
					if(objItemCollection != null && objItemCollection.size() !=0)
					{
						IItem itemImageOverlay = objItemCollection.get(0);
						String strImageOverlaySOPInstanceUID = (String)itemImageOverlay.getValue(0x0008, 0x1155);
						if(!objPrintDBAgent.updateRelationShipTable(EnumSOPClassUIDs.BASICCOLORIMAGEBOX,EnumSOPClassUIDs.BASICPRINTIMAGEOVERLAYBOXIOD, strSOPInstance, strImageOverlaySOPInstanceUID))
						{
							if(DicomConfig.createInstance().isLoggingOn())
							{
								DicomLogger objDicomLogger =DicomLogger.getLogger();
								objDicomLogger.log(ResourceBundleWrapper.getValue("exceptions","InstanceUpdationFailed", new Locale(DicomConfig.createInstance().getLocale())) + strSOPInstance);
							}
							return EnumImplOpsStatus.FAILURE.getValue();
						}
					}
				}
				if(DicomConfig.createInstance().isLoggingOn())
				{
					DicomLogger objDicomLogger = DicomLogger.getLogger();
					objDicomLogger.log(ResourceBundleWrapper.getValue("status", "Print.BasicColorImageInstanceUpdated", new Locale(DicomConfig.createInstance().getLocale())) + strSOPInstance);
				}
				return EnumImplOpsStatus.SUCCESS.getValue();
			}
		}
		default : 
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger objDicomLogger =DicomLogger.getLogger();
				objDicomLogger.log(ResourceBundleWrapper.getValue("exceptions","ServiceRequestNotSupported", new Locale(DicomConfig.createInstance().getLocale())) + enumRequestType);
			}
			return EnumImplOpsStatus.FAILURE.getValue();
		}
		}
	}
}
