diff --git a/src/main/java/org/codelibs/fess/app/web/api/admin/relatedcontent/ApiAdminRelatedcontentAction.java b/src/main/java/org/codelibs/fess/app/web/api/admin/relatedcontent/ApiAdminRelatedcontentAction.java new file mode 100644 index 000000000..121c08d3b --- /dev/null +++ b/src/main/java/org/codelibs/fess/app/web/api/admin/relatedcontent/ApiAdminRelatedcontentAction.java @@ -0,0 +1,134 @@ +/* + * 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.relatedcontent; + +import static org.codelibs.fess.app.web.admin.relatedcontent.AdminRelatedcontentAction.getRelatedContent; + +import java.util.List; +import java.util.stream.Collectors; + +import javax.annotation.Resource; + +import org.codelibs.fess.app.pager.RelatedContentPager; +import org.codelibs.fess.app.service.RelatedContentService; +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.RelatedContent; +import org.lastaflute.web.Execute; +import org.lastaflute.web.response.JsonResponse; + +public class ApiAdminRelatedcontentAction extends FessApiAdminAction { + + // =================================================================================== + // Attribute + // ========= + @Resource + private RelatedContentService relatedContentService; + + // =================================================================================== + // Search Execute + // ============== + + // GET /api/admin/relatedcontent/settings + // POST /api/admin/relatedcontent/settings + @Execute + public JsonResponse settings(final SearchBody body) { + validateApi(body, messages -> {}); + final RelatedContentPager pager = copyBeanToNewBean(body, RelatedContentPager.class); + final List list = relatedContentService.getRelatedContentList(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/relatedcontent/setting/{id} + @Execute + public JsonResponse get$setting(final String id) { + return asJson(new ApiConfigResponse() + .setting(relatedContentService.getRelatedContent(id).map(entity -> createEditBody(entity)).orElseGet(() -> { + throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id)); + return null; + })).status(Status.OK).result()); + } + + // PUT /api/admin/relatedcontent/setting + @Execute + public JsonResponse put$setting(final CreateBody body) { + validateApi(body, messages -> {}); + body.crudMode = CrudMode.CREATE; + final RelatedContent relatedContent = getRelatedContent(body).map(entity -> { + try { + relatedContentService.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(relatedContent.getId()).created(true).status(Status.OK).result()); + } + + // POST /api/admin/relatedcontent/setting + @Execute + public JsonResponse post$setting(final EditBody body) { + validateApi(body, messages -> {}); + body.crudMode = CrudMode.EDIT; + final RelatedContent relatedContent = getRelatedContent(body).map(entity -> { + try { + relatedContentService.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(relatedContent.getId()).created(false).status(Status.OK).result()); + } + + // DELETE /api/admin/relatedcontent/setting/{id} + @Execute + public JsonResponse delete$setting(final String id) { + relatedContentService.getRelatedContent(id).ifPresent(entity -> { + try { + relatedContentService.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 RelatedContent 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/relatedcontent/CreateBody.java b/src/main/java/org/codelibs/fess/app/web/api/admin/relatedcontent/CreateBody.java new file mode 100644 index 000000000..b3dc68fdc --- /dev/null +++ b/src/main/java/org/codelibs/fess/app/web/api/admin/relatedcontent/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.relatedcontent; + +import org.codelibs.fess.app.web.admin.relatedcontent.CreateForm; + +public class CreateBody extends CreateForm { +} diff --git a/src/main/java/org/codelibs/fess/app/web/api/admin/relatedcontent/EditBody.java b/src/main/java/org/codelibs/fess/app/web/api/admin/relatedcontent/EditBody.java new file mode 100644 index 000000000..fb1111603 --- /dev/null +++ b/src/main/java/org/codelibs/fess/app/web/api/admin/relatedcontent/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.relatedcontent; + +import org.codelibs.fess.app.web.admin.relatedcontent.EditForm; + +public class EditBody extends EditForm { +} diff --git a/src/main/java/org/codelibs/fess/app/web/api/admin/relatedcontent/SearchBody.java b/src/main/java/org/codelibs/fess/app/web/api/admin/relatedcontent/SearchBody.java new file mode 100644 index 000000000..641f67f52 --- /dev/null +++ b/src/main/java/org/codelibs/fess/app/web/api/admin/relatedcontent/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.relatedcontent; + +import org.codelibs.fess.app.web.api.admin.BaseSearchBody; + +public class SearchBody extends BaseSearchBody { + public String id; +} diff --git a/src/main/java/org/codelibs/fess/app/web/api/admin/relatedquery/ApiAdminRelatedqueryAction.java b/src/main/java/org/codelibs/fess/app/web/api/admin/relatedquery/ApiAdminRelatedqueryAction.java new file mode 100644 index 000000000..2be0825a7 --- /dev/null +++ b/src/main/java/org/codelibs/fess/app/web/api/admin/relatedquery/ApiAdminRelatedqueryAction.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.relatedquery; + +import static org.codelibs.core.stream.StreamUtil.stream; +import static org.codelibs.fess.app.web.admin.relatedquery.AdminRelatedqueryAction.getRelatedQuery; + +import java.util.List; +import java.util.stream.Collectors; + +import javax.annotation.Resource; + +import org.codelibs.core.lang.StringUtil; +import org.codelibs.fess.Constants; +import org.codelibs.fess.app.pager.RelatedQueryPager; +import org.codelibs.fess.app.service.RelatedQueryService; +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.RelatedQuery; +import org.lastaflute.web.Execute; +import org.lastaflute.web.response.JsonResponse; + +public class ApiAdminRelatedqueryAction extends FessApiAdminAction { + + // =================================================================================== + // Attribute + // ========= + @Resource + private RelatedQueryService relatedQueryService; + + // =================================================================================== + // Search Execute + // ============== + + // GET /api/admin/relatedquery/settings + // POST /api/admin/relatedquery/settings + @Execute + public JsonResponse settings(final SearchBody body) { + validateApi(body, messages -> {}); + final RelatedQueryPager pager = copyBeanToNewBean(body, RelatedQueryPager.class); + final List list = relatedQueryService.getRelatedQueryList(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/relatedquery/setting/{id} + @Execute + public JsonResponse get$setting(final String id) { + return asJson(new ApiConfigResponse() + .setting(relatedQueryService.getRelatedQuery(id).map(entity -> createEditBody(entity)).orElseGet(() -> { + throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id)); + return null; + })).status(Status.OK).result()); + } + + // PUT /api/admin/relatedquery/setting + @Execute + public JsonResponse put$setting(final CreateBody body) { + validateApi(body, messages -> {}); + body.crudMode = CrudMode.CREATE; + final RelatedQuery relatedQuery = getRelatedQuery(body).map(entity -> { + try { + relatedQueryService.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(relatedQuery.getId()).created(true).status(Status.OK).result()); + } + + // POST /api/admin/relatedquery/setting + @Execute + public JsonResponse post$setting(final EditBody body) { + validateApi(body, messages -> {}); + body.crudMode = CrudMode.EDIT; + final RelatedQuery relatedQuery = getRelatedQuery(body).map(entity -> { + try { + relatedQueryService.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(relatedQuery.getId()).created(false).status(Status.OK).result()); + } + + // DELETE /api/admin/relatedquery/setting/{id} + @Execute + public JsonResponse delete$setting(final String id) { + relatedQueryService.getRelatedQuery(id).ifPresent(entity -> { + try { + relatedQueryService.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 RelatedQuery entity) { + final EditBody body = new EditBody(); + copyBeanToBean(entity, body, copyOp -> { + copyOp.excludeNull(); + copyOp.exclude(Constants.QUERIES); + }); + body.queries = stream(entity.getQueries()).get(stream -> stream.filter(StringUtil::isNotBlank).collect(Collectors.joining("\n"))); + return body; + } +} diff --git a/src/main/java/org/codelibs/fess/app/web/api/admin/relatedquery/CreateBody.java b/src/main/java/org/codelibs/fess/app/web/api/admin/relatedquery/CreateBody.java new file mode 100644 index 000000000..24b008644 --- /dev/null +++ b/src/main/java/org/codelibs/fess/app/web/api/admin/relatedquery/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.relatedquery; + +import org.codelibs.fess.app.web.admin.relatedquery.CreateForm; + +public class CreateBody extends CreateForm { +} diff --git a/src/main/java/org/codelibs/fess/app/web/api/admin/relatedquery/EditBody.java b/src/main/java/org/codelibs/fess/app/web/api/admin/relatedquery/EditBody.java new file mode 100644 index 000000000..e83a48d4c --- /dev/null +++ b/src/main/java/org/codelibs/fess/app/web/api/admin/relatedquery/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.relatedquery; + +import org.codelibs.fess.app.web.admin.relatedquery.EditForm; + +public class EditBody extends EditForm { +} diff --git a/src/main/java/org/codelibs/fess/app/web/api/admin/relatedquery/SearchBody.java b/src/main/java/org/codelibs/fess/app/web/api/admin/relatedquery/SearchBody.java new file mode 100644 index 000000000..c27148ec5 --- /dev/null +++ b/src/main/java/org/codelibs/fess/app/web/api/admin/relatedquery/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.relatedquery; + +import org.codelibs.fess.app.web.api.admin.BaseSearchBody; + +public class SearchBody extends BaseSearchBody { + public String id; +} diff --git a/src/test/java/org/codelibs/fess/it/admin/RelatedContentTests.java b/src/test/java/org/codelibs/fess/it/admin/RelatedContentTests.java new file mode 100644 index 000000000..2cfb1371c --- /dev/null +++ b/src/test/java/org/codelibs/fess/it/admin/RelatedContentTests.java @@ -0,0 +1,83 @@ +/* + * 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.it.admin; + +import java.util.HashMap; +import java.util.Map; + +import org.codelibs.fess.it.CrudTestBase; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; + +@Tag("it") +public class RelatedContentTests extends CrudTestBase { + + private static final String NAME_PREFIX = "relatedContentTest_"; + private static final String API_PATH = "/api/admin/relatedcontent"; + private static final String LIST_ENDPOINT_SUFFIX = "settings"; + private static final String ITEM_ENDPOINT_SUFFIX = "setting"; + + private static final String KEY_PROPERTY = "term"; + + @Override + protected String getNamePrefix() { + return NAME_PREFIX; + } + + @Override + protected String getApiPath() { + return API_PATH; + } + + @Override + protected String getKeyProperty() { + return KEY_PROPERTY; + } + + @Override + protected String getListEndpointSuffix() { + return LIST_ENDPOINT_SUFFIX; + } + + @Override + protected String getItemEndpointSuffix() { + return ITEM_ENDPOINT_SUFFIX; + } + + @Override + protected Map createTestParam(int id) { + final Map requestBody = new HashMap<>(); + final String keyProp = NAME_PREFIX + id; + requestBody.put(KEY_PROPERTY, keyProp); + requestBody.put("content", "query" + id); + return requestBody; + } + + @Override + protected Map getUpdateMap() { + final Map updateMap = new HashMap<>(); + updateMap.put("content", "new_query"); + return updateMap; + } + + @Test + void crudTest() { + testCreate(); + testRead(); + testUpdate(); + testDelete(); + } +} diff --git a/src/test/java/org/codelibs/fess/it/admin/RelatedQueryTests.java b/src/test/java/org/codelibs/fess/it/admin/RelatedQueryTests.java new file mode 100644 index 000000000..3faef2380 --- /dev/null +++ b/src/test/java/org/codelibs/fess/it/admin/RelatedQueryTests.java @@ -0,0 +1,83 @@ +/* + * 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.it.admin; + +import java.util.HashMap; +import java.util.Map; + +import org.codelibs.fess.it.CrudTestBase; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; + +@Tag("it") +public class RelatedQueryTests extends CrudTestBase { + + private static final String NAME_PREFIX = "relatedQueryTest_"; + private static final String API_PATH = "/api/admin/relatedquery"; + private static final String LIST_ENDPOINT_SUFFIX = "settings"; + private static final String ITEM_ENDPOINT_SUFFIX = "setting"; + + private static final String KEY_PROPERTY = "term"; + + @Override + protected String getNamePrefix() { + return NAME_PREFIX; + } + + @Override + protected String getApiPath() { + return API_PATH; + } + + @Override + protected String getKeyProperty() { + return KEY_PROPERTY; + } + + @Override + protected String getListEndpointSuffix() { + return LIST_ENDPOINT_SUFFIX; + } + + @Override + protected String getItemEndpointSuffix() { + return ITEM_ENDPOINT_SUFFIX; + } + + @Override + protected Map createTestParam(int id) { + final Map requestBody = new HashMap<>(); + final String keyProp = NAME_PREFIX + id; + requestBody.put(KEY_PROPERTY, keyProp); + requestBody.put("queries", "query" + id); + return requestBody; + } + + @Override + protected Map getUpdateMap() { + final Map updateMap = new HashMap<>(); + updateMap.put("queries", "new_query"); + return updateMap; + } + + @Test + void crudTest() { + testCreate(); + testRead(); + testUpdate(); + testDelete(); + } +}