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


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.dicomparserserializers.DicomSerializer;
import cdac.medinfo.sdk.dcm30_04.enums.EnumCommandField;
import cdac.medinfo.sdk.dcm30_04.services.colorpalette.dbhelper.ColorPaletteDBAgent;
import cdac.medinfo.sdk.dcm30_04.services.dicomnetcommons.EnumImplOpsStatus;
import cdac.medinfo.sdk.dcm30_04.services.dicomnetcommons.SinkException;
import cdac.medinfo.sdk.dcm30_04.services.dimsecommons.DicomMessage;
import cdac.medinfo.sdk.dcm30_04.services.dimsecommons.IDataSink;
import cdac.medinfo.sdk.dcm30_04.services.dimsecommons.IQueryModel;
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 provides functionality to query the data base and find 
 * the resultant matches depending on specified DataSet.
 */
public class ColorPalette_SinkImp implements IDataSink
{
	private ColorPaletteDBAgent objColorPaletteDBAgent = null;

	/**
	 * Instantiates a new data sink implementation.
	 * @param strConnectionString the string connection string
	 * @param strDriverName the string driver name
	 * @throws SQLException the SQL exception
	 * @throws ClassNotFoundException the class not found exception
	 * @throws InstantiationException the instantiation exception
	 * @throws IllegalAccessException the illegal access exception
	 */
	public ColorPalette_SinkImp(String strConnectionString, String strDriverName) throws SQLException,ClassNotFoundException, InstantiationException, IllegalAccessException
	{
		objColorPaletteDBAgent = ColorPaletteDBAgent.createInstance(strConnectionString,strDriverName);
	}

	/**
	 * Populates the SOP Instance information in Database
	 * 
	 * @param objIDataSet instance of IDataSet
	 * @param strValue null in case of ColorPalette
	 * @return short - shows status value which will be used in generation of response
	 * Refer EnumImplOpsStatus for supported response status values.
	 */
	public short store(DicomMessage objDicomMessage, String strStoragePath)
	{
		try
		{	
			if(objDicomMessage != null)
			{
				EnumCommandField enumRequestType = objDicomMessage.getCommandType();
				switch(enumRequestType)
				{
				case C_STORE_RQ :
				{
					IDataSet objIDataSet = objDicomMessage.getDataSet();
					if(store(objIDataSet, strStoragePath))
						return EnumImplOpsStatus.SUCCESS.getValue();
					else
						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();
				}
				}
			}
		}
		catch(IOException ioe)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger objDicomLogger =DicomLogger.getLogger();
				objDicomLogger.log(ioe.getMessage());
			}
			throw new SinkException(ResourceBundleWrapper.getValue("exceptions", "IOException", new Locale(DicomConfig.createInstance().getLocale())), ioe);
		}		
		return EnumImplOpsStatus.FAILURE.getValue();
	}
	
	/**
	 * This function stores the DataSet at the specified location.
	 */
	private boolean store(IDataSet objIDataSet, String strStoragePath)
	{
		//boolean status = true;
		String strSOPInstanceUID = objIDataSet.getValue(0x0008, 0x0018).toString();
		if(strStoragePath.endsWith("/") == true)
			strStoragePath = strStoragePath.concat(strSOPInstanceUID);
		else
			strStoragePath = strStoragePath.concat("/").concat(strSOPInstanceUID);

		try
		{
			{				
				objColorPaletteDBAgent.storeInstanceInfo(objIDataSet);
			}
			
			if(objIDataSet != null)
			{
				DicomSerializer objDicomSerializer = new DicomSerializer();
				objDicomSerializer.serializeAsPart10File(strStoragePath + ".dcm", objIDataSet, null, true);
			}
		}
		catch(IOException ie)
		{

			if(DicomConfig.createInstance().isLoggingOn() )
			{
				DicomLogger objDicomLogger = DicomLogger.getLogger();
				objDicomLogger.log(ie.getMessage());
			}
			throw new SinkException(ResourceBundleWrapper.getValue("exceptions", "IOException", new Locale(DicomConfig.createInstance().getLocale())),ie) ;
		}
		catch(SQLException ioe)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger objDicomLogger = DicomLogger.getLogger();
				objDicomLogger.log(ioe.getMessage());
			}
			throw new SinkException(ResourceBundleWrapper.getValue("exceptions", "SQLException", new Locale(DicomConfig.createInstance().getLocale())),ioe) ;
		}
		return true;
	}

	@Override
	public short store(IQueryModel objQueryModel, String strValue)throws SinkException 
	{
		// TODO Auto-generated method stub
		return 0;
	}
	
}
