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.failureurl;
17  
18  import java.util.List;
19  import java.util.stream.Collectors;
20  
21  import javax.annotation.Resource;
22  
23  import org.codelibs.fess.app.pager.FailureUrlPager;
24  import org.codelibs.fess.app.service.FailureUrlService;
25  import org.codelibs.fess.app.web.api.ApiResult;
26  import org.codelibs.fess.app.web.api.ApiResult.ApiLogResponse;
27  import org.codelibs.fess.app.web.api.ApiResult.ApiResponse;
28  import org.codelibs.fess.app.web.api.ApiResult.Status;
29  import org.codelibs.fess.app.web.api.admin.FessApiAdminAction;
30  import org.codelibs.fess.es.config.exentity.FailureUrl;
31  import org.codelibs.fess.helper.ProcessHelper;
32  import org.lastaflute.web.Execute;
33  import org.lastaflute.web.response.JsonResponse;
34  
35  /**
36   * @author Keiichi Watanabe
37   */
38  public class ApiAdminFailureurlAction extends FessApiAdminAction {
39  
40      // ===================================================================================
41      //                                                                           Attribute
42      //                                                                           =========
43      @Resource
44      private FailureUrlService failureUrlService;
45      @Resource
46      private FailureUrlPager failureUrlPager;
47      @Resource
48      protected ProcessHelper processHelper;
49  
50      // ===================================================================================
51      //                                                                      Search Execute
52      //                                                                      ==============
53  
54      // GET /api/admin/failureurl/logs
55      // POST /api/admin/failureurl/logs
56      @Execute
57      public JsonResponse<ApiResult> logs(final SearchBody body) {
58          validateApi(body, messages -> {});
59          final FailureUrlPager pager = copyBeanToNewBean(body, FailureUrlPager.class);
60          final List<FailureUrl> list = failureUrlService.getFailureUrlList(pager);
61          return asJson(new ApiResult.ApiLogsResponse<EditBody>().logs(list.stream().map(this::createEditBody).collect(Collectors.toList()))
62                  .total(pager.getAllRecordCount()).status(ApiResult.Status.OK).result());
63      }
64  
65      // GET /api/admin/failureurl/log/{id}
66      @Execute
67      public JsonResponse<ApiResult> get$log(final String id) {
68          return asJson(new ApiLogResponse().log(failureUrlService.getFailureUrl(id).map(this::createEditBody).orElseGet(() -> {
69              throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id));
70              return null;
71          })).status(Status.OK).result());
72      }
73  
74      // DELETE /api/admin/failureurl/log/{id}
75      @Execute
76      public JsonResponse<ApiResult> delete$log(final String id) {
77          failureUrlService.getFailureUrl(id).ifPresent(entity -> {
78              try {
79                  failureUrlService.delete(entity);
80                  saveInfo(messages -> messages.addSuccessCrudDeleteCrudTable(GLOBAL));
81              } catch (final Exception e) {
82                  throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToDeleteCrudTable(GLOBAL, buildThrowableMessage(e)));
83              }
84          }).orElse(() -> {
85              throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id));
86          });
87          return asJson(new ApiResponse().status(Status.OK).result());
88      }
89  
90      // DELETE /api/admin/failureurl/all
91      @Execute
92      public JsonResponse<ApiResult> delete$all() {
93          try {
94              failureUrlService.deleteAll(failureUrlPager);
95              failureUrlPager.clear();
96              saveInfo(messages -> messages.addSuccessFailureUrlDeleteAll(GLOBAL));
97          } catch (final Exception e) {
98              throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToDeleteCrudTable(GLOBAL, buildThrowableMessage(e)));
99          }
100         return asJson(new ApiResponse().status(Status.OK).result());
101     }
102 
103     protected EditBody createEditBody(final FailureUrl entity) {
104         final EditBody body = new EditBody();
105         copyBeanToBean(entity, body, copyOp -> {
106             copyOp.excludeNull();
107         });
108         return body;
109     }
110 }