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.dict.kuromoji;
17  
18  import static org.codelibs.fess.app.web.admin.dict.kuromoji.AdminDictKuromojiAction.createKuromojiItem;
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.KuromojiPager;
28  import org.codelibs.fess.app.service.KuromojiService;
29  import org.codelibs.fess.app.web.CrudMode;
30  import org.codelibs.fess.app.web.admin.dict.kuromoji.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.kuromoji.KuromojiFile;
34  import org.codelibs.fess.dict.kuromoji.KuromojiItem;
35  import org.lastaflute.web.Execute;
36  import org.lastaflute.web.response.JsonResponse;
37  import org.lastaflute.web.response.StreamResponse;
38  
39  public class ApiAdminDictKuromojiAction extends FessApiAdminAction {
40  
41      @Resource
42      private KuromojiService kuromojiService;
43  
44      // GET /api/admin/dict/kuromoji/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 KuromojiPager pager = copyBeanToNewBean(body, KuromojiPager.class);
50          return asJson(new ApiResult.ApiConfigsResponse<EditBody>()
51                  .settings(
52                          kuromojiService.getKuromojiList(body.dictId, pager).stream()
53                                  .map(protwordsItem -> createEditBody(protwordsItem, dictId)).collect(Collectors.toList()))
54                  .status(ApiResult.Status.OK).result());
55      }
56  
57      // GET /api/admin/dict/kuromoji/setting/{dictId}/{id}
58      @Execute
59      public JsonResponse<ApiResult> get$setting(final String dictId, final long id) {
60          return asJson(new ApiResult.ApiConfigResponse()
61                  .setting(kuromojiService.getKuromojiItem(dictId, id).map(entity -> createEditBody(entity, dictId)).orElseGet(() -> {
62                      throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, String.valueOf(id)));
63                      return null;
64                  })).status(ApiResult.Status.OK).result());
65      }
66  
67      // PUT /api/admin/dict/kuromoji/setting/{dictId}
68      @Execute
69      public JsonResponse<ApiResult> put$setting(final String dictId, final CreateBody body) {
70          body.dictId = dictId;
71          validateApi(body, messages -> {});
72          body.crudMode = CrudMode.CREATE;
73          final KuromojiItem entity = createKuromojiItem(body).orElseGet(() -> {
74              throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToCreateInstance(GLOBAL));
75              return null;
76          });
77          kuromojiService.store(body.dictId, entity);
78          return asJson(new ApiResult.ApiUpdateResponse().id(String.valueOf(entity.getId())).created(true).status(ApiResult.Status.OK)
79                  .result());
80      }
81  
82      // POST /api/admin/dict/kuromoji/setting/{dictId}
83      @Execute
84      public JsonResponse<ApiResult> post$setting(final String dictId, final EditBody body) {
85          body.dictId = dictId;
86          validateApi(body, messages -> {});
87          body.crudMode = CrudMode.EDIT;
88          final KuromojiItem entity = createKuromojiItem(body).orElseGet(() -> {
89              throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, String.valueOf(body.id)));
90              return null;
91          });
92          kuromojiService.store(body.dictId, entity);
93          return asJson(new ApiResult.ApiUpdateResponse().id(String.valueOf(entity.getId())).created(false).status(ApiResult.Status.OK)
94                  .result());
95      }
96  
97      // DELETE /api/admin/dict/kuromoji/setting/{dictId}/{id}
98      @Execute
99      public JsonResponse<ApiResult> delete$setting(final String dictId, final long id) {
100         kuromojiService.getKuromojiItem(dictId, id).ifPresent(entity -> {
101             kuromojiService.delete(dictId, entity);
102             saveInfo(messages -> messages.addSuccessCrudDeleteCrudTable(GLOBAL));
103         }).orElse(() -> {
104             throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, String.valueOf(id)));
105         });
106         return asJson(new ApiResult.ApiUpdateResponse().id(String.valueOf(id)).created(false).status(ApiResult.Status.OK).result());
107     }
108 
109     // POST /api/admin/dict/kuromoji/upload/{dictId}
110     @Execute
111     public JsonResponse<ApiResult> post$upload(final String dictId, final UploadForm form) {
112         form.dictId = dictId;
113         validateApi(form, messages -> {});
114         final KuromojiFile file = kuromojiService.getKuromojiFile(form.dictId).orElseGet(() -> {
115             throwValidationErrorApi(messages -> messages.addErrorsFailedToUploadProtwordsFile(GLOBAL));
116             return null;
117         });
118         try (InputStream inputStream = form.kuromojiFile.getInputStream()) {
119             file.update(inputStream);
120         } catch (final IOException e) {
121             throwValidationErrorApi(messages -> messages.addErrorsFailedToUploadProtwordsFile(GLOBAL));
122         }
123         return asJson(new ApiResult.ApiResponse().status(ApiResult.Status.OK).result());
124     }
125 
126     // GET /api/admin/dict/kuromoji/download/{dictId}
127     @Execute
128     public StreamResponse get$download(final String dictId, final DownloadBody body) {
129         body.dictId = dictId;
130         validateApi(body, messages -> {});
131         return kuromojiService.getKuromojiFile(body.dictId).map(file -> {
132             return asStream(new File(file.getPath()).getName()).contentTypeOctetStream().stream(out -> {
133                 try (InputStream inputStream = file.getInputStream()) {
134                     out.write(inputStream);
135                 }
136             });
137         }).orElseGet(() -> {
138             throwValidationErrorApi(messages -> messages.addErrorsFailedToDownloadProtwordsFile(GLOBAL));
139             return null;
140         });
141     }
142 
143     protected EditBody createEditBody(final KuromojiItem entity, final String dictId) {
144         final EditBody body = new EditBody();
145         body.id = entity.getId();
146         body.dictId = dictId;
147         body.token = entity.getToken();
148         body.reading = entity.getReading();
149         body.pos = entity.getPos();
150         body.segmentation = entity.getSegmentation();
151         return body;
152     }
153 }