use of WEBPIECESxPACKAGE.base.libs.UserRole in project webpieces by deanhiller.
the class AjaxCrudUserController method postDeleteUser.
public Redirect postDeleteUser(int id) {
UserDbo ref = Em.get().find(UserDbo.class, id);
List<UserRole> roles = ref.getRoles();
for (UserRole r : roles) {
Em.get().remove(r);
}
Em.get().remove(ref);
Em.get().flush();
Current.flash().setMessage("User deleted");
Current.flash().keep();
return Actions.redirect(AjaxCrudUserRouteId.AJAX_LIST_USERS);
}
use of WEBPIECESxPACKAGE.base.libs.UserRole in project webpieces by deanhiller.
the class PopulateDatabase method createSomeData.
private void createSomeData() {
EntityManager mgr = factory.createEntityManager();
List<UserDbo> users = UserDbo.findAll(mgr);
if (users.size() > 0)
//This database has users, exit immediately to not screw up existing data
return;
EntityTransaction tx = mgr.getTransaction();
tx.begin();
UserDbo user1 = new UserDbo();
user1.setEmail("dean@somewhere.com");
user1.setName("SomeName");
user1.setFirstName("Dean");
user1.setLastName("Hill");
UserDbo user2 = new UserDbo();
user2.setEmail("bob@somewhere.com");
user2.setName("Bob'sName");
user2.setFirstName("Bob");
user2.setLastName("LastBob");
user2.setLevelOfEducation(EducationEnum.MIDDLE_SCHOOL);
UserRole role1 = new UserRole(user2, RoleEnum.DELINQUINT);
UserRole role2 = new UserRole(user2, RoleEnum.BADASS);
mgr.persist(user1);
mgr.persist(user2);
mgr.persist(role1);
mgr.persist(role2);
mgr.flush();
tx.commit();
}
use of WEBPIECESxPACKAGE.base.libs.UserRole 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);
}
use of WEBPIECESxPACKAGE.base.libs.UserRole 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);
}
use of WEBPIECESxPACKAGE.base.libs.UserRole in project webpieces by deanhiller.
the class CrudUserController method postDeleteUser.
public Redirect postDeleteUser(int id) {
UserDbo ref = Em.get().find(UserDbo.class, id);
List<UserRole> roles = ref.getRoles();
for (UserRole r : roles) {
Em.get().remove(r);
}
Em.get().remove(ref);
Em.get().flush();
Current.flash().setMessage("User deleted");
Current.flash().keep();
return Actions.redirect(CrudUserRouteId.LIST_USERS);
}
Aggregations