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.joblog;
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.JobLogPager;
24  import org.codelibs.fess.app.service.JobLogService;
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.JobLog;
31  import org.lastaflute.web.Execute;
32  import org.lastaflute.web.response.JsonResponse;
33  
34  /**
35   * @author Keiichi Watanabe
36   */
37  public class ApiAdminJoblogAction extends FessApiAdminAction {
38  
39      // ===================================================================================
40      //                                                                           Attribute
41      //                                                                           =========
42      @Resource
43      private JobLogService jobLogService;
44  
45      // ===================================================================================
46      //                                                                      Search Execute
47      //                                                                      ==============
48  
49      // GET /api/admin/joblog/logs
50      @Execute
51      public JsonResponse<ApiResult> logs(final SearchBody body) {
52          validateApi(body, messages -> {});
53          final JobLogPager pager = copyBeanToNewBean(body, JobLogPager.class);
54          final List<JobLog> list = jobLogService.getJobLogList(pager);
55          return asJson(new ApiResult.ApiLogsResponse<EditBody>().logs(list.stream().map(this::createEditBody).collect(Collectors.toList()))
56                  .total(pager.getAllRecordCount()).status(ApiResult.Status.OK).result());
57      }
58  
59      // GET /api/admin/joblog/log/{id}
60      @Execute
61      public JsonResponse<ApiResult> get$log(final String id) {
62          return asJson(new ApiLogResponse().log(jobLogService.getJobLog(id).map(this::createEditBody).orElseGet(() -> {
63              throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id));
64              return null;
65          })).status(Status.OK).result());
66      }
67  
68      // DELETE /api/admin/joblog/log/{id}
69      @Execute
70      public JsonResponse<ApiResult> delete$log(final String id) {
71          jobLogService.getJobLog(id).ifPresent(entity -> {
72              try {
73                  jobLogService.delete(entity);
74                  saveInfo(messages -> messages.addSuccessCrudDeleteCrudTable(GLOBAL));
75              } catch (final Exception e) {
76                  throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToDeleteCrudTable(GLOBAL, buildThrowableMessage(e)));
77              }
78          }).orElse(() -> {
79              throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id));
80          });
81          return asJson(new ApiResponse().status(Status.OK).result());
82      }
83  
84      protected EditBody createEditBody(final JobLog entity) {
85          final EditBody body = new EditBody();
86          copyBeanToBean(entity, body, copyOp -> {
87              copyOp.excludeNull();
88          });
89          return body;
90      }
91  }