refactoring for role
This commit is contained in:
parent
b99bb9cc4e
commit
4c06712c22
5 changed files with 205 additions and 115 deletions
|
@ -28,6 +28,7 @@ import org.codelibs.fess.es.user.cbean.RoleCB;
|
|||
import org.codelibs.fess.es.user.exbhv.RoleBhv;
|
||||
import org.codelibs.fess.es.user.exentity.Role;
|
||||
import org.dbflute.cbean.result.PagingResultBean;
|
||||
import org.dbflute.optional.OptionalEntity;
|
||||
|
||||
public class RoleService implements Serializable {
|
||||
|
||||
|
@ -52,17 +53,8 @@ public class RoleService implements Serializable {
|
|||
return roleList;
|
||||
}
|
||||
|
||||
public Role getRole(final Map<String, String> keys) {
|
||||
final Role role = roleBhv.selectEntity(cb -> {
|
||||
cb.query().docMeta().setId_Equal(keys.get("id"));
|
||||
setupEntityCondition(cb, keys);
|
||||
}).orElse(null);//TODO
|
||||
if (role == null) {
|
||||
// TODO exception?
|
||||
return null;
|
||||
}
|
||||
|
||||
return role;
|
||||
public OptionalEntity<Role> getRole(final String id) {
|
||||
return roleBhv.selectByPK(id);
|
||||
}
|
||||
|
||||
public void store(final Role role) {
|
||||
|
|
|
@ -16,8 +16,6 @@
|
|||
package org.codelibs.fess.app.web.admin.role;
|
||||
|
||||
import java.util.Base64;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
|
@ -28,6 +26,7 @@ import org.codelibs.fess.app.web.CrudMode;
|
|||
import org.codelibs.fess.app.web.base.FessAdminAction;
|
||||
import org.codelibs.fess.es.user.exentity.Role;
|
||||
import org.codelibs.fess.helper.SystemHelper;
|
||||
import org.dbflute.optional.OptionalEntity;
|
||||
import org.lastaflute.web.Execute;
|
||||
import org.lastaflute.web.callback.ActionRuntime;
|
||||
import org.lastaflute.web.response.HtmlResponse;
|
||||
|
@ -37,6 +36,7 @@ import org.lastaflute.web.validation.VaErrorHook;
|
|||
|
||||
/**
|
||||
* @author shinsuke
|
||||
* @author Keiichi Watanabe
|
||||
*/
|
||||
public class AdminRoleAction extends FessAdminAction {
|
||||
|
||||
|
@ -63,14 +63,14 @@ public class AdminRoleAction extends FessAdminAction {
|
|||
// Search Execute
|
||||
// ==============
|
||||
@Execute
|
||||
public HtmlResponse index(final RoleSearchForm form) {
|
||||
public HtmlResponse index(final SearchForm form) {
|
||||
return asHtml(path_AdminRole_IndexJsp).renderWith(data -> {
|
||||
searchPaging(data, form);
|
||||
});
|
||||
}
|
||||
|
||||
@Execute
|
||||
public HtmlResponse list(final Integer pageNumber, final RoleSearchForm form) {
|
||||
public HtmlResponse list(final Integer pageNumber, final SearchForm form) {
|
||||
rolePager.setCurrentPageNumber(pageNumber);
|
||||
return asHtml(path_AdminRole_IndexJsp).renderWith(data -> {
|
||||
searchPaging(data, form);
|
||||
|
@ -78,15 +78,15 @@ public class AdminRoleAction extends FessAdminAction {
|
|||
}
|
||||
|
||||
@Execute
|
||||
public HtmlResponse search(final RoleSearchForm form) {
|
||||
copyBeanToBean(form.searchParams, rolePager, op -> op.exclude(Constants.PAGER_CONVERSION_RULE));
|
||||
public HtmlResponse search(final SearchForm form) {
|
||||
copyBeanToBean(form, rolePager, op -> op.exclude(Constants.PAGER_CONVERSION_RULE));
|
||||
return asHtml(path_AdminRole_IndexJsp).renderWith(data -> {
|
||||
searchPaging(data, form);
|
||||
});
|
||||
}
|
||||
|
||||
@Execute
|
||||
public HtmlResponse reset(final RoleSearchForm form) {
|
||||
public HtmlResponse reset(final SearchForm form) {
|
||||
rolePager.clear();
|
||||
return asHtml(path_AdminRole_IndexJsp).renderWith(data -> {
|
||||
searchPaging(data, form);
|
||||
|
@ -94,17 +94,17 @@ public class AdminRoleAction extends FessAdminAction {
|
|||
}
|
||||
|
||||
@Execute
|
||||
public HtmlResponse back(final RoleSearchForm form) {
|
||||
public HtmlResponse back(final SearchForm form) {
|
||||
return asHtml(path_AdminRole_IndexJsp).renderWith(data -> {
|
||||
searchPaging(data, form);
|
||||
});
|
||||
}
|
||||
|
||||
protected void searchPaging(final RenderData data, final RoleSearchForm form) {
|
||||
protected void searchPaging(final RenderData data, final SearchForm form) {
|
||||
data.register("roleItems", roleService.getRoleList(rolePager)); // page navi
|
||||
|
||||
// restore from pager
|
||||
copyBeanToBean(rolePager, form.searchParams, op -> op.exclude(Constants.PAGER_CONVERSION_RULE));
|
||||
copyBeanToBean(rolePager, form, op -> op.include("id"));
|
||||
}
|
||||
|
||||
// ===================================================================================
|
||||
|
@ -114,46 +114,86 @@ public class AdminRoleAction extends FessAdminAction {
|
|||
// Entry Page
|
||||
// ----------
|
||||
@Execute(token = TxToken.SAVE)
|
||||
public HtmlResponse createpage(final RoleEditForm form) {
|
||||
form.initialize();
|
||||
form.crudMode = CrudMode.CREATE;
|
||||
public HtmlResponse createpage() {
|
||||
return asHtml(path_AdminRole_EditJsp).useForm(CreateForm.class, op -> {
|
||||
op.setup(form -> {
|
||||
form.initialize();
|
||||
form.crudMode = CrudMode.CREATE;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@Execute(token = TxToken.SAVE)
|
||||
public HtmlResponse editpage(final int crudMode, final String id) {
|
||||
verifyCrudMode(crudMode, CrudMode.EDIT);
|
||||
return asHtml(path_AdminRole_EditJsp).useForm(EditForm.class, op -> {
|
||||
op.setup(form -> {
|
||||
roleService.getRole(id).ifPresent(entity -> {
|
||||
copyBeanToBean(entity, form, copyOp -> {
|
||||
copyOp.excludeNull();
|
||||
});
|
||||
}).orElse(() -> {
|
||||
throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id), toEditHtml());
|
||||
});
|
||||
form.crudMode = crudMode;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@Execute(token = TxToken.SAVE)
|
||||
public HtmlResponse createagain(final CreateForm form) {
|
||||
verifyCrudMode(form.crudMode, CrudMode.CREATE);
|
||||
validate(form, messages -> {}, toEditHtml());
|
||||
return asHtml(path_AdminRole_EditJsp);
|
||||
}
|
||||
|
||||
@Execute(token = TxToken.SAVE)
|
||||
public HtmlResponse editpage(final int crudMode, final String id, final RoleEditForm form) {
|
||||
form.crudMode = crudMode;
|
||||
form.id = id;
|
||||
verifyCrudMode(form, CrudMode.EDIT);
|
||||
loadRole(form);
|
||||
public HtmlResponse editagain(final EditForm form) {
|
||||
verifyCrudMode(form.crudMode, CrudMode.EDIT);
|
||||
validate(form, messages -> {}, toEditHtml());
|
||||
return asHtml(path_AdminRole_EditJsp);
|
||||
}
|
||||
|
||||
@Execute(token = TxToken.SAVE)
|
||||
public HtmlResponse editagain(final RoleEditForm form) {
|
||||
return asHtml(path_AdminRole_EditJsp);
|
||||
}
|
||||
|
||||
@Execute(token = TxToken.SAVE)
|
||||
public HtmlResponse editfromconfirm(final RoleEditForm form) {
|
||||
public HtmlResponse editfromconfirm(final EditForm form) {
|
||||
validate(form, messages -> {}, toEditHtml());
|
||||
form.crudMode = CrudMode.EDIT;
|
||||
loadRole(form);
|
||||
final String id = form.id;
|
||||
roleService.getRole(id).ifPresent(entity -> {
|
||||
copyBeanToBean(entity, form, op -> {});
|
||||
}).orElse(() -> {
|
||||
throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id), toEditHtml());
|
||||
});
|
||||
return asHtml(path_AdminRole_EditJsp);
|
||||
}
|
||||
|
||||
@Execute(token = TxToken.SAVE)
|
||||
public HtmlResponse deletepage(final int crudMode, final String id, final RoleEditForm form) {
|
||||
form.crudMode = crudMode;
|
||||
form.id = id;
|
||||
verifyCrudMode(form, CrudMode.DELETE);
|
||||
loadRole(form);
|
||||
return asHtml(path_AdminRole_ConfirmJsp);
|
||||
public HtmlResponse deletepage(final int crudMode, final String id) {
|
||||
verifyCrudMode(crudMode, CrudMode.DELETE);
|
||||
return asHtml(path_AdminRole_ConfirmJsp).useForm(EditForm.class, op -> {
|
||||
op.setup(form -> {
|
||||
roleService.getRole(id).ifPresent(entity -> {
|
||||
copyBeanToBean(entity, form, copyOp -> {
|
||||
copyOp.excludeNull();
|
||||
});
|
||||
}).orElse(() -> {
|
||||
throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id), toEditHtml());
|
||||
});
|
||||
form.crudMode = crudMode;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@Execute(token = TxToken.SAVE)
|
||||
public HtmlResponse deletefromconfirm(final RoleEditForm form) {
|
||||
public HtmlResponse deletefromconfirm(final EditForm form) {
|
||||
validate(form, messages -> {}, toEditHtml());
|
||||
form.crudMode = CrudMode.DELETE;
|
||||
loadRole(form);
|
||||
final String id = form.id;
|
||||
roleService.getRole(id).ifPresent(entity -> {
|
||||
copyBeanToBean(entity, form, op -> {});
|
||||
}).orElse(() -> {
|
||||
throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id), toEditHtml());
|
||||
});
|
||||
return asHtml(path_AdminRole_ConfirmJsp);
|
||||
}
|
||||
|
||||
|
@ -161,23 +201,33 @@ public class AdminRoleAction extends FessAdminAction {
|
|||
// Confirm
|
||||
// -------
|
||||
@Execute
|
||||
public HtmlResponse confirmpage(final int crudMode, final String id, final RoleEditForm form) {
|
||||
form.crudMode = crudMode;
|
||||
form.id = id;
|
||||
verifyCrudMode(form, CrudMode.CONFIRM);
|
||||
loadRole(form);
|
||||
public HtmlResponse confirmpage(final int crudMode, final String id) {
|
||||
verifyCrudMode(crudMode, CrudMode.CONFIRM);
|
||||
return asHtml(path_AdminRole_ConfirmJsp).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), toEditHtml());
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@Execute(token = TxToken.VALIDATE_KEEP)
|
||||
public HtmlResponse confirmfromcreate(final CreateForm form) {
|
||||
validate(form, messages -> {}, toEditHtml());
|
||||
form.crudMode = CrudMode.CREATE;
|
||||
return asHtml(path_AdminRole_ConfirmJsp);
|
||||
}
|
||||
|
||||
@Execute(token = TxToken.VALIDATE_KEEP)
|
||||
public HtmlResponse confirmfromcreate(final RoleEditForm form) {
|
||||
validate(form, messages -> {}, toEditHtml());
|
||||
return asHtml(path_AdminRole_ConfirmJsp);
|
||||
}
|
||||
|
||||
@Execute(token = TxToken.VALIDATE_KEEP)
|
||||
public HtmlResponse confirmfromupdate(final RoleEditForm form) {
|
||||
public HtmlResponse confirmfromupdate(final EditForm form) {
|
||||
validate(form, messages -> {}, toEditHtml());
|
||||
form.crudMode = CrudMode.EDIT;
|
||||
return asHtml(path_AdminRole_ConfirmJsp);
|
||||
}
|
||||
|
||||
|
@ -185,69 +235,80 @@ public class AdminRoleAction extends FessAdminAction {
|
|||
// Actually Crud
|
||||
// -------------
|
||||
@Execute(token = TxToken.VALIDATE)
|
||||
public HtmlResponse create(final RoleEditForm form) {
|
||||
public HtmlResponse create(final CreateForm form) {
|
||||
verifyCrudMode(form.crudMode, CrudMode.CREATE);
|
||||
validate(form, messages -> {}, toEditHtml());
|
||||
roleService.store(createRole(form));
|
||||
saveInfo(messages -> messages.addSuccessCrudCreateCrudTable(GLOBAL));
|
||||
createRole(form).ifPresent(entity -> {
|
||||
copyBeanToBean(form, entity, op -> op.exclude(Constants.COMMON_CONVERSION_RULE));
|
||||
entity.setId(Base64.getEncoder().encodeToString(entity.getName().getBytes(Constants.CHARSET_UTF_8)));
|
||||
roleService.store(entity);
|
||||
saveInfo(messages -> messages.addSuccessCrudCreateCrudTable(GLOBAL));
|
||||
}).orElse(() -> {
|
||||
throwValidationError(messages -> messages.addErrorsCrudFailedToCreateCrudTable(GLOBAL), toEditHtml());
|
||||
});
|
||||
return redirect(getClass());
|
||||
}
|
||||
|
||||
@Execute(token = TxToken.VALIDATE)
|
||||
public HtmlResponse update(final RoleEditForm form) {
|
||||
public HtmlResponse update(final EditForm form) {
|
||||
verifyCrudMode(form.crudMode, CrudMode.EDIT);
|
||||
validate(form, messages -> {}, toEditHtml());
|
||||
roleService.store(createRole(form));
|
||||
saveInfo(messages -> messages.addSuccessCrudUpdateCrudTable(GLOBAL));
|
||||
createRole(form).ifPresent(entity -> {
|
||||
copyBeanToBean(form, entity, op -> op.exclude(Constants.COMMON_CONVERSION_RULE));
|
||||
entity.setId(Base64.getEncoder().encodeToString(entity.getName().getBytes(Constants.CHARSET_UTF_8)));
|
||||
roleService.store(entity);
|
||||
saveInfo(messages -> messages.addSuccessCrudUpdateCrudTable(GLOBAL));
|
||||
}).orElse(() -> {
|
||||
throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, form.id), toEditHtml());
|
||||
});
|
||||
return redirect(getClass());
|
||||
}
|
||||
|
||||
@Execute
|
||||
public HtmlResponse delete(final RoleEditForm form) {
|
||||
verifyCrudMode(form, CrudMode.DELETE);
|
||||
roleService.delete(getRole(form));
|
||||
saveInfo(messages -> messages.addSuccessCrudDeleteCrudTable(GLOBAL));
|
||||
public HtmlResponse delete(final EditForm form) {
|
||||
verifyCrudMode(form.crudMode, CrudMode.DELETE);
|
||||
validate(form, messages -> {}, toEditHtml());
|
||||
final String id = form.id;
|
||||
roleService.getRole(id).ifPresent(entity -> {
|
||||
roleService.delete(entity);
|
||||
saveInfo(messages -> messages.addSuccessCrudDeleteCrudTable(GLOBAL));
|
||||
}).orElse(() -> {
|
||||
throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id), toEditHtml());
|
||||
});
|
||||
return redirect(getClass());
|
||||
}
|
||||
|
||||
// ===================================================================================
|
||||
// Assist Logic
|
||||
// ============
|
||||
protected void loadRole(final RoleEditForm form) {
|
||||
copyBeanToBean(getRole(form), form, op -> op.exclude("crudMode"));
|
||||
}
|
||||
|
||||
protected Role getRole(final RoleEditForm form) {
|
||||
final Role role = roleService.getRole(createKeyMap(form));
|
||||
if (role == null) {
|
||||
throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, form.id), toEditHtml());
|
||||
protected OptionalEntity<Role> createRole(final CreateForm form) {
|
||||
switch (form.crudMode) {
|
||||
case CrudMode.CREATE:
|
||||
if (form instanceof CreateForm) {
|
||||
final Role entity = new Role();
|
||||
return OptionalEntity.of(entity);
|
||||
}
|
||||
break;
|
||||
case CrudMode.EDIT:
|
||||
if (form instanceof EditForm) {
|
||||
return roleService.getRole(((EditForm) form).id).map(entity -> {
|
||||
return entity;
|
||||
});
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return role;
|
||||
}
|
||||
|
||||
protected Role createRole(final RoleEditForm form) {
|
||||
Role role;
|
||||
if (form.crudMode == CrudMode.EDIT) {
|
||||
role = getRole(form);
|
||||
} else {
|
||||
role = new Role();
|
||||
}
|
||||
copyBeanToBean(form, role, op -> op.exclude(Constants.COMMON_CONVERSION_RULE));
|
||||
role.setId(Base64.getEncoder().encodeToString(role.getName().getBytes(Constants.CHARSET_UTF_8)));
|
||||
return role;
|
||||
}
|
||||
|
||||
protected Map<String, String> createKeyMap(final RoleEditForm form) {
|
||||
final Map<String, String> keys = new HashMap<String, String>();
|
||||
keys.put("id", form.id);
|
||||
return keys;
|
||||
return OptionalEntity.empty();
|
||||
}
|
||||
|
||||
// ===================================================================================
|
||||
// Small Helper
|
||||
// ============
|
||||
protected void verifyCrudMode(final RoleEditForm form, final int expectedMode) {
|
||||
if (form.crudMode != expectedMode) {
|
||||
protected void verifyCrudMode(final int crudMode, final int expectedMode) {
|
||||
if (crudMode != expectedMode) {
|
||||
throwValidationError(messages -> {
|
||||
messages.addErrorsCrudInvalidMode(GLOBAL, String.valueOf(expectedMode), String.valueOf(form.crudMode));
|
||||
messages.addErrorsCrudInvalidMode(GLOBAL, String.valueOf(expectedMode), String.valueOf(crudMode));
|
||||
}, toEditHtml());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,31 +17,26 @@ package org.codelibs.fess.app.web.admin.role;
|
|||
|
||||
import java.io.Serializable;
|
||||
|
||||
import javax.validation.constraints.Digits;
|
||||
import javax.validation.constraints.Size;
|
||||
|
||||
import org.lastaflute.web.validation.Required;
|
||||
|
||||
/**
|
||||
* @author shinsuke
|
||||
* @author Keiichi Watanabe
|
||||
*/
|
||||
public class RoleEditForm implements Serializable {
|
||||
public class CreateForm implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
//@Digits(integer=10, fraction=0)
|
||||
@Digits(integer = 10, fraction = 0)
|
||||
public int crudMode;
|
||||
|
||||
//@Required(target = "confirmfromupdate,update,delete")
|
||||
//@Maxbytelength(maxbytelength = 1000)
|
||||
public String id;
|
||||
|
||||
//@Required(target = "confirmfromcreate,create,confirmfromupdate,update,delete")
|
||||
//@Maxbytelength(maxbytelength = 100)
|
||||
@Required
|
||||
@Size(max = 100)
|
||||
public String name;
|
||||
|
||||
//@Required(target = "confirmfromupdate,update,delete")
|
||||
//@Digits(integer=10, fraction=0)
|
||||
public String versionNo;
|
||||
|
||||
public void initialize() {
|
||||
id = null;
|
||||
name = null;
|
||||
versionNo = null;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
/*
|
||||
* Copyright 2012-2015 CodeLibs Project and the Others.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||
* either express or implied. See the License for the specific language
|
||||
* governing permissions and limitations under the License.
|
||||
*/
|
||||
package org.codelibs.fess.app.web.admin.role;
|
||||
|
||||
import javax.validation.constraints.Digits;
|
||||
import javax.validation.constraints.Size;
|
||||
|
||||
import org.lastaflute.web.validation.Required;
|
||||
|
||||
/**
|
||||
* @author Keiichi Watanabe
|
||||
*/
|
||||
public class EditForm extends CreateForm {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Required
|
||||
@Size(max = 1000)
|
||||
public String id;
|
||||
|
||||
@Size(max = 1000)
|
||||
public String updatedBy;
|
||||
|
||||
@Digits(integer = 19, fraction = 0)
|
||||
public Long updatedTime;
|
||||
|
||||
@Required
|
||||
@Digits(integer = 10, fraction = 0)
|
||||
public Integer versionNo;
|
||||
|
||||
}
|
|
@ -16,15 +16,13 @@
|
|||
package org.codelibs.fess.app.web.admin.role;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author shinsuke
|
||||
*/
|
||||
public class RoleSearchForm implements Serializable {
|
||||
public class SearchForm implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public Map<String, String> searchParams = new HashMap<String, String>();
|
||||
public String id;
|
||||
}
|
Loading…
Add table
Reference in a new issue