View Javadoc
1   /*
2    * Copyright 2012-2017 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>()
60                  .logs(list.stream().map(entity -> createEditBody(entity)).collect(Collectors.toList())).total(pager.getAllRecordCount())
61                  .status(ApiResult.Status.OK).result());
62      }
63  
64      // GET /api/admin/crawlinginfo/log/{id}
65      @Execute
66      public JsonResponse<ApiResult> get$log(final String id) {
67          return asJson(new ApiLogResponse()
68                  .log(crawlingInfoService.getCrawlingInfo(id).map(entity -> createEditBody(entity)).orElseGet(() -> {
69                      throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id));
70                      return null;
71                  })).status(Status.OK).result());
72      }
73  
74      // DELETE /api/admin/crawlinginfo/log/{id}
75      @Execute
76      public JsonResponse<ApiResult> delete$log(final String id) {
77          crawlingInfoService.getCrawlingInfo(id).ifPresent(entity -> {
78              try {
79                  crawlingInfoService.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/crawlinginfo/all
91      @Execute
92      public JsonResponse<ApiResult> delete$all() {
93          try {
94              crawlingInfoService.deleteOldSessions(processHelper.getRunningSessionIdSet());
95              saveInfo(messages -> messages.addSuccessCrawlingInfoDeleteAll(GLOBAL));
96          } catch (final Exception e) {
97              throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToDeleteCrudTable(GLOBAL, buildThrowableMessage(e)));
98          }
99          return asJson(new ApiResponse().status(Status.OK).result());
100     }
101 
102     protected EditBody createEditBody(final CrawlingInfo entity) {
103         final EditBody body = new EditBody();
104         copyBeanToBean(entity, body, copyOp -> {
105             copyOp.excludeNull();
106         });
107         return body;
108     }
109 }