/*  OpenOlap viewer
 *  パッケージ名：openolap.viewer.dao
 *  ファイル：PostgresUserDAO.java
 *  説明：
 *
 *  作成日: 2004/01/30
 */
package openolap.viewer.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import openolap.viewer.User;
import openolap.viewer.common.CommonUtils;

/**
 *  クラス：PostgresUserDAO
 *  説明：
 */
public class PostgresUserDAO implements UserDAO {

	Connection conn = null;

	// ********** コンストラクタ **********

	public PostgresUserDAO (Connection conn) {
		this.conn = conn;
	}


	public User getUser(String userName, String password) throws SQLException {

		User user = null;

		String SQL = "";
		SQL += "select ";
		SQL += "    id,";				// id
		SQL += "    name,";				// ユーザ名
		SQL += "    adminflg, ";		// 管理者フラグ
		SQL += "    export_file_type ";	// エクスポートタイプ
		SQL += "from ";
		SQL += "    oo_v_user ";
		SQL += "where ";
		SQL += "    name='" + userName + "' and ";
		SQL += "    password='" + password + "'";
//System.out.println("select report SQL:" + SQL);

		Statement stmt = null;
		ResultSet rs = null;

		try {
			stmt = conn.createStatement();
			rs = stmt.executeQuery(SQL);

			while ( rs.next() ) {
				String id = rs.getString("id");
				String name = rs.getString("name");
				String adminflg = rs.getString("adminflg");

				user = new User(rs.getString("id"),
								 rs.getString("name"),
								 CommonUtils.FLGTobool(rs.getString("adminflg")),
								 rs.getString("export_file_type"));
			}
		

		} catch (SQLException e) {
			throw e;
		} catch (Exception e) { 
			e.printStackTrace();
		} finally {
			try {
				if (rs != null){
					rs.close();
				}
			} catch (SQLException e) {
				throw e;
			} finally {
				try {
					if (stmt != null){
						stmt.close();
					}
				} catch (SQLException e) {
					throw e;
				}
			}
		}
		
		return user;
	}
}
