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.crawlinginfo;
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.CrawlingInfoPager;
24  import org.codelibs.fess.app.service.CrawlingInfoService;
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.CrawlingInfo;
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 ApiAdminCrawlinginfoAction extends FessApiAdminAction {
39  
40      // ===================================================================================
41      //                                                                           Attribute
42      //                                                                           =========
43      @Resource
44      private CrawlingInfoService crawlingInfoService;
45      @Resource
46      protected ProcessHelper processHelper;
47  
48      // ===================================================================================
49      //                                                                      Search Execute
50      //                                                                      ==============
51  
52      // GET /api/admin/crawlinginfo/logs
53      // POST /api/admin/crawlinginfo/logs
54      @Execute
55      public JsonResponse<ApiResult> logs(final SearchBody body) {
56          validateApi(body, messages -> {});
57          final CrawlingInfoPager pager = copyBeanToNewBean(body, CrawlingInfoPager.class);
58          final List<CrawlingInfo> list = crawlingInfoService.getCrawlingInfoList(pager);
59          return asJson(new ApiResult.ApiLogsResponse<EditBody>().logs(list.stream().map(this::createEditBody).collect(Collectors.toList()))
60                  .total(pager.getAllRecordCount()).status(ApiResult.Status.OK).result());
61      }
62  
63      // GET /api/admin/crawlinginfo/log/{id}
64      @Execute
65      public JsonResponse<ApiResult> get$log(final String id) {
66          return asJson(new ApiLogResponse().log(crawlingInfoService.getCrawlingInfo(id).map(this::createEditBody).orElseGet(() -> {
67              throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id));
68              return null;
69          })).status(Status.OK).result());
70      }
71  
72      // DELETE /api/admin/crawlinginfo/log/{id}
73      @Execute
74      public JsonResponse<ApiResult> delete$log(final String id) {
75          crawlingInfoService.getCrawlingInfo(id).ifPresent(entity -> {
76              try {
77                  crawlingInfoService.delete(entity);
78                  saveInfo(messages -> messages.addSuccessCrudDeleteCrudTable(GLOBAL));
79              } catch (final Exception e) {
80                  throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToDeleteCrudTable(GLOBAL, buildThrowableMessage(e)));
81              }
82          }).orElse(() -> {
83              throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id));
84          });
85          return asJson(new ApiResponse().status(Status.OK).result());
86      }
87  
88      // DELETE /api/admin/crawlinginfo/all
89      @Execute
90      public JsonResponse<ApiResult> delete$all() {
91          try {
92              crawlingInfoService.deleteOldSessions(processHelper.getRunningSessionIdSet());
93              saveInfo(messages -> messages.addSuccessCrawlingInfoDeleteAll(GLOBAL));
94          } catch (final Exception e) {
95              throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToDeleteCrudTable(GLOBAL, buildThrowableMessage(e)));
96          }
97          return asJson(new ApiResponse().status(Status.OK).result());
98      }
99  
100     protected EditBody createEditBody(final CrawlingInfo entity) {
101         final EditBody body = new EditBody();
102         copyBeanToBean(entity, body, copyOp -> {
103             copyOp.excludeNull();
104         });
105         return body;
106     }
107 }