private String createMetaKeywords(final PointOfServiceData pointOfServiceData) {
    final AddressData address = pointOfServiceData.getAddress();

    final String[] keywords = {
      address.getTown(), address.getPostalCode(), address.getCountry().getName()
    };
    return StringUtils.join(keywords, ',');
  }
  @ResponseBody
  @RequestMapping(
      value = "/summary/setDeliveryAddress.json",
      method = {RequestMethod.GET, RequestMethod.POST})
  @RequireHardLogIn
  public CartData setDeliveryAddress(@RequestParam(value = "addressId") final String addressId) {
    final AddressData addressData = new AddressData();
    addressData.setId(addressId);

    final CartData cartData = new CartData();
    cartData.setDeliveryAddress(addressData);

    return getB2BCheckoutFacade().updateCheckoutCart(cartData);
  }
  @RequestMapping(
      value = "/remove-address/" + ADDRESS_CODE_PATH_VARIABLE_PATTERN,
      method = {RequestMethod.GET, RequestMethod.POST})
  @RequireHardLogIn
  public String removeAddress(
      @PathVariable("addressCode") final String addressCode,
      final RedirectAttributes redirectModel) {
    final AddressData addressData = new AddressData();
    addressData.setId(addressCode);
    userFacade.removeAddress(addressData);

    GlobalMessages.addFlashMessage(
        redirectModel, GlobalMessages.CONF_MESSAGES_HOLDER, "account.confirmation.address.removed");
    return REDIRECT_TO_ADDRESS_BOOK_PAGE;
  }
 @RequestMapping(
     value = "/set-default-address/" + ADDRESS_CODE_PATH_VARIABLE_PATTERN,
     method = RequestMethod.GET)
 @RequireHardLogIn
 public String setDefaultAddress(
     @PathVariable("addressCode") final String addressCode,
     final RedirectAttributes redirectModel) {
   final AddressData addressData = new AddressData();
   addressData.setDefaultAddress(true);
   addressData.setVisibleInAddressBook(true);
   addressData.setId(addressCode);
   userFacade.setDefaultAddress(addressData);
   GlobalMessages.addFlashMessage(
       redirectModel,
       GlobalMessages.CONF_MESSAGES_HOLDER,
       "account.confirmation.default.address.changed");
   return REDIRECT_TO_ADDRESS_BOOK_PAGE;
 }
 @ResponseBody
 @RequestMapping(
     value = "/summary/getDeliveryAddresses.json",
     method = {RequestMethod.GET, RequestMethod.POST})
 @RequireHardLogIn
 public List<? extends AddressData> getDeliveryAddresses() {
   final List<? extends AddressData> deliveryAddresses =
       getCheckoutFacade().getSupportedDeliveryAddresses(true);
   if (deliveryAddresses == null) {
     return Collections.<AddressData>emptyList();
   }
   for (final AddressData address : deliveryAddresses) {
     if (getUserFacade().isDefaultAddress(address.getId())) {
       address.setDefaultAddress(true);
       break;
     }
   }
   return deliveryAddresses;
 }
 /**
  * Gets the customer bill to data.
  *
  * @param addressData the address data
  * @return the customer bill to data
  */
 private CustomerBillToData getCustomerBillToData(final AddressData addressData) {
   final CustomerBillToData billToData = new CustomerBillToData();
   billToData.setBillToCity(addressData.getTown());
   billToData.setBillToFirstName(addressData.getFirstName());
   billToData.setBillToLastName(addressData.getLastName());
   billToData.setBillToStreet1(addressData.getLine1());
   billToData.setBillToStreet2(addressData.getLine2());
   final RegionData regionData = addressData.getRegion();
   String regionIsoCode = "MH";
   if (regionData != null) {
     regionIsoCode = regionData.getIsocode();
   }
   billToData.setBillToState(regionIsoCode);
   billToData.setBillToCountry(addressData.getCountry().getIsocode());
   billToData.setBillToPostalCode(addressData.getPostalCode());
   return billToData;
 }
  @RequestMapping(value = "/summary/createUpdatePaymentDetails.json", method = RequestMethod.POST)
  @RequireHardLogIn
  public String createUpdatePaymentDetails(
      final Model model, @Valid final PaymentDetailsForm form, final BindingResult bindingResult) {
    paymentDetailsValidator.validate(form, bindingResult);

    final boolean editMode = StringUtils.isNotBlank(form.getPaymentId());

    if (bindingResult.hasErrors()) {
      model.addAttribute("edit", Boolean.valueOf(editMode));

      return ControllerConstants.Views.Fragments.SingleStepCheckout.PaymentDetailsFormPopup;
    }

    final CCPaymentInfoData paymentInfoData = new CCPaymentInfoData();
    paymentInfoData.setId(form.getPaymentId());
    paymentInfoData.setCardType(form.getCardTypeCode());
    paymentInfoData.setAccountHolderName(form.getNameOnCard());
    paymentInfoData.setCardNumber(form.getCardNumber());
    paymentInfoData.setStartMonth(form.getStartMonth());
    paymentInfoData.setStartYear(form.getStartYear());
    paymentInfoData.setExpiryMonth(form.getExpiryMonth());
    paymentInfoData.setExpiryYear(form.getExpiryYear());
    paymentInfoData.setSaved(Boolean.TRUE.equals(form.getSaveInAccount()));
    paymentInfoData.setIssueNumber(form.getIssueNumber());

    final AddressData addressData;
    if (!editMode && Boolean.FALSE.equals(form.getNewBillingAddress())) {
      addressData = getCheckoutCart().getDeliveryAddress();
      if (addressData == null) {
        GlobalMessages.addErrorMessage(
            model, "checkout.paymentMethod.createSubscription.billingAddress.noneSelected");

        model.addAttribute("edit", Boolean.valueOf(editMode));
        return ControllerConstants.Views.Fragments.SingleStepCheckout.PaymentDetailsFormPopup;
      }

      addressData.setBillingAddress(true); // mark this as billing address
    } else {
      final AddressForm addressForm = form.getBillingAddress();

      addressData = new AddressData();
      if (addressForm != null) {
        addressData.setId(addressForm.getAddressId());
        addressData.setTitleCode(addressForm.getTitleCode());
        addressData.setFirstName(addressForm.getFirstName());
        addressData.setLastName(addressForm.getLastName());
        addressData.setLine1(addressForm.getLine1());
        addressData.setLine2(addressForm.getLine2());
        addressData.setTown(addressForm.getTownCity());
        addressData.setPostalCode(addressForm.getPostcode());
        addressData.setCountry(getI18NFacade().getCountryForIsocode(addressForm.getCountryIso()));
        addressData.setShippingAddress(Boolean.TRUE.equals(addressForm.getShippingAddress()));
        addressData.setBillingAddress(Boolean.TRUE.equals(addressForm.getBillingAddress()));
      }
    }

    paymentInfoData.setBillingAddress(addressData);

    final CCPaymentInfoData newPaymentSubscription =
        getCheckoutFacade().createPaymentSubscription(paymentInfoData);
    if (newPaymentSubscription != null
        && StringUtils.isNotBlank(newPaymentSubscription.getSubscriptionId())) {
      if (Boolean.TRUE.equals(form.getSaveInAccount())
          && getUserFacade().getCCPaymentInfos(true).size() <= 1) {
        getUserFacade().setDefaultPaymentInfo(newPaymentSubscription);
      }
      getCheckoutFacade().setPaymentDetails(newPaymentSubscription.getId());
    } else {
      GlobalMessages.addErrorMessage(model, "checkout.paymentMethod.createSubscription.failed");

      model.addAttribute("edit", Boolean.valueOf(editMode));
      return ControllerConstants.Views.Fragments.SingleStepCheckout.PaymentDetailsFormPopup;
    }

    model.addAttribute("createUpdateStatus", "Success");
    model.addAttribute("paymentId", newPaymentSubscription.getId());

    return REDIRECT_PREFIX
        + "/checkout/single/summary/getPaymentDetailsForm.json?paymentId="
        + paymentInfoData.getId()
        + "&createUpdateStatus=Success";
  }
  @RequestMapping(value = "/billingaddressform", method = RequestMethod.GET)
  public String getCountryAddressForm(
      @RequestParam("countryIsoCode") final String countryIsoCode,
      @RequestParam("useDeliveryAddress") final boolean useDeliveryAddress,
      final Model model) {
    model.addAttribute("supportedCountries", getCountries());
    model.addAttribute("regions", getI18NFacade().getRegionsForCountryIso(countryIsoCode));
    model.addAttribute("country", countryIsoCode);

    final SopPaymentDetailsForm sopPaymentDetailsForm = new SopPaymentDetailsForm();
    model.addAttribute("sopPaymentDetailsForm", sopPaymentDetailsForm);
    if (useDeliveryAddress) {
      final AddressData deliveryAddress =
          getCheckoutFacade().getCheckoutCart().getDeliveryAddress();

      if (deliveryAddress.getRegion() != null
          && !StringUtils.isEmpty(deliveryAddress.getRegion().getIsocode())) {
        sopPaymentDetailsForm.setBillTo_state(deliveryAddress.getRegion().getIsocodeShort());
      }

      sopPaymentDetailsForm.setBillTo_titleCode(deliveryAddress.getTitleCode());
      sopPaymentDetailsForm.setBillTo_firstName(deliveryAddress.getFirstName());
      sopPaymentDetailsForm.setBillTo_lastName(deliveryAddress.getLastName());
      sopPaymentDetailsForm.setBillTo_street1(deliveryAddress.getLine1());
      sopPaymentDetailsForm.setBillTo_street2(deliveryAddress.getLine2());
      sopPaymentDetailsForm.setBillTo_city(deliveryAddress.getTown());
      sopPaymentDetailsForm.setBillTo_postalCode(deliveryAddress.getPostalCode());
      sopPaymentDetailsForm.setBillTo_country(deliveryAddress.getCountry().getIsocode());
      sopPaymentDetailsForm.setBillTo_phoneNumber(deliveryAddress.getPhone());
    }
    return ControllerConstants.Views.Fragments.Checkout.BillingAddressForm;
  }
  @RequestMapping(value = "/summary/createUpdateDeliveryAddress.json", method = RequestMethod.POST)
  @RequireHardLogIn
  public String createUpdateDeliveryAddress(
      final Model model, @Valid final AddressForm form, final BindingResult bindingResult) {
    if (bindingResult.hasErrors()) {
      model.addAttribute("edit", Boolean.valueOf(StringUtils.isNotBlank(form.getAddressId())));
      // Work out if the address form should be displayed based on the payment type
      final B2BPaymentTypeData paymentType = getCheckoutFacade().getCheckoutCart().getPaymentType();
      final boolean payOnAccount =
          paymentType != null
              && CheckoutPaymentType.ACCOUNT.getCode().equals(paymentType.getCode());
      model.addAttribute("showAddressForm", Boolean.valueOf(!payOnAccount));

      return ControllerConstants.Views.Fragments.SingleStepCheckout.DeliveryAddressFormPopup;
    }

    // create delivery address and set it on cart
    final AddressData addressData = new AddressData();
    addressData.setId(form.getAddressId());
    addressData.setTitleCode(form.getTitleCode());
    addressData.setFirstName(form.getFirstName());
    addressData.setLastName(form.getLastName());
    addressData.setLine1(form.getLine1());
    addressData.setLine2(form.getLine2());
    addressData.setTown(form.getTownCity());
    addressData.setPostalCode(form.getPostcode());
    addressData.setCountry(getI18NFacade().getCountryForIsocode(form.getCountryIso()));
    addressData.setShippingAddress(
        Boolean.TRUE.equals(form.getShippingAddress())
            || Boolean.TRUE.equals(form.getSaveInAddressBook()));

    addressData.setVisibleInAddressBook(
        Boolean.TRUE.equals(form.getSaveInAddressBook())
            || StringUtils.isNotBlank(form.getAddressId()));
    addressData.setDefaultAddress(Boolean.TRUE.equals(form.getDefaultAddress()));

    if (StringUtils.isBlank(form.getAddressId())) {
      getUserFacade().addAddress(addressData);
    } else {
      getUserFacade().editAddress(addressData);
    }

    getCheckoutFacade().setDeliveryAddress(addressData);

    if (getCheckoutFacade().getCheckoutCart().getDeliveryMode() == null) {
      getCheckoutFacade().setDeliveryModeIfAvailable();
    }

    model.addAttribute("createUpdateStatus", "Success");
    model.addAttribute("addressId", addressData.getId());

    return REDIRECT_PREFIX
        + "/checkout/single/summary/getDeliveryAddressForm.json?addressId="
        + addressData.getId()
        + "&createUpdateStatus=Success";
  }
  @RequestMapping(
      value = "/summary/getPaymentDetailsForm.json",
      method = {RequestMethod.GET, RequestMethod.POST})
  @RequireHardLogIn
  public String getPaymentDetailsForm(
      final Model model,
      @RequestParam(value = "paymentId") final String paymentId,
      @RequestParam(value = "createUpdateStatus") final String createUpdateStatus) {
    CCPaymentInfoData paymentInfoData = null;
    if (StringUtils.isNotBlank(paymentId)) {
      paymentInfoData = getUserFacade().getCCPaymentInfoForCode(paymentId);
    }

    final PaymentDetailsForm paymentDetailsForm = new PaymentDetailsForm();

    if (paymentInfoData != null) {
      paymentDetailsForm.setPaymentId(paymentInfoData.getId());
      paymentDetailsForm.setCardTypeCode(paymentInfoData.getCardType());
      paymentDetailsForm.setNameOnCard(paymentInfoData.getAccountHolderName());
      paymentDetailsForm.setCardNumber(paymentInfoData.getCardNumber());
      paymentDetailsForm.setStartMonth(paymentInfoData.getStartMonth());
      paymentDetailsForm.setStartYear(paymentInfoData.getStartYear());
      paymentDetailsForm.setExpiryMonth(paymentInfoData.getExpiryMonth());
      paymentDetailsForm.setExpiryYear(paymentInfoData.getExpiryYear());
      paymentDetailsForm.setSaveInAccount(Boolean.valueOf(paymentInfoData.isSaved()));
      paymentDetailsForm.setIssueNumber(paymentInfoData.getIssueNumber());

      final AddressForm addressForm = new AddressForm();
      final AddressData addressData = paymentInfoData.getBillingAddress();
      if (addressData != null) {
        addressForm.setAddressId(addressData.getId());
        addressForm.setTitleCode(addressData.getTitleCode());
        addressForm.setFirstName(addressData.getFirstName());
        addressForm.setLastName(addressData.getLastName());
        addressForm.setLine1(addressData.getLine1());
        addressForm.setLine2(addressData.getLine2());
        addressForm.setTownCity(addressData.getTown());
        addressForm.setPostcode(addressData.getPostalCode());
        addressForm.setCountryIso(addressData.getCountry().getIsocode());
        addressForm.setShippingAddress(Boolean.valueOf(addressData.isShippingAddress()));
        addressForm.setBillingAddress(Boolean.valueOf(addressData.isBillingAddress()));
      }

      paymentDetailsForm.setBillingAddress(addressForm);
    }

    model.addAttribute("edit", Boolean.valueOf(paymentInfoData != null));
    model.addAttribute("paymentInfoData", getUserFacade().getCCPaymentInfos(true));
    model.addAttribute(paymentDetailsForm);
    model.addAttribute("createUpdateStatus", createUpdateStatus);
    return ControllerConstants.Views.Fragments.SingleStepCheckout.PaymentDetailsFormPopup;
  }
  @RequestMapping(value = "/addressform", method = RequestMethod.GET)
  public String getCountryAddressForm(
      @RequestParam("addressCode") final String addressCode,
      @RequestParam("countryIsoCode") final String countryIsoCode,
      final Model model) {
    model.addAttribute("supportedCountries", getCountries());
    model.addAttribute("regions", getI18NFacade().getRegionsForCountryIso(countryIsoCode));
    model.addAttribute("country", countryIsoCode);

    final AddressForm addressForm = new AddressForm();
    model.addAttribute("addressForm", addressForm);
    for (final AddressData addressData : userFacade.getAddressBook()) {
      if (addressData.getId() != null
          && addressData.getId().equals(addressCode)
          && countryIsoCode.equals(addressData.getCountry().getIsocode())) {
        model.addAttribute("addressData", addressData);
        addressForm.setAddressId(addressData.getId());
        addressForm.setTitleCode(addressData.getTitleCode());
        addressForm.setFirstName(addressData.getFirstName());
        addressForm.setLastName(addressData.getLastName());
        addressForm.setLine1(addressData.getLine1());
        addressForm.setLine2(addressData.getLine2());
        addressForm.setTownCity(addressData.getTown());
        addressForm.setPostcode(addressData.getPostalCode());
        addressForm.setCountryIso(addressData.getCountry().getIsocode());

        if (addressData.getRegion() != null
            && !StringUtils.isEmpty(addressData.getRegion().getIsocode())) {
          addressForm.setRegionIso(addressData.getRegion().getIsocode());
        }

        break;
      }
    }
    return ControllerConstants.Views.Fragments.Account.CountryAddressForm;
  }
  @RequestMapping(
      value = "/summary/getDeliveryAddressForm.json",
      method = {RequestMethod.GET, RequestMethod.POST})
  @RequireHardLogIn
  public String getDeliveryAddressForm(
      final Model model,
      @RequestParam(value = "addressId") final String addressId,
      @RequestParam(value = "createUpdateStatus") final String createUpdateStatus) {
    AddressData addressData = null;
    if (addressId != null && !addressId.isEmpty()) {
      addressData = getCheckoutFacade().getDeliveryAddressForCode(addressId);
    }

    final AddressForm addressForm = new AddressForm();

    final boolean hasAddressData = addressData != null;
    if (hasAddressData) {
      addressForm.setAddressId(addressData.getId());
      addressForm.setTitleCode(addressData.getTitleCode());
      addressForm.setFirstName(addressData.getFirstName());
      addressForm.setLastName(addressData.getLastName());
      addressForm.setLine1(addressData.getLine1());
      addressForm.setLine2(addressData.getLine2());
      addressForm.setTownCity(addressData.getTown());
      addressForm.setPostcode(addressData.getPostalCode());
      addressForm.setCountryIso(addressData.getCountry().getIsocode());
      addressForm.setShippingAddress(Boolean.valueOf(addressData.isShippingAddress()));
      addressForm.setBillingAddress(Boolean.valueOf(addressData.isBillingAddress()));
    }

    model.addAttribute("edit", Boolean.valueOf(hasAddressData));
    model.addAttribute("noAddresses", Boolean.valueOf(getUserFacade().isAddressBookEmpty()));

    model.addAttribute(addressForm);
    model.addAttribute("createUpdateStatus", createUpdateStatus);

    // Work out if the address form should be displayed based on the payment type
    final B2BPaymentTypeData paymentType = getCheckoutFacade().getCheckoutCart().getPaymentType();
    final boolean payOnAccount =
        paymentType != null && CheckoutPaymentType.ACCOUNT.getCode().equals(paymentType.getCode());
    model.addAttribute("showAddressForm", Boolean.valueOf(!payOnAccount));

    return ControllerConstants.Views.Fragments.SingleStepCheckout.DeliveryAddressFormPopup;
  }
  @RequestMapping(
      value = "/edit-address/" + ADDRESS_CODE_PATH_VARIABLE_PATTERN,
      method = RequestMethod.GET)
  @RequireHardLogIn
  public String editAddress(
      @PathVariable("addressCode") final String addressCode, final Model model)
      throws CMSItemNotFoundException {
    final AddressForm addressForm = new AddressForm();
    model.addAttribute("countryData", checkoutFacade.getDeliveryCountries());
    model.addAttribute("titleData", userFacade.getTitles());
    model.addAttribute("addressForm", addressForm);
    model.addAttribute("addressBookEmpty", Boolean.valueOf(userFacade.isAddressBookEmpty()));

    for (final AddressData addressData : userFacade.getAddressBook()) {
      if (addressData.getId() != null && addressData.getId().equals(addressCode)) {
        model.addAttribute(
            "regions",
            getI18NFacade().getRegionsForCountryIso(addressData.getCountry().getIsocode()));
        model.addAttribute("country", addressData.getCountry().getIsocode());
        model.addAttribute("addressData", addressData);
        addressForm.setAddressId(addressData.getId());
        addressForm.setTitleCode(addressData.getTitleCode());
        addressForm.setFirstName(addressData.getFirstName());
        addressForm.setLastName(addressData.getLastName());
        addressForm.setLine1(addressData.getLine1());
        addressForm.setLine2(addressData.getLine2());
        addressForm.setTownCity(addressData.getTown());
        addressForm.setPostcode(addressData.getPostalCode());
        addressForm.setCountryIso(addressData.getCountry().getIsocode());
        if (addressData.getRegion() != null
            && !StringUtils.isEmpty(addressData.getRegion().getIsocode())) {
          addressForm.setRegionIso(addressData.getRegion().getIsocode());
        }

        if (isDefaultAddress(addressData.getId())) {
          addressForm.setDefaultAddress(Boolean.TRUE);
          model.addAttribute("isDefaultAddress", Boolean.TRUE);
        } else {
          addressForm.setDefaultAddress(Boolean.FALSE);
          model.addAttribute("isDefaultAddress", Boolean.FALSE);
        }
        break;
      }
    }

    storeCmsPageInModel(model, getContentPageForLabelOrId(ADD_EDIT_ADDRESS_CMS_PAGE));
    setUpMetaDataForContentPage(model, getContentPageForLabelOrId(ADD_EDIT_ADDRESS_CMS_PAGE));

    final List<Breadcrumb> breadcrumbs = accountBreadcrumbBuilder.getBreadcrumbs(null);
    breadcrumbs.add(
        new Breadcrumb(
            "/my-account/address-book",
            getMessageSource()
                .getMessage("text.account.addressBook", null, getI18nService().getCurrentLocale()),
            null));
    breadcrumbs.add(
        new Breadcrumb(
            "#",
            getMessageSource()
                .getMessage(
                    "text.account.addressBook.addEditAddress",
                    null,
                    getI18nService().getCurrentLocale()),
            null));
    model.addAttribute("breadcrumbs", breadcrumbs);
    model.addAttribute("metaRobots", "no-index,no-follow");
    model.addAttribute("edit", Boolean.TRUE);
    return ControllerConstants.Views.Pages.Account.AccountEditAddressPage;
  }
 /**
  * Method checks if address is set as default
  *
  * @param addressId - identifier for address to check
  * @return true if address is default, false if address is not default
  */
 protected boolean isDefaultAddress(final String addressId) {
   final AddressData defaultAddress = userFacade.getDefaultAddress();
   return (defaultAddress != null
       && defaultAddress.getId() != null
       && defaultAddress.getId().equals(addressId));
 }
  @RequestMapping(value = "/add-address", method = RequestMethod.POST)
  @RequireHardLogIn
  public String addAddress(
      final AddressForm addressForm,
      final BindingResult bindingResult,
      final Model model,
      final HttpServletRequest request,
      final RedirectAttributes redirectModel)
      throws CMSItemNotFoundException {
    getAddressValidator().validate(addressForm, bindingResult);
    if (bindingResult.hasErrors()) {
      GlobalMessages.addErrorMessage(model, "form.global.error");
      storeCmsPageInModel(model, getContentPageForLabelOrId(ADD_EDIT_ADDRESS_CMS_PAGE));
      setUpMetaDataForContentPage(model, getContentPageForLabelOrId(ADD_EDIT_ADDRESS_CMS_PAGE));
      setUpAddressFormAfterError(addressForm, model);
      return getViewForPage(model);
    }

    final AddressData newAddress = new AddressData();
    newAddress.setTitleCode(addressForm.getTitleCode());
    newAddress.setFirstName(addressForm.getFirstName());
    newAddress.setLastName(addressForm.getLastName());
    newAddress.setLine1(addressForm.getLine1());
    newAddress.setLine2(addressForm.getLine2());
    newAddress.setTown(addressForm.getTownCity());
    newAddress.setPostalCode(addressForm.getPostcode());
    newAddress.setBillingAddress(false);
    newAddress.setShippingAddress(true);
    newAddress.setVisibleInAddressBook(true);
    newAddress.setCountry(getI18NFacade().getCountryForIsocode(addressForm.getCountryIso()));

    if (addressForm.getRegionIso() != null && !StringUtils.isEmpty(addressForm.getRegionIso())) {
      newAddress.setRegion(
          getI18NFacade().getRegion(addressForm.getCountryIso(), addressForm.getRegionIso()));
    }

    if (userFacade.isAddressBookEmpty()) {
      newAddress.setDefaultAddress(true);
      newAddress.setVisibleInAddressBook(true);
    } else {
      newAddress.setDefaultAddress(
          addressForm.getDefaultAddress() != null
              && addressForm.getDefaultAddress().booleanValue());
    }

    final AddressVerificationResult<AddressVerificationDecision> verificationResult =
        getAddressVerificationFacade().verifyAddressData(newAddress);
    final boolean addressRequiresReview =
        getAddressVerificationResultHandler()
            .handleResult(
                verificationResult,
                newAddress,
                model,
                redirectModel,
                bindingResult,
                getAddressVerificationFacade().isCustomerAllowedToIgnoreAddressSuggestions(),
                "checkout.multi.address.added");

    if (addressRequiresReview) {
      model.addAttribute(
          "regions", getI18NFacade().getRegionsForCountryIso(addressForm.getCountryIso()));
      model.addAttribute("country", addressForm.getCountryIso());
      storeCmsPageInModel(model, getContentPageForLabelOrId(ADD_EDIT_ADDRESS_CMS_PAGE));
      setUpMetaDataForContentPage(model, getContentPageForLabelOrId(ADD_EDIT_ADDRESS_CMS_PAGE));
      return getViewForPage(model);
    }

    userFacade.addAddress(newAddress);

    return REDIRECT_TO_ADDRESS_BOOK_PAGE;
  }
  @RequestMapping(value = "/select-suggested-address", method = RequestMethod.POST)
  public String doSelectSuggestedAddress(
      final AddressForm addressForm, final RedirectAttributes redirectModel) {
    final Set<String> resolveCountryRegions =
        org.springframework.util.StringUtils.commaDelimitedListToSet(
            Config.getParameter("resolve.country.regions"));

    final AddressData selectedAddress = new AddressData();
    selectedAddress.setId(addressForm.getAddressId());
    selectedAddress.setTitleCode(addressForm.getTitleCode());
    selectedAddress.setFirstName(addressForm.getFirstName());
    selectedAddress.setLastName(addressForm.getLastName());
    selectedAddress.setLine1(addressForm.getLine1());
    selectedAddress.setLine2(addressForm.getLine2());
    selectedAddress.setTown(addressForm.getTownCity());
    selectedAddress.setPostalCode(addressForm.getPostcode());
    selectedAddress.setBillingAddress(false);
    selectedAddress.setShippingAddress(true);
    selectedAddress.setVisibleInAddressBook(true);

    final CountryData countryData = i18NFacade.getCountryForIsocode(addressForm.getCountryIso());
    selectedAddress.setCountry(countryData);

    if (resolveCountryRegions.contains(countryData.getIsocode())) {
      if (addressForm.getRegionIso() != null && !StringUtils.isEmpty(addressForm.getRegionIso())) {
        final RegionData regionData =
            getI18NFacade().getRegion(addressForm.getCountryIso(), addressForm.getRegionIso());
        selectedAddress.setRegion(regionData);
      }
    }

    if (resolveCountryRegions.contains(countryData.getIsocode())) {
      if (addressForm.getRegionIso() != null && !StringUtils.isEmpty(addressForm.getRegionIso())) {
        final RegionData regionData =
            getI18NFacade().getRegion(addressForm.getCountryIso(), addressForm.getRegionIso());
        selectedAddress.setRegion(regionData);
      }
    }

    if (Boolean.TRUE.equals(addressForm.getEditAddress())) {
      selectedAddress.setDefaultAddress(
          Boolean.TRUE.equals(addressForm.getDefaultAddress())
              || userFacade.getAddressBook().size() <= 1);
      userFacade.editAddress(selectedAddress);
    } else {
      selectedAddress.setDefaultAddress(
          Boolean.TRUE.equals(addressForm.getDefaultAddress()) || userFacade.isAddressBookEmpty());
      userFacade.addAddress(selectedAddress);
    }

    GlobalMessages.addFlashMessage(
        redirectModel, GlobalMessages.CONF_MESSAGES_HOLDER, "account.confirmation.address.added");

    return REDIRECT_TO_ADDRESS_BOOK_PAGE;
  }
  @RequestMapping(
      value = "/edit-address/" + ADDRESS_CODE_PATH_VARIABLE_PATTERN,
      method = RequestMethod.POST)
  @RequireHardLogIn
  public String editAddress(
      final AddressForm addressForm,
      final BindingResult bindingResult,
      final Model model,
      final RedirectAttributes redirectModel)
      throws CMSItemNotFoundException {
    getAddressValidator().validate(addressForm, bindingResult);
    if (bindingResult.hasErrors()) {
      GlobalMessages.addErrorMessage(model, "form.global.error");
      storeCmsPageInModel(model, getContentPageForLabelOrId(ADD_EDIT_ADDRESS_CMS_PAGE));
      setUpMetaDataForContentPage(model, getContentPageForLabelOrId(ADD_EDIT_ADDRESS_CMS_PAGE));
      setUpAddressFormAfterError(addressForm, model);
      return ControllerConstants.Views.Pages.Account.AccountEditAddressPage;
    }

    model.addAttribute("metaRobots", "no-index,no-follow");

    final AddressData newAddress = new AddressData();
    newAddress.setId(addressForm.getAddressId());
    newAddress.setTitleCode(addressForm.getTitleCode());
    newAddress.setFirstName(addressForm.getFirstName());
    newAddress.setLastName(addressForm.getLastName());
    newAddress.setLine1(addressForm.getLine1());
    newAddress.setLine2(addressForm.getLine2());
    newAddress.setTown(addressForm.getTownCity());
    newAddress.setPostalCode(addressForm.getPostcode());
    newAddress.setBillingAddress(false);
    newAddress.setShippingAddress(true);
    newAddress.setVisibleInAddressBook(true);
    newAddress.setCountry(getI18NFacade().getCountryForIsocode(addressForm.getCountryIso()));

    if (addressForm.getRegionIso() != null && !StringUtils.isEmpty(addressForm.getRegionIso())) {
      newAddress.setRegion(
          getI18NFacade().getRegion(addressForm.getCountryIso(), addressForm.getRegionIso()));
    }

    if (Boolean.TRUE.equals(addressForm.getDefaultAddress())
        || userFacade.getAddressBook().size() <= 1) {
      newAddress.setDefaultAddress(true);
      newAddress.setVisibleInAddressBook(true);
    }

    final AddressVerificationResult<AddressVerificationDecision> verificationResult =
        getAddressVerificationFacade().verifyAddressData(newAddress);
    final boolean addressRequiresReview =
        getAddressVerificationResultHandler()
            .handleResult(
                verificationResult,
                newAddress,
                model,
                redirectModel,
                bindingResult,
                getAddressVerificationFacade().isCustomerAllowedToIgnoreAddressSuggestions(),
                "checkout.multi.address.updated");

    if (addressRequiresReview) {
      model.addAttribute(
          "regions", getI18NFacade().getRegionsForCountryIso(addressForm.getCountryIso()));
      model.addAttribute("country", addressForm.getCountryIso());
      model.addAttribute("edit", Boolean.TRUE);
      storeCmsPageInModel(model, getContentPageForLabelOrId(ADD_EDIT_ADDRESS_CMS_PAGE));
      setUpMetaDataForContentPage(model, getContentPageForLabelOrId(ADD_EDIT_ADDRESS_CMS_PAGE));
      return ControllerConstants.Views.Pages.Account.AccountEditAddressPage;
    }

    userFacade.editAddress(newAddress);

    return REDIRECT_TO_ADDRESS_BOOK_PAGE;
  }