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.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(new ApiResult.ApiConfigsResponse<EditBody>()
65                  .settings(list.stream().map(entity -> createEditBody(entity)).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()
73                  .setting(reqHeaderService.getRequestHeader(id).map(entity -> createEditBody(entity)).orElseGet(() -> {
74                      throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id));
75                      return null;
76                  })).status(Status.OK).result());
77      }
78  
79      // PUT /api/admin/reqheader/setting
80      @Execute
81      public JsonResponse<ApiResult> put$setting(final CreateBody body) {
82          validateApi(body, messages -> {});
83          if (!isValidWebConfigId(body.webConfigId)) {
84              return asJson(new ApiErrorResponse().message("invalid webConfigId").status(Status.BAD_REQUEST).result());
85          }
86  
87          body.crudMode = CrudMode.CREATE;
88          final RequestHeader reqHeader = getRequestHeader(body).map(entity -> {
89              try {
90                  reqHeaderService.store(entity);
91              } catch (final Exception e) {
92                  throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToCreateCrudTable(GLOBAL, buildThrowableMessage(e)));
93              }
94              return entity;
95          }).orElseGet(() -> {
96              throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToCreateInstance(GLOBAL));
97              return null;
98          });
99  
100         return asJson(new ApiUpdateResponse().id(reqHeader.getId()).created(true).status(Status.OK).result());
101     }
102 
103     // POST /api/admin/reqheader/setting
104     @Execute
105     public JsonResponse<ApiResult> post$setting(final EditBody body) {
106         validateApi(body, messages -> {});
107         body.crudMode = CrudMode.EDIT;
108         final RequestHeader reqHeader = getRequestHeader(body).map(entity -> {
109             try {
110                 reqHeaderService.store(entity);
111             } catch (final Exception e) {
112                 throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToUpdateCrudTable(GLOBAL, buildThrowableMessage(e)));
113             }
114             return entity;
115         }).orElseGet(() -> {
116             throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, body.id));
117             return null;
118         });
119         return asJson(new ApiUpdateResponse().id(reqHeader.getId()).created(false).status(Status.OK).result());
120     }
121 
122     // DELETE /api/admin/reqheader/setting/{id}
123     @Execute
124     public JsonResponse<ApiResult> delete$setting(final String id) {
125         reqHeaderService.getRequestHeader(id).ifPresent(entity -> {
126             try {
127                 reqHeaderService.delete(entity);
128                 saveInfo(messages -> messages.addSuccessCrudDeleteCrudTable(GLOBAL));
129             } catch (final Exception e) {
130                 throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToDeleteCrudTable(GLOBAL, buildThrowableMessage(e)));
131             }
132         }).orElse(() -> {
133             throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id));
134         });
135         return asJson(new ApiResponse().status(Status.OK).result());
136     }
137 
138     protected EditBody createEditBody(final RequestHeader entity) {
139         final EditBody body = new EditBody();
140         copyBeanToBean(entity, body, copyOp -> {
141             copyOp.excludeNull();
142         });
143         return body;
144     }
145 
146     protected Boolean isValidWebConfigId(final String webconfigId) {
147         return webConfigService.getWebConfig(webconfigId).isPresent();
148     }
149 }