diff --git a/src/main/java/org/codelibs/fess/app/web/admin/group/AdminGroupAction.java b/src/main/java/org/codelibs/fess/app/web/admin/group/AdminGroupAction.java index d46e75d05..a6d613e82 100644 --- a/src/main/java/org/codelibs/fess/app/web/admin/group/AdminGroupAction.java +++ b/src/main/java/org/codelibs/fess/app/web/admin/group/AdminGroupAction.java @@ -25,6 +25,7 @@ import org.codelibs.fess.app.service.GroupService; import org.codelibs.fess.app.web.CrudMode; import org.codelibs.fess.app.web.base.FessAdminAction; import org.codelibs.fess.es.user.exentity.Group; +import org.codelibs.fess.util.ComponentUtil; import org.codelibs.fess.util.RenderDataUtil; import org.dbflute.optional.OptionalEntity; import org.dbflute.optional.OptionalThing; @@ -234,7 +235,7 @@ public class AdminGroupAction extends FessAdminAction { // =================================================================================== // Assist Logic // ============ - private OptionalEntity getEntity(final CreateForm form) { + private static OptionalEntity getEntity(final CreateForm form) { switch (form.crudMode) { case CrudMode.CREATE: return OptionalEntity.of(new Group()).map(entity -> { @@ -243,7 +244,7 @@ public class AdminGroupAction extends FessAdminAction { }); case CrudMode.EDIT: if (form instanceof EditForm) { - return groupService.getGroup(((EditForm) form).id); + return ComponentUtil.getComponent(GroupService.class).getGroup(((EditForm) form).id); } break; default: @@ -252,7 +253,7 @@ public class AdminGroupAction extends FessAdminAction { return OptionalEntity.empty(); } - protected OptionalEntity getGroup(final CreateForm form) { + public static OptionalEntity getGroup(final CreateForm form) { return getEntity(form).map(entity -> { copyBeanToBean(form, entity, op -> op.exclude(Constants.COMMON_CONVERSION_RULE)); return entity; diff --git a/src/main/java/org/codelibs/fess/app/web/api/admin/group/ApiAdminGroupAction.java b/src/main/java/org/codelibs/fess/app/web/api/admin/group/ApiAdminGroupAction.java new file mode 100644 index 000000000..55bc8ac33 --- /dev/null +++ b/src/main/java/org/codelibs/fess/app/web/api/admin/group/ApiAdminGroupAction.java @@ -0,0 +1,131 @@ +/* + * 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.group; + +import java.util.List; +import java.util.stream.Collectors; + +import javax.annotation.Resource; + +import org.codelibs.fess.Constants; +import org.codelibs.fess.app.pager.GroupPager; +import org.codelibs.fess.app.service.GroupService; +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.Group; +import org.codelibs.fess.es.user.exentity.User; +import org.lastaflute.web.Execute; +import org.lastaflute.web.response.JsonResponse; + +import static org.codelibs.fess.app.web.admin.group.AdminGroupAction.*; + +public class ApiAdminGroupAction extends FessApiAdminAction { + + @Resource + private GroupService groupService; + + // GET /api/admin/group + // POST /api/admin/group + @Execute + public JsonResponse settings(final SearchBody body) { + validateApi(body, messages -> {}); + final GroupPager pager = new GroupPager(); + copyBeanToBean(body, pager, op -> op.exclude(Constants.PAGER_CONVERSION_RULE)); + final List list = groupService.getGroupList(pager); + return asJson(new ApiResult.ApiConfigsResponse() + .settings(list.stream().map(entity -> createEditBody(entity)).collect(Collectors.toList())) + .total(pager.getAllRecordCount()).status(ApiResult.Status.OK).result()); + } + + // GET /api/admin/group/setting/{id} + @Execute + public JsonResponse get$setting(final String id) { + return asJson(new ApiResult.ApiConfigResponse() + .setting(groupService.getGroup(id).map(entity -> createEditBody(entity)).orElseGet(() -> { + throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id)); + return null; + })).status(ApiResult.Status.OK).result()); + } + + // PUT /api/admin/group/setting + @Execute + public JsonResponse put$setting(final CreateBody body) { + validateApi(body, messages -> {}); + body.crudMode = CrudMode.CREATE; + final Group entity = getGroup(body).orElseGet(() -> { + throwValidationErrorApi(messages -> { + messages.addErrorsCrudFailedToCreateInstance(GLOBAL); + }); + return null; + }); + try { + groupService.store(entity); + saveInfo(messages -> messages.addSuccessCrudCreateCrudTable(GLOBAL)); + } 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/group/setting + @Execute + public JsonResponse post$setting(final EditBody body) { + validateApi(body, messages -> {}); + body.crudMode = CrudMode.EDIT; + final Group entity = getGroup(body).orElseGet(() -> { + throwValidationErrorApi(messages -> { + messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, body.id); + }); + return null; + }); + try { + groupService.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/group/setting/{id} + @Execute + public JsonResponse delete$setting(final String id) { + final Group entity = groupService.getGroup(id).orElseGet(() -> { + throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id)); + return null; + }); + getUserBean().ifPresent(u -> { + if (u.getFessUser() instanceof User && entity.getName().equals(u.getUserId())) { + throwValidationErrorApi(messages -> messages.addErrorsCouldNotDeleteLoggedInUser(GLOBAL)); + } + }); + try { + groupService.delete(entity); + saveInfo(messages -> messages.addSuccessCrudDeleteCrudTable(GLOBAL)); + } 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 Group entity) { + final EditBody body = new EditBody(); + copyBeanToBean(entity, body, copyOp -> { + copyOp.excludeNull(); + }); + return body; + } +} diff --git a/src/main/java/org/codelibs/fess/app/web/api/admin/group/CreateBody.java b/src/main/java/org/codelibs/fess/app/web/api/admin/group/CreateBody.java new file mode 100644 index 000000000..2610fd928 --- /dev/null +++ b/src/main/java/org/codelibs/fess/app/web/api/admin/group/CreateBody.java @@ -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.group; + +import org.codelibs.fess.app.web.admin.group.CreateForm; + +public class CreateBody extends CreateForm { +} diff --git a/src/main/java/org/codelibs/fess/app/web/api/admin/group/EditBody.java b/src/main/java/org/codelibs/fess/app/web/api/admin/group/EditBody.java new file mode 100644 index 000000000..df09ccd64 --- /dev/null +++ b/src/main/java/org/codelibs/fess/app/web/api/admin/group/EditBody.java @@ -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.group; + +import org.codelibs.fess.app.web.admin.group.EditForm; + +public class EditBody extends EditForm { +} diff --git a/src/main/java/org/codelibs/fess/app/web/api/admin/group/SearchBody.java b/src/main/java/org/codelibs/fess/app/web/api/admin/group/SearchBody.java new file mode 100644 index 000000000..84d99adc0 --- /dev/null +++ b/src/main/java/org/codelibs/fess/app/web/api/admin/group/SearchBody.java @@ -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.group; + +import org.codelibs.fess.app.web.admin.group.SearchForm; + +public class SearchBody extends SearchForm { +}