package com.reallinkbilling.resource;

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

import org.apache.struts2.dispatcher.SessionMap;

import com.reallinkbilling.db.GetDBConnection;


public class CheckOldPasswordCorrect {
	
	
	
	public static boolean getOldPasswordCorrect(String userName, String oldPassword, SessionMap<String, Object> session)
	{
		boolean oldPasswordCorrect = false;
		
		
		String shopId = (String) session.get("shopId");
		String shopCode = (String) session.get("shopCode");
    	
		
		
		Connection connection = null;
		PreparedStatement preparedStatement= null;
		ResultSet resultSet = null;
		
		String sql = "";
		String db = (String) session.get("db");
		
		
			try
			{
				
				connection = GetDBConnection.getDBConnection(db);
				
				
				sql = "SELECT "
						+ "shop_user_full_name,"
						+ "shop_user_password_1 "
						+ "FROM "
						+ "shop_user_master "
						+ "WHERE "
						+ "shop_user_full_name = ? "
						+ "AND "
						+ "shop_user_password_1 = ? "
						+ "AND "
						+ "shop_id = ? "
						+ "AND "
						+ "shop_code = ? ";

				
				preparedStatement = connection.prepareStatement(sql);
				
				preparedStatement.setString(1, userName);
				preparedStatement.setString(2, oldPassword);
				preparedStatement.setString(3, shopId);
				preparedStatement.setString(4, shopCode);
				
				
				resultSet = preparedStatement.executeQuery();
				
				if(resultSet.next()){
					oldPasswordCorrect = true;
					
				}
						
				
				
			} catch (SQLException e) {
				e.printStackTrace();
				oldPasswordCorrect = false;
				
			} catch (Exception e) {
				e.printStackTrace();
				
				
				oldPasswordCorrect = false;
			}finally {
				if(resultSet != null)
				{
					try {
						resultSet.close(); resultSet = null;
					} catch (SQLException e) {
						
					}
				}
				if(preparedStatement != null)
				{
					try {preparedStatement.close(); preparedStatement = null;} catch (SQLException e) {}
				}
				if(connection != null)
				{
					
					try {connection.close(); connection = null;}catch (SQLException e) {}
				}
				
			}
		
		
		return oldPasswordCorrect;
	}

}
