/*
 * Author: Gagan B.
 * Date: 08-JAN-2026
 * Purpose: This class will be called when ManageSQL Transaction is submitted. 
 * It will be used to update Submitted by's value (EMP_CODE__GIVEN) with login code of whoever will submit the Manage SQL transaction.
 * */

package ibase.webitm.ejb.sys;

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

import ibase.system.config.ConnDriver;
import ibase.utility.BaseLogger;
import ibase.utility.E12GenericUtility;
import ibase.utility.UserInfoBean;
import ibase.webitm.ejb.ActionHandlerEJB;
import ibase.webitm.ejb.ITMDBAccessEJB;

public class ManageSQLSubmit extends ActionHandlerEJB {

	@Override
	public String confirm(String tranID, String xtraParams, String forcedFlag) {

		BaseLogger.log("3", null, null, "ManageSQLSubmit confirm called"
				+ "tranID + [" + tranID + "]"
				+ "xtraParams + [" + xtraParams + "]"
				+ "forcedFlag + [" + forcedFlag + "]");

		String responseStr = "";
		UserInfoBean userInfo = getUserInfo();
		Connection conn = null;

		BaseLogger.log("3", null, null, "ManageSQLSubmit userInfo" + userInfo);

		try {
			ITMDBAccessEJB itmDBAccessEJB = new ITMDBAccessEJB();

			String profileId = checkNull(userInfo.getProfileId());
			// if(!profileId.trim().equalsIgnoreCase("MSDEVTL")){

			// 	return itmDBAccessEJB.getErrorString("", "MSQLCNS", "", "", conn);

			// }
			ConnDriver connDriver = new ConnDriver();
			conn = connDriver.getConnectDB(userInfo.getTransDB());
			String ExecutiveApproval = "";
			String ExecutionRights = "";
			String wfStatus = "";
			int aprvLevel = 0;

			String checkExecAprvVal = "select exec_aprv, exec_rights, wf_status, aprv_level from sql_changes where tran_id = ?";
			PreparedStatement checkExecAprvValPs = conn.prepareStatement(checkExecAprvVal);
			checkExecAprvValPs.setString(1, tranID);
			ResultSet checkExecAprvValRs = checkExecAprvValPs.executeQuery();
			while (checkExecAprvValRs.next()) {
				ExecutiveApproval = checkNull(checkExecAprvValRs.getString("exec_aprv"));
				ExecutionRights = checkNull(checkExecAprvValRs.getString("exec_rights"));
				wfStatus = checkNull(checkExecAprvValRs.getString("wf_status"));
				aprvLevel = checkExecAprvValRs.getInt("aprv_level");


			}
			BaseLogger.log("3", null, null, "ManageSQLSubmit exec_aprv:: " + ExecutiveApproval);
			BaseLogger.log("3", null, null, "ManageSQLSubmit exec_rights:: " + ExecutionRights);
			BaseLogger.log("3", null, null, "ManageSQLSubmit wfStatus:: " + wfStatus);
			BaseLogger.log("3", null, null, "ManageSQLSubmit aprvLevel:: " + aprvLevel);

			// if(aprvLevel<1){
			// BaseLogger.log("3", null, null, "Transaction is not proceed aprvLevel:: " + aprvLevel);
			// 		return itmDBAccessEJB.getErrorString("", "MSQLCNSBP", "", "", conn);
					
			// }

			// if(!ExecutiveApproval.trim().equalsIgnoreCase("Y")){
			// 	BaseLogger.log("3", null, null, "Execution Approval is not Y cannot submit:: " + ExecutiveApproval);	
			// 		return itmDBAccessEJB.getErrorString("", "MSQLEXAPN", "", "", conn);			
			// }

			if (checkExecAprvValPs != null) {
				checkExecAprvValPs.close();
				checkExecAprvValPs = null;
			}
			if (checkExecAprvValRs != null) {
				checkExecAprvValRs.close();
				checkExecAprvValRs = null;
			}

			// if(wfStatus.trim().equalsIgnoreCase("S") || wfStatus.trim().equalsIgnoreCase("A")){
            //         BaseLogger.log("3", null, null, "Transaction already submitted....");
			// 		return itmDBAccessEJB.getErrorString("", "VTCAN127", "", "", conn);
			// }

			// if(ExecutionRights.trim().equalsIgnoreCase("A") || ExecutionRights.trim().equalsIgnoreCase("P") || ExecutionRights.trim().equalsIgnoreCase("C") || ExecutionRights.trim().equalsIgnoreCase("Q")){
            //         BaseLogger.log("3", null, null, "For Execution Rights A, P, C and Q can not be submit....");
			// 		return itmDBAccessEJB.getErrorString("", "MSQLNS", "", "", conn);
			// }
			if (ExecutiveApproval.trim().equalsIgnoreCase("Y")) {
				String updateAprvLevelSql = "update sql_changes set aprv_level = '2' where tran_id = ?";
				PreparedStatement updateAprvLevelPs = conn.prepareStatement(updateAprvLevelSql);
				updateAprvLevelPs.setString(1, tranID);
				int updateAprvLevelCount = updateAprvLevelPs.executeUpdate();
				BaseLogger.log("3", null, null, "ManageSQLSubmit updateAprvLevelCount::: " + updateAprvLevelCount);

				if (updateAprvLevelPs != null) {
					updateAprvLevelPs.close();
					updateAprvLevelPs = null;
				}
			}

			if (updateSubmittedBy(userInfo, tranID, conn)) {
				conn.commit();   
				responseStr = (new ITMDBAccessEJB()).getErrorString("", "VTSUBMTSUC", "", "", userInfo.getTransDB());
			} else {
				responseStr = (new ITMDBAccessEJB()).getErrorString("", "VTSUBMTREJ", "", "", userInfo.getTransDB());
			}
		} catch (Exception e) {
			BaseLogger.log("3", null, null, "ManageSQLSubmit Exception. [" + E12GenericUtility.getStackTrace(e) + "]");
		} finally {
			if (conn != null) {
				try {
					conn.close();
					conn = null;
				} catch (Exception e) {
					BaseLogger.log("3", null, null, "ManageSQLSubmit Exception for closing connection. ["
							+ E12GenericUtility.getStackTrace(e) + "]");

				}

			}
		}

		return responseStr;

	}

	public boolean updateSubmittedBy(UserInfoBean userInfo, String tranID, Connection conn) {
		String updateSQL = "UPDATE SQL_CHANGES SET EMP_CODE__GIVEN = ?, WF_STATUS = 'S' WHERE TRAN_ID = ?";

		try (
				PreparedStatement psmt = conn.prepareStatement(updateSQL);) {

			psmt.setString(1, userInfo.getLoginCode().trim());
			psmt.setString(2, tranID);

			int updateRowCount = psmt.executeUpdate();

			if (updateRowCount > 0) {
				return true;
			} else {
				return false;
			}

		} catch (Exception e) {
			BaseLogger.log("3", null, null,
					"updateSubmittedBy Exception. [" + E12GenericUtility.getStackTrace(e) + "]");
			return false;
		}

	}

	private String checkNull(String input) {
		if (input == null) {
			input = "";
		}
		return input;
	}

}
