Search in sources :

Example 1 with RoleEnum

use of WEBPIECESxPACKAGE.base.libs.RoleEnum in project webpieces by deanhiller.

the class CrudUserController method postSaveUser.

public Redirect postSaveUser(@UseQuery("findByIdWithRoleJoin") UserDbo entity, List<RoleEnum> selectedRoles, String password) {
    //need to figure out how to do i18n for the messages in that case
    if (password == null) {
        Current.validation().addError("password", "password is required");
    } else if (password.length() < 4) {
        Current.validation().addError("password", "Value is too short");
    }
    if (entity.getFirstName() == null) {
        Current.validation().addError("entity.firstName", "First name is required");
    } else if (entity.getFirstName().length() < 3) {
        Current.validation().addError("entity.firstName", "First name must be more than 2 characters");
    }
    //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();
    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);
    }
    Em.get().merge(entity);
    Em.get().flush();
    return Actions.redirect(CrudUserRouteId.LIST_USERS);
}
Also used : FlashAndRedirect(org.webpieces.router.api.actions.FlashAndRedirect) UserRole(WEBPIECESxPACKAGE.base.libs.UserRole) RoleEnum(WEBPIECESxPACKAGE.base.libs.RoleEnum)

Example 2 with RoleEnum

use of WEBPIECESxPACKAGE.base.libs.RoleEnum in project webpieces by deanhiller.

the class CrudUserController method userAddEdit.

public Action userAddEdit(Integer id) {
    if (id == null) {
        return Actions.renderThis("entity", new UserDbo(), "levels", EducationEnum.values(), "roles", RoleEnum.values());
    }
    UserDbo user = UserDbo.findWithJoin(Em.get(), id);
    List<UserRole> roles = user.getRoles();
    List<RoleEnum> selectedRoles = roles.stream().map(r -> r.getRole()).collect(Collectors.toList());
    return Actions.renderThis("entity", user, "levels", EducationEnum.values(), "roles", RoleEnum.values(), "selectedRoles", selectedRoles);
}
Also used : Logger(org.webpieces.util.logging.Logger) FlashAndRedirect(org.webpieces.router.api.actions.FlashAndRedirect) Redirect(org.webpieces.router.api.actions.Redirect) Render(org.webpieces.router.api.actions.Render) EntityManager(javax.persistence.EntityManager) Singleton(javax.inject.Singleton) RoleEnum(WEBPIECESxPACKAGE.base.libs.RoleEnum) GET_EDIT_USER_FORM(WEBPIECESxPACKAGE.base.crud.CrudUserRouteId.GET_EDIT_USER_FORM) Collectors(java.util.stream.Collectors) Action(org.webpieces.router.api.actions.Action) List(java.util.List) Query(javax.persistence.Query) Em(org.webpieces.plugins.hibernate.Em) GET_ADD_USER_FORM(WEBPIECESxPACKAGE.base.crud.CrudUserRouteId.GET_ADD_USER_FORM) UseQuery(org.webpieces.plugins.hibernate.UseQuery) UserDbo(WEBPIECESxPACKAGE.base.libs.UserDbo) LoggerFactory(org.webpieces.util.logging.LoggerFactory) Current(org.webpieces.ctx.api.Current) Actions(org.webpieces.router.api.actions.Actions) EducationEnum(WEBPIECESxPACKAGE.base.libs.EducationEnum) UserRole(WEBPIECESxPACKAGE.base.libs.UserRole) UserDbo(WEBPIECESxPACKAGE.base.libs.UserDbo) UserRole(WEBPIECESxPACKAGE.base.libs.UserRole) RoleEnum(WEBPIECESxPACKAGE.base.libs.RoleEnum)

Aggregations

RoleEnum (WEBPIECESxPACKAGE.base.libs.RoleEnum)2 UserRole (WEBPIECESxPACKAGE.base.libs.UserRole)2 FlashAndRedirect (org.webpieces.router.api.actions.FlashAndRedirect)2 GET_ADD_USER_FORM (WEBPIECESxPACKAGE.base.crud.CrudUserRouteId.GET_ADD_USER_FORM)1 GET_EDIT_USER_FORM (WEBPIECESxPACKAGE.base.crud.CrudUserRouteId.GET_EDIT_USER_FORM)1 EducationEnum (WEBPIECESxPACKAGE.base.libs.EducationEnum)1 UserDbo (WEBPIECESxPACKAGE.base.libs.UserDbo)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 Singleton (javax.inject.Singleton)1 EntityManager (javax.persistence.EntityManager)1 Query (javax.persistence.Query)1 Current (org.webpieces.ctx.api.Current)1 Em (org.webpieces.plugins.hibernate.Em)1 UseQuery (org.webpieces.plugins.hibernate.UseQuery)1 Action (org.webpieces.router.api.actions.Action)1 Actions (org.webpieces.router.api.actions.Actions)1 Redirect (org.webpieces.router.api.actions.Redirect)1 Render (org.webpieces.router.api.actions.Render)1 Logger (org.webpieces.util.logging.Logger)1