This commit is contained in:
yfujita 2017-03-28 17:29:37 +09:00
parent 56d7181c7b
commit cede5b410d
5 changed files with 190 additions and 3 deletions

View file

@ -25,6 +25,7 @@ import org.codelibs.fess.app.service.RoleService;
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.util.ComponentUtil;
import org.codelibs.fess.util.RenderDataUtil;
import org.dbflute.optional.OptionalEntity;
import org.dbflute.optional.OptionalThing;
@ -188,7 +189,7 @@ public class AdminRoleAction extends FessAdminAction {
// ===================================================================================
// Assist Logic
// ============
private OptionalEntity<Role> getEntity(final CreateForm form) {
private static OptionalEntity<Role> getEntity(final CreateForm form) {
switch (form.crudMode) {
case CrudMode.CREATE:
return OptionalEntity.of(new Role()).map(entity -> {
@ -197,7 +198,7 @@ public class AdminRoleAction extends FessAdminAction {
});
case CrudMode.EDIT:
if (form instanceof EditForm) {
return roleService.getRole(((EditForm) form).id);
return ComponentUtil.getComponent(RoleService.class).getRole(((EditForm) form).id);
}
break;
default:
@ -206,7 +207,7 @@ public class AdminRoleAction extends FessAdminAction {
return OptionalEntity.empty();
}
protected OptionalEntity<Role> getRole(final CreateForm form) {
public static OptionalEntity<Role> getRole(final CreateForm form) {
return getEntity(form).map(entity -> {
copyBeanToBean(form, entity, op -> op.exclude(Constants.COMMON_CONVERSION_RULE));
return entity;

View file

@ -0,0 +1,123 @@
/*
* Copyright 2012-2017 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.api.admin.role;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.codelibs.fess.Constants;
import org.codelibs.fess.app.pager.RolePager;
import org.codelibs.fess.app.service.RoleService;
import org.codelibs.fess.app.web.CrudMode;
import org.codelibs.fess.app.web.api.ApiResult;
import org.codelibs.fess.app.web.api.admin.FessApiAdminAction;
import org.codelibs.fess.es.user.exentity.Role;
import org.lastaflute.web.Execute;
import org.lastaflute.web.response.JsonResponse;
import static org.codelibs.fess.app.web.admin.role.AdminRoleAction.*;
public class ApiAdminRoleAction extends FessApiAdminAction {
@Resource
private RoleService roleService;
// GET /api/admin/role
// POST /api/admin/role
@Execute
public JsonResponse<ApiResult> settings(final SearchBody body) {
validateApi(body, messages -> {});
final RolePager pager = new RolePager();
copyBeanToBean(body, pager, op -> op.exclude(Constants.PAGER_CONVERSION_RULE));
final List<Role> list = roleService.getRoleList(pager);
return asJson(new ApiResult.ApiConfigsResponse<EditBody>()
.settings(list.stream().map(entity -> createEditBody(entity)).collect(Collectors.toList()))
.total(pager.getAllRecordCount()).status(ApiResult.Status.OK).result());
}
// GET /api/admin/role/setting/{id}
@Execute
public JsonResponse<ApiResult> get$setting(final String id) {
return asJson(new ApiResult.ApiConfigResponse()
.setting(roleService.getRole(id).map(entity -> createEditBody(entity)).orElseGet(() -> {
throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id));
return null;
})).status(ApiResult.Status.OK).result());
}
// PUT /api/admin/role/setting
@Execute
public JsonResponse<ApiResult> put$setting(final CreateBody body) {
validateApi(body, messages -> {});
body.crudMode = CrudMode.CREATE;
final Role entity = getRole(body).orElseGet(() -> {
throwValidationErrorApi(messages -> {
messages.addErrorsCrudFailedToCreateInstance(GLOBAL);
});
return null;
});
try {
roleService.store(entity);
} catch (final Exception e) {
throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToCreateCrudTable(GLOBAL, buildThrowableMessage(e)));
}
return asJson(new ApiResult.ApiUpdateResponse().id(entity.getId()).created(true).status(ApiResult.Status.OK).result());
}
// POST /api/admin/role/setting
@Execute
public JsonResponse<ApiResult> post$setting(final EditBody body) {
validateApi(body, messages -> {});
body.crudMode = CrudMode.EDIT;
final Role entity = getRole(body).orElseGet(() -> {
throwValidationErrorApi(messages -> {
messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, body.id);
});
return null;
});
try {
roleService.store(entity);
} catch (final Exception e) {
throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToUpdateCrudTable(GLOBAL, buildThrowableMessage(e)));
}
return asJson(new ApiResult.ApiUpdateResponse().id(entity.getId()).created(false).status(ApiResult.Status.OK).result());
}
// DELETE /api/admin/role/setting/{id}
@Execute
public JsonResponse<ApiResult> delete$setting(final String id) {
final Role entity = roleService.getRole(id).orElseGet(() -> {
throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id));
return null;
});
try {
roleService.delete(entity);
} catch (final Exception e) {
throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToDeleteCrudTable(GLOBAL, buildThrowableMessage(e)));
}
return asJson(new ApiResult.ApiUpdateResponse().id(id).created(false).status(ApiResult.Status.OK).result());
}
protected EditBody createEditBody(final Role entity) {
final EditBody body = new EditBody();
copyBeanToBean(entity, body, copyOp -> {
copyOp.excludeNull();
});
return body;
}
}

View file

@ -0,0 +1,21 @@
/*
* Copyright 2012-2017 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.api.admin.role;
import org.codelibs.fess.app.web.admin.role.CreateForm;
public class CreateBody extends CreateForm {
}

View file

@ -0,0 +1,21 @@
/*
* Copyright 2012-2017 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.api.admin.role;
import org.codelibs.fess.app.web.admin.role.EditForm;
public class EditBody extends EditForm {
}

View file

@ -0,0 +1,21 @@
/*
* Copyright 2012-2017 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.api.admin.role;
import org.codelibs.fess.app.web.admin.role.SearchForm;
public class SearchBody extends SearchForm {
}