Repository navigation
Expand file tree
/
Copy pathBillRepository.java
More file actions
151 lines (132 loc) · 6 KB
/
Copy pathBillRepository.java
File metadata and controls
151 lines (132 loc) · 6 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package openconsignment.repository;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.inject.Inject;
import openconsignment.common.SqlLoader;
import openconsignment.common.SqlQuery;
import openconsignment.entity.BillEntity;
import openconsignment.entity.LREntity;
public class BillRepository {
private final Connection conn;
@Inject
public BillRepository(Connection conn) {
this.conn = conn;
}
public void save(BillEntity billEntity) throws SQLException {
try {
String sql = SqlLoader.load(SqlQuery.BILL_SAVE);
conn.setAutoCommit(false);
deleteBill(billEntity.getBillNo());
PreparedStatement preparedStatement = conn.prepareStatement(sql);
preparedStatement.setString(1, billEntity.getSupplierName());
preparedStatement.setString(2, billEntity.getBuyerName());
preparedStatement.setString(3, billEntity.getBillNo());
preparedStatement.setString(4, billEntity.getBillDate());
preparedStatement.setString(5, billEntity.getTransport());
preparedStatement.setString(6, billEntity.getLrDate());
preparedStatement.setString(7, billEntity.getBillAmount());
preparedStatement.setString(8, billEntity.getBillAmount());
preparedStatement.setString(9, billEntity.getBillAmount());
preparedStatement.execute();
if (null != billEntity.getLrEntities()) {
for (LREntity lr : billEntity.getLrEntities()) {
sql = SqlLoader.load(SqlQuery.BILL_SAVE_LR);
preparedStatement = conn.prepareStatement(sql);
preparedStatement.setString(1, billEntity.getBillNo());
preparedStatement.setString(2, lr.getLrNo());
preparedStatement.setString(3, lr.getPm());
preparedStatement.execute();
}
}
conn.commit();
} catch (SQLException ex) {
conn.rollback();
Logger.getLogger(BillRepository.class.getName()).log(Level.SEVERE, ex.toString(), ex);
throw ex;
}
}
public BillEntity getBill(String billNo) throws SQLException {
try {
String sql = SqlLoader.load(SqlQuery.BILL_GET);
PreparedStatement preparedStatement = conn.prepareStatement(sql);
preparedStatement.setString(1, billNo);
ResultSet billResultSet = preparedStatement.executeQuery();
if (billResultSet.isClosed()) {
return null;
}
BillEntity billEntity = BillEntity.builder()
.billNo(billResultSet.getString("Bill No."))
.supplierName(billResultSet.getString("Supplier Name"))
.buyerName(billResultSet.getString("Buyer Name"))
.billDate(billResultSet.getString("Bill Date"))
.transport(billResultSet.getString("Transport"))
.lrDate(billResultSet.getString("LR Date"))
.billAmount(billResultSet.getString("Bill Amount"))
.build();
sql = SqlLoader.load(SqlQuery.BILL_GET_LRS);
preparedStatement = conn.prepareStatement(sql);
preparedStatement.setString(1, billNo);
ResultSet lrPmResultSet = preparedStatement.executeQuery();
List<LREntity> lrpmList = new ArrayList<>();
while (lrPmResultSet.next()) {
lrpmList.add(new LREntity(
lrPmResultSet.getString("Bill No."),
lrPmResultSet.getString("LR No."),
lrPmResultSet.getString("PM")));
}
billEntity.setLrEntities(lrpmList);
return billEntity;
} catch (SQLException ex) {
Logger.getLogger(BillRepository.class.getName()).log(Level.SEVERE, ex.toString(), ex);
throw ex;
}
}
public void deleteBill(String billNo) throws SQLException {
try {
conn.setAutoCommit(false);
String sql = SqlLoader.load(SqlQuery.BILL_DELETE);
PreparedStatement preparedStatement = conn.prepareStatement(sql);
preparedStatement.setString(1, billNo);
preparedStatement.execute();
String sql2 = SqlLoader.load(SqlQuery.BILL_DELETE_LRS);
preparedStatement = conn.prepareStatement(sql2);
preparedStatement.setString(1, billNo);
preparedStatement.execute();
conn.commit();
} catch (SQLException ex) {
conn.rollback();
Logger.getLogger(BillRepository.class.getName()).log(Level.SEVERE, ex.toString(), ex);
throw ex;
}
}
public List<BillEntity> getBills() throws SQLException {
List<BillEntity> res = new ArrayList<>();
try {
String sql = SqlLoader.load(SqlQuery.BILL_GET_ALL);
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
res.add(BillEntity.builder()
.supplierName(rs.getString("Supplier Name"))
.buyerName(rs.getString("Buyer Name"))
.billNo(rs.getString("Bill No."))
.billDate(rs.getString("Bill Date"))
.transport(rs.getString("Transport"))
.lrDate(rs.getString("LR Date"))
.billAmount(rs.getString("Bill Amount"))
// .lrEntities() skipped as it is not required
.build());
}
return res;
} catch (SQLException ex) {
Logger.getLogger(BillRepository.class.getName()).log(Level.SEVERE, ex.toString(), ex);
throw ex;
}
}
}