<commit_before>package gov.nih.nci.cdmsconnector.util;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;
import java.util.Set;


public class PropertiesUtil {

	// prc added this string for v2.3 DB property retrieval
	private final static String GET_PROPS_FROM_DB = 
		"select PROPERTY_KEY, PROPERTY_VALUE " 
		+ "from CLINICAL_CONNECTOR_PROPERTIES ";

	private static Properties props;
	private static String propertiesFileLocation;

	public static Properties getPropertiesFromFile() throws Exception {

		if (props == null) {
			String catalinaHome = System.getProperty("catalina.home");
			String propertiesFilePath = "c3d/c3dgridservice.properties";
			java.io.InputStream inputStream = null;

			if (catalinaHome != null) {
				propertiesFilePath = catalinaHome + "/conf/"
						+ propertiesFilePath;
				inputStream = new FileInputStream(propertiesFilePath);
			} else {
				inputStream = FileLoader.getInstance().getFileAsStream(
						"registerConsumer_gridservice.properties");
			}

			Properties properties = new Properties();
			if (inputStream != null) {
				try {
					properties.load(inputStream);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (!properties.isEmpty()) {

				validateProperties(properties);
				props = properties;
			}
		}

		return props;
	}

	public static Properties getDBProperties() throws Exception {

		if (props == null) {
			String catalinaHome = System.getProperty("catalina.home");
			String propertiesFilePath = "c3d/c3dgridservice.properties";
			java.io.InputStream inputStream = null;

			if (catalinaHome != null) {
				propertiesFilePath = catalinaHome + "/conf/"
						+ propertiesFilePath;
				inputStream = new FileInputStream(propertiesFilePath);
			} else {
				inputStream = FileLoader.getInstance().getFileAsStream(
						"registerConsumer_gridservice.properties");
			}

			Properties properties = new Properties();
			if (inputStream != null) {
				try {
					properties.load(inputStream);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (!properties.isEmpty()) {

				validateDBProperties(properties);
				props = properties;
			}
		}

		return props;
	}

	public static Properties getPropertiesFromDB() throws Exception {
		
		Connection cn = null;
		PreparedStatement stmt1 = null;

		//if (props == null) {
		try {
			getDBProperties();
		} catch (Exception e) {

		}

		try {
			String propKey = null, propVal = null;

			Class.forName(props.getProperty(Constants.C3DDS_DRIVERCLASSNAME));
			cn = DriverManager.getConnection(
					props.getProperty(Constants.C3DDS_URL), 
					props.getProperty(Constants.C3DDS_USERNAME), 
					props.getProperty(Constants.C3DDS_PASSWORD));

			stmt1 = cn.prepareStatement(GET_PROPS_FROM_DB);
				
			ResultSet rs1 = stmt1.executeQuery();
			while (rs1.next()) {
					
				//recordExists = true;
				propKey = rs1.getString("PROPERTY_KEY");
				propVal = rs1.getString("PROPERTY_VALUE");

				props.setProperty(propKey, propVal);
			}
	            				
		} finally {
			try {
				stmt1.close();
			} catch (Exception ex) {
			}
			try {
				cn.close();
			} catch (Exception ex) {
			}
		}

		if (!props.isEmpty()) {
			validateProperties(props);
		}

		//}
		//showDBProperties();
		return props;

	}
	
	public static Properties loadProperties(String path) throws Exception{
		propertiesFileLocation=path+"/c3dgridservice.properties";
		getPropertiesFromFile();
		props.put("CONF_FILES_DIR", path);
		return props;
	}

	private static void validateProperties(Properties properties)
			throws Exception {

		validateDBProperties(properties);
		// TODO
		/*<commit_msg>added exception logging.<commit_after>package gov.nih.nci.cdmsconnector.util;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;
import java.util.Set;


public class PropertiesUtil {

	// prc added this string for v2.3 DB property retrieval
	private final static String GET_PROPS_FROM_DB = 
		"select PROPERTY_KEY, PROPERTY_VALUE " 
		+ "from CLINICAL_CONNECTOR_PROPERTIES ";

	private static Properties props;
	private static String propertiesFileLocation;

	public static Properties getPropertiesFromFile() throws Exception {

		if (props == null) {
			String catalinaHome = System.getProperty("catalina.home");
			String propertiesFilePath = "c3d/c3dgridservice.properties";
			java.io.InputStream inputStream = null;

			if (catalinaHome != null) {
				propertiesFilePath = catalinaHome + "/conf/"
						+ propertiesFilePath;
				inputStream = new FileInputStream(propertiesFilePath);
			} else {
				inputStream = FileLoader.getInstance().getFileAsStream(
						"registerConsumer_gridservice.properties");
			}

			Properties properties = new Properties();
			if (inputStream != null) {
				try {
					properties.load(inputStream);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (!properties.isEmpty()) {

				validateProperties(properties);
				props = properties;
			}
		}

		return props;
	}

	public static Properties getDBProperties() throws Exception {

		if (props == null) {
			String catalinaHome = System.getProperty("catalina.home");
			String propertiesFilePath = "c3d/c3dgridservice.properties";
			java.io.InputStream inputStream = null;

			if (catalinaHome != null) {
				propertiesFilePath = catalinaHome + "/conf/"
						+ propertiesFilePath;
				inputStream = new FileInputStream(propertiesFilePath);
			} else {
				inputStream = FileLoader.getInstance().getFileAsStream(
						"registerConsumer_gridservice.properties");
			}

			Properties properties = new Properties();
			if (inputStream != null) {
				try {
					properties.load(inputStream);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (!properties.isEmpty()) {

				validateDBProperties(properties);
				props = properties;
			}
		}

		return props;
	}

	public static Properties getPropertiesFromDB() throws Exception {
		
		Connection cn = null;
		PreparedStatement stmt1 = null;

		//if (props == null) {
		try {
			getDBProperties();
		} catch (Exception e) {
			e.printStackTrace();
		}

		try {
			String propKey = null, propVal = null;

			Class.forName(props.getProperty(Constants.C3DDS_DRIVERCLASSNAME));
			cn = DriverManager.getConnection(
					props.getProperty(Constants.C3DDS_URL), 
					props.getProperty(Constants.C3DDS_USERNAME), 
					props.getProperty(Constants.C3DDS_PASSWORD));

			stmt1 = cn.prepareStatement(GET_PROPS_FROM_DB);
				
			ResultSet rs1 = stmt1.executeQuery();
			while (rs1.next()) {
					
				//recordExists = true;
				propKey = rs1.getString("PROPERTY_KEY");
				propVal = rs1.getString("PROPERTY_VALUE");

				props.setProperty(propKey, propVal);
			}
	            				
		} finally {
			try {
				stmt1.close();
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			try {
				cn.close();
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}

		if (!props.isEmpty()) {
			validateProperties(props);
		}

		//}
		//showDBProperties();
		return props;

	}
	
	public static Properties loadProperties(String path) throws Exception{
		propertiesFileLocation=path+"/c3dgridservice.properties";
		getPropertiesFromFile();
		props.put("CONF_FILES_DIR", path);
		return props;
	}

	private static void validateProperties(Properties properties)
			throws Exception {

		validateDBProperties(properties);
		// TODO
		/*
{'fraction_non_alphanumeric': 0.10929319371727748, 'fraction_numerical': 0.0049738219895287955, 'mean_word_length': 3.586434573829532, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2019146', 'n_tokens_mistral': 2746, 'n_tokens_neox': 2202, 'n_words': 481}