/*******************************************************************************
 *  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.ael.aelsinkimpl;

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

import javax.xml.parsers.ParserConfigurationException;

import cdac.medinfo.sdk.dcm30_04.commoninterface.IDataSet;
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.srtree.SRDocument;
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 Application Event Logging Service Class defines an application-level class-of-service that
 *	facilitates the network transfer of Event Log Records to be logged or recorded in a central
 *	location	
 *	AELDataSinkImpl provides the implementation for generating SR Document and stores it at specified location.
 */

public class AELDataSinkImpl implements IDataSink  
{

	/**
	 * Instantiates a default data sink implementation.
	 */
	public AELDataSinkImpl() 
	{
		this.enumDocumentType = EnumDocumentType.MS_DOC;
	}
	
	/**
	 * The type of document to be created, whether it is MS doc or XML doc.
	 */
	private EnumDocumentType enumDocumentType = null;
	
	/**
	 * 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();
	}


	/**
	 * Updates the Attribute Values of an instance of the Procedure Event Logging SOP Class.
	 * @param objDicomMessage instance of DicomMessage.
	 * @param strStoragePath Storage Path for SR document in Application Event Logging.
	 * @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();
			
			//To get SRDocumentContentModule, creating object of ProcedureLogIOD and then retrieving module from that.
			//ProcedureLogIOD objProcedureLogIOD = new ProcedureLogIOD();		
			//SRDocumentContentModule objDocumentContentModule = objProcedureLogIOD.getSRDocumentContentModule();
			//SRTree objTree = new SRTree(objDocumentContentModule);
			SRDocument objDocument = new SRDocument();
			
			EnumDocumentType enumDocumentType = getEnumDocumentType();
			String strDocType = null;
			
			if(enumDocumentType!=null)
				strDocType = getEnumDocumentType().getValue();
			else
				strDocType = EnumDocumentType.MS_DOC.getValue();
			
			if(strDocType.equalsIgnoreCase("MS_DOC"))
			{
				if(objDocument.serializeSRDoc(objIDataSet,strStoragePath))
				{
					return EnumImplOpsStatus.SUCCESS.getValue();
				}
				else
				{
					return EnumImplOpsStatus.FAILURE.getValue();
				}
			}
			else if(strDocType.equalsIgnoreCase("XML_DOC"))
			{
				if(objDocument.serializeXMLSRDoc(objIDataSet,strStoragePath))
				{
					return EnumImplOpsStatus.SUCCESS.getValue();
				}
				else
				{
					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);
		}
		catch(ParserConfigurationException pce)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger objDicomLogger =DicomLogger.getLogger();
				objDicomLogger.log(pce.getMessage());
			}
		}
		return EnumImplOpsStatus.FAILURE.getValue();
	}
}
