@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;
  }
 @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;
 }
 /**
  * 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 = "/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;
  }