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.reqheader;
17  
18  import static org.codelibs.fess.app.web.admin.reqheader.AdminReqheaderAction.getRequestHeader;
19  
20  import java.util.List;
21  import java.util.stream.Collectors;
22  
23  import javax.annotation.Resource;
24  
25  import org.codelibs.fess.app.pager.ReqHeaderPager;
26  import org.codelibs.fess.app.service.RequestHeaderService;
27  import org.codelibs.fess.app.service.WebConfigService;
28  import org.codelibs.fess.app.web.CrudMode;
29  import org.codelibs.fess.app.web.api.ApiResult;
30  import org.codelibs.fess.app.web.api.ApiResult.ApiConfigResponse;
31  import org.codelibs.fess.app.web.api.ApiResult.ApiErrorResponse;
32  import org.codelibs.fess.app.web.api.ApiResult.ApiResponse;
33  import org.codelibs.fess.app.web.api.ApiResult.ApiUpdateResponse;
34  import org.codelibs.fess.app.web.api.ApiResult.Status;
35  import org.codelibs.fess.app.web.api.admin.FessApiAdminAction;
36  import org.codelibs.fess.es.config.exentity.RequestHeader;
37  import org.lastaflute.web.Execute;
38  import org.lastaflute.web.response.JsonResponse;
39  
40  /**
41   * @author Keiichi Watanabe
42   */
43  public class ApiAdminReqheaderAction extends FessApiAdminAction {
44  
45      // ===================================================================================
46      //                                                                           Attribute
47      //                                                                           =========
48      @Resource
49      private RequestHeaderService reqHeaderService;
50      @Resource
51      private WebConfigService webConfigService;
52  
53      // ===================================================================================
54      //                                                                      Search Execute
55      //                                                                      ==============
56  
57      // GET /api/admin/reqheader/settings
58      // POST /api/admin/reqheader/settings
59      @Execute
60      public JsonResponse<ApiResult> settings(final SearchBody body) {
61          validateApi(body, messages -> {});
62          final ReqHeaderPager pager = copyBeanToNewBean(body, ReqHeaderPager.class);
63          final List<RequestHeader> list = reqHeaderService.getRequestHeaderList(pager);
64          return asJson(
65                  new ApiResult.ApiConfigsResponse<EditBody>().settings(list.stream().map(this::createEditBody).collect(Collectors.toList()))
66                          .total(pager.getAllRecordCount()).status(ApiResult.Status.OK).result());
67      }
68  
69      // GET /api/admin/reqheader/setting/{id}
70      @Execute
71      public JsonResponse<ApiResult> get$setting(final String id) {
72          return asJson(new ApiConfigResponse().setting(reqHeaderService.getRequestHeader(id).map(this::createEditBody).orElseGet(() -> {
73              throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id));
74              return null;
75          })).status(Status.OK).result());
76      }
77  
78      // PUT /api/admin/reqheader/setting
79      @Execute
80      public JsonResponse<ApiResult> put$setting(final CreateBody body) {
81          validateApi(body, messages -> {});
82          if (!isValidWebConfigId(body.webConfigId)) {
83              return asJson(new ApiErrorResponse().message("invalid webConfigId").status(Status.BAD_REQUEST).result());
84          }
85  
86          body.crudMode = CrudMode.CREATE;
87          final RequestHeader reqHeader = getRequestHeader(body).map(entity -> {
88              try {
89                  reqHeaderService.store(entity);
90              } catch (final Exception e) {
91                  throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToCreateCrudTable(GLOBAL, buildThrowableMessage(e)));
92              }
93              return entity;
94          }).orElseGet(() -> {
95              throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToCreateInstance(GLOBAL));
96              return null;
97          });
98  
99          return asJson(new ApiUpdateResponse().id(reqHeader.getId()).created(true).status(Status.OK).result());
100     }
101 
102     // POST /api/admin/reqheader/setting
103     @Execute
104     public JsonResponse<ApiResult> post$setting(final EditBody body) {
105         validateApi(body, messages -> {});
106         body.crudMode = CrudMode.EDIT;
107         final RequestHeader reqHeader = getRequestHeader(body).map(entity -> {
108             try {
109                 reqHeaderService.store(entity);
110             } catch (final Exception e) {
111                 throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToUpdateCrudTable(GLOBAL, buildThrowableMessage(e)));
112             }
113             return entity;
114         }).orElseGet(() -> {
115             throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, body.id));
116             return null;
117         });
118         return asJson(new ApiUpdateResponse().id(reqHeader.getId()).created(false).status(Status.OK).result());
119     }
120 
121     // DELETE /api/admin/reqheader/setting/{id}
122     @Execute
123     public JsonResponse<ApiResult> delete$setting(final String id) {
124         reqHeaderService.getRequestHeader(id).ifPresent(entity -> {
125             try {
126                 reqHeaderService.delete(entity);
127                 saveInfo(messages -> messages.addSuccessCrudDeleteCrudTable(GLOBAL));
128             } catch (final Exception e) {
129                 throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToDeleteCrudTable(GLOBAL, buildThrowableMessage(e)));
130             }
131         }).orElse(() -> {
132             throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id));
133         });
134         return asJson(new ApiResponse().status(Status.OK).result());
135     }
136 
137     protected EditBody createEditBody(final RequestHeader entity) {
138         final EditBody body = new EditBody();
139         copyBeanToBean(entity, body, copyOp -> {
140             copyOp.excludeNull();
141         });
142         return body;
143     }
144 
145     protected Boolean isValidWebConfigId(final String webconfigId) {
146         return webConfigService.getWebConfig(webconfigId).isPresent();
147     }
148 }