@Override public Address getAddress( Long userId, final MerchantStore merchantStore, boolean isBillingAddress) throws Exception { LOG.info("Fetching customer for id {} ", userId); Address address = null; final Customer customerModel = customerService.getById(userId); if (customerModel == null) { LOG.error("Customer with ID {} does not exists..", userId); throw new CustomerNotFoundException("customer with given id does not exists"); } if (isBillingAddress) { LOG.info("getting billing address.."); CustomerBillingAddressPopulator billingAddressPopulator = new CustomerBillingAddressPopulator(); address = billingAddressPopulator.populate( customerModel, merchantStore, merchantStore.getDefaultLanguage()); address.setBillingAddress(true); return address; } LOG.info("getting Delivery address.."); CustomerDeliveryAddressPopulator deliveryAddressPopulator = new CustomerDeliveryAddressPopulator(); return deliveryAddressPopulator.populate( customerModel, merchantStore, merchantStore.getDefaultLanguage()); }
@Override public void updateAddress( Long userId, MerchantStore merchantStore, Address address, final Language language) throws Exception { Customer customerModel = customerService.getById(userId); Map<String, Country> countriesMap = countryService.getCountriesMap(language); Country country = countriesMap.get(address.getCountry()); if (customerModel == null) { LOG.error("Customer with ID {} does not exists..", userId); throw new CustomerNotFoundException("customer with given id does not exists"); } if (address.isBillingAddress()) { LOG.info("updating customer billing address.."); PersistableCustomerBillingAddressPopulator billingAddressPopulator = new PersistableCustomerBillingAddressPopulator(); customerModel = billingAddressPopulator.populate( address, customerModel, merchantStore, merchantStore.getDefaultLanguage()); customerModel.getBilling().setCountry(country); if (StringUtils.isNotBlank(address.getZone())) { Zone zone = zoneService.getByCode(address.getZone()); if (zone == null) { throw new ConversionException("Unsuported zone code " + address.getZone()); } customerModel.getBilling().setZone(zone); customerModel.getBilling().setState(null); } else { customerModel.getBilling().setZone(null); } } else { LOG.info("updating customer shipping address.."); PersistableCustomerShippingAddressPopulator shippingAddressPopulator = new PersistableCustomerShippingAddressPopulator(); customerModel = shippingAddressPopulator.populate( address, customerModel, merchantStore, merchantStore.getDefaultLanguage()); customerModel.getDelivery().setCountry(country); if (StringUtils.isNotBlank(address.getZone())) { Zone zone = zoneService.getByCode(address.getZone()); if (zone == null) { throw new ConversionException("Unsuported zone code " + address.getZone()); } customerModel.getDelivery().setZone(zone); customerModel.getDelivery().setState(null); } else { customerModel.getDelivery().setZone(null); } } // same update address with customer model this.customerService.saveOrUpdate(customerModel); }