/*******************************************************************************
 *  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.storagecommitment.storagecommitmentsinkimpl;

import java.io.File;
import java.io.IOException;
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.exceptions.SerializeException;
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;


/**
 * StorageCommitmentSinkImpl is the default implementation for DataSink implementation for Storage Commitment.
 * This implementation provides the storage of Data Sets
 * for which Storage Commitment request is received.
 */
public class StorageCommitmentSinkImpl implements IDataSink 
{

	/**
	 * This function Stores the DataSet at the Storage Path Specified by the user.
	 * @param objDicomMessage instance of Dicom Message Containing DataSet for which
	 * storage commitment has to be provided.
	 * @param strStoragePath String value of Storage Path where the Data Set are stored
	 * for which storage commitment is provided.
	 * @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
		{
			File objDir = new File(strStoragePath);
			objDir.mkdirs();
			IDataSet objIDataSet = objDicomMessage.getDataSet();
			String strSOPInstanceUID = objIDataSet.getValue(0x0008, 0x0018).toString();
			if(strStoragePath.endsWith("/") == true)
				strStoragePath = strStoragePath.concat(strSOPInstanceUID);
			else
				strStoragePath = strStoragePath.concat("/").concat(strSOPInstanceUID);

			DicomSerializer objDicomSerializer = new DicomSerializer();
//			objDicomSerializer.serializeAsPart10File(strStoragePath,objDicomMessage.getDataSet(),true);
			objDicomSerializer.serializeAsPart10File(strStoragePath,objIDataSet,null,objIDataSet.getDictionaryReader(),true);
		}
		catch(SerializeException se)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(se.getMessage());
			}
			throw new SinkException(ResourceBundleWrapper.getValue("exceptions", "STORAGECOMMITMENT.StorageException", new Locale(DicomConfig.createInstance().getLocale())),se);
		}
		catch(IOException ie)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ie.getMessage());
			}
			throw new SinkException(ResourceBundleWrapper.getValue("exceptions", "STORAGECOMMITMENT.StorageException", new Locale(DicomConfig.createInstance().getLocale())),ie);
		}
		return EnumImplOpsStatus.SUCCESS.getValue();
	}

	//The implementation of this method still needs to be provided.
	/**
	 * This function Stores the DataSet.
	 * @return short - shows status value which will be used in generation of response
	 * 					Refer EnumImplOpsStatus for supported response status values.
	 */
	public short store(IQueryModel objQueryModel, String strStoragePath) throws SinkException
	{
		return EnumImplOpsStatus.FAILURE.getValue();
	}
}
