/*******************************************************************************
 *  Copyright 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.basicmediastorageapplicationprofiles;

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

import cdac.medinfo.sdk.dcm30_04.services.dicomdir.DICOMDIR;
import cdac.medinfo.sdk.dcm30_04.services.mdelete.MDelete;
import cdac.medinfo.sdk.dcm30_04.services.minquirefile.MInquireFile;
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 is implemented to use DIMSE_M-Services.
 * The MediaStorage_FSU supports the Media Storage Operations M-Write, M-delete, M-Inquire File and M-Inquire File-Set.
 * It checks available free space on disk and creates DICOM DIR File Set on Disk.
 * It deletes Dicomdir File Set.
 * */

public class MediaStorage_FSU 

{
	/**
	 * This function rewrite DicomDir File Set.
	 * It deletes Dicomdir File Set.
	 * @throws IOException
	 * @param choice,strStoragePath
	 * */
	public void process(String choice,String strStoragePath, String strFilePath) throws IOException
	{
		if(strStoragePath == null||strStoragePath == "" ||strStoragePath == "null"||strStoragePath.isEmpty()||choice == null||choice == "" ||choice == "null"||choice.isEmpty())
		{
			DicomLogger.getLogger().log(ResourceBundleWrapper.getValue("warning", "PrimaryAttributeMissing", new Locale(DicomConfig.createInstance().getLocale())));
		}
		else
		{
			File dir = new File(strStoragePath);
			if (dir.exists())
			{
				String strStoragePath1=null;
				String selection =null;
				strStoragePath1 = strStoragePath.substring(0,strStoragePath.lastIndexOf(File.separator));
				selection = choice.toUpperCase();	

				switch(selection)
				{
				case "D":
					// Creates the object of MDelete.
					//call the function to delete DICOM DIR File Set
					DICOMDIR objDICOMDIR = new DICOMDIR(strStoragePath);
					if(objDICOMDIR.isDICOMDIRExist())
					{
						MInquireFile objMInquireFile= new MInquireFile();
						objMInquireFile.processTime(strStoragePath);
						MDelete objMDelete = new MDelete();
						objMDelete.process(new File(strStoragePath));
						DicomLogger.getLogger().log(ResourceBundleWrapper.getValue("status", "DICOMDIRFILE.Delete", new Locale(DicomConfig.createInstance().getLocale())) );
					}
					else
					{
						DicomLogger.getLogger().log(ResourceBundleWrapper.getValue("warning", "DICOMDIR.FileNotFound", new Locale(DicomConfig.createInstance().getLocale())));
					}
					break;
				case "R":
					DICOMDIR objDICOMDIR1 = new DICOMDIR(strStoragePath);
					if(objDICOMDIR1.isDICOMDIRExist())
					{
						MDelete objMDelete = new MDelete();
						objMDelete.process(new File(strStoragePath));
						MInquireFile objMInquireFile= new MInquireFile();
						objMInquireFile.processTime(strStoragePath);
						// Creates the object of MediaStorage_FSC.
						//call the function to rewrite DICOM DIR File Set
						MediaStorage_FSC objMediaStorage_FSC = new MediaStorage_FSC();
						objMediaStorage_FSC.process(strFilePath,strStoragePath1);
					}
					else
					{
						DicomLogger.getLogger().log(ResourceBundleWrapper.getValue("warning", "DICOMDIR.FileNotFound", new Locale(DicomConfig.createInstance().getLocale())));
					}
					break;
				default:
					DicomLogger.getLogger().log(ResourceBundleWrapper.getValue("warning", "InvalidChoice", new Locale(DicomConfig.createInstance().getLocale())));
					break;
				}
			}
			else
			{
				DicomLogger.getLogger().log(ResourceBundleWrapper.getValue("warning", "DICOMDIR.FileNotFound", new Locale(DicomConfig.createInstance().getLocale())));
			}
		}
	}
}


