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

import DataEntryBean.ChequeDetailsBean;
import DataEntryBean.FarmerDetailsBean;
import LoginDb.DbHandler;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import oracle.jdbc.OracleTypes;

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



     public String saveFarmerlandDetails(String tellerId, String pacsId, String cif ,ArrayList<FarmerDetailsBean> alFarmerDetList) {
        Connection con = null;
        CallableStatement proc = null;
        int res = 0;
        FarmerDetailsBean farmerLandDet=null;
        String message = null;
        String messageBackEnd = null;

        try {

            if (alFarmerDetList.size() > 0) {
                con = DbHandler.getDBConnection();
                con.setAutoCommit(false);
                for (int j = 0; j < alFarmerDetList.size(); j++) {
                    proc = con.prepareCall("{ call operations1.farmer_details(?,?,?,?,?,?,?,?,?,?,?,?) }");
                    farmerLandDet = alFarmerDetList.get(j);

                    proc.setString(1, pacsId);
                    proc.setString(2, null);
                    proc.setString(3, tellerId);
                    proc.setString(4, cif);
                    proc.setString(5, farmerLandDet.getManja());
                    proc.setString(6, farmerLandDet.getDgNo());
                    proc.setString(7, farmerLandDet.getgP());
                    proc.setString(8, farmerLandDet.getVillage());
                    proc.setString(9, farmerLandDet.getKhatiyan());
                    proc.setDouble(10, farmerLandDet.getArea());
                    proc.registerOutParameter(11, OracleTypes.NUMBER);
                    proc.registerOutParameter(12, OracleTypes.VARCHAR);
                    proc.executeUpdate();

                    res = proc.getInt(11);//error message from  procedure
                   // messageBackEnd = proc.getString(12);
                    if (res != 0) {
                            con.rollback();
                            break;
                        }
                  
                    proc.close();
                }

                if (res ==0 ) {
                        message = messageBackEnd;
                        con.commit();
                    }
                else
                {
                    message = messageBackEnd;
                }
            }

        } catch (Exception e) {
            message= "Error in processsing cif land details Updation for :"+cif;
            if (con != null) {
                try {
                    con.rollback();
                } catch (SQLException ex) {
                   // Logger.getLogger(FamerDetailsUpdationDao.class.getName()).log(Level.SEVERE, null, ex);
                   System.out.println("Error occurred during processing.");
                }
                }
           // e.printStackTrace();
           // System.out.println("Exception occured  in saveFarmerlandDetails for user Id :" + tellerId);
           System.out.println("Error occurred during processing.");
        } finally {
            try {
                con.commit();
                if (con != null) {
                    con.close();
                }
            } catch (Exception e) {
               // e.printStackTrace();
               System.out.println("Error occurred during connection close.");
            }
        }

        return message;
    }
}
