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

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

/**
 * This class is used to read values from a 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 String value of properties complete file path.
	 */
	public PropertiesReader(String strPropertiesFile)
	{
		objProperties = new Properties();
		this.strPropertiesFile = strPropertiesFile;
		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(IOException ioe)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ioe.getMessage());
			}
		}
	}

	/**
	 * 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)
		{
			DicomLogger.getLogger().log(ioe);
		}

	}

	/**
	 * Retrieves the property value from the file.
	 * @param strPropertyName name of the property.
	 * @return String - String value of property.
	 */
	public String getProperty(String strPropertyName)
	{
		Object objPropertyValue = objProperties.get(strPropertyName);
		objProperties.getProperty(strPropertyName, strPropertyName);
		return objPropertyValue.toString();
	}

	/**
	 * This function 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(IOException ioe)
		{
			DicomLogger.getLogger().log(ioe);
		}
	}
}
