use of webpiecesxxxxxpackage.db.RoleEnum 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);
}
use of webpiecesxxxxxpackage.db.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(), "selectedRoles", null, "password", null);
}
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, "password", null);
}
Aggregations