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.duplicatehost;
17  
18  import static org.codelibs.fess.app.web.admin.duplicatehost.AdminDuplicatehostAction.getDuplicateHost;
19  
20  import java.util.List;
21  import java.util.stream.Collectors;
22  
23  import javax.annotation.Resource;
24  
25  import org.codelibs.fess.app.pager.DuplicateHostPager;
26  import org.codelibs.fess.app.service.DuplicateHostService;
27  import org.codelibs.fess.app.web.CrudMode;
28  import org.codelibs.fess.app.web.api.ApiResult;
29  import org.codelibs.fess.app.web.api.ApiResult.ApiConfigResponse;
30  import org.codelibs.fess.app.web.api.ApiResult.ApiResponse;
31  import org.codelibs.fess.app.web.api.ApiResult.ApiUpdateResponse;
32  import org.codelibs.fess.app.web.api.ApiResult.Status;
33  import org.codelibs.fess.app.web.api.admin.FessApiAdminAction;
34  import org.codelibs.fess.es.config.exentity.DuplicateHost;
35  import org.lastaflute.web.Execute;
36  import org.lastaflute.web.response.JsonResponse;
37  
38  /**
39   * @author Keiichi Watanabe
40   */
41  public class ApiAdminDuplicatehostAction extends FessApiAdminAction {
42  
43      // ===================================================================================
44      //                                                                           Attribute
45      //                                                                           =========
46      @Resource
47      private DuplicateHostService duplicateHostService;
48  
49      // ===================================================================================
50      //                                                                      Search Execute
51      //                                                                      ==============
52  
53      // GET /api/admin/duplicatehost/settings
54      // POST /api/admin/duplicatehost/settings
55      @Execute
56      public JsonResponse<ApiResult> settings(final SearchBody body) {
57          validateApi(body, messages -> {});
58          final DuplicateHostPager pager = copyBeanToNewBean(body, DuplicateHostPager.class);
59          final List<DuplicateHost> list = duplicateHostService.getDuplicateHostList(pager);
60          return asJson(
61                  new ApiResult.ApiConfigsResponse<EditBody>().settings(list.stream().map(this::createEditBody).collect(Collectors.toList()))
62                          .total(pager.getAllRecordCount()).status(ApiResult.Status.OK).result());
63      }
64  
65      // GET /api/admin/duplicatehost/setting/{id}
66      @Execute
67      public JsonResponse<ApiResult> get$setting(final String id) {
68          return asJson(new ApiConfigResponse().setting(duplicateHostService.getDuplicateHost(id).map(this::createEditBody).orElseGet(() -> {
69              throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id));
70              return null;
71          })).status(Status.OK).result());
72      }
73  
74      // PUT /api/admin/duplicatehost/setting
75      @Execute
76      public JsonResponse<ApiResult> put$setting(final CreateBody body) {
77          validateApi(body, messages -> {});
78          body.crudMode = CrudMode.CREATE;
79          final DuplicateHost duplicateHost = getDuplicateHost(body).map(entity -> {
80              try {
81                  duplicateHostService.store(entity);
82              } catch (final Exception e) {
83                  throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToCreateCrudTable(GLOBAL, buildThrowableMessage(e)));
84              }
85              return entity;
86          }).orElseGet(() -> {
87              throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToCreateInstance(GLOBAL));
88              return null;
89          });
90  
91          return asJson(new ApiUpdateResponse().id(duplicateHost.getId()).created(true).status(Status.OK).result());
92      }
93  
94      // POST /api/admin/duplicatehost/setting
95      @Execute
96      public JsonResponse<ApiResult> post$setting(final EditBody body) {
97          validateApi(body, messages -> {});
98          body.crudMode = CrudMode.EDIT;
99          final DuplicateHost duplicateHost = getDuplicateHost(body).map(entity -> {
100             try {
101                 duplicateHostService.store(entity);
102             } catch (final Exception e) {
103                 throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToUpdateCrudTable(GLOBAL, buildThrowableMessage(e)));
104             }
105             return entity;
106         }).orElseGet(() -> {
107             throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, body.id));
108             return null;
109         });
110         return asJson(new ApiUpdateResponse().id(duplicateHost.getId()).created(false).status(Status.OK).result());
111     }
112 
113     // DELETE /api/admin/duplicatehost/setting/{id}
114     @Execute
115     public JsonResponse<ApiResult> delete$setting(final String id) {
116         duplicateHostService.getDuplicateHost(id).ifPresent(entity -> {
117             try {
118                 duplicateHostService.delete(entity);
119                 saveInfo(messages -> messages.addSuccessCrudDeleteCrudTable(GLOBAL));
120             } catch (final Exception e) {
121                 throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToDeleteCrudTable(GLOBAL, buildThrowableMessage(e)));
122             }
123         }).orElse(() -> {
124             throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id));
125         });
126         return asJson(new ApiResponse().status(Status.OK).result());
127     }
128 
129     protected EditBody createEditBody(final DuplicateHost entity) {
130         final EditBody body = new EditBody();
131         copyBeanToBean(entity, body, copyOp -> {
132             copyOp.excludeNull();
133         });
134         return body;
135     }
136 }