AS400ServiceInterface.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. package id.co.hanoman.service.as400;
  2. import java.math.BigDecimal;
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.Statement;
  8. import java.util.ArrayList;
  9. import java.util.LinkedList;
  10. import java.util.List;
  11. import javax.naming.InitialContext;
  12. import javax.sql.DataSource;
  13. import org.apache.commons.logging.Log;
  14. import org.apache.commons.logging.LogFactory;
  15. import id.co.hanoman.service.PersistanceServiceInterface;
  16. import id.co.hanoman.service.ServiceUtil;
  17. import id.co.hanoman.service.ePayment.AppSetting;
  18. import id.co.hanoman.service.mwapp.SysParam;
  19. public class AS400ServiceInterface {
  20. protected Log log = LogFactory.getLog(this.getClass());
  21. public Branch[] getBranchList(String mwappDs, String branchCode) {
  22. return new Branch[] {};
  23. }
  24. public Branch[] getBranchList(String mwappDs, BigDecimal branchCodeStart, BigDecimal branchCodeEnd) {
  25. Connection conn = null;
  26. PreparedStatement ps = null;
  27. ResultSet rs = null;
  28. Branch[] result = null;
  29. try {
  30. System.out.println("before connect");
  31. conn = initialAS400Connection();
  32. System.out.println("after connect");
  33. String as400Schema = getAppSettingValue(mwappDs, "EPAYMENT_AS400_SCHEMA", "I");
  34. String sql = "SELECT JHDATA.JDBR, JHDATA.JDNAME FROM PAR" + as400Schema + "SIBS.JHDATA JHDATA WHERE JHDATA.JDBR >= ? AND JHDATA.JDBR <= ?";
  35. ps = conn.prepareStatement(sql);
  36. ps.setBigDecimal(1, branchCodeStart);
  37. ps.setBigDecimal(2, branchCodeEnd);
  38. rs = ps.executeQuery();
  39. System.out.println("BRANCH");
  40. List<Branch> list = new ArrayList<Branch>();
  41. while (rs.next()) {
  42. String code = rs.getString(1);
  43. String name = rs.getString(2);
  44. Branch branch = new Branch();
  45. branch.setCode(code);
  46. branch.setName(name);
  47. list.add(branch);
  48. }
  49. result = new Branch[list.size()];
  50. result = list.toArray(result);
  51. }catch (Exception e) {
  52. e.printStackTrace();
  53. } finally {
  54. if (rs != null) {
  55. try {
  56. rs.close();
  57. } catch (Exception e) {
  58. }
  59. }
  60. if (ps != null) {
  61. try {
  62. ps.close();
  63. } catch (Exception e) {
  64. }
  65. }
  66. if (conn != null) {
  67. try {
  68. System.out.println("-------------- Closing Connection AS400 ---------------");
  69. conn.close();
  70. } catch (Exception e) {
  71. }
  72. }
  73. }
  74. return result;
  75. }
  76. public KeperluanBayar[] getKeperluanBayar(String mwappDs, String univCode) {
  77. Connection conn = null;
  78. PreparedStatement ps = null;
  79. ResultSet rs = null;
  80. KeperluanBayar[] result = null;
  81. try {
  82. System.out.println("before connect");
  83. conn = initialAS400Connection();
  84. System.out.println("after connect");
  85. String as400Schema = getAppSettingValue(mwappDs, "EPAYMENT_AS400_SCHEMA", "I");
  86. String sql = "SELECT UPLPAR.UNPYTP, UPLPAR.UNPYDS FROM BW" + as400Schema + "DAT.UPLPAR AS UPLPAR WHERE UPLPAR.UNVCOD = ?";
  87. ps = conn.prepareStatement(sql);
  88. ps.setString(1, univCode);
  89. rs = ps.executeQuery();
  90. List<KeperluanBayar> list = new ArrayList<KeperluanBayar>();
  91. while (rs.next()) {
  92. String code = rs.getString(1);
  93. String name = rs.getString(2);
  94. KeperluanBayar keperluanBayar = new KeperluanBayar();
  95. keperluanBayar.setCode(code);
  96. keperluanBayar.setName(name);
  97. list.add(keperluanBayar);
  98. }
  99. result = new KeperluanBayar[list.size()];
  100. result = list.toArray(result);
  101. }catch (Exception e) {
  102. e.printStackTrace();
  103. } finally {
  104. if (rs != null) {
  105. try {
  106. rs.close();
  107. } catch (Exception e) {
  108. }
  109. }
  110. if (ps != null) {
  111. try {
  112. ps.close();
  113. } catch (Exception e) {
  114. }
  115. }
  116. if (conn != null) {
  117. try {
  118. System.out.println("-------------- Closing Connection AS400 ---------------");
  119. conn.close();
  120. } catch (Exception e) {
  121. }
  122. }
  123. }
  124. return result;
  125. }
  126. public Teller getTellerByGroupAndId(String mwappDs, String group, String telId) {
  127. Connection conn = null;
  128. PreparedStatement ps = null;
  129. ResultSet rs = null;
  130. Teller teller = null;
  131. try {
  132. System.out.println("before connect");
  133. conn = initialAS400Connection();
  134. System.out.println("after connect");
  135. String as400Schema = getParamValue(mwappDs, "MDW_AS400_SCHEMA");
  136. String sql = "SELECT TLTEL.TLNUM, TLTEL.TLNAME, TLCTLU.TLCTL FROM SET" + as400Schema + "ENV.TLTEL AS TLTEL JOIN SET" + as400Schema + "ENV.TLCTLU AS TLCTLU ON TLTEL.TLTBRN = TLCTLU.TLBRN# WHERE TLTEL.TLTTYP = ? AND TLTEL.TLNUM = ?";
  137. ps = conn.prepareStatement(sql);
  138. ps.setString(1, group);
  139. ps.setString(2, telId);
  140. rs = ps.executeQuery();
  141. List<Teller> list = new ArrayList<Teller>();
  142. if (rs.next()) {
  143. teller = new Teller();
  144. String tellerId = rs.getString(1);
  145. String tellerName = rs.getString(2);
  146. String ctrlUnitId = rs.getString(3);
  147. teller.setTellerName(tellerName);
  148. teller.setTellerId(tellerId);
  149. teller.setCtrlUnitId(ctrlUnitId);
  150. teller.setGroup(group);
  151. }
  152. }catch (Exception e) {
  153. e.printStackTrace();
  154. } finally {
  155. if (rs != null) {
  156. try {
  157. rs.close();
  158. } catch (Exception e) {
  159. }
  160. }
  161. if (ps != null) {
  162. try {
  163. ps.close();
  164. } catch (Exception e) {
  165. }
  166. }
  167. if (conn != null) {
  168. try {
  169. System.out.println("-------------- Closing Connection AS400 ---------------");
  170. conn.close();
  171. } catch (Exception e) {
  172. }
  173. }
  174. }
  175. return teller;
  176. }
  177. public Teller[] getTellerList(String mwappDs, String group) {
  178. Connection conn = null;
  179. PreparedStatement ps = null;
  180. ResultSet rs = null;
  181. Teller[] result = null;
  182. try {
  183. System.out.println("before connect");
  184. conn = initialAS400Connection();
  185. System.out.println("after connect");
  186. String as400Schema = getParamValue(mwappDs, "MDW_AS400_SCHEMA");
  187. String sql = "SELECT TLTEL.TLNUM, TLTEL.TLNAME, TLCTLU.TLCTL FROM SET" + as400Schema + "ENV.TLTEL AS TLTEL JOIN SET" + as400Schema + "ENV.TLCTLU AS TLCTLU ON TLTEL.TLTBRN = TLCTLU.TLBRN# WHERE TLTEL.TLTTYP = ? ORDER BY TLTEL.TLNUM";
  188. ps = conn.prepareStatement(sql);
  189. ps.setString(1, group);
  190. rs = ps.executeQuery();
  191. List<Teller> list = new ArrayList<Teller>();
  192. while (rs.next()) {
  193. String tellerId = rs.getString(1);
  194. String tellerName = rs.getString(2);
  195. String ctrlUnitId = rs.getString(3);
  196. Teller teller = new Teller();
  197. teller.setTellerName(tellerName);
  198. teller.setTellerId(tellerId);
  199. teller.setCtrlUnitId(ctrlUnitId);
  200. teller.setGroup(group);
  201. list.add(teller);
  202. }
  203. result = new Teller[list.size()];
  204. result = list.toArray(result);
  205. }catch (Exception e) {
  206. e.printStackTrace();
  207. } finally {
  208. if (rs != null) {
  209. try {
  210. rs.close();
  211. } catch (Exception e) {
  212. }
  213. }
  214. if (ps != null) {
  215. try {
  216. ps.close();
  217. } catch (Exception e) {
  218. }
  219. }
  220. if (conn != null) {
  221. try {
  222. System.out.println("-------------- Closing Connection AS400 ---------------");
  223. conn.close();
  224. } catch (Exception e) {
  225. }
  226. }
  227. }
  228. return result;
  229. }
  230. public BillerQuery[] queryBiller(String mwappDs, String filter) {
  231. Connection xconn = null;
  232. Statement stmtg = null;
  233. ResultSet rsg = null;
  234. BillerQuery[] rs = null;
  235. try {
  236. System.out.println("before connect");
  237. xconn = initialAS400Connection();
  238. System.out.println("after connect");
  239. String as400Schema = getAppSettingValue(mwappDs, "EPAYMENT_AS400_SCHEMA", "I");
  240. stmtg = xconn.createStatement();
  241. if(filter == null) filter = new String();
  242. String sql = "SELECT UNCODE, UNNAME FROM BW" + as400Schema + "DAT.unlist WHERE 1=1 " + filter;
  243. rsg = stmtg.executeQuery(sql);
  244. List<BillerQuery> list = new ArrayList<BillerQuery>();
  245. while (rsg.next()) {
  246. String code = rsg.getString(1).trim();
  247. String name = rsg.getString(2).trim();
  248. BillerQuery billerQuery = new BillerQuery();
  249. billerQuery.setCode(code);
  250. billerQuery.setName(name);
  251. list.add(billerQuery);
  252. }
  253. rs = new BillerQuery[list.size()];
  254. rs = list.toArray(rs);
  255. System.out.println("Total Biller Query ::::: "+rs.length);
  256. }catch (Exception e) {
  257. e.printStackTrace();
  258. } finally {
  259. if (rsg != null) {
  260. try {
  261. rsg.close();
  262. } catch (Exception e) {
  263. }
  264. }
  265. if (stmtg != null) {
  266. try {
  267. stmtg.close();
  268. } catch (Exception e) {
  269. }
  270. }
  271. if (xconn != null) {
  272. try {
  273. System.out.println("------------------------- Closing Connection AS400 -------------------------");
  274. xconn.close();
  275. } catch (Exception e) {
  276. }
  277. }
  278. }
  279. return rs;
  280. }
  281. private Connection initialAS400Connection() {
  282. Connection conn = null;
  283. try {
  284. // ----------------- jdbc -----------------------
  285. // System.out.println("connecting as400 jdbc");
  286. // Class.forName("com.ibm.as400.access.AS400JDBCDriver");
  287. // conn = DriverManager.getConnection("jdbc:as400://172.18.30.104/BWIDAT", "BTNSOA", "BTNSOA");
  288. // ----------------- jndi -----------------------
  289. System.out.println("connecting as400 datasource");
  290. InitialContext ctx = new InitialContext();
  291. DataSource ds = (DataSource) ctx.lookup("jdbc/as400/btn_ds");
  292. conn = ds.getConnection();
  293. System.out.println("connected as400");
  294. } catch (Exception e) {
  295. e.printStackTrace();
  296. throw new RuntimeException(e.getMessage(), e);
  297. }
  298. return conn;
  299. }
  300. private String getAppSettingValue(String ds, String code, String defaultValue) throws Exception {
  301. String settingValue = defaultValue;
  302. try {
  303. if (code == null || "".equals(code)) {
  304. } else {
  305. PersistanceServiceInterface psiEpay = ServiceUtil.getDaoClient(null);
  306. AppSetting appSetting = (AppSetting) psiEpay.queryById(ds, "AppSetting", code, null);
  307. if (appSetting != null) {
  308. settingValue = appSetting.getSettingValue();
  309. if (log.isDebugEnabled())
  310. log.debug("settingValue[" + code + "] :: " + settingValue);
  311. if (settingValue == null || "".equals(settingValue))
  312. settingValue = defaultValue;
  313. }
  314. }
  315. } catch (Exception e) {
  316. log.error(e.getMessage(), e);
  317. throw new RuntimeException(e.getMessage(), e);
  318. }
  319. return settingValue;
  320. }
  321. private String getParamValue(String ds, String code) throws Exception {
  322. String paramValue = null;
  323. try {
  324. if (code == null || "".equals(code)) {
  325. } else {
  326. PersistanceServiceInterface psiEpay = ServiceUtil.getDaoClient(null);
  327. SysParam sysParam = (SysParam) psiEpay.queryById(ds, "SysParam", code, null);
  328. if (sysParam != null) {
  329. paramValue = sysParam.getParamValue();
  330. if (log.isDebugEnabled())
  331. log.debug("paramValue[" + code + "] :: " + paramValue);
  332. if (paramValue == null || "".equals(paramValue))
  333. paramValue = null;
  334. }
  335. }
  336. } catch (Exception e) {
  337. log.error(e.getMessage(), e);
  338. throw new RuntimeException(e.getMessage(), e);
  339. }
  340. return paramValue;
  341. }
  342. }