This commit is contained in:
Eko Haryadi
2025-08-20 08:56:18 +07:00
parent 4496edd8c8
commit be06f273c5
36 changed files with 3486 additions and 0 deletions

View File

@@ -0,0 +1,252 @@
package id.co.iconpln.apkt.data309.service;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import id.co.iconpln.apkt.data309.dto.Data309;
import id.co.iconpln.apkt.data309.dto.Row;
import id.co.iconpln.apkt.data309.entity.SS309;
import id.co.iconpln.apkt.data309.entity.Ulp;
import id.co.iconpln.apkt.data309.utils.XmlToJsonConverter;
import jakarta.enterprise.context.ApplicationScoped;
@ApplicationScoped
public class Data309Service {
public void syncronize(String unit, String thnbln) throws IOException, InterruptedException {
// URL of the SOAP service
URL url = new URL("http://10.72.35.15:7076/wsIntegPDPJ/WsIntegSimDisService");
// Open connection
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "text/xml");
connection.setDoOutput(true);
String xmlRequest = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ws=\"http://ws.iconpln.com/\">\n"
+
" <soapenv:Header/>\n" +
" <soapenv:Body>\n" +
" <ws:getData309>\n" +
" <unit>" + unit + "</unit>\n" +
" <thblrek>" + thnbln + "</thblrek>\n" +
" </ws:getData309>\n" +
" </soapenv:Body>\n" +
"</soapenv:Envelope>";
try (OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream())) {
writer.write(xmlRequest);
writer.flush();
}
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(connection.getInputStream()))) {
StringBuilder buildNewData = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
buildNewData.append(line);
}
ObjectMapper objectMapper = new ObjectMapper();
String json = XmlToJsonConverter.convertXmlToJson(buildNewData.toString());
JsonNode rootNode = objectMapper.readTree(json);
// Get the 'item' array from JSON
JsonNode itemNode = rootNode.path("Body")
.path("getData309Response")
.path("return")
.path("item");
// Process each item
Map<String, Object> obj = new HashMap<>();
Integer i = 0;
for (JsonNode node : itemNode) {
String itemValue = node.asText();
// Check if the item is XML
if (itemValue.startsWith("<?xml")) {
// Process XML content separately
// You may parse this XML or handle it accordingly
String xmlContent = itemValue;
i++;
obj.put(i.toString(), XmlToJsonConverter
.convertXmlToJson(xmlContent));
}
}
String jsonResult = objectMapper.writeValueAsString(obj);
// Create ObjectMapper instance
ObjectMapper mapperFromObjectNode = new ObjectMapper();
// Parse the JSON string into a JsonNode
JsonNode jsonNode = mapperFromObjectNode.readTree(jsonResult);
// Retrieve the nested JSON string and remove unnecessary backslashes
String cleanedJsonString = jsonNode.get("1").asText();
// Parse the cleaned JSON string back into a JsonNode to ensure it's valid
JsonNode cleanedJsonNode = objectMapper.readTree(cleanedJsonString);
// Pretty print the cleaned JSON
String prettyCleanedJsonString = objectMapper.writerWithDefaultPrettyPrinter()
.writeValueAsString(cleanedJsonNode);
ObjectMapper mapperToDatabase = new ObjectMapper();
mapperToDatabase.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapperToDatabase.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
mapperToDatabase.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
mapperToDatabase.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
mapperToDatabase.configure(DeserializationFeature.FAIL_ON_TRAILING_TOKENS, true);
mapperToDatabase.configure(DeserializationFeature.FAIL_ON_MISSING_EXTERNAL_TYPE_ID_PROPERTY,
true);
Data309 data309 = mapperToDatabase.readValue(prettyCleanedJsonString.replace("\n", ""),
Data309.class);
if (data309.getRow() != null) {
Integer u = 1;
Integer in = 1;
for (Row newData : data309.getRow()) {
Ulp ulp = Ulp.find("unit_ap2t = ?1", newData.getUnitup()).firstResult();
SS309 ss309 = SS309
.find("id_ulp = ?1 and thbllap = ?2 and no_baris = ?3",
ulp.getId(),
newData.getThbllap(), newData.getNoBaris())
.firstResult();
String upsert;
if (ss309 == null) {
ss309 = new SS309();
upsert = in++ + " di tambahkan";
} else {
upsert = u++ + " di update";
}
ss309.setThbllap(!newData.getThbllap().equals("null") ? newData.getThbllap()
: null);
ss309.setJenislap(!newData.getJenislap().equals("null") ? newData.getJenislap()
: null);
ss309.setId_ulp(ulp == null ? null : ulp.getId());
ss309.setNo_baris(
!newData.getNoBaris().equals("null")
? Integer.valueOf(newData.getNoBaris())
: null);
ss309.setGoltarif(!newData.getGoltarif().equals("null") ? newData.getGoltarif()
: null);
ss309.setTarif(!newData.getTarif().equals("null") ? newData.getTarif() : null);
ss309.setDaya_min(
!newData.getDayaMin().equals("null")
? Integer.valueOf(newData.getDayaMin())
: null);
ss309.setDaya_max(
!newData.getDayaMax().equals("null")
? Integer.valueOf(newData.getDayaMax())
: null);
ss309.setLabel(!newData.getLabel().equals("null") ? newData.getLabel() : null);
ss309.setLabel_asterik(
!newData.getLabelAsterik().equals("null")
? newData.getLabelAsterik()
: null);
ss309.setJmldaya(
!newData.getJmldaya().equals("null")
? new BigDecimal(newData.getJmldaya())
: null);
ss309.setJmlplg(
!newData.getJmlplg().equals("null")
? new BigDecimal(newData.getJmlplg())
: BigDecimal.ZERO);
ss309.setJmlkwh(!newData.getJmlkwh().equals("null")
? new BigDecimal(newData.getJmlkwh())
: null);
ss309.setKwhlwbp(
!newData.getKwhlwbp().equals("null")
? new BigDecimal(newData.getKwhlwbp())
: null);
ss309.setKwhwbp(!newData.getKwhwbp().equals("null")
? new BigDecimal(newData.getKwhwbp())
: null);
ss309.setKelbkvarh(
!newData.getKelbkvarh().equals("null")
? new BigDecimal(newData.getKelbkvarh())
: null);
ss309.setRpbeban(
!newData.getRpbeban().equals("null")
? new BigDecimal(newData.getRpbeban())
: null);
ss309.setRpkwh(!newData.getRpkwh().equals("null")
? new BigDecimal(newData.getRpkwh())
: null);
ss309.setRpkvarh(
!newData.getRpkvarh().equals("null")
? new BigDecimal(newData.getRpkvarh())
: null);
ss309.setRpptl(!newData.getRpptl().equals("null")
? new BigDecimal(newData.getRpptl())
: null);
ss309.setRata_va_langganan(
!newData.getRataVaLangganan().equals("null")
? new BigDecimal(newData.getRataVaLangganan())
: null);
ss309.setRata_kwh_langganan(
!newData.getRataKwhLangganan().equals("null")
? new BigDecimal(newData.getRataKwhLangganan())
: null);
ss309.setRata_rp_kwh(
!newData.getRataRpKwh().equals("null")
? new BigDecimal(newData.getRataRpKwh())
: null);
ss309.setJam_nyala(
!newData.getJamNyala().equals("null")
? new BigDecimal(newData.getJamNyala())
: null);
ss309.setKwh_sd_bln_ini(
!newData.getKwhSdBlnIni().equals("null")
? new BigDecimal(newData.getKwhSdBlnIni())
: null);
ss309.setKvarh_sd_bln_ini(
!newData.getKvarhSdBlnIni().equals("null")
? new BigDecimal(newData.getKvarhSdBlnIni())
: null);
ss309.setR_beban_sd_bln_ini(
!newData.getBBebanSdBlnIni().equals("null")
? new BigDecimal(newData.getBBebanSdBlnIni())
: null);
ss309.setB_kwh_sd_bln_ini(
!newData.getBKwhSdBlnIni().equals("null")
? new BigDecimal(newData.getBKwhSdBlnIni())
: null);
ss309.setB_kvarh_sd_bln_ini(
!newData.getBKvarhSdBlnIni().equals("null")
? new BigDecimal(newData.getBKvarhSdBlnIni())
: null);
ss309.setJumlah_sd_bln_ini(
!newData.getJumlahSdBlnIni().equals("null")
? new BigDecimal(newData.getJumlahSdBlnIni())
: null);
ss309.setRp_kwh_sd_bln_ini(
!newData.getRpKwhSdBlnIni().equals("null")
? new BigDecimal(newData.getRpKwhSdBlnIni())
: null);
ss309.persist();
System.out.println(upsert);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}