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.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(new ApiResult.ApiConfigsResponse<EditBody>()
47                  .settings(list.stream().map(entity -> createEditBody(entity)).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()
55                  .setting(roleService.getRole(id).map(entity -> createEditBody(entity)).orElseGet(() -> {
56                      throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id));
57                      return null;
58                  })).status(ApiResult.Status.OK).result());
59      }
60  
61      // PUT /api/admin/role/setting
62      @Execute
63      public JsonResponse<ApiResult> put$setting(final CreateBody body) {
64          validateApi(body, messages -> {});
65          body.crudMode = CrudMode.CREATE;
66          final Role entity = getRole(body).orElseGet(() -> {
67              throwValidationErrorApi(messages -> {
68                  messages.addErrorsCrudFailedToCreateInstance(GLOBAL);
69              });
70              return null;
71          });
72          try {
73              roleService.store(entity);
74          } catch (final Exception e) {
75              throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToCreateCrudTable(GLOBAL, buildThrowableMessage(e)));
76          }
77          return asJson(new ApiResult.ApiUpdateResponse().id(entity.getId()).created(true).status(ApiResult.Status.OK).result());
78      }
79  
80      // POST /api/admin/role/setting
81      @Execute
82      public JsonResponse<ApiResult> post$setting(final EditBody body) {
83          validateApi(body, messages -> {});
84          body.crudMode = CrudMode.EDIT;
85          final Role entity = getRole(body).orElseGet(() -> {
86              throwValidationErrorApi(messages -> {
87                  messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, body.id);
88              });
89              return null;
90          });
91          try {
92              roleService.store(entity);
93          } catch (final Exception e) {
94              throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToUpdateCrudTable(GLOBAL, buildThrowableMessage(e)));
95          }
96          return asJson(new ApiResult.ApiUpdateResponse().id(entity.getId()).created(false).status(ApiResult.Status.OK).result());
97      }
98  
99      // DELETE /api/admin/role/setting/{id}
100     @Execute
101     public JsonResponse<ApiResult> delete$setting(final String id) {
102         final Role entity = roleService.getRole(id).orElseGet(() -> {
103             throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id));
104             return null;
105         });
106         try {
107             roleService.delete(entity);
108         } catch (final Exception e) {
109             throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToDeleteCrudTable(GLOBAL, buildThrowableMessage(e)));
110         }
111         return asJson(new ApiResult.ApiUpdateResponse().id(id).created(false).status(ApiResult.Status.OK).result());
112     }
113 
114     protected EditBody createEditBody(final Role entity) {
115         final EditBody body = new EditBody();
116         copyBeanToBean(entity, body, copyOp -> {
117             copyOp.excludeNull();
118         });
119         return body;
120     }
121 }