
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import LoginDb.DbHandler;
import java.io.IOException;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Tcs Helpdesk10
 */
public class ModifyLoanInterestServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        HttpSession session = request.getSession(false);
        Connection connection = null;
        ResultSet rs = null;
        Statement statement = null;
        CallableStatement proc = null;
        int searchFound = 0;
        String message = "";
        String pacsId = (String) session.getAttribute("pacsId");
         String headPacsId = (String) session.getAttribute("headPacsId");
       String tellerId=(String)session.getAttribute("user");
        String JSP_Name = request.getParameter("screenName");
        //String acc_no = request.getParameter("accountNo");
        //String int_rate = request.getParameter("int_rate");
       // String pen_int_rate=request.getParameter("pen_int_rate");
        //String incr_value = request.getParameter("incr_value");
        //String acc_read=request.getParameter("accountNo_read");
       // String pen_incr_value=request.getParameter("pen_incr_value");

        System.out.println("JSP_NAME  "+JSP_Name);

if( int_rate == null || incr_value == null || int_rate.equals("") || incr_value.equals("")||pen_int_rate == null || pen_incr_value == null || pen_int_rate.equals("") || pen_incr_value.equals("") ){

        int counter =0;
        try{
        connection = DbHandler.getDBConnection();

        String sql = "select p.intt_rate as intt_rate,p.penal_intt_rate as penal_intt_rate from loan_interest_map p where p.key_1='"+acc_no+"'";
       
        
        statement = connection.createStatement();
       // rs = statement.executeQuery(sql);
        while (rs.next()) {
            counter++;
            int_rate=rs.getString("intt_rate");
            pen_int_rate=rs.getString("penal_intt_rate");
            System.out.println("Interest Rate"+int_rate );
            System.out.println("Interest Rate"+pen_int_rate );
        }
    }catch(Exception ex){
       // ex.printStackTrace();
       System.out.println("Error occurred during processing.");
    } finally {
            try {
                connection.close();
                statement.close();
            } catch (SQLException e) {
               // System.out.println(e.toString());
               System.out.println("Error occurred during connection close.");
            }
        }
        if(counter == 0){
            message = "No data exists for selected item. Please enter correct details.";
            request.setAttribute("message", message);
            request.setAttribute("displayFlag", "N");
        }else{
            request.setAttribute("displayFlag", "Y");
        }

        }else{
        System.out.println("Loan Update");
        System.out.println(acc_no+int_rate+incr_value);
        connection = DbHandler.getDBConnection();

        try {
                proc = connection.prepareCall("{ call operations1.LOAN_INT_UPDATE(?,?,?,?,?,?) }");

                //int i = Integer.parseInt(int_rate) + Integer.parseInt(incr_value);

                 proc.setString(1, pacsId);
                 proc.setString(2, acc_read);
                 proc.setDouble(3, Double.parseDouble(incr_value));
                 proc.setString(4, tellerId);
                 proc.registerOutParameter(5, java.sql.Types.VARCHAR);
                 proc.setDouble(6, Double.parseDouble(pen_incr_value));

                 proc.executeUpdate();

                 //message = proc.getString(5);
                 request.setAttribute("message", message);

            } catch (SQLException ex) {
                     message = "Error Occurred: "+ex.getMessage()+"Please contact Administrator";
                     request.setAttribute("message", message);
                    // ex.printStackTrace();
                    System.out.println("Error occurred during processing.");
            } finally {
                try {
                    connection.close();
                } catch (SQLException e) {
                //System.out.println(e.toString());
                    System.out.println("Error occurred during processing.");
                }
            }
                request.setAttribute("displayFlag", "N");
        }

        request.setAttribute("int_rate", int_rate);
        request.setAttribute("pen_int_rate", pen_int_rate);
        request.setAttribute("acc_no", acc_no);
        request.getRequestDispatcher("/Loan/LoanModify.jsp").forward(request, response);

    }
}
