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

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

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

import com.commercetools.sunrise.framework.viewmodels.content.PageContent;
import io.sphere.sdk.client.ClientErrorException;
import org.apache.commons.beanutils.BeanUtils;
import play.data.Form;
import play.mvc.Result;

import java.util.Map;
import java.util.concurrent.CompletionStage;

/**
 * Approach to handle form data (Template Method Pattern).
 * @param  type of the input data of the form, 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 WithContentFormFlow extends WithFormFlow, WithContent {

    default CompletionStage showFormPage(final I input, final F emptyFormData) {
        final Form form = createFilledForm(input, emptyFormData);
        return okResultWithPageContent(createPageContent(input, form));
    }

    default CompletionStage showFormPageWithErrors(final I input, final Form form) {
        return badRequestResultWithPageContent(createPageContent(input, form));
    }

    @Override
    default CompletionStage handleInvalidForm(final I input, final Form form) {
        return showFormPageWithErrors(input, form);
    }

    @Override
    default CompletionStage handleClientErrorFailedAction(final I input, final Form form, final ClientErrorException clientErrorException) {
        saveUnexpectedFormError(form, clientErrorException);
        return showFormPageWithErrors(input, form);
    }

    PageContent createPageContent(final I input, final Form form);

    default Form createFilledForm(final I input, final F formData) {
        preFillFormData(input, formData);
        try {
            final Map classFieldValues = BeanUtils.describe(formData);
            final Form filledForm = createForm().bind(classFieldValues);
            filledForm.discardErrors();
            return filledForm;
        } catch (ReflectiveOperationException e) {
            throw new RuntimeException("Form cannot be populated for class " + getFormDataClass().getCanonicalName(), e);
        }
    }

    void preFillFormData(final I input, final F formData);
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy