From b8efc4f67a9ed5717f448bec92c952f7e26b586f Mon Sep 17 00:00:00 2001 From: Rob Stine Date: Tue, 9 Mar 2021 14:00:18 -0500 Subject: [PATCH] Add days out of country to loader and rule runner. --- .../gov/gtas/model/PassengerTripDetails.java | 11 +++ .../mappings/PassengerDetailsMapping.java | 2 + .../mappings/PassengerMapping.java | 2 + .../gov/gtas/services/PnrMessageService.java | 86 +++++++++++++++++++ .../rule/builder/RuleTemplateConstants.java | 1 + .../db/2.0/passenger_trip_detail_revert.sql | 1 + .../db/2.0/passenger_trip_detail_update.sql | 3 + 7 files changed, 106 insertions(+) create mode 100644 gtas-parent/scripts/db/2.0/passenger_trip_detail_revert.sql create mode 100644 gtas-parent/scripts/db/2.0/passenger_trip_detail_update.sql diff --git a/gtas-parent/gtas-commons/src/main/java/gov/gtas/model/PassengerTripDetails.java b/gtas-parent/gtas-commons/src/main/java/gov/gtas/model/PassengerTripDetails.java index 802ebbdfb6..4c0551def5 100644 --- a/gtas-parent/gtas-commons/src/main/java/gov/gtas/model/PassengerTripDetails.java +++ b/gtas-parent/gtas-commons/src/main/java/gov/gtas/model/PassengerTripDetails.java @@ -78,6 +78,9 @@ public void setPassenger(Passenger passenger) { @Column(name = "hours_before_takeoff") private Integer hoursBeforeTakeOff; + + @Column(name = "days_out_of_country") + private Double daysOutOfCountry; @Transient private String totalBagWeight; @@ -249,4 +252,12 @@ public Integer getCoTravelerCount() { public void setCoTravelerCount(Integer coTravelerCount) { this.coTravelerCount = coTravelerCount; } + + public Double getDaysOutOfCountry() { + return daysOutOfCountry; + } + + public void setDaysOutOfCountry(Double daysOutOfCountry) { + this.daysOutOfCountry = daysOutOfCountry; + } } \ No newline at end of file diff --git a/gtas-parent/gtas-commons/src/main/java/gov/gtas/querybuilder/mappings/PassengerDetailsMapping.java b/gtas-parent/gtas-commons/src/main/java/gov/gtas/querybuilder/mappings/PassengerDetailsMapping.java index bb0512f25c..020b66e62b 100644 --- a/gtas-parent/gtas-commons/src/main/java/gov/gtas/querybuilder/mappings/PassengerDetailsMapping.java +++ b/gtas-parent/gtas-commons/src/main/java/gov/gtas/querybuilder/mappings/PassengerDetailsMapping.java @@ -29,6 +29,8 @@ public enum PassengerDetailsMapping implements IEntityMapping { RESIDENCY_COUNTRY("residencyCountry", "Residency Country", TypeEnum.STRING.getType()), PASSENGER_TYPE("passengerType", "Type", TypeEnum.STRING.getType()), + + DAYS_OUT_OF_COUNTRY("daysOutOfCountry", "Days out of Country", TypeEnum.DOUBLE.getType()), TRAVEL_FREQUENCY("travelFrequency", "Travel Frequency", TypeEnum.STRING.getType()); diff --git a/gtas-parent/gtas-commons/src/main/java/gov/gtas/querybuilder/mappings/PassengerMapping.java b/gtas-parent/gtas-commons/src/main/java/gov/gtas/querybuilder/mappings/PassengerMapping.java index b876a1596c..eb41c24613 100644 --- a/gtas-parent/gtas-commons/src/main/java/gov/gtas/querybuilder/mappings/PassengerMapping.java +++ b/gtas-parent/gtas-commons/src/main/java/gov/gtas/querybuilder/mappings/PassengerMapping.java @@ -41,6 +41,8 @@ public enum PassengerMapping implements IEntityMapping { SEAT("seat", "Seat", TypeEnum.STRING.getType()), PASSENGER_TYPE("passengerDetails.passengerType", "Type", TypeEnum.STRING.getType()), + + DAYS_OUT_OF_COUNTRY("passengerTripDetails.daysOutOfCountry", "Days out of Country", TypeEnum.DOUBLE.getType()), TRAVEL_FREQUENCY("passengerTripDetails.travelFrequency", "Travel Frequency", TypeEnum.STRING.getType()); diff --git a/gtas-parent/gtas-loader/src/main/java/gov/gtas/services/PnrMessageService.java b/gtas-parent/gtas-loader/src/main/java/gov/gtas/services/PnrMessageService.java index 98d392c311..edcd42e989 100644 --- a/gtas-parent/gtas-loader/src/main/java/gov/gtas/services/PnrMessageService.java +++ b/gtas-parent/gtas-loader/src/main/java/gov/gtas/services/PnrMessageService.java @@ -187,6 +187,7 @@ public MessageInformation load(MessageDto msgDto) { pnr.setPassengerCount(pnr.getPassengers().size()); TripTypeEnum tripType = loaderRepo.calculateTripType(pnr.getFlightLegs(), pnr.getDwellTimes()); pnr.setTripType(tripType.toString()); + calculateTimeOutOfCountry(pnr, primeFlight); if (tamrEnabled) { List tamrPassengers = tamrAdapter .convertPassengers(pnr.getFlights().iterator().next(), pnr.getPassengers()); @@ -218,6 +219,91 @@ public MessageInformation load(MessageDto msgDto) { return messageInformation; } + private void calculateTimeOutOfCountry(Pnr pnr, Flight primeFlight) { + + List flistFlightLegs = pnr.getFlightLegs(); + if (pnr.getFlightLegs().size() < 2) { + return; + } + // All following logic to determine time out of country depends on the flight legs being in order. + flistFlightLegs.sort(Comparator.comparing(FlightLeg::getLegNumber)); + // find index of prime flight. + int flightIndex = -1; + for (int i = 0; i < flistFlightLegs.size(); i++) { + if (flistFlightLegs.get(i).getFlight() != null) { + flightIndex = i; + break; + } + } + // Set the time out of country using different logic if flight is in-bound or out-bound. + Double timeOutOfCountryInDays = null; + String homeCountry = lookupRepo.getAppConfigOption(AppConfigurationRepository.HOME_COUNTRY); + + if ("I".equalsIgnoreCase(primeFlight.getDirection())) { + // This will take the first booking in-bound to the home country after an out-bound prime flight + timeOutOfCountryInDays = getInboundTimeOutOfCountry(primeFlight, flistFlightLegs, flightIndex, homeCountry); + } else if ("O".equalsIgnoreCase(primeFlight.getDirection())) { + // This will take the first flight in-bound to the home country after an out-bound booking detail + timeOutOfCountryInDays = getOutboundTimeOutOfCountry(primeFlight, flistFlightLegs, flightIndex, homeCountry); + } + // If a time was able to be calculated update the passenger trip details to reflect how many days a + // traveler is out of the country + if (timeOutOfCountryInDays != null) { + for (Passenger p : pnr.getPassengers()) { + PassengerTripDetails ptd = p.getPassengerTripDetails(); + ptd.setDaysOutOfCountry(timeOutOfCountryInDays); + } + } + } + + // Iterate through flight legs starting at the first flight leaving the home country + // and ending with the incoming prime flight to see if a time out of country can be determined + private Double getOutboundTimeOutOfCountry(Flight primeFlight, List flistFlightLegs, int flightIndex, String homeCountry) { + Double timeOutOfCountryInDays = null; + + for (int i = flightIndex; i < flistFlightLegs.size(); i++) { + BookingDetail bd = flistFlightLegs.get(i).getBookingDetail(); + + if (flistFlightLegs.get(i).getFlight() == null + && bd != null + && homeCountry.equalsIgnoreCase(bd.getDestinationCountry())) { + Date entryDate = bd.getEta(); + Date exitDate = primeFlight.getMutableFlightDetails().getEtd(); + timeOutOfCountryInDays = differenceInDays(exitDate, entryDate); + break; + } + } + return timeOutOfCountryInDays; + } + + // Iterate through flight legs starting at the first booking detail leaving the home country + // and ending with the incoming prime flight to see if a time out of country can be determined + private Double getInboundTimeOutOfCountry(Flight primeFlight, List flistFlightLegs, int flightIndex, String homeCountry) { + Double timeOutOfCountryInDays = null; + + for (int i = 0; i < flightIndex; i++) { + BookingDetail bd = flistFlightLegs.get(i).getBookingDetail(); + if (flistFlightLegs.get(i).getFlight() == null + && bd != null + && homeCountry.equalsIgnoreCase(bd.getOriginCountry())) { + Date entryDate = primeFlight.getMutableFlightDetails().getEta(); + Date exitDate = bd.getEtd(); + timeOutOfCountryInDays = differenceInDays(entryDate, exitDate); + } + } + return timeOutOfCountryInDays; + } + + + private Double differenceInDays(Date firstDate, Date secondDate) { + double timeOutOfCountryInDays; + long diff = secondDate.getTime() - firstDate.getTime(); + int hours = (int) TimeUnit.HOURS.convert(diff, TimeUnit.MILLISECONDS); + DecimalFormat df = new DecimalFormat("#.##"); + timeOutOfCountryInDays = Double.valueOf(df.format((double) hours / 24)); + return timeOutOfCountryInDays; + } + private void addSegments(Pnr pnr, PnrVo vo) { for (SavedSegmentVo ssVo : vo.getSavedSegments() ) { String segmentName = ssVo.getSegmentName(); diff --git a/gtas-parent/gtas-rulesvc/src/main/java/gov/gtas/rule/builder/RuleTemplateConstants.java b/gtas-parent/gtas-rulesvc/src/main/java/gov/gtas/rule/builder/RuleTemplateConstants.java index 6967c17777..c863546d63 100644 --- a/gtas-parent/gtas-rulesvc/src/main/java/gov/gtas/rule/builder/RuleTemplateConstants.java +++ b/gtas-parent/gtas-rulesvc/src/main/java/gov/gtas/rule/builder/RuleTemplateConstants.java @@ -68,6 +68,7 @@ public class RuleTemplateConstants { passengerTripDetailsMap.put("PASSENGERTRIPDETAILS.DEBARKCOUNTRY", "debarkCountry"); passengerTripDetailsMap.put("PASSENGERTRIPDETAILS.COTRAVELERCOUNT", "coTravelerCount"); passengerTripDetailsMap.put("PASSENGERTRIPDETAILS.HOURSBEFORETAKEOFF", "hoursBeforeTakeOff"); + passengerTripDetailsMap.put("PASSENGERTRIPDETAILS.DAYSOUTOFCOUNTRY", "daysOutOfCountry"); passTripDetailsMap = Collections.unmodifiableMap(passengerTripDetailsMap); } diff --git a/gtas-parent/scripts/db/2.0/passenger_trip_detail_revert.sql b/gtas-parent/scripts/db/2.0/passenger_trip_detail_revert.sql new file mode 100644 index 0000000000..bb7e829419 --- /dev/null +++ b/gtas-parent/scripts/db/2.0/passenger_trip_detail_revert.sql @@ -0,0 +1 @@ +alter table passenger_trip_details drop column `days_out_of_country`; \ No newline at end of file diff --git a/gtas-parent/scripts/db/2.0/passenger_trip_detail_update.sql b/gtas-parent/scripts/db/2.0/passenger_trip_detail_update.sql new file mode 100644 index 0000000000..041eed4b95 --- /dev/null +++ b/gtas-parent/scripts/db/2.0/passenger_trip_detail_update.sql @@ -0,0 +1,3 @@ +alter table passenger_trip_details +add column `days_out_of_country` double DEFAULT NULL; +