1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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(kuromojiService.getKuromojiList(body.dictId, pager).stream()
52 .map(protwordsItem -> createEditBody(protwordsItem, dictId)).collect(Collectors.toList()))
53 .status(ApiResult.Status.OK).result());
54 }
55
56
57 @Execute
58 public JsonResponse<ApiResult> get$setting(final String dictId, final long id) {
59 return asJson(new ApiResult.ApiConfigResponse()
60 .setting(kuromojiService.getKuromojiItem(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
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 KuromojiItem entity = createKuromojiItem(this, body, () -> {
73 throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToCreateInstance(GLOBAL));
74 return null;
75 }).orElseGet(() -> {
76 throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToCreateInstance(GLOBAL));
77 return null;
78 });
79 kuromojiService.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
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 KuromojiItem entity = createKuromojiItem(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 kuromojiService.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
103 @Execute
104 public JsonResponse<ApiResult> delete$setting(final String dictId, final long id) {
105 kuromojiService.getKuromojiItem(dictId, id).ifPresent(entity -> {
106 kuromojiService.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
115 @Execute
116 public JsonResponse<ApiResult> post$upload(final String dictId, final UploadForm form) {
117 form.dictId = dictId;
118 validateApi(form, messages -> {});
119 final KuromojiFile file = kuromojiService.getKuromojiFile(form.dictId).orElseGet(() -> {
120 throwValidationErrorApi(messages -> messages.addErrorsFailedToUploadProtwordsFile(GLOBAL));
121 return null;
122 });
123 try (InputStream inputStream = form.kuromojiFile.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
132 @Execute
133 public StreamResponse get$download(final String dictId, final DownloadBody body) {
134 body.dictId = dictId;
135 validateApi(body, messages -> {});
136 return kuromojiService.getKuromojiFile(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 KuromojiItem entity, final String dictId) {
146 final EditBody body = new EditBody();
147 body.id = entity.getId();
148 body.dictId = dictId;
149 body.token = entity.getToken();
150 body.reading = entity.getReading();
151 body.pos = entity.getPos();
152 body.segmentation = entity.getSegmentation();
153 return body;
154 }
155 }