@Override
public BigDecimal getUrnoByFilterQuery(EntDbAfttab params, ADID adid,
short fromOffset, short toOffset) {
// BigDecimal uaft = null;
long startTime = new Date().getTime();
HpUfisCalendar ufisCalendarFrom = new HpUfisCalendar();
HpUfisCalendar ufisCalendarTo = new HpUfisCalendar();
HpUfisCalendar ufisCurrentCalendar = new HpUfisCalendar();
BigDecimal result = null;
try {
ufisCalendarFrom.setTime(ufisCalendarFrom.getCurrentUTCTime());
ufisCalendarTo.setTime(ufisCalendarTo.getCurrentUTCTime());
ufisCurrentCalendar
.setTime(ufisCurrentCalendar.getCurrentUTCTime());
// LOG.info("ufisCalendarFrom:"+ufisCalendarFrom.getCedaString()+" ufisCalendarTo:"+ufisCalendarTo.getCedaString()+" ufisCurrentCalendar:"+ufisCurrentCalendar.getCedaString());
long currTimeInMillis = ufisCurrentCalendar.getTimeInMillis();
// LOG.info("Current Time:" + ufisCurrentCalendar.getCedaString());
// LOG.info("Current Time In Millis:" + currTimeInMillis);
CriteriaBuilder cb = em.getCriteriaBuilder();
// CriteriaQuery cq = cb.createQuery();
CriteriaQuery<Tuple> cq = cb.createTupleQuery();
// fromO
Root<EntDbAfttab> root = cq.from(EntDbAfttab.class);
// select
// cq.select(root.<BigDecimal>get("urno"));
// cq.select(root.get("urno").alias("urno"));
cq.multiselect(root.get("urno").alias("urno"), root.get("stoa")
.alias("stoa"), root.get("stod").alias("stod"));
// result value limited filters
List<Predicate> filters = new LinkedList<>();
// LOG.info("FLNO:" + params.getFlno());
// flno
if (HpUfisUtils.isNotEmptyStr(params.getFlno())) {
// filters.add(cb.equal(cb.trim(root.<String> get("flno")),
// params.getFlno()));
filters.add(cb.equal(cb.trim(root.<String> get("flno")), params
.getFlno().trim()));
}
filters.add(cb.not(cb.in(root.get("ftyp")).value('T').value('G')
.value('X').value('N')));
// range
ufisCalendarFrom.DateAdd(fromOffset, EnumTimeInterval.Minutes);
ufisCalendarTo.DateAdd(toOffset, EnumTimeInterval.Minutes);
// LOG.info("PROVEO: fromOffset={}",
// ufisCalendarFrom.getCedaString());
// LOG.info("PROVEO: toOffset={}", ufisCalendarTo.getCedaString());
LOG.info("ufisCalendarFrom After adding Offset:"
+ ufisCalendarFrom.getCedaString()
+ " ufisCalendarTo after adding Offset:"
+ ufisCalendarTo.getCedaString());
if (adid != null) {
// time range
if (ADID.A.equals(adid)) {
filters.add(cb.between(root.<String> get("stoa"),
ufisCalendarFrom.getCedaString(),
ufisCalendarTo.getCedaString()));
} else {
filters.add(cb.between(root.<String> get("stod"),
ufisCalendarFrom.getCedaString(),
ufisCalendarTo.getCedaString()));
}
}
// SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
// cb.diff(root.<String> get("stoa"),sdf.parse(new
// HpUfisCalendar().getCedaString()));
cq.where(cb.and(filters.toArray(new Predicate[0])));
if (ADID.A.equals(adid)) {
cq.orderBy(cb.desc(root.<String> get("stoa")));
} else {
cq.orderBy(cb.desc(root.<String> get("stod")));
}
Query query = em.createQuery(cq);
// List<BigDecimal> result = query.getResultList();
List<Tuple> results = query.getResultList();
if (results == null || results.size() == 0) {
LOG.warn("PROVEO: Cannot find flight from afttab");
// LOG.info("CriteriaQuery: {}", cq.toString());
} else if (results.size() > 1) {
long min = 0;
long stoaCal = 0;
long stodCal = 0;
LOG.info("PROVEO: More than 1 record found for flight from afttab");
// LOG.info("CriteriaQuery: {}", cq.toString());
for (int i = 0; i < results.size(); i++) {
if (ADID.A == adid) {
// LOG.info("results.get(i).get(1):" +
// results.get(i).get(1) + " String:" +
// results.get(i).get(1).toString());
stoaCal = new HpUfisCalendar(results.get(i).get(1)
.toString(), "yyyyMMddHHmmss")
.getTimeInMillis();
// LOG.info("STOACAL:" + stoaCal);
if (i == 0) {
min = Math.abs(currTimeInMillis - stoaCal);
result = (BigDecimal) results.get(0).get(0);
}
if (Math.abs(currTimeInMillis - stoaCal) < min) {
min = Math.abs(currTimeInMillis - stoaCal);
result = (BigDecimal) results.get(i).get(0);
}
// LOG.info("MIN:" + min + " Result:" + result);
} else if (ADID.D == adid) {
// LOG.info("results.get(i).get(2):" +
// results.get(i).get(2) + " String:" +
// results.get(i).get(2).toString());
stodCal = new HpUfisCalendar(results.get(i).get(2)
.toString(), "yyyyMMddHHmmss")
.getTimeInMillis();
// LOG.info("STODCAL:" + stodCal);
if (i == 0) {
min = Math.abs(currTimeInMillis - stodCal);