/src/main/java/com/ex/DAO/MBSDAO.java

https://gitlab.com/kevinnguye86/DreamWalker_Test · Java · 475 lines · 364 code · 96 blank · 15 comment · 3 complexity · ed3c75aaa9eb394c6de56751730ffa1c MD5 · raw file

  1. package com.ex.DAO;
  2. import java.sql.Connection;
  3. import java.sql.Date;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import com.ex.Util.ConnFactory;
  10. import com.ex.pojos.Loan;
  11. import com.ex.pojos.MBS;
  12. public class MBSDAO {
  13. //private static ConnFactory connFactory = ConnFactory.getConnFactory();
  14. // here is where the queries will go.
  15. private static final String MBS_SUM_AMOUNT = "update MBS Set sum_amount =(Select sum(amount) from Loan where mbs_id=?)where mbs_id=?";
  16. private static final String MBS_AVG_RATE ="update MBS Set avg_rate =(Select avg(rate) from Loan where mbs_id=?)where mbs_id=?";
  17. private static final String MBS_NUM_LOANS = "update MBS Set num_loans =(Select count(*) from Loan where mbs_id=?)where mbs_id=?";
  18. private static final String MBS_APPROVED = "update MBS set status='approved', approved_date=?, denial_reason = null where mbs_id=?";
  19. private static final String MBS_DENIED = "update MBS set status='denied', denial_reason=? where mbs_id=?";
  20. private static final String MBS_SUBMIT = "update MBS set status='pending' where mbs_id=?";
  21. private static final String MBS_NEW = "insert into MBS values(?,?,?,?,?,?,null,'pending',null)";
  22. private static final String MBS_UPDATE = "update MBS set SMO=?, bank_id=?";
  23. private static final String ALL_MBS = "select mbs_id, bank_name, sum_amount, avg_rate, num_of_loans, SMO, approved_date, status, denial_reason, b.bank_id from MBS m left join bank b on m.bank_id = b.bank_id order by mbs_id";
  24. private static final String ALL_MBS_WITH_STATUS = "select mbs_id, bank_name, sum_amount, avg_rate, num_of_loans, SMO, approved_date, status, denial_reason, b.bank_id from MBS m left join bank b on m.bank_id = b.bank_id where m.status=? order by mbs_id";
  25. private static final String THIS_MBS = "select mbs_id, bank_name, sum_amount, avg_rate, num_of_loans, SMO, approved_date, status, denial_reason, b.bank_id from MBS m left join bank b on m.bank_id = b.bank_id where m.mbs_id=? order by mbs_id";
  26. private static final String DENIED_MBS_SAVE = "update MBS set num_of_loans=?, sum_amount=?, avg_rate=?, SMO=? where mbs_id=?";
  27. private static final String DENIED_MBS_SUBMIT = "update MBS set num_of_loans=?, sum_amount=?, avg_rate=?, SMO=?, denial_reason=null, status='pending' where mbs_id=?";
  28. public static void save_denied_mbs(int num_loans, double sum, double avg, String smo, int mbs_id, String[] loans){
  29. try
  30. {
  31. Connection conn = ConnFactory.getConnection();
  32. PreparedStatement pstmt = conn.prepareStatement(DENIED_MBS_SAVE);
  33. pstmt.setInt(1, num_loans);
  34. pstmt.setDouble(2, sum);
  35. pstmt.setDouble(3, avg);
  36. pstmt.setString(4, smo);
  37. pstmt.setInt(5, mbs_id);
  38. pstmt.executeUpdate();
  39. String sql = "update loans set mbs_id=null where mbs_id=?";
  40. pstmt = conn.prepareStatement(sql);
  41. pstmt.setInt(1, mbs_id);
  42. pstmt.executeUpdate();
  43. sql = "update loans set mbs_id=? where loan_id in (" + loans[0];
  44. for(int i = 1; i < loans.length; i++){
  45. sql += ", " + loans[i];
  46. }
  47. sql += ")";
  48. pstmt = conn.prepareStatement(sql);
  49. pstmt.setInt(1, mbs_id);
  50. pstmt.executeUpdate();
  51. pstmt.close();
  52. conn.close();
  53. } catch (SQLException e) {
  54. e.printStackTrace();
  55. }
  56. }
  57. public static void submit_denied_mbs(int num_loans, double sum, double avg, String smo, int mbs_id, String[] loans){
  58. try
  59. {
  60. Connection conn = ConnFactory.getConnection();
  61. PreparedStatement pstmt = conn.prepareStatement(DENIED_MBS_SUBMIT);
  62. pstmt.setInt(1, num_loans);
  63. pstmt.setDouble(2, sum);
  64. pstmt.setDouble(3, avg);
  65. pstmt.setString(4, smo);
  66. pstmt.setInt(5, mbs_id);
  67. pstmt.executeUpdate();
  68. String sql = "update loans set mbs_id=null where mbs_id=?";
  69. pstmt = conn.prepareStatement(sql);
  70. pstmt.setInt(1, mbs_id);
  71. pstmt.executeUpdate();
  72. sql = "update loans set mbs_id=? where loan_id in (" + loans[0];
  73. for(int i = 1; i < loans.length; i++){
  74. sql += ", " + loans[i];
  75. }
  76. sql += ")";
  77. pstmt = conn.prepareStatement(sql);
  78. pstmt.setInt(1, mbs_id);
  79. pstmt.executeUpdate();
  80. pstmt.close();
  81. conn.close();
  82. } catch (SQLException e) {
  83. e.printStackTrace();
  84. }
  85. }
  86. public static List<MBS> AllMBS()
  87. {
  88. List<MBS> mbs = new ArrayList<MBS>();
  89. try(Connection conn = ConnFactory.getConnection();
  90. PreparedStatement pstmt = conn.prepareStatement(ALL_MBS);)
  91. {
  92. ResultSet rs = pstmt.executeQuery();
  93. while(rs.next())
  94. {
  95. MBS m = new MBS();
  96. m.setMbs_id(rs.getInt(1));
  97. m.setBank_name(rs.getString(2));
  98. m.setSum_amount(rs.getDouble(3));
  99. m.setAvg_rate(rs.getDouble(4));
  100. m.setNum_loans(rs.getInt(5));
  101. m.setSMO(rs.getString(6));
  102. m.setApproved_date(rs.getDate(7));
  103. m.setStatus(rs.getString(8));
  104. m.setDenial_reason(rs.getString(9));
  105. m.setBank_id(rs.getInt(10));
  106. mbs.add(m);
  107. }
  108. pstmt.close();
  109. conn.close();
  110. } catch (SQLException e) {
  111. // TODO Auto-generated catch block
  112. e.printStackTrace();
  113. }
  114. return mbs;
  115. }
  116. public static List<MBS> StatusMBS(String status)
  117. {
  118. List<MBS> mbs = new ArrayList<MBS>();
  119. try(Connection conn = ConnFactory.getConnection();
  120. PreparedStatement pstmt = conn.prepareStatement(ALL_MBS_WITH_STATUS);)
  121. {
  122. pstmt.setString(1, status);
  123. ResultSet rs = pstmt.executeQuery();
  124. while(rs.next())
  125. {
  126. MBS m = new MBS();
  127. m.setMbs_id(rs.getInt(1));
  128. m.setBank_name(rs.getString(2));
  129. m.setSum_amount(rs.getInt(3));
  130. m.setAvg_rate(rs.getInt(4));
  131. m.setNum_loans(rs.getInt(5));
  132. m.setSMO(rs.getString(6));
  133. m.setApproved_date(rs.getDate(7));
  134. m.setStatus(rs.getString(8));
  135. m.setDenial_reason(rs.getString(9));
  136. m.setBank_id(rs.getInt(10));
  137. mbs.add(m);
  138. }
  139. pstmt.close();
  140. conn.close();
  141. } catch (SQLException e) {
  142. // TODO Auto-generated catch block
  143. e.printStackTrace();
  144. }
  145. return mbs;
  146. }
  147. public static List<MBS> BankStatusMBS(int bank_id, String status)
  148. {
  149. String sql = "select mbs_id, bank_name, sum_amount, avg_rate, num_of_loans, SMO, approved_date,"
  150. + " status, denial_reason, b.bank_id from MBS m left join bank b on m.bank_id = b.bank_id"
  151. + " where m.status=? AND b.bank_id=? order by mbs_id";
  152. List<MBS> mbs = new ArrayList<MBS>();
  153. try(Connection conn = ConnFactory.getConnection();
  154. PreparedStatement pstmt = conn.prepareStatement(sql);)
  155. {
  156. pstmt.setString(1, status);
  157. pstmt.setInt(2, bank_id);
  158. ResultSet rs = pstmt.executeQuery();
  159. while(rs.next())
  160. {
  161. MBS m = new MBS();
  162. m.setMbs_id(rs.getInt(1));
  163. m.setBank_name(rs.getString(2));
  164. m.setSum_amount(rs.getDouble(3));
  165. m.setAvg_rate(rs.getDouble(4));
  166. m.setNum_loans(rs.getInt(5));
  167. m.setSMO(rs.getString(6));
  168. m.setApproved_date(rs.getDate(7));
  169. m.setStatus(rs.getString(8));
  170. m.setDenial_reason(rs.getString(9));
  171. m.setBank_id(rs.getInt(10));
  172. mbs.add(m);
  173. }
  174. pstmt.close();
  175. conn.close();
  176. } catch (SQLException e) {
  177. // TODO Auto-generated catch block
  178. e.printStackTrace();
  179. }
  180. return mbs;
  181. }
  182. public static MBS ThisMBS(int mbs_id)
  183. {
  184. MBS m = new MBS();
  185. try(Connection conn = ConnFactory.getConnection();
  186. PreparedStatement pstmt = conn.prepareStatement(THIS_MBS);)
  187. {
  188. pstmt.setInt(1, mbs_id);
  189. ResultSet rs = pstmt.executeQuery();
  190. rs.next();
  191. m.setMbs_id(rs.getInt(1));
  192. m.setBank_name(rs.getString(2));
  193. m.setSum_amount(rs.getDouble(3));
  194. m.setAvg_rate(rs.getDouble(4));
  195. m.setNum_loans(rs.getInt(5));
  196. m.setSMO(rs.getString(6));
  197. m.setApproved_date(rs.getDate(7));
  198. m.setStatus(rs.getString(8));
  199. m.setDenial_reason(rs.getString(9));
  200. m.setBank_id(rs.getInt(10));
  201. pstmt.close();
  202. conn.close();
  203. } catch (SQLException e) {
  204. // TODO Auto-generated catch block
  205. e.printStackTrace();
  206. }
  207. return m;
  208. }
  209. public static void Sum(int mbs_id)
  210. {
  211. try(Connection conn = ConnFactory.getConnection();
  212. PreparedStatement pstmt = conn.prepareStatement(MBS_SUM_AMOUNT);)
  213. {
  214. pstmt.setInt(1, mbs_id);
  215. pstmt.setInt(2, mbs_id);
  216. pstmt.executeUpdate();
  217. pstmt.close();
  218. conn.close();
  219. } catch (SQLException e) {
  220. // TODO Auto-generated catch block
  221. e.printStackTrace();
  222. }
  223. }
  224. public static void Rate(int mbs_id)
  225. {
  226. try(Connection conn = ConnFactory.getConnection();
  227. PreparedStatement pstmt = conn.prepareStatement(MBS_AVG_RATE);)
  228. {
  229. pstmt.setInt(1, mbs_id);
  230. pstmt.setInt(2, mbs_id);
  231. pstmt.executeUpdate();
  232. pstmt.close();
  233. conn.close();
  234. } catch (SQLException e) {
  235. // TODO Auto-generated catch block
  236. e.printStackTrace();
  237. }
  238. }
  239. public static void NumLoans(int mbs_id)
  240. {
  241. try(Connection conn = ConnFactory.getConnection();
  242. PreparedStatement pstmt = conn.prepareStatement(MBS_NUM_LOANS);)
  243. {
  244. pstmt.setInt(1, mbs_id);
  245. pstmt.setInt(2, mbs_id);
  246. pstmt.executeUpdate();
  247. pstmt.close();
  248. conn.close();
  249. } catch (SQLException e) {
  250. // TODO Auto-generated catch block
  251. e.printStackTrace();
  252. }
  253. }
  254. public static void Approved(int mbs_id)
  255. {
  256. try(Connection conn = ConnFactory.getConnection();
  257. PreparedStatement pstmt = conn.prepareStatement(MBS_APPROVED);)
  258. {
  259. pstmt.setDate(1, new Date(System.currentTimeMillis()));
  260. pstmt.setInt(2, mbs_id);
  261. pstmt.executeUpdate();
  262. pstmt.close();
  263. conn.close();
  264. } catch (SQLException e) {
  265. // TODO Auto-generated catch block
  266. e.printStackTrace();
  267. }
  268. }
  269. public static void Denied(String denial_reason, int mbs_id)
  270. {
  271. try(Connection conn = ConnFactory.getConnection();
  272. PreparedStatement pstmt = conn.prepareStatement(MBS_DENIED);)
  273. {
  274. pstmt.setString(1,denial_reason);
  275. pstmt.setInt(2, mbs_id);
  276. pstmt.executeUpdate();
  277. pstmt.close();
  278. conn.close();
  279. } catch (SQLException e) {
  280. // TODO Auto-generated catch block
  281. e.printStackTrace();
  282. }
  283. }
  284. public static void Submit(int mbs_id)
  285. {
  286. try(Connection conn = ConnFactory.getConnection();
  287. PreparedStatement pstmt = conn.prepareStatement(MBS_SUBMIT);)
  288. {
  289. pstmt.setInt(1, mbs_id);
  290. pstmt.executeUpdate();
  291. pstmt.close();
  292. conn.close();
  293. } catch (SQLException e) {
  294. // TODO Auto-generated catch block
  295. e.printStackTrace();
  296. }
  297. }
  298. public static int NewMBS(int bank_id, double sum, double avg_rate, int num_loans, String smo)
  299. {
  300. int mbs_id=0;
  301. try
  302. {
  303. Connection conn = ConnFactory.getConnection();
  304. PreparedStatement pstmt = null;
  305. pstmt = conn.prepareStatement("select max(mbs_id) from MBS");
  306. ResultSet rs = pstmt.executeQuery();
  307. rs.next();
  308. mbs_id = rs.getInt(1) + 1;
  309. pstmt = conn.prepareStatement(MBS_NEW);
  310. pstmt.setInt(1, mbs_id);
  311. pstmt.setInt(2, bank_id);
  312. pstmt.setDouble(3, sum);
  313. pstmt.setDouble(4, avg_rate);
  314. pstmt.setInt(5, num_loans);
  315. pstmt.setString(6, smo);
  316. pstmt.executeUpdate();
  317. pstmt = conn.prepareStatement("update loans set mbs_id = ? where bank_id = ? and mbs_id is null");
  318. pstmt.setInt(1, mbs_id);
  319. pstmt.setInt(2, bank_id);
  320. pstmt.executeUpdate();
  321. pstmt.close();
  322. conn.close();
  323. } catch (SQLException e) {
  324. // TODO Auto-generated catch block
  325. e.printStackTrace();
  326. }
  327. return mbs_id;
  328. }
  329. public static int NewManualMBS(int bank_id, double sum, double avg_rate, int num_loans, String smo, String[] loan_id_list)
  330. {
  331. int mbs_id=0;
  332. String sql = "update loans set mbs_id=? where loan_id in ("+ loan_id_list[0];
  333. for(String str: loan_id_list){
  334. sql += ", " + str;
  335. }
  336. sql += ")";
  337. try
  338. {
  339. Connection conn = ConnFactory.getConnection();
  340. PreparedStatement pstmt = null;
  341. pstmt = conn.prepareStatement("select max(mbs_id) from MBS");
  342. ResultSet rs = pstmt.executeQuery();
  343. rs.next();
  344. mbs_id = rs.getInt(1) + 1;
  345. pstmt = conn.prepareStatement(MBS_NEW);
  346. pstmt.setInt(1, mbs_id);
  347. pstmt.setInt(2, bank_id);
  348. pstmt.setDouble(3, sum);
  349. pstmt.setDouble(4, avg_rate);
  350. pstmt.setInt(5, num_loans);
  351. pstmt.setString(6, smo);
  352. pstmt.executeUpdate();
  353. pstmt = conn.prepareStatement(sql);
  354. pstmt.setInt(1, mbs_id);
  355. pstmt.executeUpdate();
  356. pstmt.close();
  357. conn.close();
  358. } catch (SQLException e) {
  359. // TODO Auto-generated catch block
  360. e.printStackTrace();
  361. }
  362. return mbs_id;
  363. }
  364. public static void UpdateMBS( String SMO, int bank_id)
  365. {
  366. try(Connection conn = ConnFactory.getConnection();
  367. PreparedStatement pstmt = conn.prepareStatement(MBS_UPDATE);)
  368. {
  369. pstmt.setString(1, SMO);
  370. pstmt.setInt(2, bank_id);
  371. pstmt.executeUpdate();
  372. pstmt.close();
  373. conn.close();
  374. } catch (SQLException e) {
  375. // TODO Auto-generated catch block
  376. e.printStackTrace();
  377. }
  378. }
  379. }