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

/**
 * This class processes Dicom Request of Type N-Action request for Print Queue Management.
 */
class PrintQueueManagementSinkImpl 
{
	@SuppressWarnings("unused")
	/**
	 * Populates , updates and deletes the SOP Instance information in Database
	 * @param objIDataSet instance of IDataSet
	 * @param objPrintDBAgent
	 * @param objDicomMessage instance of IDicomMessage 
	 * @param strValue SOP instance value which is to be created , updated or deleted.
	 * @return short - shows status value which will be used in generation of response
	 * 				Refer EnumImplOpsStatus for supported response status values.
	 * @throws IOException
	 * @throws SQLException
	 */
	short store(Print_DBAgent objPrintDBAgent, DicomMessage objDicomMessage, String strValue) throws IOException, SQLException
	{
		EnumCommandField enumRequestType = objDicomMessage.getCommandType();
		switch(enumRequestType)
		{
		case N_ACTION_RQ :
		{
			ICommandSet objICommandSet = objDicomMessage.getCommandSet();
			IDataSet objIDataSet = objDicomMessage.getDataSet();
			Object objValue = objICommandSet.getValue(0x0000,0x1008);
			String strPrintJobID = null;
			String strOwnerID = null;
			if(objValue != null)
			{
				short shActionTypeID = Short.parseShort(objValue.toString());
				objValue = objIDataSet.getValue(0x2100,0x0010);
				if(objValue != null)
				{
					strPrintJobID = objValue.toString();
				}
				else
				{
					if(DicomConfig.createInstance().isLoggingOn())
					{
						DicomLogger objDicomLogger =DicomLogger.getLogger();
						objDicomLogger.log(ResourceBundleWrapper.getValue("exceptions","NoJobIDProvided", new Locale(DicomConfig.createInstance().getLocale())) + enumRequestType);
					}
					return EnumImplOpsStatus.FAILURE.getValue();
				}
				objValue = objIDataSet.getValue(0x2100,0x0160);
				if(objValue != null)
				{
					strOwnerID = objValue.toString();
				}
				else
				{
					if(DicomConfig.createInstance().isLoggingOn())
					{
						DicomLogger objDicomLogger =DicomLogger.getLogger();
						objDicomLogger.log(ResourceBundleWrapper.getValue("exceptions","NoOwnerIDProvided", new Locale(DicomConfig.createInstance().getLocale())) + enumRequestType);
					}
					return EnumImplOpsStatus.FAILURE.getValue();
				}
				if(shActionTypeID == 1)
				{
					String strPrintPriority = null;
					objValue = objIDataSet.getValue(0x2000,0x0020);
					if(objValue != null)
					{
						strPrintPriority = objValue.toString();
					}
					else
					{
						if(DicomConfig.createInstance().isLoggingOn())
						{
							DicomLogger objDicomLogger =DicomLogger.getLogger();
							objDicomLogger.log(ResourceBundleWrapper.getValue("exceptions","NoPrintPriorityProvided", new Locale(DicomConfig.createInstance().getLocale())) + enumRequestType);
						}
						return EnumImplOpsStatus.FAILURE.getValue();
					}
					if(prioritizePrintQueueMang(objPrintDBAgent,strValue,objIDataSet))
					{
						if(DicomConfig.createInstance().isLoggingOn())
						{
							DicomLogger objDicomLogger = DicomLogger.getLogger();
							objDicomLogger.log(ResourceBundleWrapper.getValue("status", "Print.PrioritizedPrintJobInstance", new Locale(DicomConfig.createInstance().getLocale())) + strValue);
						}
						return EnumImplOpsStatus.SUCCESS.getValue();
					}
					else
					{
						if(DicomConfig.createInstance().isLoggingOn())
						{
							DicomLogger objDicomLogger =DicomLogger.getLogger();
							objDicomLogger.log(ResourceBundleWrapper.getValue("exceptions","PrintPrioritizeActionFailed", new Locale(DicomConfig.createInstance().getLocale())) + enumRequestType);
						}
						return EnumImplOpsStatus.FAILURE.getValue();
					}
				}
				else if(shActionTypeID == 2)
				{
					if(deleteActionPrintQueueMang(objPrintDBAgent,strValue,objIDataSet))
					{
						if(DicomConfig.createInstance().isLoggingOn())
						{
							DicomLogger objDicomLogger = DicomLogger.getLogger();
							objDicomLogger.log(ResourceBundleWrapper.getValue("status", "Print.PrintJobInstanceDeleted", new Locale(DicomConfig.createInstance().getLocale())) + strValue);
						}
						return EnumImplOpsStatus.SUCCESS.getValue();
					}
					else
					{
						if(DicomConfig.createInstance().isLoggingOn())
						{
							DicomLogger objDicomLogger =DicomLogger.getLogger();
							objDicomLogger.log(ResourceBundleWrapper.getValue("exceptions","PrintJobDeleteActionFailed", new Locale(DicomConfig.createInstance().getLocale())) + enumRequestType);
						}
						return EnumImplOpsStatus.FAILURE.getValue();
					}
				}
				else
				{
					if(DicomConfig.createInstance().isLoggingOn())
					{
						DicomLogger objDicomLogger =DicomLogger.getLogger();
						objDicomLogger.log(ResourceBundleWrapper.getValue("exceptions","ActionTypeIDNotDefined", new Locale(DicomConfig.createInstance().getLocale())) + enumRequestType);
					}
					return EnumImplOpsStatus.FAILURE.getValue();
				}
			}
			else
			{
				if(DicomConfig.createInstance().isLoggingOn())
				{
					DicomLogger objDicomLogger =DicomLogger.getLogger();
					objDicomLogger.log(ResourceBundleWrapper.getValue("exceptions","ActionTypeIDNotDefined", new Locale(DicomConfig.createInstance().getLocale())) + enumRequestType);
				}
				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 deletes the Print JOB of Print Job ID provided in the data base.
	 */
	private boolean deleteActionPrintQueueMang(Print_DBAgent objPrintDBAgent,String strSOPInstanceUID , IDataSet objIDataSet) throws IOException, SQLException
	{
		return objPrintDBAgent.deletePrintJOB(EnumSOPClassUIDs.PRINTQUEUEMANAGEMENTIOD, strSOPInstanceUID, objIDataSet);
	}

	/**
	 * This function prioritize the Print JOB of Print Job ID provided in the data base.
	 */
	private boolean prioritizePrintQueueMang(Print_DBAgent objPrintDBAgent,String strSOPInstanceUID,IDataSet objIDataSet) throws IOException, SQLException
	{
		return objPrintDBAgent.updatePrintJob(EnumSOPClassUIDs.PRINTQUEUEMANAGEMENTIOD, strSOPInstanceUID, objIDataSet);
	}

}
