/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Dao;

import LoginDb.DbHandler;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author 1004242
 */
public class ChangeUserPasswordDao {


    public List<String> CheckPreviousPwd(String user) {
        Connection con = null;
        StringBuilder checkPrevPwdQuery = null;
        PreparedStatement ps = null;
        ResultSet res = null;
        List<String> passwordList = new ArrayList<String>();

        try {
            con = DbHandler.getDBConnection();
            checkPrevPwdQuery = new StringBuilder("select PREV_PWD_OLD,PREV_PWD, password from login_details where login_id = ?");

            ps = con.prepareStatement(checkPrevPwdQuery.toString());
            ps.setString(1, user);

            res = ps.executeQuery();
            if (res.next()) {
                passwordList.add(res.getString("password"));
                passwordList.add(res.getString("PREV_PWD"));
                passwordList.add(res.getString("PREV_PWD_OLD"));
            }

        } catch (Exception e) {
           // e.printStackTrace();
           // System.out.println("Exception occured fetching  prev_pwd in CheckPreviousPwd for user Id :" + user);
           System.out.println("Error occurred during processing.");
        
        } finally {
            try {
                if (con != null) {
                    con.close();
                }
            } catch (Exception e) {
               // e.printStackTrace();
               System.out.println("Error occurred during connection close.");
            }
        }

        return passwordList;
    }

    public int updateNewAndOldPwd(String user, String newPwd, String currentPwd, String prevPwd, String pacs_Id) {
        Connection con = null;
        StringBuilder updatePwdQuery = null;
        PreparedStatement ps = null;
        int res = 0;
        try {

            con = DbHandler.getDBConnection();
            updatePwdQuery = new StringBuilder("update login_details set password= ?, prev_pwd = ?, prev_pwd_old = ? ,first_login_flag='T',PWD_UPDATE_DATE=trunc(sysdate),PWD_EXPIRY_DATE=trunc(sysdate)+30,IS_PWD_EXPIRED='N' where login_id = ? and pacs_id = ? ");

            ps = con.prepareStatement(updatePwdQuery.toString());
            ps.setString(1, newPwd);
            ps.setString(2, currentPwd);
            ps.setString(3, prevPwd);
            ps.setString(4, user);
            ps.setString(5, pacs_Id);
            res = ps.executeUpdate();
           // con.commit();

        } catch (Exception e) {
           // e.printStackTrace();
           // System.out.println("Exception occured updating  prev_pwd in updateNewAndOldPwd for user Id :" + user);
           System.out.println("Error occurred during processing.");
           
        } finally {
            try {
                if (con != null) {
                    con.close();
                }
            } catch (Exception e) {
               // e.printStackTrace();
               System.out.println("Error occurred during connection close.");
            }
        }
        return res;
    }
}
