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.relatedquery;
17  
18  import static org.codelibs.core.stream.StreamUtil.stream;
19  import static org.codelibs.fess.app.web.admin.relatedquery.AdminRelatedqueryAction.getRelatedQuery;
20  
21  import java.util.List;
22  import java.util.stream.Collectors;
23  
24  import javax.annotation.Resource;
25  
26  import org.codelibs.core.lang.StringUtil;
27  import org.codelibs.fess.Constants;
28  import org.codelibs.fess.app.pager.RelatedQueryPager;
29  import org.codelibs.fess.app.service.RelatedQueryService;
30  import org.codelibs.fess.app.web.CrudMode;
31  import org.codelibs.fess.app.web.api.ApiResult;
32  import org.codelibs.fess.app.web.api.ApiResult.ApiConfigResponse;
33  import org.codelibs.fess.app.web.api.ApiResult.ApiResponse;
34  import org.codelibs.fess.app.web.api.ApiResult.ApiUpdateResponse;
35  import org.codelibs.fess.app.web.api.ApiResult.Status;
36  import org.codelibs.fess.app.web.api.admin.FessApiAdminAction;
37  import org.codelibs.fess.es.config.exentity.RelatedQuery;
38  import org.lastaflute.web.Execute;
39  import org.lastaflute.web.response.JsonResponse;
40  
41  public class ApiAdminRelatedqueryAction extends FessApiAdminAction {
42  
43      // ===================================================================================
44      //                                                                           Attribute
45      //                                                                           =========
46      @Resource
47      private RelatedQueryService relatedQueryService;
48  
49      // ===================================================================================
50      //                                                                      Search Execute
51      //                                                                      ==============
52  
53      // GET /api/admin/relatedquery/settings
54      // POST /api/admin/relatedquery/settings
55      @Execute
56      public JsonResponse<ApiResult> settings(final SearchBody body) {
57          validateApi(body, messages -> {});
58          final RelatedQueryPager pager = copyBeanToNewBean(body, RelatedQueryPager.class);
59          final List<RelatedQuery> list = relatedQueryService.getRelatedQueryList(pager);
60          return asJson(new ApiResult.ApiConfigsResponse<EditBody>()
61                  .settings(list.stream().map(entity -> createEditBody(entity)).collect(Collectors.toList()))
62                  .total(pager.getAllRecordCount()).status(ApiResult.Status.OK).result());
63      }
64  
65      // GET /api/admin/relatedquery/setting/{id}
66      @Execute
67      public JsonResponse<ApiResult> get$setting(final String id) {
68          return asJson(new ApiConfigResponse()
69                  .setting(relatedQueryService.getRelatedQuery(id).map(entity -> createEditBody(entity)).orElseGet(() -> {
70                      throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id));
71                      return null;
72                  })).status(Status.OK).result());
73      }
74  
75      // PUT /api/admin/relatedquery/setting
76      @Execute
77      public JsonResponse<ApiResult> put$setting(final CreateBody body) {
78          validateApi(body, messages -> {});
79          body.crudMode = CrudMode.CREATE;
80          final RelatedQuery relatedQuery = getRelatedQuery(body).map(entity -> {
81              try {
82                  relatedQueryService.store(entity);
83              } catch (final Exception e) {
84                  throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToCreateCrudTable(GLOBAL, buildThrowableMessage(e)));
85              }
86              return entity;
87          }).orElseGet(() -> {
88              throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToCreateInstance(GLOBAL));
89              return null;
90          });
91  
92          return asJson(new ApiUpdateResponse().id(relatedQuery.getId()).created(true).status(Status.OK).result());
93      }
94  
95      // POST /api/admin/relatedquery/setting
96      @Execute
97      public JsonResponse<ApiResult> post$setting(final EditBody body) {
98          validateApi(body, messages -> {});
99          body.crudMode = CrudMode.EDIT;
100         final RelatedQuery relatedQuery = getRelatedQuery(body).map(entity -> {
101             try {
102                 relatedQueryService.store(entity);
103             } catch (final Exception e) {
104                 throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToUpdateCrudTable(GLOBAL, buildThrowableMessage(e)));
105             }
106             return entity;
107         }).orElseGet(() -> {
108             throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, body.id));
109             return null;
110         });
111         return asJson(new ApiUpdateResponse().id(relatedQuery.getId()).created(false).status(Status.OK).result());
112     }
113 
114     // DELETE /api/admin/relatedquery/setting/{id}
115     @Execute
116     public JsonResponse<ApiResult> delete$setting(final String id) {
117         relatedQueryService.getRelatedQuery(id).ifPresent(entity -> {
118             try {
119                 relatedQueryService.delete(entity);
120                 saveInfo(messages -> messages.addSuccessCrudDeleteCrudTable(GLOBAL));
121             } catch (final Exception e) {
122                 throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToDeleteCrudTable(GLOBAL, buildThrowableMessage(e)));
123             }
124         }).orElse(() -> {
125             throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id));
126         });
127         return asJson(new ApiResponse().status(Status.OK).result());
128     }
129 
130     protected EditBody createEditBody(final RelatedQuery entity) {
131         final EditBody body = new EditBody();
132         copyBeanToBean(entity, body, copyOp -> {
133             copyOp.excludeNull();
134             copyOp.exclude(Constants.QUERIES);
135         });
136         body.queries = stream(entity.getQueries()).get(stream -> stream.filter(StringUtil::isNotBlank).collect(Collectors.joining("\n")));
137         return body;
138     }
139 }