All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.commercetools.sunrise.myaccount.addressbook.addaddress.SunriseAddAddressController Maven / Gradle / Ivy

The newest version!
package com.commercetools.sunrise.myaccount.addressbook.addaddress;

import com.commercetools.sunrise.framework.viewmodels.content.PageContent;
import com.commercetools.sunrise.framework.controllers.SunriseContentFormController;
import com.commercetools.sunrise.framework.controllers.WithContentFormFlow;
import com.commercetools.sunrise.framework.hooks.EnableHooks;
import com.commercetools.sunrise.framework.reverserouters.SunriseRoute;
import com.commercetools.sunrise.framework.reverserouters.myaccount.addressbook.AddressBookReverseRouter;
import com.commercetools.sunrise.framework.template.engine.ContentRenderer;
import com.commercetools.sunrise.myaccount.CustomerFinder;
import com.commercetools.sunrise.myaccount.MyAccountController;
import com.commercetools.sunrise.myaccount.WithRequiredCustomer;
import com.commercetools.sunrise.myaccount.addressbook.AddressFormData;
import com.commercetools.sunrise.myaccount.addressbook.addaddress.viewmodels.AddAddressPageContentFactory;
import com.neovisionaries.i18n.CountryCode;
import io.sphere.sdk.customers.Customer;
import io.sphere.sdk.models.Address;
import play.data.Form;
import play.data.FormFactory;
import play.mvc.Result;

import java.util.concurrent.CompletionStage;

public abstract class SunriseAddAddressController extends SunriseContentFormController
        implements MyAccountController, WithContentFormFlow, WithRequiredCustomer {

    private final AddressFormData formData;
    private final CustomerFinder customerFinder;
    private final AddAddressControllerAction controllerAction;
    private final AddAddressPageContentFactory pageContentFactory;
    private final CountryCode country;

    protected SunriseAddAddressController(final ContentRenderer contentRenderer, final FormFactory formFactory,
                                          final AddressFormData formData, final CustomerFinder customerFinder,
                                          final AddAddressControllerAction controllerAction,
                                          final AddAddressPageContentFactory pageContentFactory,
                                          final CountryCode country) {
        super(contentRenderer, formFactory);
        this.formData = formData;
        this.customerFinder = customerFinder;
        this.controllerAction = controllerAction;
        this.pageContentFactory = pageContentFactory;
        this.country = country;
    }

    @Override
    public final Class getFormDataClass() {
        return formData.getClass();
    }

    @Override
    public final CustomerFinder getCustomerFinder() {
        return customerFinder;
    }

    @EnableHooks
    @SunriseRoute(AddressBookReverseRouter.ADD_ADDRESS_PAGE)
    public CompletionStage show(final String languageTag) {
        return requireCustomer(customer -> showFormPage(customer, formData));
    }

    @EnableHooks
    @SunriseRoute(AddressBookReverseRouter.ADD_ADDRESS_PROCESS)
    public CompletionStage process(final String languageTag) {
        return requireCustomer(this::processForm);
    }

    @Override
    public CompletionStage executeAction(final Customer customer, final AddressFormData formData) {
        return controllerAction.apply(customer, formData);
    }

    @Override
    public abstract CompletionStage handleSuccessfulAction(final Customer updatedCustomer, final AddressFormData formData);

    @Override
    public PageContent createPageContent(final Customer customer, final Form form) {
        return pageContentFactory.create(customer, form);
    }

    @Override
    public void preFillFormData(final Customer customer, final AddressFormData formData) {
        final Address address = Address.of(country)
                .withTitle(customer.getTitle())
                .withFirstName(customer.getFirstName())
                .withLastName(customer.getLastName())
                .withEmail(customer.getEmail());
        formData.applyAddress(address);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy