Search in sources :

Example 1 with Airport

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;
}
Also used : GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) GregorianCalendar(java.util.GregorianCalendar) Booking(com.liferay.faces.demos.dto.Booking) Date(java.util.Date) BigDecimal(java.math.BigDecimal) Random(java.util.Random) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) Airport(com.liferay.faces.demos.dto.Airport) LatLng(com.javadocmd.simplelatlng.LatLng) SimpleDateFormat(java.text.SimpleDateFormat)

Example 2 with Airport

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);
}
Also used : Airport(com.liferay.faces.demos.dto.Airport) PostConstruct(javax.annotation.PostConstruct)

Example 3 with Airport

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);
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) URL(java.net.URL) IOException(java.io.IOException) FacesException(javax.faces.FacesException) FacesException(javax.faces.FacesException) Airport(com.liferay.faces.demos.dto.Airport) FileOutputStream(java.io.FileOutputStream) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) File(java.io.File) PostConstruct(javax.annotation.PostConstruct)

Aggregations

Airport (com.liferay.faces.demos.dto.Airport)3 PostConstruct (javax.annotation.PostConstruct)2 LatLng (com.javadocmd.simplelatlng.LatLng)1 Booking (com.liferay.faces.demos.dto.Booking)1 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 BigDecimal (java.math.BigDecimal)1 URL (java.net.URL)1 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1 GregorianCalendar (java.util.GregorianCalendar)1 Random (java.util.Random)1