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.group;
17  
18  import static org.codelibs.fess.app.web.admin.group.AdminGroupAction.getGroup;
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.GroupPager;
26  import org.codelibs.fess.app.service.GroupService;
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.Group;
31  import org.codelibs.fess.es.user.exentity.User;
32  import org.lastaflute.web.Execute;
33  import org.lastaflute.web.response.JsonResponse;
34  
35  public class ApiAdminGroupAction extends FessApiAdminAction {
36  
37      @Resource
38      private GroupService groupService;
39  
40      // GET /api/admin/group
41      // POST /api/admin/group
42      @Execute
43      public JsonResponse<ApiResult> settings(final SearchBody body) {
44          validateApi(body, messages -> {});
45          final GroupPager pager = copyBeanToNewBean(body, GroupPager.class);
46          final List<Group> list = groupService.getGroupList(pager);
47          return asJson(new ApiResult.ApiConfigsResponse<EditBody>()
48                  .settings(list.stream().map(entity -> createEditBody(entity)).collect(Collectors.toList()))
49                  .total(pager.getAllRecordCount()).status(ApiResult.Status.OK).result());
50      }
51  
52      // GET /api/admin/group/setting/{id}
53      @Execute
54      public JsonResponse<ApiResult> get$setting(final String id) {
55          return asJson(new ApiResult.ApiConfigResponse()
56                  .setting(groupService.getGroup(id).map(entity -> createEditBody(entity)).orElseGet(() -> {
57                      throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id));
58                      return null;
59                  })).status(ApiResult.Status.OK).result());
60      }
61  
62      // PUT /api/admin/group/setting
63      @Execute
64      public JsonResponse<ApiResult> put$setting(final CreateBody body) {
65          validateApi(body, messages -> {});
66          body.crudMode = CrudMode.CREATE;
67          final Group entity = getGroup(body).orElseGet(() -> {
68              throwValidationErrorApi(messages -> {
69                  messages.addErrorsCrudFailedToCreateInstance(GLOBAL);
70              });
71              return null;
72          });
73          try {
74              groupService.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/group/setting
83      @Execute
84      public JsonResponse<ApiResult> post$setting(final EditBody body) {
85          validateApi(body, messages -> {});
86          body.crudMode = CrudMode.EDIT;
87          final Group entity = getGroup(body).orElseGet(() -> {
88              throwValidationErrorApi(messages -> {
89                  messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, body.id);
90              });
91              return null;
92          });
93          try {
94              groupService.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/group/setting/{id}
102     @Execute
103     public JsonResponse<ApiResult> delete$setting(final String id) {
104         final Group entity = groupService.getGroup(id).orElseGet(() -> {
105             throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id));
106             return null;
107         });
108         getUserBean().ifPresent(u -> {
109             if (u.getFessUser() instanceof User && entity.getName().equals(u.getUserId())) {
110                 throwValidationErrorApi(messages -> messages.addErrorsCouldNotDeleteLoggedInUser(GLOBAL));
111             }
112         });
113         try {
114             groupService.delete(entity);
115             saveInfo(messages -> messages.addSuccessCrudDeleteCrudTable(GLOBAL));
116         } catch (final Exception e) {
117             throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToDeleteCrudTable(GLOBAL, buildThrowableMessage(e)));
118         }
119         return asJson(new ApiResult.ApiUpdateResponse().id(id).created(false).status(ApiResult.Status.OK).result());
120     }
121 
122     protected EditBody createEditBody(final Group entity) {
123         final EditBody body = new EditBody();
124         copyBeanToBean(entity, body, copyOp -> {
125             copyOp.excludeNull();
126         });
127         return body;
128     }
129 }