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.pathmap;
17  
18  import static org.codelibs.fess.app.web.admin.pathmap.AdminPathmapAction.getPathMapping;
19  
20  import java.util.List;
21  import java.util.stream.Collectors;
22  
23  import javax.annotation.Resource;
24  
25  import org.codelibs.fess.Constants;
26  import org.codelibs.fess.app.pager.PathMapPager;
27  import org.codelibs.fess.app.service.PathMappingService;
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.admin.FessApiAdminAction;
31  import org.codelibs.fess.es.config.exentity.PathMapping;
32  import org.lastaflute.web.Execute;
33  import org.lastaflute.web.response.JsonResponse;
34  
35  public class ApiAdminPathmapAction extends FessApiAdminAction {
36  
37      @Resource
38      private PathMappingService pathMappingService;
39  
40      // GET /api/admin/pathmap
41      // POST /api/admin/pathmap
42      @Execute
43      public JsonResponse<ApiResult> settings(final SearchBody body) {
44          validateApi(body, messages -> {});
45          final PathMapPager pager = copyBeanToNewBean(body, PathMapPager.class);
46          final List<PathMapping> list = pathMappingService.getPathMappingList(pager);
47          return asJson(
48                  new ApiResult.ApiConfigsResponse<EditBody>().settings(list.stream().map(this::createEditBody).collect(Collectors.toList()))
49                          .total(pager.getAllRecordCount()).status(ApiResult.Status.OK).result());
50      }
51  
52      // GET /api/admin/pathmap/setting/{id}
53      @Execute
54      public JsonResponse<ApiResult> get$setting(final String id) {
55          return asJson(
56                  new ApiResult.ApiConfigResponse().setting(pathMappingService.getPathMapping(id).map(this::createEditBody).orElseGet(() -> {
57                      throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id));
58                      return null;
59                  })).status(ApiResult.Status.OK).result());
60      }
61  
62      // PUT /api/admin/pathmap/setting
63      @Execute
64      public JsonResponse<ApiResult> put$setting(final CreateBody body) {
65          validateApi(body, messages -> {});
66          body.crudMode = CrudMode.CREATE;
67          final PathMapping entity = getPathMapping(body).orElseGet(() -> {
68              throwValidationErrorApi(messages -> {
69                  messages.addErrorsCrudFailedToCreateInstance(GLOBAL);
70              });
71              return null;
72          });
73          try {
74              pathMappingService.store(entity);
75              saveInfo(messages -> messages.addSuccessCrudCreateCrudTable(GLOBAL));
76          } catch (final Exception e) {
77              throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToCreateCrudTable(GLOBAL, buildThrowableMessage(e)));
78          }
79          return asJson(new ApiResult.ApiUpdateResponse().id(entity.getId()).created(true).status(ApiResult.Status.OK).result());
80      }
81  
82      // POST /api/admin/pathmap/setting
83      @Execute
84      public JsonResponse<ApiResult> post$setting(final EditBody body) {
85          validateApi(body, messages -> {});
86          body.crudMode = CrudMode.EDIT;
87          final PathMapping entity = getPathMapping(body).orElseGet(() -> {
88              throwValidationErrorApi(messages -> {
89                  messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, body.id);
90              });
91              return null;
92          });
93          try {
94              pathMappingService.store(entity);
95          } catch (final Exception e) {
96              throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToUpdateCrudTable(GLOBAL, buildThrowableMessage(e)));
97          }
98          return asJson(new ApiResult.ApiUpdateResponse().id(entity.getId()).created(false).status(ApiResult.Status.OK).result());
99      }
100 
101     // DELETE /api/admin/pathmap/setting/{id}
102     @Execute
103     public JsonResponse<ApiResult> delete$setting(final String id) {
104         final PathMapping entity = pathMappingService.getPathMapping(id).orElseGet(() -> {
105             throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id));
106             return null;
107         });
108         try {
109             pathMappingService.delete(entity);
110             saveInfo(messages -> messages.addSuccessCrudDeleteCrudTable(GLOBAL));
111         } catch (final Exception e) {
112             throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToDeleteCrudTable(GLOBAL, buildThrowableMessage(e)));
113         }
114         return asJson(new ApiResult.ApiUpdateResponse().id(id).created(false).status(ApiResult.Status.OK).result());
115     }
116 
117     protected EditBody createEditBody(final PathMapping entity) {
118         final EditBody body = new EditBody();
119         copyBeanToBean(entity, body, op -> op.exclude(Constants.COMMON_CONVERSION_RULE));
120         return body;
121     }
122 }