Map<EntDbLoadUld, EntDbLoadUld> updatedLoadUlds = new HashMap<>();
//TODO: to confirm mei => if no value eg weight status in xml but there is in db, shall update with blank???
for (UWSULDDetails uwsUld : uwsInfo.getUWSULDDetails()) {
//TODO: to refactor ummmm
EntDbLoadUld loadUld = existedLoadUlds(existingLoadUlds, uwsUld.getULDNumber());
EntDbLoadUld originalUld = null;
boolean isExisted = true;
if(loadUld == null) {
loadUld = new EntDbLoadUld();
isExisted = false;
initialize(loadUld, entDbAfttab);
} else {
originalUld = new EntDbLoadUld(loadUld);
}
loadUld.setIdFlight(entDbAfttab.getUrno());
loadUld.setUldCurrFltno(flightNumber);
loadUld.setUldNumber(uwsUld.getULDNumber());
loadUld.setUldDest3(uwsUld.getDest3());
loadUld.setUldWeight(uwsUld.getWeight()+"");
loadUld.setUldWeightStatus(uwsUld.getWeightStatus());
loadUld.setUldLoadCat1(uwsUld.getLoadCat1());
loadUld.setUldLoadCat2(uwsUld.getLoadCat2());
loadUld.setUldContourCode(uwsUld.getLoadContourCode());
loadUld.setUldContourNum(uwsUld.getLoadContourNumber());
loadUld.setShcList(uwsUld.getShcList());
loadUld.setShcRemarks(uwsUld.getShcRemarks());
loadUld.setDataSource(HpEKConstants.ULD_SOURCE_SKYCHAIN);
loadUld.setRecFlag(" ");
if(isExisted) {
loadUld.setUpdatedUser(HpEKConstants.ULD_SOURCE_SKYCHAIN);
loadUld.setUpdatedDate(HpUfisCalendar.getCurrentUTCTime());
updatedLoadUlds.put(loadUld, originalUld);
} else {
loadUld.setCreatedUser(HpEKConstants.ULD_SOURCE_SKYCHAIN);
loadUld.setCreatedDate(HpUfisCalendar.getCurrentUTCTime());
newLoadUlds.add(loadUld);
}
}
if(!uwsInfo.getBULKULDDetails().isEmpty()) {
//Delete existing bulk records regarding the provided flight
dLLoadUldBean.deleteBulkUldsByFlight(entDbAfttab.getUrno());
}
for (BULKULDDetails bulkUld : uwsInfo.getBULKULDDetails()) {
//TODO: to refactor ummmm
EntDbLoadUld loadUld = new EntDbLoadUld();
initialize(loadUld, entDbAfttab);
loadUld.setUldNumber(bulkUld.getULDNumber());
loadUld.setIdFlight(entDbAfttab.getUrno());
loadUld.setUldCurrFltno(flightNumber);
loadUld.setUldDest3(bulkUld.getDest3());
loadUld.setUldWeight(bulkUld.getWeight()+"");
loadUld.setUldWeightStatus(bulkUld.getWeightStatus());
loadUld.setUldLoadCat1(bulkUld.getLoadCat1());
loadUld.setUldLoadCat2(bulkUld.getLoadCat2());
loadUld.setUldContourCode(bulkUld.getLoadContourCode());
loadUld.setUldContourNum(bulkUld.getLoadContourNumber());
loadUld.setShcList(bulkUld.getShcList());
loadUld.setShcRemarks(bulkUld.getShcRemarks());
loadUld.setDataSource(HpEKConstants.ULD_SOURCE_SKYCHAIN);
loadUld.setRecFlag(" ");
loadUld.setCreatedUser(HpEKConstants.ULD_SOURCE_SKYCHAIN);
loadUld.setCreatedDate(HpUfisCalendar.getCurrentUTCTime());
newLoadUlds.add(loadUld);
}
for (SIULDDetails siUld : uwsInfo.getSIULDDetails()) {
EntDbLoadUld loadUld = existedLoadUlds(updatedLoadUlds.keySet(), siUld.getULDNumber());
if(loadUld == null) {
loadUld = existedLoadUlds(newLoadUlds, siUld.getULDNumber());
}
if(loadUld != null) {
String temp = siUld.getRampTransferFlno();
//TODO: clean code, to reimplement or refactor
//Reset if there is no QRTX
if(isNullOrEmptyStr(temp)) {
loadUld.setRampTransferFlag("");
loadUld.setRampTransferFlno("");
loadUld.setIdRampTransferFlight("");
} else {
//FIXME: to write API for flight format. This is just temp fix
loadUld.setRampTransferFlag("Y");
String rampFltNo = formatFlightNo(temp);
LOG.debug("Ramp Transfer Flno - {}", rampFltNo);
loadUld.setRampTransferFlno(rampFltNo);
HpUfisCalendar startDate = new HpUfisCalendar();
startDate.setTime(entDbAfttab.getTifd());// eg. 06/19/2013 21:45
startDate.add(Calendar.HOUR, basicDataSingleton.getRampOffset()); // eg. 06/19/2013 13:45
HpUfisCalendar endDate = new HpUfisCalendar();
endDate.setTime(entDbAfttab.getTifd());// eg. 06/19/2013 21:45
LOG.debug("Ramp Tnxfer Flight Criteria : RampTnxferFlno - {}, Startdate - {}, EndDate - {}", loadUld.getRampTransferFlno(), startDate.toString(), endDate.toString());
EntDbAfttab entDbAfttabTransfer = afttabBean.findRampTransferFlight(loadUld.getRampTransferFlno(), startDate, endDate);
//TODO: to confirm -> if no msg, jz drop whole msg?
if(entDbAfttabTransfer != null) {
loadUld.setIdRampTransferFlight(entDbAfttabTransfer.getUrno()+"");
} else {
LOG.warn("No flight is found for ramp transfer.");
}
String transferFltDebugInfo = String.format(", Ramp Tnxfer Flno (PKey) - %s[%s]", loadUld.getRampTransferFlno(), isNullOrEmptyStr(loadUld.getIdRampTransferFlight()) ? "No P-Key found"
: loadUld.getIdRampTransferFlight());
LOG.warn(
"ULD for transfer flight : ULD - {}, {}", loadUld.getUldNumber(), transferFltDebugInfo);
}
} else {
LOG.warn("ULD for transfer flight is not found {}", siUld.getULDNumber());
}
}
int total = 0;
int index = 0;
for (Entry<EntDbLoadUld, EntDbLoadUld> entry : updatedLoadUlds.entrySet()) {
boolean isLast = (index == updatedLoadUlds.entrySet().size() - 1 && newLoadUlds.isEmpty());
EntDbLoadUld resultUld = dLLoadUldBean.merge(entry.getKey());
boolean success = resultUld != null;
if(success){
EntDbLoadUld original = entry.getValue();
if (HpUfisUtils.isNotEmptyStr(HpCommonConfig.notifyTopic)) {
// send notification message to topic
ufisTopicProducer.sendNotification(isLast, HpUfisAppConstants.UfisASCommands.URT, String.valueOf(resultUld.getIdFlight()), original, resultUld);
} else {
// if topic not defined, send notification to queue
ufisQueueProducer.sendNotification(isLast, HpUfisAppConstants.UfisASCommands.URT, String.valueOf(resultUld.getIdFlight()), original, resultUld);
}
LOG.debug("Notify ULDs(UWS) from SkyChain");
}
LOG.debug("Uld No. {} has {}been processed.", entry.getKey().getUldNumber(), success ? "" : "not ");
index++;
total++;
}
index = 0;
for (EntDbLoadUld entDbLoadUld : newLoadUlds) {
boolean isLast = index == newLoadUlds.size() - 1;
EntDbLoadUld resultUld = dLLoadUldBean.merge(entDbLoadUld);
boolean success = resultUld != null;
if(success){
if (HpUfisUtils.isNotEmptyStr(HpCommonConfig.notifyTopic)) {
// send notification message to topic
ufisTopicProducer.sendNotification(isLast, HpUfisAppConstants.UfisASCommands.IRT, String.valueOf(resultUld.getIdFlight()), null, resultUld);
} else {
// if topic not defined, send notification to queue
ufisQueueProducer.sendNotification(isLast, HpUfisAppConstants.UfisASCommands.IRT, String.valueOf(resultUld.getIdFlight()), null, resultUld);
}
LOG.debug("Notify ULDs(UWS) from SkyChain");
}
LOG.debug("Uld No. {} has {}been processed.", entDbLoadUld.getUldNumber(), success ? "" : "not ");
index++;