Search in sources :

Example 1 with DistanceMatrixRow

use of com.google.maps.model.DistanceMatrixRow in project shopizer by shopizer-ecommerce.

the class ShippingDistancePreProcessorImpl method prePostProcessShippingQuotes.

public void prePostProcessShippingQuotes(ShippingQuote quote, List<PackageDetails> packages, BigDecimal orderTotal, Delivery delivery, ShippingOrigin origin, MerchantStore store, IntegrationConfiguration globalShippingConfiguration, IntegrationModule currentModule, ShippingConfiguration shippingConfiguration, List<IntegrationModule> allModules, Locale locale) throws IntegrationException {
    if (delivery.getZone() == null) {
        return;
    }
    boolean zoneAllowed = false;
    if (allowedZonesCodes != null) {
        for (String zoneCode : allowedZonesCodes) {
            if (zoneCode.equals(delivery.getZone().getCode())) {
                zoneAllowed = true;
                break;
            }
        }
    }
    if (!zoneAllowed) {
        return;
    }
    if (StringUtils.isBlank(delivery.getPostalCode())) {
        return;
    }
    Validate.notNull(apiKey, "Requires the configuration of google apiKey");
    GeoApiContext context = new GeoApiContext().setApiKey(apiKey);
    // build origin address
    StringBuilder originAddress = new StringBuilder();
    originAddress.append(origin.getAddress()).append(BLANK).append(origin.getCity()).append(BLANK).append(origin.getPostalCode()).append(BLANK);
    if (!StringUtils.isBlank(origin.getState())) {
        originAddress.append(origin.getState()).append(" ");
    }
    if (origin.getZone() != null) {
        originAddress.append(origin.getZone().getCode()).append(" ");
    }
    originAddress.append(origin.getCountry().getIsoCode());
    // build destination address
    StringBuilder destinationAddress = new StringBuilder();
    destinationAddress.append(delivery.getAddress()).append(BLANK);
    if (!StringUtils.isBlank(delivery.getCity())) {
        destinationAddress.append(delivery.getCity()).append(BLANK);
    }
    destinationAddress.append(delivery.getPostalCode()).append(BLANK);
    if (!StringUtils.isBlank(delivery.getState())) {
        destinationAddress.append(delivery.getState()).append(" ");
    }
    if (delivery.getZone() != null) {
        destinationAddress.append(delivery.getZone().getCode()).append(" ");
    }
    destinationAddress.append(delivery.getCountry().getIsoCode());
    try {
        GeocodingResult[] originAdressResult = GeocodingApi.geocode(context, originAddress.toString()).await();
        GeocodingResult[] destinationAdressResult = GeocodingApi.geocode(context, destinationAddress.toString()).await();
        if (originAdressResult.length > 0 && destinationAdressResult.length > 0) {
            LatLng originLatLng = originAdressResult[0].geometry.location;
            LatLng destinationLatLng = destinationAdressResult[0].geometry.location;
            delivery.setLatitude(String.valueOf(destinationLatLng.lat));
            delivery.setLongitude(String.valueOf(destinationLatLng.lng));
            // keep latlng for further usage in order to display the map
            DistanceMatrix distanceRequest = DistanceMatrixApi.newRequest(context).origins(new LatLng(originLatLng.lat, originLatLng.lng)).destinations(new LatLng(destinationLatLng.lat, destinationLatLng.lng)).awaitIgnoreError();
            if (distanceRequest != null) {
                DistanceMatrixRow distanceMax = distanceRequest.rows[0];
                Distance distance = distanceMax.elements[0].distance;
                quote.getQuoteInformations().put(Constants.DISTANCE_KEY, 0.001 * distance.inMeters);
            } else {
                LOGGER.error("Expected distance inner google api to return DistanceMatrix, it returned null. API key might not be working for this request");
            }
        }
    } catch (Exception e) {
        LOGGER.error("Exception while calculating the shipping distance", e);
    }
}
Also used : GeocodingResult(com.google.maps.model.GeocodingResult) LatLng(com.google.maps.model.LatLng) DistanceMatrix(com.google.maps.model.DistanceMatrix) GeoApiContext(com.google.maps.GeoApiContext) DistanceMatrixRow(com.google.maps.model.DistanceMatrixRow) Distance(com.google.maps.model.Distance) IntegrationException(com.salesmanager.core.modules.integration.IntegrationException)

Aggregations

GeoApiContext (com.google.maps.GeoApiContext)1 Distance (com.google.maps.model.Distance)1 DistanceMatrix (com.google.maps.model.DistanceMatrix)1 DistanceMatrixRow (com.google.maps.model.DistanceMatrixRow)1 GeocodingResult (com.google.maps.model.GeocodingResult)1 LatLng (com.google.maps.model.LatLng)1 IntegrationException (com.salesmanager.core.modules.integration.IntegrationException)1