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

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

import javax.xml.parsers.ParserConfigurationException;


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.enums.EnumDocumentType;
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.services.spss.dbhelper.SPSSDBAgent;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConfig;
import cdac.medinfo.sdk.dcm30_04.utils.DicomLogger;

import cdac.medinfo.sdk.dcm30_04.utils.ResourceBundleWrapper;

/**
 *	The Softcopy Presentation State Storage SOP Classes extend the functionality of the Storage Service
 *  class.This class creates the Presentation Document for required information send in data set.
 */

public class SPSSSinkImpl implements IDataSink {


	/**
	 * The type of document to be created, whether it is MS doc .
	 */
	private EnumDocumentType enumDocumentType = null;

	private SPSSDBAgent objSPSSDBAgent = null;

	/**
	 * Instantiates a default data sink implementation.
	 */
	public SPSSSinkImpl() 
	{
		this.enumDocumentType = EnumDocumentType.MS_DOC;
	}
	/**
	 * 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 SPSSSinkImpl(String strConnectionString, String strDriverName) throws SQLException,ClassNotFoundException, InstantiationException, IllegalAccessException
	{
		objSPSSDBAgent = SPSSDBAgent.createInstance(strConnectionString,strDriverName);
	}
	
	/**
	 * Retrieves the Document type to be created.
	 * @return
	 */
	public EnumDocumentType getEnumDocumentType() 
	{
		return enumDocumentType;
	}

	/**
	 * Sets the Document type to be created.
	 * @param enumDocumentType
	 */
	public void setEnumDocumentType(EnumDocumentType enumDocumentType) 
	{
		this.enumDocumentType = enumDocumentType;
	}

	/**
	 * Populates the DataBase if available from given query model and serializes the given buffer.
	 * @param objQueryModel instance of QueryModel
	 * @param buffer instance of DicomBufferStream
	 * @param strStoragePath string value of Data Base storage path
	 * @return short - shows status value which will be used in generation of response
	 */
	public short store(IQueryModel objQueryModel, String strStoragePath)
	{
		return EnumImplOpsStatus.FAILURE.getValue();
	}


	/**
	 * Store the Attribute Values of an instance of the Gray Scale SoftCopy Presentation SOP Class
	 * @param objDicomMessage instance of DicomMessage
	 * @param strStoragePath Storage Path for PS document in SPSS.
	 * @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
		{
			IDataSet objIDataSet = objDicomMessage.getDataSet();

			PSDocument objDocument = new PSDocument();
			
			EnumDocumentType enumDocumentType = getEnumDocumentType();
			String strDocType = null;
			
			if(objDicomMessage != null)
			{
				EnumCommandField enumRequestType = objDicomMessage.getCommandType();
				switch(enumRequestType)
				{
				case C_STORE_RQ :
				{
					String strSOPInstance = objDicomMessage.getCommandSet().getValue(0x0000,0x1000).toString();
				
						if(enumDocumentType!=null)
							strDocType = getEnumDocumentType().getValue();
						else
							strDocType = EnumDocumentType.MS_DOC.getValue();
						
						if(strDocType.equalsIgnoreCase("MS_DOC"))
						{
							if(objSPSSDBAgent.storeInstanceInfo(objDocument.serializePSDoc(objDicomMessage,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( Exception 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();
	}

}
