Search in sources :

Example 1 with MapService

use of com.axelor.apps.base.service.MapService in project axelor-open-suite by axelor.

the class AddressController method viewDirection.

public void viewDirection(ActionRequest request, ActionResponse response) {
    AddressRepository addressRepository = Beans.get(AddressRepository.class);
    try {
        MapService mapService = Beans.get(MapService.class);
        String key = null;
        if (Beans.get(AppBaseService.class).getAppBase().getMapApiSelect() == AppBaseRepository.MAP_API_GOOGLE) {
            key = mapService.getGoogleMapsApiKey();
        }
        Company company = Optional.ofNullable(AuthUtils.getUser()).map(User::getActiveCompany).orElse(null);
        if (company == null) {
            response.setFlash(I18n.get(IExceptionMessage.PRODUCT_NO_ACTIVE_COMPANY));
            return;
        }
        Address departureAddress = company.getAddress();
        if (departureAddress == null) {
            response.setFlash(I18n.get(IExceptionMessage.ADDRESS_7));
            return;
        }
        departureAddress = addressRepository.find(departureAddress.getId());
        Optional<Pair<BigDecimal, BigDecimal>> departureLatLong = Beans.get(AddressService.class).getOrUpdateLatLong(departureAddress);
        if (!departureLatLong.isPresent()) {
            response.setFlash(String.format(I18n.get(IExceptionMessage.ADDRESS_5), departureAddress.getFullName()));
            return;
        }
        Address arrivalAddress = request.getContext().asType(Address.class);
        arrivalAddress = addressRepository.find(arrivalAddress.getId());
        Optional<Pair<BigDecimal, BigDecimal>> arrivalLatLong = Beans.get(AddressService.class).getOrUpdateLatLong(arrivalAddress);
        if (!arrivalLatLong.isPresent()) {
            response.setFlash(String.format(I18n.get(IExceptionMessage.ADDRESS_5), arrivalAddress.getFullName()));
            return;
        }
        Map<String, Object> mapView = new HashMap<>();
        mapView.put("title", "Map");
        mapView.put("resource", mapService.getDirectionUrl(key, departureLatLong.get(), arrivalLatLong.get()));
        mapView.put("viewType", "html");
        response.setView(mapView);
        response.setReload(true);
    } catch (Exception e) {
        TraceBackService.trace(response, e);
    }
}
Also used : Company(com.axelor.apps.base.db.Company) PartnerAddress(com.axelor.apps.base.db.PartnerAddress) Address(com.axelor.apps.base.db.Address) HashMap(java.util.HashMap) AxelorException(com.axelor.exception.AxelorException) IOException(java.io.IOException) AddressService(com.axelor.apps.base.service.AddressService) AppBaseService(com.axelor.apps.base.service.app.AppBaseService) AddressRepository(com.axelor.apps.base.db.repo.AddressRepository) PartnerAddressRepository(com.axelor.apps.base.db.repo.PartnerAddressRepository) MapService(com.axelor.apps.base.service.MapService) Pair(org.apache.commons.lang3.tuple.Pair)

Example 2 with MapService

use of com.axelor.apps.base.service.MapService in project axelor-open-suite by axelor.

the class AddressController method viewMap.

public void viewMap(ActionRequest request, ActionResponse response) {
    try {
        Address address = request.getContext().asType(Address.class);
        address = Beans.get(AddressRepository.class).find(address.getId());
        Optional<Pair<BigDecimal, BigDecimal>> latLong = Beans.get(AddressService.class).getOrUpdateLatLong(address);
        if (latLong.isPresent()) {
            MapService mapService = Beans.get(MapService.class);
            Map<String, Object> mapView = new HashMap<>();
            mapView.put("title", "Map");
            mapView.put("resource", mapService.getMapUrl(latLong.get(), address.getFullName()));
            mapView.put("viewType", "html");
            response.setView(mapView);
        } else {
            response.setFlash(String.format(I18n.get(IExceptionMessage.ADDRESS_5), address.getFullName()));
        }
        response.setReload(true);
    } catch (Exception e) {
        TraceBackService.trace(response, e);
    }
}
Also used : PartnerAddress(com.axelor.apps.base.db.PartnerAddress) Address(com.axelor.apps.base.db.Address) AddressService(com.axelor.apps.base.service.AddressService) HashMap(java.util.HashMap) MapService(com.axelor.apps.base.service.MapService) AxelorException(com.axelor.exception.AxelorException) IOException(java.io.IOException) Pair(org.apache.commons.lang3.tuple.Pair)

Aggregations

Address (com.axelor.apps.base.db.Address)2 PartnerAddress (com.axelor.apps.base.db.PartnerAddress)2 AddressService (com.axelor.apps.base.service.AddressService)2 MapService (com.axelor.apps.base.service.MapService)2 AxelorException (com.axelor.exception.AxelorException)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 Pair (org.apache.commons.lang3.tuple.Pair)2 Company (com.axelor.apps.base.db.Company)1 AddressRepository (com.axelor.apps.base.db.repo.AddressRepository)1 PartnerAddressRepository (com.axelor.apps.base.db.repo.PartnerAddressRepository)1 AppBaseService (com.axelor.apps.base.service.app.AppBaseService)1