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.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>()
62                  .logs(list.stream().map(entity -> createEditBody(entity)).collect(Collectors.toList())).total(pager.getAllRecordCount())
63                  .status(ApiResult.Status.OK).result());
64      }
65  
66      // GET /api/admin/failureurl/log/{id}
67      @Execute
68      public JsonResponse<ApiResult> get$log(final String id) {
69          return asJson(new ApiLogResponse().log(failureUrlService.getFailureUrl(id).map(entity -> createEditBody(entity)).orElseGet(() -> {
70              throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id));
71              return null;
72          })).status(Status.OK).result());
73      }
74  
75      // DELETE /api/admin/failureurl/log/{id}
76      @Execute
77      public JsonResponse<ApiResult> delete$log(final String id) {
78          failureUrlService.getFailureUrl(id).ifPresent(entity -> {
79              try {
80                  failureUrlService.delete(entity);
81                  saveInfo(messages -> messages.addSuccessCrudDeleteCrudTable(GLOBAL));
82              } catch (final Exception e) {
83                  throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToDeleteCrudTable(GLOBAL, buildThrowableMessage(e)));
84              }
85          }).orElse(() -> {
86              throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id));
87          });
88          return asJson(new ApiResponse().status(Status.OK).result());
89      }
90  
91      // DELETE /api/admin/failureurl/all
92      @Execute
93      public JsonResponse<ApiResult> delete$all() {
94          try {
95              failureUrlService.deleteAll(failureUrlPager);
96              failureUrlPager.clear();
97              saveInfo(messages -> messages.addSuccessFailureUrlDeleteAll(GLOBAL));
98          } catch (final Exception e) {
99              throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToDeleteCrudTable(GLOBAL, buildThrowableMessage(e)));
100         }
101         return asJson(new ApiResponse().status(Status.OK).result());
102     }
103 
104     protected EditBody createEditBody(final FailureUrl entity) {
105         final EditBody body = new EditBody();
106         copyBeanToBean(entity, body, copyOp -> {
107             copyOp.excludeNull();
108         });
109         return body;
110     }
111 }