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

import LoginDb.DbHandler;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import oracle.jdbc.OracleTypes;

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

    public String createUpdateAgentLimit(String tellerId, String pacsId, String agentId, String limit) {
        Connection con = null;
        CallableStatement proc = null;
        Statement qry = null;
        String res = null;
        ResultSet rs = null;
        String txnType;
        int count = 0;

        try {
            con = DbHandler.getDBConnection();
            qry = con.createStatement();
            rs = qry.executeQuery("select count(*) from DDS_AGENT_LIMIT where agent_id='" + agentId + "' and pacs_id='" + pacsId + "'");
            while (rs.next()) {
                count = Integer.parseInt(rs.getString(1));
            }
            txnType = (count > 0) ? "UPDATE" : "CREATE";
            proc = con.prepareCall("{ call operations1.DDS_AGENT_LIMIT(?,?,?,?,?,?) }");

            proc.setString(1, tellerId);
            proc.setString(2, pacsId);
            proc.setString(3, agentId);
            proc.setString(4, limit);
            proc.setString(5, txnType);
            proc.registerOutParameter(6, OracleTypes.VARCHAR);

            proc.executeUpdate();

          //  res = proc.getString(6);//error message from dds procedure

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

        return res;
    }
}
