use of com.liferay.faces.demos.dto.Airport in project liferay-faces-bridge-impl by liferay.
the class FlightServiceMockImpl method searchDirect.
@Override
public List<Booking> searchDirect(long departureAirportId, Date departureDate, long arrivalAirportId) {
DateFormat dateFormat = new SimpleDateFormat();
List<Booking> searchResults = new ArrayList<Booking>();
Airport departureAirport = airportService.findById(departureAirportId);
double departureLatitude = departureAirport.getLatitude();
double departureLongitude = departureAirport.getLongitude();
LatLng departureLatLong = new LatLng(departureLatitude, departureLongitude);
Airport arrivalAirport = airportService.findById(arrivalAirportId);
double arrivalLatitude = arrivalAirport.getLatitude();
double arrivalLongitude = arrivalAirport.getLongitude();
LatLng arrivalLatLong = new LatLng(arrivalLatitude, arrivalLongitude);
double distanceInKilometers = LatLngTool.distance(departureLatLong, arrivalLatLong, LengthUnit.KILOMETER);
double durationInHours = distanceInKilometers / AVG_SPEED_KM_PER_HOUR;
double priceUSD = distanceInKilometers * PRICE_USD_PER_KM;
Calendar departureCalendar = new GregorianCalendar();
departureCalendar.setTime(departureDate);
departureCalendar.set(Calendar.HOUR_OF_DAY, 6);
Random random = new Random();
while (departureCalendar.get(Calendar.HOUR_OF_DAY) < 18) {
Calendar arrivalCalendar = (Calendar) departureCalendar.clone();
arrivalCalendar.add(Calendar.HOUR_OF_DAY, (int) Math.round(durationInHours));
Booking flight = new Booking();
Date flightDepartureDate = departureCalendar.getTime();
flight.setDepartureDate(flightDepartureDate);
flight.setArrivalId(arrivalAirportId);
Date flightArrivalDate = arrivalCalendar.getTime();
flight.setArrivalDate(flightArrivalDate);
flight.setDistance(distanceInKilometers);
flight.setDuration(durationInHours);
flight.setBookingId(Math.abs(random.nextLong()));
String flightNumber = Integer.toString(Math.abs(random.nextInt()));
flight.setLabel(flightNumber);
StringBuilder description = new StringBuilder();
description.append("Flight#");
description.append(flightNumber);
description.append(" departing from ");
description.append(departureAirport.getCity());
description.append(" (");
description.append(departureAirport.getCode());
description.append(") on ");
description.append(dateFormat.format(flightDepartureDate));
description.append(" arriving at ");
description.append(arrivalAirport.getCity());
description.append("(");
description.append(arrivalAirport.getCode());
description.append(") on ");
description.append(dateFormat.format(flightArrivalDate));
flight.setDescription(description.toString());
BigDecimal price = new BigDecimal(priceUSD);
price.setScale(2, BigDecimal.ROUND_HALF_UP);
flight.setPrice(price);
departureCalendar.add(Calendar.MINUTE, 90);
searchResults.add(flight);
}
return searchResults;
}
use of com.liferay.faces.demos.dto.Airport in project liferay-faces-bridge-impl by liferay.
the class FlightSearchModelBean method postConstruct.
@PostConstruct
public void postConstruct() {
long departureAirportId = bookingFlowModelBean.getBookingDepartureId();
Airport departureAirport = airportService.findById(departureAirportId);
this.departureAirportName = departureAirport.getName();
this.departureCity = departureAirport.getCity();
long arrivalAirportId = bookingFlowModelBean.getBookingArrivalId();
Airport arrivalAirport = airportService.findById(arrivalAirportId);
this.setArrivalAirportName(arrivalAirport.getName());
this.setArrivalCity(arrivalAirport.getCity());
scopeTrackingBean.setFlightSearchModelBeanInScope(true);
}
use of com.liferay.faces.demos.dto.Airport in project liferay-faces-bridge-impl by liferay.
the class AirportServiceMockImpl method postConstruct.
@PostConstruct
public void postConstruct() {
this.airports = new ArrayList<Airport>();
this.airportMap = new HashMap<Long, Airport>();
String tempFolderPath = System.getProperty(JAVA_IO_TMPDIR);
File tempFolder = new File(tempFolderPath);
File airportsFile = new File(tempFolder, AIRPORTS_FILENAME);
if (!airportsFile.exists()) {
URL url = null;
InputStream inputStream = null;
OutputStream outputStream = null;
try {
url = new URL(AIRPORTS_URL);
logger.info("Downloading url=[{0}]", url);
inputStream = url.openStream();
outputStream = new FileOutputStream(airportsFile);
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, bytesRead);
}
} catch (Exception e) {
throw new FacesException("Error when trying to download url=[" + url + "]", e);
} finally {
close(outputStream);
close(inputStream);
}
}
FileReader fileReader = null;
BufferedReader bufferedReader = null;
try {
fileReader = new FileReader(airportsFile);
bufferedReader = new BufferedReader(fileReader);
String csvLine;
while ((csvLine = bufferedReader.readLine()) != null) {
if (csvLine != null) {
csvLine = csvLine.replaceAll(", ", " ");
csvLine = csvLine.replaceAll("\"", "");
String[] csvParts = csvLine.split(",");
String country = csvParts[3];
if (UNITED_STATES.equals(country) || CANADA.equals(country)) {
Airport airport = new Airport();
long airportId = LongHelper.toLong(csvParts[0]);
airport.setAirportId(airportId);
airport.setName(csvParts[1]);
airport.setCity(csvParts[2]);
airport.setCountry(country);
airport.setCode(csvParts[4]);
airport.setLatitude(Double.parseDouble(csvParts[6]));
airport.setLongitude(Double.parseDouble(csvParts[7]));
this.airports.add(airport);
this.airportMap.put(airportId, airport);
}
}
}
} catch (Exception e) {
throw new FacesException("Error when populating the list of airports from " + airportsFile.getPath(), e);
} finally {
close(bufferedReader);
close(fileReader);
}
this.airportMap = Collections.unmodifiableMap(airportMap);
Collections.sort(this.airports, new AirportComparator());
this.airports = Collections.unmodifiableList(airports);
}
Aggregations