Search in sources :

Example 1 with FlashAndRedirect

use of org.webpieces.router.api.controller.actions.FlashAndRedirect in project webpieces by deanhiller.

the class CrudUserController method postSaveUser.

public Redirect postSaveUser(@UseQuery("findByIdWithRoleJoin") UserDbo entity, List<RoleEnum> selectedRoles, @NotBlank @Size(min = 4, max = 20) String password) {
    // the form with what the user typed in along with errors
    if (Current.validation().hasErrors()) {
        log.info("page has errors");
        FlashAndRedirect redirect = new FlashAndRedirect(Current.getContext(), "Errors in form below");
        // make sure secure fields are not put in flash cookie!!!
        redirect.setSecureFields("entity.password");
        redirect.setIdFieldAndValue("id", entity.getId());
        return Actions.redirectFlashAll(GET_ADD_USER_FORM, GET_EDIT_USER_FORM, redirect);
    }
    Current.flash().setMessage("User successfully saved");
    Current.flash().keep(true);
    Current.validation().keep(false);
    List<UserRole> roles = entity.getRoles();
    for (UserRole r : roles) {
        Em.get().remove(r);
    }
    roles.clear();
    for (RoleEnum r : selectedRoles) {
        UserRole role = new UserRole(entity, r);
        Em.get().persist(role);
    }
    // WTF...this now can update an entity that did not exist before...fun times.
    // Docs say "@throws EntityExistsException if the entity already exists." but that's NOT true.
    // we use this for INSERT and UPDATE and it works great!!
    Em.get().persist(entity);
    Em.get().flush();
    return Actions.redirect(CrudUserRouteId.LIST_USERS);
}
Also used : FlashAndRedirect(org.webpieces.router.api.controller.actions.FlashAndRedirect) UserRole(webpiecesxxxxxpackage.db.UserRole) RoleEnum(webpiecesxxxxxpackage.db.RoleEnum)

Example 2 with FlashAndRedirect

use of org.webpieces.router.api.controller.actions.FlashAndRedirect in project webpieces by deanhiller.

the class CrudTestController method postSaveUserForMultiSelect.

public Redirect postSaveUserForMultiSelect(UserTestDbo entity, List<Role> selectedRoles, String password) {
    if (password == null) {
        Current.validation().addError("password", "password is required");
    } else if (password.length() < 4) {
        Current.validation().addError("password", "Value is too short");
    }
    if (Current.validation().hasErrors()) {
        log.info("page has errors");
        FlashAndRedirect redirect = new FlashAndRedirect(Current.getContext(), "Errors in form below");
        // make sure secure fields are not put in flash cookie!!!
        redirect.setSecureFields("entity.password");
        redirect.setIdFieldAndValue("id", entity.getId());
        return Actions.redirectFlashAll(ADD_USER_PAGE, HibernateRouteId.MULTISELECT, redirect);
    }
    Current.flash().setMessage("User successfully saved");
    Em.get().merge(entity);
    Em.get().flush();
    return Actions.redirect(HibernateRouteId.LIST_USERS);
}
Also used : FlashAndRedirect(org.webpieces.router.api.controller.actions.FlashAndRedirect)

Example 3 with FlashAndRedirect

use of org.webpieces.router.api.controller.actions.FlashAndRedirect in project webpieces by deanhiller.

the class CrudTestController method postSaveUser.

public Redirect postSaveUser(UserTestDbo entity, String password) {
    if (password == null) {
        Current.validation().addError("password", "password is required");
    } else if (password.length() < 4) {
        Current.validation().addError("password", "Value is too short");
    }
    if (Current.validation().hasErrors()) {
        log.info("page has errors");
        FlashAndRedirect redirect = new FlashAndRedirect(Current.getContext(), "Errors in form below");
        // make sure secure fields are not put in flash cookie!!!
        redirect.setSecureFields("password");
        redirect.setIdFieldAndValue("id", entity.getId());
        return Actions.redirectFlashAll(ADD_USER_PAGE, EDIT_USER_PAGE, redirect);
    }
    Current.flash().setMessage("User successfully saved");
    Em.get().merge(entity);
    Em.get().flush();
    return Actions.redirect(HibernateRouteId.LIST_USERS);
}
Also used : FlashAndRedirect(org.webpieces.router.api.controller.actions.FlashAndRedirect)

Example 4 with FlashAndRedirect

use of org.webpieces.router.api.controller.actions.FlashAndRedirect in project webpieces by deanhiller.

the class ScopesController method postSaveUser.

// very typical post for adding user to database
public Redirect postSaveUser(UserDto user) {
    if (user.getFirstName().length() < 3) {
        Current.validation().addError("user.firstName", "First name must be more than 2 characters");
    }
    // the form with what the user typed in along with errors
    if (Current.validation().hasErrors()) {
        Current.flash().setMessage("Errors in form below");
        FlashAndRedirect redirect = new FlashAndRedirect(Current.getContext(), "Errors in form below");
        redirect.setIdFieldAndValue("id", user.getId());
        return Actions.redirectFlashAll(ScopesRouteId.ADD_USER, ScopesRouteId.EDIT_USER, redirect);
    }
    Current.flash().setMessage("User successfully saved");
    Current.flash().keep();
    return Actions.redirect(ScopesRouteId.LIST_USER);
}
Also used : FlashAndRedirect(org.webpieces.router.api.controller.actions.FlashAndRedirect)

Aggregations

FlashAndRedirect (org.webpieces.router.api.controller.actions.FlashAndRedirect)4 RoleEnum (webpiecesxxxxxpackage.db.RoleEnum)1 UserRole (webpiecesxxxxxpackage.db.UserRole)1