Browse Source

fix #948 Admin API: /api/admin/boostdoc

Shinsuke Sugaya 8 years ago
parent
commit
e93fcd4d24

+ 8 - 4
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<BoostDocumentRule> getEntity(final CreateForm form, final String username, final long currentTime) {
+    public static OptionalEntity<BoostDocumentRule> 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<BoostDocumentRule> getBoostDocumentRule(final CreateForm form) {
+    public static OptionalEntity<BoostDocumentRule> 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;
         });
     }

+ 33 - 0
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();
+    }
+
+}

+ 2 - 10
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 {
 
 }

+ 16 - 8
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<ApiResult> settings(SearchBody body) {
+    public JsonResponse<ApiResult> settings(BaseSearchBody body) {
         validateApi(body, messages -> {});
         final BadWordPager pager = new BadWordPager();
         pager.setPageSize(body.size);

+ 15 - 0
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;

+ 15 - 0
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;

+ 15 - 0
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;

+ 17 - 9
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 {
 
 }

+ 138 - 0
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<ApiResult> settings(SearchBody body) {
+        validateApi(body, messages -> {});
+        final BoostDocPager pager = new BoostDocPager();
+        pager.setPageSize(body.size);
+        pager.setCurrentPageNumber(body.page);
+        final List<BoostDocumentRule> list = boostDocumentRuleService.getBoostDocumentRuleList(pager);
+        return asJson(new ApiConfigsResponse<EditBody>()
+                .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<ApiResult> 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<ApiResult> 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<ApiResult> 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<ApiResult> 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;
+    }
+}

+ 22 - 0
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 {
+
+}

+ 22 - 0
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 {
+
+}

+ 22 - 0
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 {
+
+}