diff --git a/src/main/java/org/codelibs/fess/app/web/admin/boostdoc/AdminBoostdocAction.java b/src/main/java/org/codelibs/fess/app/web/admin/boostdoc/AdminBoostdocAction.java index 14d1e77b6..e8caafdc7 100644 --- a/src/main/java/org/codelibs/fess/app/web/admin/boostdoc/AdminBoostdocAction.java +++ b/src/main/java/org/codelibs/fess/app/web/admin/boostdoc/AdminBoostdocAction.java @@ -17,12 +17,15 @@ package org.codelibs.fess.app.web.admin.boostdoc; import javax.annotation.Resource; +import org.codelibs.core.beans.util.BeanUtil; import org.codelibs.fess.Constants; import org.codelibs.fess.app.pager.BoostDocPager; import org.codelibs.fess.app.service.BoostDocumentRuleService; import org.codelibs.fess.app.web.CrudMode; import org.codelibs.fess.app.web.base.FessAdminAction; import org.codelibs.fess.es.config.exentity.BoostDocumentRule; +import org.codelibs.fess.helper.SystemHelper; +import org.codelibs.fess.util.ComponentUtil; import org.codelibs.fess.util.RenderDataUtil; import org.dbflute.optional.OptionalEntity; import org.dbflute.optional.OptionalThing; @@ -225,7 +228,7 @@ public class AdminBoostdocAction 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 BoostDocumentRule()).map(entity -> { @@ -235,7 +238,7 @@ public class AdminBoostdocAction extends FessAdminAction { }); case CrudMode.EDIT: if (form instanceof EditForm) { - return boostDocumentRuleService.getBoostDocumentRule(((EditForm) form).id); + return ComponentUtil.getComponent(BoostDocumentRuleService.class).getBoostDocumentRule(((EditForm) form).id); } break; default: @@ -244,13 +247,14 @@ public class AdminBoostdocAction extends FessAdminAction { return OptionalEntity.empty(); } - protected OptionalEntity getBoostDocumentRule(final CreateForm form) { + public static OptionalEntity getBoostDocumentRule(final CreateForm form) { + final SystemHelper systemHelper = ComponentUtil.getSystemHelper(); final String username = systemHelper.getUsername(); final long currentTime = systemHelper.getCurrentTimeAsLong(); return getEntity(form, username, currentTime).map(entity -> { entity.setUpdatedBy(username); entity.setUpdatedTime(currentTime); - copyBeanToBean(form, entity, op -> op.exclude(Constants.COMMON_CONVERSION_RULE)); + BeanUtil.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/BaseSearchBody.java b/src/main/java/org/codelibs/fess/app/web/api/admin/BaseSearchBody.java new file mode 100644 index 000000000..d7e6b73b6 --- /dev/null +++ b/src/main/java/org/codelibs/fess/app/web/api/admin/BaseSearchBody.java @@ -0,0 +1,33 @@ +/* + * 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; + +import org.codelibs.fess.Constants; +import org.codelibs.fess.util.ComponentUtil; +import org.lastaflute.web.validation.Required; + +public class BaseSearchBody { + + @Required + public Integer size = ComponentUtil.getFessConfig().getPagingPageSizeAsInteger(); + @Required + public Integer page = Constants.DEFAULT_ADMIN_PAGE_NUMBER; + + public BaseSearchBody() { + super(); + } + +} \ No newline at end of file diff --git a/src/main/java/org/codelibs/fess/app/web/api/admin/accesstoken/SearchBody.java b/src/main/java/org/codelibs/fess/app/web/api/admin/accesstoken/SearchBody.java index 8e38d3080..a857680e5 100644 --- a/src/main/java/org/codelibs/fess/app/web/api/admin/accesstoken/SearchBody.java +++ b/src/main/java/org/codelibs/fess/app/web/api/admin/accesstoken/SearchBody.java @@ -15,16 +15,8 @@ */ package org.codelibs.fess.app.web.api.admin.accesstoken; -import org.codelibs.fess.Constants; -import org.codelibs.fess.util.ComponentUtil; -import org.lastaflute.web.validation.Required; +import org.codelibs.fess.app.web.api.admin.BaseSearchBody; -public class SearchBody { - - @Required - public Integer size = ComponentUtil.getFessConfig().getPagingPageSizeAsInteger(); - - @Required - public Integer page = Constants.DEFAULT_ADMIN_PAGE_NUMBER; +public class SearchBody extends BaseSearchBody { } diff --git a/src/main/java/org/codelibs/fess/app/web/api/admin/badword/ApiAdminBadwordAction.java b/src/main/java/org/codelibs/fess/app/web/api/admin/badword/ApiAdminBadwordAction.java index 1463ab7ce..2b271aafb 100644 --- a/src/main/java/org/codelibs/fess/app/web/api/admin/badword/ApiAdminBadwordAction.java +++ b/src/main/java/org/codelibs/fess/app/web/api/admin/badword/ApiAdminBadwordAction.java @@ -15,6 +15,20 @@ */ package org.codelibs.fess.app.web.api.admin.badword; +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.io.Reader; +import java.io.Writer; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; +import java.util.stream.Collectors; + +import javax.annotation.Resource; + import org.codelibs.fess.Constants; import org.codelibs.fess.app.pager.BadWordPager; import org.codelibs.fess.app.service.BadWordService; @@ -23,6 +37,7 @@ import org.codelibs.fess.app.web.admin.badword.CreateForm; import org.codelibs.fess.app.web.admin.badword.EditForm; import org.codelibs.fess.app.web.admin.badword.UploadForm; import org.codelibs.fess.app.web.api.ApiResult; +import org.codelibs.fess.app.web.api.admin.BaseSearchBody; import org.codelibs.fess.app.web.api.admin.FessApiAdminAction; import org.codelibs.fess.es.config.exentity.BadWord; import org.codelibs.fess.exception.FessSystemException; @@ -32,13 +47,6 @@ import org.lastaflute.web.Execute; import org.lastaflute.web.response.JsonResponse; import org.lastaflute.web.response.StreamResponse; -import javax.annotation.Resource; -import java.io.*; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.List; -import java.util.stream.Collectors; - public class ApiAdminBadwordAction extends FessApiAdminAction { @Resource @@ -50,7 +58,7 @@ public class ApiAdminBadwordAction extends FessApiAdminAction { // GET /api/admin/badword // POST /api/admin/badword @Execute - public JsonResponse settings(SearchBody body) { + public JsonResponse settings(BaseSearchBody body) { validateApi(body, messages -> {}); final BadWordPager pager = new BadWordPager(); pager.setPageSize(body.size); diff --git a/src/main/java/org/codelibs/fess/app/web/api/admin/badword/CreateBody.java b/src/main/java/org/codelibs/fess/app/web/api/admin/badword/CreateBody.java index fda6c1e8c..c52cef181 100644 --- a/src/main/java/org/codelibs/fess/app/web/api/admin/badword/CreateBody.java +++ b/src/main/java/org/codelibs/fess/app/web/api/admin/badword/CreateBody.java @@ -1,3 +1,18 @@ +/* + * 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.badword; import org.codelibs.fess.app.web.admin.badword.CreateForm; diff --git a/src/main/java/org/codelibs/fess/app/web/api/admin/badword/DownloadBody.java b/src/main/java/org/codelibs/fess/app/web/api/admin/badword/DownloadBody.java index c023d5097..ec0b47db1 100644 --- a/src/main/java/org/codelibs/fess/app/web/api/admin/badword/DownloadBody.java +++ b/src/main/java/org/codelibs/fess/app/web/api/admin/badword/DownloadBody.java @@ -1,3 +1,18 @@ +/* + * 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.badword; import org.codelibs.fess.app.web.admin.badword.DownloadForm; diff --git a/src/main/java/org/codelibs/fess/app/web/api/admin/badword/EditBody.java b/src/main/java/org/codelibs/fess/app/web/api/admin/badword/EditBody.java index 5655a2790..e31a1c664 100644 --- a/src/main/java/org/codelibs/fess/app/web/api/admin/badword/EditBody.java +++ b/src/main/java/org/codelibs/fess/app/web/api/admin/badword/EditBody.java @@ -1,3 +1,18 @@ +/* + * 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.badword; import org.codelibs.fess.app.web.admin.badword.EditForm; diff --git a/src/main/java/org/codelibs/fess/app/web/api/admin/badword/SearchBody.java b/src/main/java/org/codelibs/fess/app/web/api/admin/badword/SearchBody.java index dd5bf43b1..de4912b3e 100644 --- a/src/main/java/org/codelibs/fess/app/web/api/admin/badword/SearchBody.java +++ b/src/main/java/org/codelibs/fess/app/web/api/admin/badword/SearchBody.java @@ -1,14 +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.badword; -import org.codelibs.fess.Constants; -import org.codelibs.fess.util.ComponentUtil; -import org.lastaflute.web.validation.Required; +import org.codelibs.fess.app.web.api.admin.BaseSearchBody; -public class SearchBody { - @Required - public Integer size = ComponentUtil.getFessConfig().getPagingPageSizeAsInteger(); - - @Required - public Integer page = Constants.DEFAULT_ADMIN_PAGE_NUMBER; +public class SearchBody extends BaseSearchBody { } diff --git a/src/main/java/org/codelibs/fess/app/web/api/admin/boostdoc/ApiAdminBoostdocAction.java b/src/main/java/org/codelibs/fess/app/web/api/admin/boostdoc/ApiAdminBoostdocAction.java new file mode 100644 index 000000000..5f10be718 --- /dev/null +++ b/src/main/java/org/codelibs/fess/app/web/api/admin/boostdoc/ApiAdminBoostdocAction.java @@ -0,0 +1,138 @@ +/* + * 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.boostdoc; + +import static org.codelibs.fess.app.web.admin.boostdoc.AdminBoostdocAction.getBoostDocumentRule; + +import java.util.List; +import java.util.stream.Collectors; + +import javax.annotation.Resource; + +import org.codelibs.fess.app.pager.BoostDocPager; +import org.codelibs.fess.app.service.BoostDocumentRuleService; +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.ApiConfigsResponse; +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.BoostDocumentRule; +import org.lastaflute.web.Execute; +import org.lastaflute.web.response.JsonResponse; + +/** + * @author shinsuke + */ +public class ApiAdminBoostdocAction extends FessApiAdminAction { + + // =================================================================================== + // Attribute + // ========= + @Resource + private BoostDocumentRuleService boostDocumentRuleService; + + // =================================================================================== + // Search Execute + // ============== + + // GET /api/admin/boostdoc + // POST /api/admin/boostdoc + @Execute + public JsonResponse settings(SearchBody body) { + validateApi(body, messages -> {}); + final BoostDocPager pager = new BoostDocPager(); + pager.setPageSize(body.size); + pager.setCurrentPageNumber(body.page); + final List list = boostDocumentRuleService.getBoostDocumentRuleList(pager); + return asJson(new ApiConfigsResponse() + .settings(list.stream().map(entity -> createEditBody(entity)).collect(Collectors.toList())) + .total(pager.getAllRecordCount()).status(Status.OK).result()); + } + + // GET /api/admin/boostdoc/setting/{id} + @Execute + public JsonResponse get$setting(String id) { + return asJson(new ApiConfigResponse() + .setting(boostDocumentRuleService.getBoostDocumentRule(id).map(entity -> createEditBody(entity)).orElseGet(() -> { + throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id)); + return null; + })).status(Status.OK).result()); + } + + // PUT /api/admin/boostdoc/setting + @Execute + public JsonResponse put$setting(CreateBody body) { + validateApi(body, messages -> {}); + body.crudMode = CrudMode.CREATE; + final BoostDocumentRule boostDoc = getBoostDocumentRule(body).map(entity -> { + try { + boostDocumentRuleService.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(boostDoc.getId()).created(true).status(Status.OK).result()); + } + + // POST /api/admin/boostdoc/setting + @Execute + public JsonResponse post$setting(final EditBody body) { + validateApi(body, messages -> {}); + body.crudMode = CrudMode.EDIT; + final BoostDocumentRule boostDoc = getBoostDocumentRule(body).map(entity -> { + try { + boostDocumentRuleService.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(boostDoc.getId()).created(false).status(Status.OK).result()); + } + + // DELETE /api/admin/boostdoc/setting/{id} + @Execute + public JsonResponse delete$setting(String id) { + boostDocumentRuleService.getBoostDocumentRule(id).ifPresent(entity -> { + try { + boostDocumentRuleService.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 BoostDocumentRule entity) { + final EditBody form = new EditBody(); + copyBeanToBean(entity, form, copyOp -> copyOp.excludeNull()); + form.crudMode = null; + return form; + } +} diff --git a/src/main/java/org/codelibs/fess/app/web/api/admin/boostdoc/CreateBody.java b/src/main/java/org/codelibs/fess/app/web/api/admin/boostdoc/CreateBody.java new file mode 100644 index 000000000..b35faf3a5 --- /dev/null +++ b/src/main/java/org/codelibs/fess/app/web/api/admin/boostdoc/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.boostdoc; + +import org.codelibs.fess.app.web.admin.boostdoc.CreateForm; + +public class CreateBody extends CreateForm { + +} diff --git a/src/main/java/org/codelibs/fess/app/web/api/admin/boostdoc/EditBody.java b/src/main/java/org/codelibs/fess/app/web/api/admin/boostdoc/EditBody.java new file mode 100644 index 000000000..f0f7f9545 --- /dev/null +++ b/src/main/java/org/codelibs/fess/app/web/api/admin/boostdoc/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.boostdoc; + +import org.codelibs.fess.app.web.admin.boostdoc.EditForm; + +public class EditBody extends EditForm { + +} diff --git a/src/main/java/org/codelibs/fess/app/web/api/admin/boostdoc/SearchBody.java b/src/main/java/org/codelibs/fess/app/web/api/admin/boostdoc/SearchBody.java new file mode 100644 index 000000000..397795b35 --- /dev/null +++ b/src/main/java/org/codelibs/fess/app/web/api/admin/boostdoc/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.boostdoc; + +import org.codelibs.fess.app.web.api.admin.BaseSearchBody; + +public class SearchBody extends BaseSearchBody { + +}