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.dict.synonym;
17  
18  import static org.codelibs.fess.app.web.admin.dict.synonym.AdminDictSynonymAction.createSynonymItem;
19  
20  import java.io.File;
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.util.stream.Collectors;
24  
25  import javax.annotation.Resource;
26  
27  import org.codelibs.fess.app.pager.SynonymPager;
28  import org.codelibs.fess.app.service.SynonymService;
29  import org.codelibs.fess.app.web.CrudMode;
30  import org.codelibs.fess.app.web.admin.dict.synonym.UploadForm;
31  import org.codelibs.fess.app.web.api.ApiResult;
32  import org.codelibs.fess.app.web.api.admin.FessApiAdminAction;
33  import org.codelibs.fess.dict.synonym.SynonymFile;
34  import org.codelibs.fess.dict.synonym.SynonymItem;
35  import org.lastaflute.web.Execute;
36  import org.lastaflute.web.response.JsonResponse;
37  import org.lastaflute.web.response.StreamResponse;
38  
39  public class ApiAdminDictSynonymAction extends FessApiAdminAction {
40  
41      @Resource
42      private SynonymService synonymService;
43  
44      // GET /api/admin/dict/synonym/settings/{dictId}
45      @Execute
46      public JsonResponse<ApiResult> get$settings(final String dictId, final SearchBody body) {
47          body.dictId = dictId;
48          validateApi(body, messages -> {});
49          final SynonymPager pager = copyBeanToNewBean(body, SynonymPager.class);
50          return asJson(new ApiResult.ApiConfigsResponse<EditBody>()
51                  .settings(synonymService.getSynonymList(body.dictId, pager).stream()
52                          .map(protwordsItem -> createEditBody(protwordsItem, dictId)).collect(Collectors.toList()))
53                  .status(ApiResult.Status.OK).result());
54      }
55  
56      // GET /api/admin/dict/synonym/setting/{dictId}/{id}
57      @Execute
58      public JsonResponse<ApiResult> get$setting(final String dictId, final long id) {
59          return asJson(new ApiResult.ApiConfigResponse()
60                  .setting(synonymService.getSynonymItem(dictId, id).map(entity -> createEditBody(entity, dictId)).orElseGet(() -> {
61                      throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, String.valueOf(id)));
62                      return null;
63                  })).status(ApiResult.Status.OK).result());
64      }
65  
66      // PUT /api/admin/dict/synonym/setting/{dictId}
67      @Execute
68      public JsonResponse<ApiResult> put$setting(final String dictId, final CreateBody body) {
69          body.dictId = dictId;
70          validateApi(body, messages -> {});
71          body.crudMode = CrudMode.CREATE;
72          final SynonymItem entity = createSynonymItem(this, body, () -> {
73              throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToCreateInstance(GLOBAL));
74              return null;
75          }).orElseGet(() -> {
76              throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToCreateInstance(GLOBAL));
77              return null;
78          });
79          synonymService.store(body.dictId, entity);
80          return asJson(
81                  new ApiResult.ApiUpdateResponse().id(String.valueOf(entity.getId())).created(true).status(ApiResult.Status.OK).result());
82      }
83  
84      // POST /api/admin/dict/synonym/setting/{dictId}
85      @Execute
86      public JsonResponse<ApiResult> post$setting(final String dictId, final EditBody body) {
87          body.dictId = dictId;
88          validateApi(body, messages -> {});
89          body.crudMode = CrudMode.EDIT;
90          final SynonymItem entity = createSynonymItem(this, body, () -> {
91              throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToUpdateCrudTable(GLOBAL, String.valueOf(body.id)));
92              return null;
93          }).orElseGet(() -> {
94              throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, String.valueOf(body.id)));
95              return null;
96          });
97          synonymService.store(body.dictId, entity);
98          return asJson(
99                  new ApiResult.ApiUpdateResponse().id(String.valueOf(entity.getId())).created(false).status(ApiResult.Status.OK).result());
100     }
101 
102     // DELETE /api/admin/dict/synonym/setting/{dictId}/{id}
103     @Execute
104     public JsonResponse<ApiResult> delete$setting(final String dictId, final long id) {
105         synonymService.getSynonymItem(dictId, id).ifPresent(entity -> {
106             synonymService.delete(dictId, entity);
107             saveInfo(messages -> messages.addSuccessCrudDeleteCrudTable(GLOBAL));
108         }).orElse(() -> {
109             throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, String.valueOf(id)));
110         });
111         return asJson(new ApiResult.ApiUpdateResponse().id(String.valueOf(id)).created(false).status(ApiResult.Status.OK).result());
112     }
113 
114     // POST /api/admin/dict/synonym/upload/{dictId}
115     @Execute
116     public JsonResponse<ApiResult> post$upload(final String dictId, final UploadForm form) {
117         form.dictId = dictId;
118         validateApi(form, messages -> {});
119         final SynonymFile file = synonymService.getSynonymFile(form.dictId).orElseGet(() -> {
120             throwValidationErrorApi(messages -> messages.addErrorsFailedToUploadProtwordsFile(GLOBAL));
121             return null;
122         });
123         try (InputStream inputStream = form.synonymFile.getInputStream()) {
124             file.update(inputStream);
125         } catch (final IOException e) {
126             throwValidationErrorApi(messages -> messages.addErrorsFailedToUploadProtwordsFile(GLOBAL));
127         }
128         return asJson(new ApiResult.ApiResponse().status(ApiResult.Status.OK).result());
129     }
130 
131     // GET /api/admin/dict/synonym/download/{dictId}
132     @Execute
133     public StreamResponse get$download(final String dictId, final DownloadBody body) {
134         body.dictId = dictId;
135         validateApi(body, messages -> {});
136         return synonymService.getSynonymFile(body.dictId)
137                 .map(file -> asStream(new File(file.getPath()).getName()).contentTypeOctetStream().stream(out -> {
138                     file.writeOut(out);
139                 })).orElseGet(() -> {
140                     throwValidationErrorApi(messages -> messages.addErrorsFailedToDownloadProtwordsFile(GLOBAL));
141                     return null;
142                 });
143     }
144 
145     protected EditBody createEditBody(final SynonymItem entity, final String dictId) {
146         final EditBody body = new EditBody();
147         body.id = entity.getId();
148         body.dictId = dictId;
149         body.inputs = entity.getInputsValue();
150         body.outputs = entity.getOutputsValue();
151         return body;
152     }
153 }