View Javadoc
1   /*
2    * Copyright 2012-2021 CodeLibs Project and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.codelibs.fess.app.web.api.admin.boostdoc;
17  
18  import static org.codelibs.fess.app.web.admin.boostdoc.AdminBoostdocAction.getBoostDocumentRule;
19  
20  import java.util.List;
21  import java.util.stream.Collectors;
22  
23  import javax.annotation.Resource;
24  
25  import org.codelibs.core.beans.util.CopyOptions;
26  import org.codelibs.fess.app.pager.BoostDocPager;
27  import org.codelibs.fess.app.service.BoostDocumentRuleService;
28  import org.codelibs.fess.app.web.CrudMode;
29  import org.codelibs.fess.app.web.api.ApiResult;
30  import org.codelibs.fess.app.web.api.ApiResult.ApiConfigResponse;
31  import org.codelibs.fess.app.web.api.ApiResult.ApiConfigsResponse;
32  import org.codelibs.fess.app.web.api.ApiResult.ApiResponse;
33  import org.codelibs.fess.app.web.api.ApiResult.ApiUpdateResponse;
34  import org.codelibs.fess.app.web.api.ApiResult.Status;
35  import org.codelibs.fess.app.web.api.admin.FessApiAdminAction;
36  import org.codelibs.fess.es.config.exentity.BoostDocumentRule;
37  import org.lastaflute.web.Execute;
38  import org.lastaflute.web.response.JsonResponse;
39  
40  /**
41   * @author shinsuke
42   */
43  public class ApiAdminBoostdocAction extends FessApiAdminAction {
44  
45      // ===================================================================================
46      //                                                                           Attribute
47      //                                                                           =========
48      @Resource
49      private BoostDocumentRuleService boostDocumentRuleService;
50  
51      // ===================================================================================
52      //                                                                      Search Execute
53      //                                                                      ==============
54  
55      // GET /api/admin/boostdoc
56      // POST /api/admin/boostdoc
57      @Execute
58      public JsonResponse<ApiResult> settings(final SearchBody body) {
59          validateApi(body, messages -> {});
60          final BoostDocPager pager = copyBeanToNewBean(body, BoostDocPager.class);
61          final List<BoostDocumentRule> list = boostDocumentRuleService.getBoostDocumentRuleList(pager);
62          return asJson(new ApiConfigsResponse<EditBody>().settings(list.stream().map(this::createEditBody).collect(Collectors.toList()))
63                  .total(pager.getAllRecordCount()).status(Status.OK).result());
64      }
65  
66      // GET /api/admin/boostdoc/setting/{id}
67      @Execute
68      public JsonResponse<ApiResult> get$setting(final String id) {
69          return asJson(new ApiConfigResponse()
70                  .setting(boostDocumentRuleService.getBoostDocumentRule(id).map(this::createEditBody).orElseGet(() -> {
71                      throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id));
72                      return null;
73                  })).status(Status.OK).result());
74      }
75  
76      // PUT /api/admin/boostdoc/setting
77      @Execute
78      public JsonResponse<ApiResult> put$setting(final CreateBody body) {
79          validateApi(body, messages -> {});
80          body.crudMode = CrudMode.CREATE;
81          final BoostDocumentRule boostDoc = getBoostDocumentRule(body).map(entity -> {
82              try {
83                  boostDocumentRuleService.store(entity);
84              } catch (final Exception e) {
85                  throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToCreateCrudTable(GLOBAL, buildThrowableMessage(e)));
86              }
87              return entity;
88          }).orElseGet(() -> {
89              throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToCreateInstance(GLOBAL));
90              return null;
91          });
92          return asJson(new ApiUpdateResponse().id(boostDoc.getId()).created(true).status(Status.OK).result());
93      }
94  
95      // POST /api/admin/boostdoc/setting
96      @Execute
97      public JsonResponse<ApiResult> post$setting(final EditBody body) {
98          validateApi(body, messages -> {});
99          body.crudMode = CrudMode.EDIT;
100         final BoostDocumentRule boostDoc = getBoostDocumentRule(body).map(entity -> {
101             try {
102                 boostDocumentRuleService.store(entity);
103             } catch (final Exception e) {
104                 throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToUpdateCrudTable(GLOBAL, buildThrowableMessage(e)));
105             }
106             return entity;
107         }).orElseGet(() -> {
108             throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, body.id));
109             return null;
110         });
111         return asJson(new ApiUpdateResponse().id(boostDoc.getId()).created(false).status(Status.OK).result());
112     }
113 
114     // DELETE /api/admin/boostdoc/setting/{id}
115     @Execute
116     public JsonResponse<ApiResult> delete$setting(final String id) {
117         boostDocumentRuleService.getBoostDocumentRule(id).ifPresent(entity -> {
118             try {
119                 boostDocumentRuleService.delete(entity);
120                 saveInfo(messages -> messages.addSuccessCrudDeleteCrudTable(GLOBAL));
121             } catch (final Exception e) {
122                 throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToDeleteCrudTable(GLOBAL, buildThrowableMessage(e)));
123             }
124         }).orElse(() -> {
125             throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id));
126         });
127         return asJson(new ApiResponse().status(Status.OK).result());
128     }
129 
130     protected EditBody createEditBody(final BoostDocumentRule entity) {
131         final EditBody form = new EditBody();
132         copyBeanToBean(entity, form, CopyOptions::excludeNull);
133         form.crudMode = null;
134         return form;
135     }
136 }