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.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>()
56                  .logs(list.stream().map(entity -> createEditBody(entity)).collect(Collectors.toList())).total(pager.getAllRecordCount())
57                  .status(ApiResult.Status.OK).result());
58      }
59  
60      // GET /api/admin/joblog/log/{id}
61      @Execute
62      public JsonResponse<ApiResult> get$log(final String id) {
63          return asJson(new ApiLogResponse().log(jobLogService.getJobLog(id).map(entity -> createEditBody(entity)).orElseGet(() -> {
64              throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id));
65              return null;
66          })).status(Status.OK).result());
67      }
68  
69      // DELETE /api/admin/joblog/log/{id}
70      @Execute
71      public JsonResponse<ApiResult> delete$log(final String id) {
72          jobLogService.getJobLog(id).ifPresent(entity -> {
73              try {
74                  jobLogService.delete(entity);
75                  saveInfo(messages -> messages.addSuccessCrudDeleteCrudTable(GLOBAL));
76              } catch (final Exception e) {
77                  throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToDeleteCrudTable(GLOBAL, buildThrowableMessage(e)));
78              }
79          }).orElse(() -> {
80              throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id));
81          });
82          return asJson(new ApiResponse().status(Status.OK).result());
83      }
84  
85      protected EditBody createEditBody(final JobLog entity) {
86          final EditBody body = new EditBody();
87          copyBeanToBean(entity, body, copyOp -> {
88              copyOp.excludeNull();
89          });
90          return body;
91      }
92  }