-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDBExecutionUtils
More file actions
113 lines (93 loc) · 4.2 KB
/
DBExecutionUtils
File metadata and controls
113 lines (93 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package update_automate_nonautomate_hbl_api.util;
import jakarta.persistence.*;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import java.sql.*;
import java.util.Date;
import java.util.Objects;
/**
* @Project : billoflading-complexbl-api-feature
* @Author : sambitkumar.pradhan (1361629)
* @Created On : 12/23/2024 3:37 PM
*/
@Component
public class DBExecutionUtils {
@Autowired
private JdbcTemplate jdbcTemplate;
@Autowired
private SessionFactory sessionFactory;
@PersistenceContext
private EntityManager entityManager;
public void setCompany(String companyCode) {
try {
jdbcTemplate.update(Constants.SET_COMPANY_PROCEDURE, companyCode);
System.out.println("Procedure Executed, Shipping Company has been set!");
} catch (Exception e) {
System.err.println("Error executing procedure: " + e.getMessage());
e.printStackTrace();
}
}
public String getBillReferenceNumber(String companyCode, String department, String masterBlRefNo,
String customer, String customerSub) {
String nvoccBillRefNo = null;
Session session = null;
try {
// Start a session using Hibernate
session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
// Set the company in the session (using Hibernate)
setCompanyInSession(session, "0001"); // Set the company for the session
// SQL to generate the bill reference number
String sql = "{call dp_bol_number.generate(?, ?, ?, ?, ?, ?, ?, ?, ?)}";
// Execute the stored procedure
CallableStatement stmt = session.doReturningWork(connection -> {
CallableStatement callableStatement = connection.prepareCall(sql);
callableStatement.setString(1, companyCode);
callableStatement.setString(2, department);
callableStatement.setString(3, masterBlRefNo);
callableStatement.setString(6, customer);
callableStatement.setString(7, customerSub);
callableStatement.setString(9, "Y");
callableStatement.registerOutParameter(4, Types.VARCHAR); // For nvocc_bill_ref
callableStatement.registerOutParameter(5, Types.DATE); // For t_date
callableStatement.registerOutParameter(8, Types.INTEGER); // For t_bol_error
callableStatement.execute();
return callableStatement;
});
// Retrieve the results from the stored procedure
nvoccBillRefNo = stmt.getString(4);
Date tDate = stmt.getDate(5);
int tBolError = stmt.getInt(8);
System.out.println("Generated NVOC Bill Ref: " + nvoccBillRefNo);
// Commit the transaction
transaction.commit();
} catch (Exception e) {
if (session != null) {
session.getTransaction().rollback(); // Rollback in case of error
}
System.err.println("Error executing procedure: " + e.getMessage());
e.printStackTrace();
} finally {
if (session != null && session.isOpen()) {
session.close(); // Always close the session
}
}
return nvoccBillRefNo;
}
// Helper method to set the company in the session (calls the procedure)
private void setCompanyInSession(Session session, String companyCode) {
// Use native SQL or a stored procedure to set the company in the Hibernate session
String setCompanySql = "{ call dp_mltc.set_company(?) }";
// Using Hibernate session to execute a native SQL query
session.doWork(connection -> {
try (CallableStatement stmt = connection.prepareCall(setCompanySql)) {
stmt.setString(1, companyCode);
stmt.execute(); // Set company for the session
}
});
}
}