List<String> pkeyColumn = new ArrayList<>();
List<Object> pkeyValue = new ArrayList<>();
for (Field f : clazz.getDeclaredFields()) {
// --------------------- PKey Field Handle ------------------------
// check whether is primary key field
Id id = f.getAnnotation(Id.class);
if (id != null) {
Column idCol = f.getAnnotation(Column.class);
if (idCol != null && idCol.name() != null) {
pkeyColumn.add(idCol.name());
// check modifier if private set accessible
if (f.getModifiers() != Modifier.PUBLIC) {
f.setAccessible(true);
}
pkeyValue.add(f.get(temp));
isPkeyFound = true;
// adding ID column to ID holder(
if (HpUfisAppConstants.CON_ID.equals(idCol.name())) {
idValue = (f.get(temp) == null ? "" : f.get(temp).toString());
}
// adding id_flight to id_flight holder
if (HpUfisAppConstants.CON_AFTTAB.equals(tabName)){
flightUrno = (f.get(temp) == null ? "" : f.get(temp).toString());
isFlightUrno = true;
}
continue;
} else {
LOG.warn("Notification: Name of Column annotation is required for field={}", f.getName());
}
}
// check whether is composite key field
EmbeddedId cpzKey = f.getAnnotation(EmbeddedId.class);
if (cpzKey != null) {
Object cpzValue = f.get(temp);
// get composite key columns
Class<?> cpzClazz = f.getType();
//LOG.debug("check 5");
for (Field cpzField : cpzClazz.getDeclaredFields()) {
Column cpzCol = cpzField.getAnnotation(Column.class);
if (cpzCol != null && cpzCol.name() != null) {
pkeyColumn.add(cpzCol.name());
// check modifier if private set accessible
if (cpzField.getModifiers() != Modifier.PUBLIC) {
cpzField.setAccessible(true);
} // LOG.debug("check 7");
pkeyValue.add(cpzField.get(cpzValue));
//LOG.debug("check 8");
isPkeyFound = true;
} else {
LOG.warn("Notification: Name of Column annotation is required for field={}", cpzField.getName());
}
}
continue;
}
// --------------------- Non-PKey Field Handle ------------------------
PropertyDescriptor pd = null;
try {
pd = new PropertyDescriptor(f.getName(), clazz);
} catch (IntrospectionException e) {
LOG.debug("No property found for field={}", f.getName());
continue;
}
Method getterMethod = pd.getReadMethod();
// According to command to build fields, odat, data
// DRT: notify id only(DRT mark as deleted rec_status)
// IRT: notify all
// URT: notify all or change only
Column col = f.getAnnotation(Column.class);
if (col == null || col.name() == null) {
LOG.warn("Column name not found in annotation for {} field={}", tabName, f.getName());
continue;
}
// if idFlights empty or null, check id_flight from field to auto-fill
if (header.getIdFlight().size() == 0) {
// whether is flight urno(UAFT, URNO of AFTTAB, ID_FLIGHT)
if (HpUfisAppConstants.CON_UAFT.equals(col.name())
|| HpUfisAppConstants.CON_ID_FLIGHT.equals(col.name())) {
isFlightUrno = true;
}
}
Object tempOldValue = null;
Object tempNewValue = null;
switch (cmd) {
case DRT:
//tempNewValue = getterMethod.invoke(data);
//fld.add(col.name().toUpperCase());
//dat.add(tempOldValue);
break;
case IRT:
if (data != null) {
tempNewValue = getterMethod.invoke(data);
fld.add(col.name().toUpperCase());
dat.add(tempNewValue);
if (isFlightUrno && tempNewValue != null) {
flightUrno = tempNewValue.toString();
//LOG.debug("check 12");
}
} else {
LOG.warn("Data parameter is required for IRT notification");
}
break;
case URT:
if (data != null && oldData != null) {
tempNewValue = getterMethod.invoke(data);
tempOldValue = getterMethod.invoke(oldData);
String fldsConfig = HpCommonConfig.fields.get(tabName);
// flight urno
if (isFlightUrno && tempNewValue != null) {
flightUrno = tempNewValue.toString();
}
//LOG.debug("check 14");
if (HpUfisUtils.isNotEmptyStr(fldsConfig)) {
// check whether only send changed fields
if (fldsConfig.equals(HpUfisAppConstants.FLD_CHANGED)) {
if (tempNewValue != null && !tempNewValue.equals(tempOldValue)) {
fld.add(col.name().toUpperCase());
odat.add(tempOldValue);
dat.add(tempNewValue);
}
// check whether send all fields
} else if (fldsConfig.equals(HpUfisAppConstants.FLD_ALL)) {
fld.add(col.name().toUpperCase());
odat.add(tempOldValue);
dat.add(tempNewValue);
// check whether send selected fields only
} else if (fldsConfig.contains(col.name().toUpperCase())) {
fld.add(col.name().toUpperCase());
odat.add(tempOldValue);
dat.add(tempNewValue);
} else {
LOG.warn("Column={} is not contained in the notify fields list", col.name().toUpperCase());
}
} else {
LOG.warn("Notify Field Configuration cannot be null or empty");
}
} else {
LOG.warn("Both old and new data are required for URT notification");
}
break;
default:
LOG.warn("Unsupported notification command: {}", cmd);
break;
}
// Add id of the flight
if (isFlightUrno && header.getIdFlight().size() == 0) {
header.getIdFlight().add(flightUrno);
}
}
LOG.debug("process fields, takes {}",System.currentTimeMillis()-startTime);
// verify fld, data and old data size
boolean isSizeMatched = true;
if (UfisASCommands.IRT == cmd) {
if (fld.size() != dat.size()) {
isSizeMatched = false;
}
} else if (UfisASCommands.URT == cmd) {
if (fld.size() != dat.size() || fld.size() != odat.size()) {
isSizeMatched = false;
}
}
if (isSizeMatched) {
// exclusion check
String exclusion = HpCommonConfig.exclusions.get(tabName);
if ((UfisASCommands.DRT != cmd) && HpUfisUtils.isNotEmptyStr(exclusion)) {
String[] excFields = exclusion.split(",");
for (String s : excFields) {
int index = fld.indexOf(s.trim());
if (index != -1) {
fld.remove(index);
dat.remove(index);
if (UfisASCommands.URT == cmd) {
odat.remove(index);
}
}
}
}
// check whether the ID column is defined in parent class
if (!isPkeyFound) {
Class<?> superClazz = clazz.getSuperclass();
do {
for (Field f : superClazz.getDeclaredFields()) {
// check whether is primary key field
Id id = f.getAnnotation(Id.class);
if (id != null) {
Column idCol = f.getAnnotation(Column.class);
if (idCol != null && idCol.name() != null) {
pkeyColumn.add(idCol.name());
// check modifier if private set accessible