/*******************************************************************************
 *  Copyright 2017 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.hl7282.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * Reads values from properties file using the key.
 * The Properties file should contain the key-value pair separated by "=". 
 */
public class PropertiesReader
{
	private Properties objProperties = null;
	private String strPropertiesFile = null;

	/**
	 * Creates pre-initialized instance of PropertiesReader using the Properties
	 * file path.
	 * @param strPropertiesFile Path of Properties File
	 */
	public PropertiesReader(String strPropertiesFile)
	{
		this.strPropertiesFile = strPropertiesFile;
		objProperties = new Properties();
		this.setPropertiesFile(strPropertiesFile);
	}

	/**
	 * Creates pre-initialized instance of PropertiesReader.
	 */
	public PropertiesReader()
	{
		objProperties = new Properties();
	}

	/**
	 * Sets the properties file path and loads the properties from the file.
	 * @param strPropertiesFile String value of properties complete file path.
	 */
	public void setPropertiesFile(String strPropertiesFile)
	{
		try
		{
			InputStream objInputStream = new FileInputStream(new File(strPropertiesFile));
			setPropertiesFile(objInputStream);
		}
		catch(FileNotFoundException fnfe)
		{
			if(HL7Config.createInstance().isLoggingOn())
			{
				HL7Logger objHL7Logger = HL7Logger.getLogger();
				objHL7Logger.log(fnfe);
			}
		}

	}

	/**
	 * Loads properties from the Input Stream provided.
	 * @param objStream instance of Input Stream.
	 */
	public void setPropertiesFile(InputStream objStream)
	{
		try
		{
			objProperties.load(objStream);
		}
		catch(IOException ioe)
		{
			if(HL7Config.createInstance().isLoggingOn())
			{
				HL7Logger objHL7Logger = HL7Logger.getLogger();
				objHL7Logger.log(ioe);
			}
		}
	}

	/**
	 * Get properties from the Input Stream provided.
	 * @param strPropertyName the property key
	 * @return String - the value in this property list with the specified key value
	 */
	public String getProperty(String strPropertyName)
	{
		Object objPropertyValue = objProperties.get(strPropertyName);
		return objPropertyValue.toString();
	}

	/**
	 * Modify the value of key as set by the user and writes it in the property file.
	 * @param key the key in properties file.
	 * @param value - the value which is to be set for the specified key.
	 */
	public void setPropertyValue(String key, String value)
	{
		try
		{
			objProperties.setProperty(key,value);
			objProperties.store(new FileOutputStream(this.strPropertiesFile), null);
		}
		catch(Exception e)
		{
			if(HL7Config.createInstance().isLoggingOn())
			{
				HL7Logger objHL7Logger = HL7Logger.getLogger();
				objHL7Logger.log(e);
			}	
		}
	}
}
