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

com.commercetools.sunrise.framework.controllers.WithFormFlow Maven / Gradle / Ivy

The newest version!
package com.commercetools.sunrise.framework.controllers;

import io.sphere.sdk.client.ClientErrorException;
import org.slf4j.Logger;
import play.data.Form;
import play.libs.concurrent.HttpExecution;
import play.mvc.Result;

import java.util.concurrent.CompletionStage;

import static io.sphere.sdk.utils.CompletableFutureUtils.exceptionallyCompletedFuture;
import static io.sphere.sdk.utils.CompletableFutureUtils.recoverWith;
import static java.util.concurrent.CompletableFuture.completedFuture;

/**
 * Approach to handle form data (Template Method Pattern).
 * @param  type of the input data, possibly a parameter object
 * @param  type of the output object, normally the updated object if the form is valid
 * @param  stereotype of the in a form wrapped class
 */
public interface WithFormFlow extends WithForm {

    Logger getLogger();

    default CompletionStage processForm(final I input) {
        return validateForm(input, bindForm()).thenComposeAsync(form -> {
            if (!form.hasErrors()) {
                return handleValidForm(input, form);
            } else {
                return handleInvalidForm(input, form);
            }
        }, HttpExecution.defaultContext());
    }

    default CompletionStage> validateForm(final I input, final Form form) {
        return completedFuture(form);
    }

    CompletionStage handleInvalidForm(final I input, final Form form);

    default CompletionStage handleValidForm(final I input, final Form form) {
        final CompletionStage resultStage = executeAction(input, form.get())
                .thenComposeAsync(output -> handleSuccessfulAction(output, form.get()), HttpExecution.defaultContext());
        return recoverWith(resultStage, throwable -> handleFailedAction(input, form, throwable), HttpExecution.defaultContext());
    }

    CompletionStage executeAction(final I input, final F formData);

    default CompletionStage handleFailedAction(final I input, final Form form, final Throwable throwable) {
        final Throwable causeThrowable = throwable.getCause();
        if (causeThrowable instanceof ClientErrorException) {
            return handleClientErrorFailedAction(input, form, (ClientErrorException) causeThrowable);
        }
        return handleGeneralFailedAction(throwable);
    }

    CompletionStage handleClientErrorFailedAction(final I input, final Form form, final ClientErrorException clientErrorException);

    default CompletionStage handleGeneralFailedAction(final Throwable throwable) {
        return exceptionallyCompletedFuture(throwable);
    }

    CompletionStage handleSuccessfulAction(final O output, final F formData);

    default void saveFormError(final Form form, final String message) {
        form.reject(message);
    }

    default void saveUnexpectedFormError(final Form form, final Throwable throwable) {
        form.reject("Something went wrong, please try again"); // TODO i18n
        getLogger().error("The CTP request raised an unexpected exception", throwable);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy