1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.codelibs.fess.app.web.admin.dict.stopwords;
17
18 import java.io.File;
19 import java.io.IOException;
20 import java.io.InputStream;
21
22 import javax.annotation.Resource;
23
24 import org.codelibs.core.beans.util.BeanUtil;
25 import org.codelibs.core.lang.StringUtil;
26 import org.codelibs.fess.Constants;
27 import org.codelibs.fess.app.pager.StopwordsPager;
28 import org.codelibs.fess.app.service.StopwordsService;
29 import org.codelibs.fess.app.web.CrudMode;
30 import org.codelibs.fess.app.web.admin.dict.AdminDictAction;
31 import org.codelibs.fess.app.web.base.FessAdminAction;
32 import org.codelibs.fess.dict.stopwords.StopwordsItem;
33 import org.codelibs.fess.util.ComponentUtil;
34 import org.codelibs.fess.util.RenderDataUtil;
35 import org.dbflute.optional.OptionalEntity;
36 import org.dbflute.optional.OptionalThing;
37 import org.lastaflute.web.Execute;
38 import org.lastaflute.web.response.ActionResponse;
39 import org.lastaflute.web.response.HtmlResponse;
40 import org.lastaflute.web.response.render.RenderData;
41 import org.lastaflute.web.ruts.process.ActionRuntime;
42 import org.lastaflute.web.validation.VaErrorHook;
43
44
45
46
47 public class AdminDictStopwordsAction extends FessAdminAction {
48
49
50
51
52 @Resource
53 private StopwordsService stopwordsService;
54 @Resource
55 private StopwordsPager stopwordsPager;
56
57
58
59
60 @Override
61 protected void setupHtmlData(final ActionRuntime runtime) {
62 super.setupHtmlData(runtime);
63 runtime.registerData("helpLink", systemHelper.getHelpLink(fessConfig.getOnlineHelpNameDictStopwords()));
64 }
65
66
67
68
69 @Execute
70 public HtmlResponse index(final SearchForm form) {
71 validate(form, messages -> {}, () -> asDictIndexHtml());
72 stopwordsPager.clear();
73 return asHtml(path_AdminDictStopwords_AdminDictStopwordsJsp).renderWith(data -> {
74 searchPaging(data, form);
75 });
76 }
77
78 @Execute
79 public HtmlResponse list(final OptionalThing<Integer> pageNumber, final SearchForm form) {
80 validate(form, messages -> {}, () -> asDictIndexHtml());
81 pageNumber.ifPresent(num -> {
82 stopwordsPager.setCurrentPageNumber(pageNumber.get());
83 }).orElse(() -> {
84 stopwordsPager.setCurrentPageNumber(0);
85 });
86 return asHtml(path_AdminDictStopwords_AdminDictStopwordsJsp).renderWith(data -> {
87 searchPaging(data, form);
88 });
89 }
90
91 @Execute
92 public HtmlResponse search(final SearchForm form) {
93 validate(form, messages -> {}, () -> asDictIndexHtml());
94 copyBeanToBean(form, stopwordsPager, op -> op.exclude(Constants.PAGER_CONVERSION_RULE));
95 return asHtml(path_AdminDictStopwords_AdminDictStopwordsJsp).renderWith(data -> {
96 searchPaging(data, form);
97 });
98 }
99
100 @Execute
101 public HtmlResponse reset(final SearchForm form) {
102 validate(form, messages -> {}, () -> asDictIndexHtml());
103 stopwordsPager.clear();
104 return asHtml(path_AdminDictStopwords_AdminDictStopwordsJsp).renderWith(data -> {
105 searchPaging(data, form);
106 });
107 }
108
109 protected void searchPaging(final RenderData data, final SearchForm form) {
110
111 RenderDataUtil.register(data, "stopwordsItemItems", stopwordsService.getStopwordsList(form.dictId, stopwordsPager));
112
113
114 BeanUtil.copyBeanToBean(stopwordsPager, form, op -> {
115 op.exclude(Constants.PAGER_CONVERSION_RULE);
116 });
117 }
118
119
120
121
122
123
124
125 @Execute
126 public HtmlResponse createnew(final String dictId) {
127 saveToken();
128 return asHtml(path_AdminDictStopwords_AdminDictStopwordsEditJsp).useForm(CreateForm.class, op -> {
129 op.setup(form -> {
130 form.initialize();
131 form.crudMode = CrudMode.CREATE;
132 form.dictId = dictId;
133 });
134 });
135 }
136
137 @Execute
138 public HtmlResponse edit(final EditForm form) {
139 validate(form, messages -> {}, () -> asListHtml(form.dictId));
140 stopwordsService
141 .getStopwordsItem(form.dictId, form.id)
142 .ifPresent(entity -> {
143 form.input = entity.getInputValue();
144 })
145 .orElse(() -> {
146 throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, form.getDisplayId()),
147 () -> asListHtml(form.dictId));
148 });
149 saveToken();
150 if (form.crudMode.intValue() == CrudMode.EDIT) {
151
152 form.crudMode = CrudMode.DETAILS;
153 return asDetailsHtml();
154 } else {
155 form.crudMode = CrudMode.EDIT;
156 return asEditHtml();
157 }
158 }
159
160
161
162
163 @Execute
164 public HtmlResponse details(final String dictId, final int crudMode, final long id) {
165 verifyCrudMode(crudMode, CrudMode.DETAILS, dictId);
166 saveToken();
167 return asDetailsHtml().useForm(
168 EditForm.class,
169 op -> {
170 op.setup(form -> {
171 stopwordsService
172 .getStopwordsItem(dictId, id)
173 .ifPresent(entity -> {
174 form.input = entity.getInputValue();
175 })
176 .orElse(() -> {
177 throwValidationError(
178 messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, dictId + ":" + id),
179 () -> asListHtml(dictId));
180 });
181 form.id = id;
182 form.crudMode = crudMode;
183 form.dictId = dictId;
184 });
185 });
186 }
187
188
189
190
191 @Execute
192 public HtmlResponse downloadpage(final String dictId) {
193 saveToken();
194 return asHtml(path_AdminDictStopwords_AdminDictStopwordsDownloadJsp).useForm(DownloadForm.class, op -> {
195 op.setup(form -> {
196 form.dictId = dictId;
197 });
198 }).renderWith(data -> {
199 stopwordsService.getStopwordsFile(dictId).ifPresent(file -> {
200 RenderDataUtil.register(data, "path", file.getPath());
201 }).orElse(() -> {
202 throwValidationError(messages -> messages.addErrorsFailedToDownloadStopwordsFile(GLOBAL), () -> asDictIndexHtml());
203 });
204 });
205 }
206
207 @Execute
208 public ActionResponse download(final DownloadForm form) {
209 validate(form, messages -> {}, () -> downloadpage(form.dictId));
210 verifyTokenKeep(() -> downloadpage(form.dictId));
211 return stopwordsService.getStopwordsFile(form.dictId).map(file -> {
212 return asStream(new File(file.getPath()).getName()).contentTypeOctetStream().stream(out -> {
213 file.writeOut(out);
214 });
215 }).orElseGet(() -> {
216 throwValidationError(messages -> messages.addErrorsFailedToDownloadStopwordsFile(GLOBAL), () -> downloadpage(form.dictId));
217 return null;
218 });
219 }
220
221
222
223
224 @Execute
225 public HtmlResponse uploadpage(final String dictId) {
226 saveToken();
227 return asHtml(path_AdminDictStopwords_AdminDictStopwordsUploadJsp).useForm(UploadForm.class, op -> {
228 op.setup(form -> {
229 form.dictId = dictId;
230 });
231 }).renderWith(data -> {
232 stopwordsService.getStopwordsFile(dictId).ifPresent(file -> {
233 RenderDataUtil.register(data, "path", file.getPath());
234 }).orElse(() -> {
235 throwValidationError(messages -> messages.addErrorsFailedToDownloadStopwordsFile(GLOBAL), () -> asDictIndexHtml());
236 });
237 });
238 }
239
240 @Execute
241 public HtmlResponse upload(final UploadForm form) {
242 validate(form, messages -> {}, () -> uploadpage(form.dictId));
243 verifyToken(() -> uploadpage(form.dictId));
244 return stopwordsService.getStopwordsFile(form.dictId).map(file -> {
245 try (InputStream inputStream = form.stopwordsFile.getInputStream()) {
246 file.update(inputStream);
247 } catch (final IOException e) {
248 throwValidationError(messages -> messages.addErrorsFailedToUploadStopwordsFile(GLOBAL), () -> {
249 return redirectWith(getClass(), moreUrl("uploadpage/" + form.dictId));
250 });
251 }
252 saveInfo(messages -> messages.addSuccessUploadStopwordsFile(GLOBAL));
253 return redirectWith(getClass(), moreUrl("uploadpage/" + form.dictId));
254 }).orElseGet(() -> {
255 throwValidationError(messages -> messages.addErrorsFailedToUploadStopwordsFile(GLOBAL), () -> uploadpage(form.dictId));
256 return null;
257 });
258
259 }
260
261
262
263
264 @Execute
265 public HtmlResponse create(final CreateForm form) {
266 verifyCrudMode(form.crudMode, CrudMode.CREATE, form.dictId);
267 validate(form, messages -> {}, () -> asEditHtml());
268 verifyToken(() -> asEditHtml());
269 createStopwordsItem(form, () -> asEditHtml()).ifPresent(entity -> {
270 stopwordsService.store(form.dictId, entity);
271 saveInfo(messages -> messages.addSuccessCrudCreateCrudTable(GLOBAL));
272 }).orElse(() -> throwValidationError(messages -> messages.addErrorsCrudFailedToCreateInstance(GLOBAL), () -> asEditHtml()));
273 return redirectWith(getClass(), moreUrl("list/1").params("dictId", form.dictId));
274 }
275
276 @Execute
277 public HtmlResponse update(final EditForm form) {
278 verifyCrudMode(form.crudMode, CrudMode.EDIT, form.dictId);
279 validate(form, messages -> {}, () -> asEditHtml());
280 verifyToken(() -> asEditHtml());
281 createStopwordsItem(form, () -> asEditHtml()).ifPresent(entity -> {
282 stopwordsService.store(form.dictId, entity);
283 saveInfo(messages -> messages.addSuccessCrudUpdateCrudTable(GLOBAL));
284 }).orElse(
285 () -> throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, form.getDisplayId()),
286 () -> asEditHtml()));
287 return redirectWith(getClass(), moreUrl("list/1").params("dictId", form.dictId));
288 }
289
290 @Execute
291 public HtmlResponse delete(final EditForm form) {
292 verifyCrudMode(form.crudMode, CrudMode.DETAILS, form.dictId);
293 validate(form, messages -> {}, () -> asDetailsHtml());
294 verifyToken(() -> asDetailsHtml());
295 stopwordsService
296 .getStopwordsItem(form.dictId, form.id)
297 .ifPresent(entity -> {
298 stopwordsService.delete(form.dictId, entity);
299 saveInfo(messages -> messages.addSuccessCrudDeleteCrudTable(GLOBAL));
300 })
301 .orElse(() -> {
302 throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, form.getDisplayId()),
303 () -> asDetailsHtml());
304 });
305 return redirectWith(getClass(), moreUrl("list/1").params("dictId", form.dictId));
306 }
307
308
309
310
311
312 private static OptionalEntity<StopwordsItem> getEntity(final CreateForm form) {
313 switch (form.crudMode) {
314 case CrudMode.CREATE:
315 final StopwordsItemstopwords/StopwordsItem.html#StopwordsItem">StopwordsItem entity = new StopwordsItem(0, StringUtil.EMPTY);
316 return OptionalEntity.of(entity);
317 case CrudMode.EDIT:
318 if (form instanceof EditForm) {
319 return ComponentUtil.getComponent(StopwordsService.class).getStopwordsItem(form.dictId, ((EditForm) form).id);
320 }
321 break;
322 default:
323 break;
324 }
325 return OptionalEntity.empty();
326 }
327
328 public static OptionalEntity<StopwordsItem> createStopwordsItem(final CreateForm form, final VaErrorHook hook) {
329 return getEntity(form).map(entity -> {
330 final String newInput = form.input;
331 validateStopwordsString(newInput, "input", hook);
332 entity.setNewInput(newInput);
333 return entity;
334 });
335 }
336
337
338
339
340 protected void verifyCrudMode(final int crudMode, final int expectedMode, final String dictId) {
341 if (crudMode != expectedMode) {
342 throwValidationError(messages -> {
343 messages.addErrorsCrudInvalidMode(GLOBAL, String.valueOf(expectedMode), String.valueOf(crudMode));
344 }, () -> asListHtml(dictId));
345 }
346 }
347
348 private static void validateStopwordsString(final String values, final String propertyName, final VaErrorHook hook) {
349 if (values.length() == 0) {
350 return;
351 }
352
353 }
354
355
356
357
358
359 protected HtmlResponse asDictIndexHtml() {
360 return redirect(AdminDictAction.class);
361 }
362
363 private HtmlResponse asListHtml(final String dictId) {
364 return asHtml(path_AdminDictStopwords_AdminDictStopwordsJsp).renderWith(data -> {
365 RenderDataUtil.register(data, "stopwordsItemItems", stopwordsService.getStopwordsList(dictId, stopwordsPager));
366 }).useForm(SearchForm.class, setup -> {
367 setup.setup(form -> {
368 copyBeanToBean(stopwordsPager, form, op -> op.include("id"));
369 });
370 });
371 }
372
373 private HtmlResponse asEditHtml() {
374 return asHtml(path_AdminDictStopwords_AdminDictStopwordsEditJsp);
375 }
376
377 private HtmlResponse asDetailsHtml() {
378 return asHtml(path_AdminDictStopwords_AdminDictStopwordsDetailsJsp);
379 }
380
381 }