package server.dto;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

import server.*;

public class DatabaseConnector {
	private static String dbHost;
	private static String dbPort;
	private static String dbUser;
	private static String dbPassword;
	private static String dbName;
	private Connection con;

	
	public DatabaseConnector() {
		Connection con = null;
		
		dbHost = ReaderConfig.getDbHostIp();
		dbPort = ReaderConfig.getDbPort();
		dbName = ReaderConfig.getDbSchema();
		dbUser = ReaderConfig.getDbUser();
		dbPassword = ReaderConfig.getDbPassword();
		
		
		try {
			Class.forName("com.mysql.jdbc.Driver");
			String url = "jdbc:mysql://" + dbHost + ":" + dbPort + "/" + dbName;
			con = DriverManager.getConnection(url, dbUser,
					dbPassword);
			this.con = con;
		} catch (Exception e) {
			e.printStackTrace();
		}

		
	}
		
	

	
	
	public UserDto selectUserById(int userId) {
		UserDto u = new UserDto();
		
		PreparedStatement st = null;
		String query = "SELECT * FROM users WHERE user_id = ?";
		
		try {
			st = con.prepareStatement(query);
			st.setInt(1, userId);
			ResultSet res = st.executeQuery();

			if (res.next()) {
				u.setUser_id(res.getInt(1));
				u.setFirst_name(res.getString(2));
				u.setLast_name(res.getString(3));
				u.setEmail(res.getString(4));
				u.setPassword(res.getString(5));
				u.setSalt(res.getString(6));
				u.setTemp_password(res.getString(7));
				u.setTemp_salt(res.getString(8));
				u.setActivation_code(res.getString(9));
				u.setPublic_key(res.getString(10));
				u.setAdministrator_flag(res.getInt(11));
				u.setStatus(res.getInt(12));				
			}
		} catch (SQLException ex) {
			Logger lgr = Logger.getLogger(DatabaseConnector.class.getName());
			lgr.log(Level.WARNING, ex.getMessage(), ex);
		}
		
		return u;
	}
	
	
}
	
	
	
	
	

@@ -161,24 +161,53 @@
 LException;%0A
+import java.util.Properties;%0A
 import java.
@@ -260,16 +260,50 @@
 ogger;%0A%0A
+import org.omg.CORBA.INITIALIZE;%0A%0A
 import s
@@ -529,17 +529,16 @@
 on con;%0A
-%0A
 %09%0A%09publi
@@ -595,41 +595,106 @@
 %09%0A%09%09
-dbHost = ReaderConfig.getD
+//System.out.println(ConfigurationProperties.dbHost());%0A%0A%09%09dbHost = ConfigurationProperties.d
 bHost
-Ip
 ();%0A
@@ -708,142 +708,174 @@
 t = 
-ReaderConfig.getDbPort();%0A%09%09dbName = ReaderConfig.getDbSchema();%0A%09%09dbUser = ReaderConfig.getDbUser();%0A%09%09dbPassword = ReaderConfig.getD
+ConfigurationProperties.dbPort();%0A%09%09dbName = ConfigurationProperties.dbSchema();%0A%09%09dbUser = ConfigurationProperties.dbUser();%0A%09%09dbPassword = ConfigurationProperties.d
 bPas

read connection parameters from the .properties file