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