@Execute public HtmlResponse delete(final EditForm form) { verifyCrudMode(form.crudMode, CrudMode.DETAILS); validate(form, messages -> {}, () -> asDetailsHtml()); verifyToken(() -> asDetailsHtml()); final String id = form.id; roleService .getRole(id) .ifPresent( entity -> { try { roleService.delete(entity); saveInfo(messages -> messages.addSuccessCrudDeleteCrudTable(GLOBAL)); } catch (final Exception e) { logger.error("Failed to delete " + entity, e); throwValidationError( messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id), () -> asDetailsHtml()); } }) .orElse( () -> { throwValidationError( messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id), () -> asDetailsHtml()); }); return redirect(getClass()); }
// ----------------------------------------------------- // Actually Crud // ------------- @Execute public HtmlResponse create(final CreateForm form) { verifyCrudMode(form.crudMode, CrudMode.CREATE); validate(form, messages -> {}, () -> asEditHtml()); verifyToken(() -> asEditHtml()); getRole(form) .ifPresent( entity -> { try { roleService.store(entity); saveInfo(messages -> messages.addSuccessCrudCreateCrudTable(GLOBAL)); } catch (final Exception e) { logger.error("Failed to add " + entity, e); throwValidationError( messages -> messages.addErrorsCrudFailedToCreateCrudTable( GLOBAL, buildThrowableMessage(e)), () -> asEditHtml()); } }) .orElse( () -> { throwValidationError( messages -> messages.addErrorsCrudFailedToCreateInstance(GLOBAL), () -> asEditHtml()); }); return redirect(getClass()); }
// ----------------------------------------------------- // Details // ------- @Execute public HtmlResponse details(final int crudMode, final String id) { verifyCrudMode(crudMode, CrudMode.DETAILS); saveToken(); return asHtml(path_AdminRole_AdminRoleDetailsJsp) .useForm( EditForm.class, op -> { op.setup( form -> { roleService .getRole(id) .ifPresent( entity -> { copyBeanToBean( entity, form, copyOp -> { copyOp.excludeNull(); }); form.crudMode = crudMode; }) .orElse( () -> { throwValidationError( messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id), () -> asListHtml()); }); }); }); }
private HtmlResponse asListHtml() { return asHtml(path_AdminRole_AdminRoleJsp) .renderWith( data -> { RenderDataUtil.register( data, "roleItems", roleService.getRoleList(rolePager)); // page navi }) .useForm( SearchForm.class, setup -> { setup.setup( form -> { copyBeanToBean(rolePager, form, op -> op.include("id")); }); }); }
// =================================================================================== // Assist Logic // ============ private OptionalEntity<Role> getEntity(final CreateForm form) { switch (form.crudMode) { case CrudMode.CREATE: return OptionalEntity.of(new Role()) .map( entity -> { entity.setId( Base64.getEncoder() .encodeToString(form.name.getBytes(Constants.CHARSET_UTF_8))); return entity; }); case CrudMode.EDIT: if (form instanceof EditForm) { return roleService.getRole(((EditForm) form).id); } break; default: break; } return OptionalEntity.empty(); }
protected void searchPaging(final RenderData data, final SearchForm form) { RenderDataUtil.register(data, "roleItems", roleService.getRoleList(rolePager)); // page navi // restore from pager copyBeanToBean(rolePager, form, op -> op.include("id")); }