package com.gcc.erppostdata.mudhalvarpadaipagam; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.gcc.erppostdata.config.AppConfig; import com.gcc.erppostdata.repo.CoworkLogRepo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Component; import javax.sql.DataSource; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.text.SimpleDateFormat; import java.time.LocalDate; import java.util.*; @Component public class CWPostData { @Autowired private JdbcTemplate jdbcTemplate; @Autowired private AppConfig appConfig; @Autowired private CoworkLogRepo coworkLogRepo; private List> coworkDataList; // @PersistenceContext(unitName = "coworkspace") // private EntityManager entityManager; // @Autowired // @Qualifier("mysqlmudhalvarpadaipagamDataSource") // private EntityManager entityManager; @Autowired public void setDataSource(@Qualifier("mysqlmudhalvarpadaipagamDataSource") DataSource dataSource) { this.jdbcTemplate = new JdbcTemplate(dataSource); } public void fetchCoworkData(String sql, String id) { System.out.println("SQL: " + sql); coworkDataList = new ArrayList<>(); List> results = jdbcTemplate.queryForList(sql); for (Map row : results) { Map data = new TreeMap<>(); String assessmentNo = LocalDate.now().getYear() + "/MUTHAPADAI/" + row.get("booking_id").toString(); // Static mandatory input data.put("serviceId", "getOtherCollection"); data.put("COLLECTION_TYPE", "Muthalvar Padaippagam"); data.put("TOTALTRANSACTIONCODE", "450310000"); // Unique data data.put("ASSESSMENTNO", assessmentNo); data.put("UNIQUERECEIPTID", row.get("booking_id").toString()); data.put("TRANSACTIONNO", row.get("order_id").toString()); data.put("TRANSACTIONREFNO", row.get("payment_id").toString()); data.put("TRANSACTIONDATE", row.get("paymentdate").toString()); data.put("TOTALTRANSACTIONAMOUNT", Double.parseDouble(row.get("amount").toString())); // Amount breakdown data.put("MSTRCODEONE", "019"); data.put("SPLITAMOUNTONE", row.get("amount").toString()); // Party details data.put("PARTYNAME", row.get("user_name").toString()); data.put("PARTYMOBILE", row.get("mobile").toString()); data.put("PARTYADDRESS", "NA");// For internal tracking coworkDataList.add(data); } postDataToERP(coworkDataList, id); } public void postDataToERP(List> coworkList, String id) { for (Map data : coworkList) { StringBuilder response = new StringBuilder(); System.out.println("Sending Data: " + data); StringBuilder encodedUrl = new StringBuilder(appConfig.getErpUrl()); try { boolean firstParam = true; // SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); // // SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); // // outputFormat.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata")); for (Map.Entry entry : data.entrySet()) { if (!firstParam) { encodedUrl.append("&"); } firstParam = false; String key = URLEncoder.encode(entry.getKey(), "UTF-8"); String value = ""; if (entry.getValue() == null) { value = ""; } else { value = entry.getValue().toString(); // if (entry.getKey().equals("TRANSACTIONDATE")) { // try { // Date date = inputFormat.parse(value); // value = outputFormat.format(date); // } catch (Exception e) { // System.out.println("Error parsing TRANSACTIONDATE: " + value); // } // } } encodedUrl.append(key).append("=").append(URLEncoder.encode(value, "UTF-8")); } String finalUrl = encodedUrl.toString(); System.out.println("Final Encoded URL: " + finalUrl); HttpURLConnection connection = (HttpURLConnection) new URL(finalUrl).openConnection(); connection.setRequestMethod("GET"); int responseCode = connection.getResponseCode(); System.out.println("Response Code: " + responseCode); if (responseCode == HttpURLConnection.HTTP_OK) { BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String inputLine; while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); } else { System.out.println("GET request failed."); } if (id != null) { updateTheLog(data, response, id); } else { saveTheLog(data, response); } } catch (Exception e) { try { if (id != null) { updateTheLog(data, response, id); } else { saveTheLog(data, response); } } catch (Exception ex) { ex.printStackTrace(); } e.printStackTrace(); } } } public void saveTheLog(Map map, StringBuilder response) throws JsonProcessingException { String status = "", msgStatus = ""; if (response != null) { ObjectMapper objectMapper = new ObjectMapper(); Map res = objectMapper.readValue(response.toString(), Map.class); msgStatus = String.valueOf(res.get("Message")); if (res.get("ResultStatus") != null) { status = String.valueOf(res.get("ResultStatus")); } } System.out.println("ResultStatus: " + status + " msgStatus: " + msgStatus); String statusCode = "0"; if ("Connection Failure".equals(msgStatus)) { statusCode = "2"; } else if ("Already Updated this collection".equals(msgStatus) && "false".equals(status)) { statusCode = "1"; } else if ("Failure".equals(msgStatus) && "false".equals(status)) { statusCode = "2"; } else if ("true".equals(status)) { statusCode = "1"; } map.put("status", statusCode); map.put("response", response.toString()); coworkLogRepo.insert(map); } public void updateTheLog(Map map, StringBuilder response, String id) throws JsonProcessingException { String status = "", msgStatus = ""; if (response != null) { ObjectMapper objectMapper = new ObjectMapper(); Map res = objectMapper.readValue(response.toString(), Map.class); msgStatus = String.valueOf(res.get("Message")); if (res.get("ResultStatus") != null) { status = String.valueOf(res.get("ResultStatus")); } } System.out.println("ResultStatus: " + status + " msgStatus: " + msgStatus); String statusCode = "0"; if ("Connection Failure".equals(msgStatus)) { statusCode = "2"; } else if ("Already Updated this collection".equals(msgStatus) && "false".equals(status)) { statusCode = "1"; } else if ("Failure".equals(msgStatus) && "false".equals(status)) { statusCode = "2"; } else if ("true".equals(status)) { statusCode = "1"; } coworkLogRepo.updateLog(statusCode, response.toString(), id); } }