diff --git a/src/main/java/org/codelibs/fess/app/web/admin/webconfig/AdminWebconfigAction.java b/src/main/java/org/codelibs/fess/app/web/admin/webconfig/AdminWebconfigAction.java index 30b0f4197..3328a0068 100644 --- a/src/main/java/org/codelibs/fess/app/web/admin/webconfig/AdminWebconfigAction.java +++ b/src/main/java/org/codelibs/fess/app/web/admin/webconfig/AdminWebconfigAction.java @@ -34,6 +34,7 @@ import org.codelibs.fess.app.web.CrudMode; import org.codelibs.fess.app.web.base.FessAdminAction; import org.codelibs.fess.es.config.exentity.WebConfig; import org.codelibs.fess.helper.PermissionHelper; +import org.codelibs.fess.helper.SystemHelper; import org.codelibs.fess.util.ComponentUtil; import org.codelibs.fess.util.RenderDataUtil; import org.dbflute.optional.OptionalEntity; @@ -269,7 +270,7 @@ public class AdminWebconfigAction extends FessAdminAction { // =================================================================================== // Assist Logic // ============ - private OptionalEntity getEntity(final CreateForm form, final String username, final long currentTime) { + public static OptionalEntity getEntity(final CreateForm form, final String username, final long currentTime) { switch (form.crudMode) { case CrudMode.CREATE: return OptionalEntity.of(new WebConfig()).map(entity -> { @@ -279,7 +280,7 @@ public class AdminWebconfigAction extends FessAdminAction { }); case CrudMode.EDIT: if (form instanceof EditForm) { - return webConfigService.getWebConfig(((EditForm) form).id); + return ComponentUtil.getComponent(WebConfigService.class).getWebConfig(((EditForm) form).id); } break; default: @@ -288,7 +289,8 @@ public class AdminWebconfigAction extends FessAdminAction { return OptionalEntity.empty(); } - protected OptionalEntity getWebConfig(final CreateForm form) { + public static OptionalEntity getWebConfig(final CreateForm form) { + final SystemHelper systemHelper = ComponentUtil.getSystemHelper(); final String username = systemHelper.getUsername(); final long currentTime = systemHelper.getCurrentTimeAsLong(); return getEntity(form, username, currentTime).map( diff --git a/src/main/java/org/codelibs/fess/app/web/api/admin/webconfig/ApiAdminWebconfigAction.java b/src/main/java/org/codelibs/fess/app/web/api/admin/webconfig/ApiAdminWebconfigAction.java new file mode 100644 index 000000000..963abed21 --- /dev/null +++ b/src/main/java/org/codelibs/fess/app/web/api/admin/webconfig/ApiAdminWebconfigAction.java @@ -0,0 +1,139 @@ +/* + * 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.webconfig; + +import static org.codelibs.fess.app.web.admin.webconfig.AdminWebconfigAction.getWebConfig; + +import java.util.List; +import java.util.stream.Collectors; + +import javax.annotation.Resource; + +import org.codelibs.fess.Constants; +import org.codelibs.fess.app.pager.WebConfigPager; +import org.codelibs.fess.app.service.WebConfigService; +import org.codelibs.fess.app.web.CrudMode; +import org.codelibs.fess.app.web.api.ApiResult; +import org.codelibs.fess.app.web.api.ApiResult.ApiConfigResponse; +import org.codelibs.fess.app.web.api.ApiResult.ApiResponse; +import org.codelibs.fess.app.web.api.ApiResult.ApiUpdateResponse; +import org.codelibs.fess.app.web.api.ApiResult.Status; +import org.codelibs.fess.app.web.api.admin.FessApiAdminAction; +import org.codelibs.fess.es.config.exentity.WebConfig; +import org.lastaflute.web.Execute; +import org.lastaflute.web.response.JsonResponse; + +/** + * @author Keiichi Watanabe + */ +public class ApiAdminWebconfigAction extends FessApiAdminAction { + + // =================================================================================== + // Attribute + // ========= + @Resource + private WebConfigService webConfigService; + + // =================================================================================== + // Search Execute + // ============== + + // GET /api/admin/webconfig + // POST /api/admin/webconfig + @Execute + public JsonResponse settings(final SearchBody body) { + validateApi(body, messages -> {}); + final WebConfigPager pager = new WebConfigPager(); + copyBeanToBean(body, pager, op -> op.exclude(Constants.PAGER_CONVERSION_RULE)); + final List list = webConfigService.getWebConfigList(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/webconfig/setting/{id} + @Execute + public JsonResponse get$setting(final String id) { + return asJson(new ApiConfigResponse() + .setting(webConfigService.getWebConfig(id).map(entity -> createEditBody(entity)).orElseGet(() -> { + throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id)); + return null; + })).status(Status.OK).result()); + } + + // PUT /api/admin/webconfig/setting + @Execute + public JsonResponse put$setting(final CreateBody body) { + validateApi(body, messages -> {}); + body.crudMode = CrudMode.CREATE; + final WebConfig webConfig = getWebConfig(body).map(entity -> { + try { + webConfigService.store(entity); + } catch (final Exception e) { + throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToCreateCrudTable(GLOBAL, buildThrowableMessage(e))); + } + return entity; + }).orElseGet(() -> { + throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToCreateInstance(GLOBAL)); + return null; + }); + + return asJson(new ApiUpdateResponse().id(webConfig.getId()).created(true).status(Status.OK).result()); + } + + // POST /api/admin/webconfig/setting + @Execute + public JsonResponse post$setting(final EditBody body) { + validateApi(body, messages -> {}); + body.crudMode = CrudMode.EDIT; + final WebConfig webConfig = getWebConfig(body).map(entity -> { + try { + webConfigService.store(entity); + } catch (final Exception e) { + throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToUpdateCrudTable(GLOBAL, buildThrowableMessage(e))); + } + return entity; + }).orElseGet(() -> { + throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, body.id)); + return null; + }); + return asJson(new ApiUpdateResponse().id(webConfig.getId()).created(false).status(Status.OK).result()); + } + + // DELETE /api/admin/webconfig/setting/{id} + @Execute + public JsonResponse delete$setting(final String id) { + webConfigService.getWebConfig(id).ifPresent(entity -> { + try { + webConfigService.delete(entity); + saveInfo(messages -> messages.addSuccessCrudDeleteCrudTable(GLOBAL)); + } catch (final Exception e) { + throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToDeleteCrudTable(GLOBAL, buildThrowableMessage(e))); + } + }).orElse(() -> { + throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id)); + }); + return asJson(new ApiResponse().status(Status.OK).result()); + } + + protected EditBody createEditBody(final WebConfig 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/webconfig/CreateBody.java b/src/main/java/org/codelibs/fess/app/web/api/admin/webconfig/CreateBody.java new file mode 100644 index 000000000..6fb47f589 --- /dev/null +++ b/src/main/java/org/codelibs/fess/app/web/api/admin/webconfig/CreateBody.java @@ -0,0 +1,22 @@ +/* + * 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.webconfig; + +import org.codelibs.fess.app.web.admin.webconfig.CreateForm; + +public class CreateBody extends CreateForm { + +} diff --git a/src/main/java/org/codelibs/fess/app/web/api/admin/webconfig/EditBody.java b/src/main/java/org/codelibs/fess/app/web/api/admin/webconfig/EditBody.java new file mode 100644 index 000000000..06db0f5cc --- /dev/null +++ b/src/main/java/org/codelibs/fess/app/web/api/admin/webconfig/EditBody.java @@ -0,0 +1,22 @@ +/* + * 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.webconfig; + +import org.codelibs.fess.app.web.admin.webconfig.EditForm; + +public class EditBody extends EditForm { + +} diff --git a/src/main/java/org/codelibs/fess/app/web/api/admin/webconfig/SearchBody.java b/src/main/java/org/codelibs/fess/app/web/api/admin/webconfig/SearchBody.java new file mode 100644 index 000000000..c44f138e2 --- /dev/null +++ b/src/main/java/org/codelibs/fess/app/web/api/admin/webconfig/SearchBody.java @@ -0,0 +1,22 @@ +/* + * 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.webconfig; + +import org.codelibs.fess.app.web.admin.webconfig.SearchForm; + +public class SearchBody extends SearchForm { + +}