1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.codelibs.fess.app.web.admin.dict.mapping;
17
18 import java.io.File;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.util.ArrayList;
22 import java.util.List;
23
24 import javax.annotation.Resource;
25
26 import org.codelibs.core.beans.util.BeanUtil;
27 import org.codelibs.core.lang.StringUtil;
28 import org.codelibs.fess.Constants;
29 import org.codelibs.fess.app.pager.CharMappingPager;
30 import org.codelibs.fess.app.service.CharMappingService;
31 import org.codelibs.fess.app.web.CrudMode;
32 import org.codelibs.fess.app.web.admin.dict.AdminDictAction;
33 import org.codelibs.fess.app.web.base.FessAdminAction;
34 import org.codelibs.fess.app.web.base.FessBaseAction;
35 import org.codelibs.fess.dict.mapping.CharMappingItem;
36 import org.codelibs.fess.util.ComponentUtil;
37 import org.codelibs.fess.util.RenderDataUtil;
38 import org.dbflute.optional.OptionalEntity;
39 import org.dbflute.optional.OptionalThing;
40 import org.lastaflute.web.Execute;
41 import org.lastaflute.web.response.ActionResponse;
42 import org.lastaflute.web.response.HtmlResponse;
43 import org.lastaflute.web.response.render.RenderData;
44 import org.lastaflute.web.ruts.process.ActionRuntime;
45 import org.lastaflute.web.validation.VaErrorHook;
46 import org.lastaflute.web.validation.exception.ValidationErrorException;
47
48
49
50
51
52 public class AdminDictMappingAction extends FessAdminAction {
53
54
55
56
57 @Resource
58 private CharMappingService charMappingService;
59 @Resource
60 private CharMappingPager charMappingPager;
61
62
63
64
65 @Override
66 protected void setupHtmlData(final ActionRuntime runtime) {
67 super.setupHtmlData(runtime);
68 runtime.registerData("helpLink", systemHelper.getHelpLink(fessConfig.getOnlineHelpNameDictMapping()));
69 }
70
71
72
73
74 @Execute
75 public HtmlResponse index(final SearchForm form) {
76 validate(form, messages -> {}, () -> asDictIndexHtml());
77 charMappingPager.clear();
78 return asHtml(path_AdminDictMapping_AdminDictMappingJsp).renderWith(data -> {
79 searchPaging(data, form);
80 });
81 }
82
83 @Execute
84 public HtmlResponse list(final OptionalThing<Integer> pageNumber, final SearchForm form) {
85 validate(form, messages -> {}, () -> asDictIndexHtml());
86 pageNumber.ifPresent(num -> {
87 charMappingPager.setCurrentPageNumber(pageNumber.get());
88 }).orElse(() -> {
89 charMappingPager.setCurrentPageNumber(0);
90 });
91 return asHtml(path_AdminDictMapping_AdminDictMappingJsp).renderWith(data -> {
92 searchPaging(data, form);
93 });
94 }
95
96 @Execute
97 public HtmlResponse search(final SearchForm form) {
98 validate(form, messages -> {}, () -> asDictIndexHtml());
99 copyBeanToBean(form, charMappingPager, op -> op.exclude(Constants.PAGER_CONVERSION_RULE));
100 return asHtml(path_AdminDictMapping_AdminDictMappingJsp).renderWith(data -> {
101 searchPaging(data, form);
102 });
103 }
104
105 @Execute
106 public HtmlResponse reset(final SearchForm form) {
107 validate(form, messages -> {}, () -> asDictIndexHtml());
108 charMappingPager.clear();
109 return asHtml(path_AdminDictMapping_AdminDictMappingJsp).renderWith(data -> {
110 searchPaging(data, form);
111 });
112 }
113
114 protected void searchPaging(final RenderData data, final SearchForm form) {
115
116 RenderDataUtil.register(data, "charMappingItemItems", charMappingService.getCharMappingList(form.dictId, charMappingPager));
117
118
119 BeanUtil.copyBeanToBean(charMappingPager, form, op -> {
120 op.exclude(Constants.PAGER_CONVERSION_RULE);
121 });
122 }
123
124
125
126
127
128
129
130 @Execute
131 public HtmlResponse createnew(final String dictId) {
132 saveToken();
133 return asHtml(path_AdminDictMapping_AdminDictMappingEditJsp).useForm(CreateForm.class, op -> {
134 op.setup(form -> {
135 form.initialize();
136 form.crudMode = CrudMode.CREATE;
137 form.dictId = dictId;
138 });
139 });
140 }
141
142 @Execute
143 public HtmlResponse edit(final EditForm form) {
144 validate(form, messages -> {}, () -> asListHtml(form.dictId));
145 charMappingService
146 .getCharMappingItem(form.dictId, form.id)
147 .ifPresent(entity -> {
148 form.inputs = entity.getInputsValue();
149 form.output = entity.getOutput();
150 })
151 .orElse(() -> {
152 throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, form.getDisplayId()),
153 () -> asListHtml(form.dictId));
154 });
155 saveToken();
156 if (form.crudMode.intValue() == CrudMode.EDIT) {
157
158 form.crudMode = CrudMode.DETAILS;
159 return asDetailsHtml();
160 } else {
161 form.crudMode = CrudMode.EDIT;
162 return asEditHtml();
163 }
164 }
165
166
167
168
169 @Execute
170 public HtmlResponse details(final String dictId, final int crudMode, final long id) {
171 verifyCrudMode(crudMode, CrudMode.DETAILS, dictId);
172 saveToken();
173 return asDetailsHtml().useForm(
174 EditForm.class,
175 op -> {
176 op.setup(form -> {
177 charMappingService
178 .getCharMappingItem(dictId, id)
179 .ifPresent(entity -> {
180 form.inputs = entity.getInputsValue();
181 form.output = entity.getOutput();
182 })
183 .orElse(() -> {
184 throwValidationError(
185 messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, dictId + ":" + id),
186 () -> asListHtml(dictId));
187 });
188 form.id = id;
189 form.crudMode = crudMode;
190 form.dictId = dictId;
191 });
192 });
193 }
194
195
196
197
198 @Execute
199 public HtmlResponse downloadpage(final String dictId) {
200 saveToken();
201 return asHtml(path_AdminDictMapping_AdminDictMappingDownloadJsp).useForm(DownloadForm.class, op -> {
202 op.setup(form -> {
203 form.dictId = dictId;
204 });
205 }).renderWith(data -> {
206 charMappingService.getCharMappingFile(dictId).ifPresent(file -> {
207 RenderDataUtil.register(data, "path", file.getPath());
208 }).orElse(() -> {
209 throwValidationError(messages -> messages.addErrorsFailedToDownloadMappingFile(GLOBAL), () -> asDictIndexHtml());
210 });
211 });
212 }
213
214 @Execute
215 public ActionResponse download(final DownloadForm form) {
216 validate(form, messages -> {}, () -> downloadpage(form.dictId));
217 verifyTokenKeep(() -> downloadpage(form.dictId));
218 return charMappingService.getCharMappingFile(form.dictId).map(file -> {
219 return asStream(new File(file.getPath()).getName()).contentTypeOctetStream().stream(out -> {
220 file.writeOut(out);
221 });
222 }).orElseGet(() -> {
223 throwValidationError(messages -> messages.addErrorsFailedToDownloadMappingFile(GLOBAL), () -> downloadpage(form.dictId));
224 return null;
225 });
226 }
227
228
229
230
231 @Execute
232 public HtmlResponse uploadpage(final String dictId) {
233 saveToken();
234 return asHtml(path_AdminDictMapping_AdminDictMappingUploadJsp).useForm(UploadForm.class, op -> {
235 op.setup(form -> {
236 form.dictId = dictId;
237 });
238 }).renderWith(data -> {
239 charMappingService.getCharMappingFile(dictId).ifPresent(file -> {
240 RenderDataUtil.register(data, "path", file.getPath());
241 }).orElse(() -> {
242 throwValidationError(messages -> messages.addErrorsFailedToDownloadMappingFile(GLOBAL), () -> asDictIndexHtml());
243 });
244 });
245 }
246
247 @Execute
248 public HtmlResponse upload(final UploadForm form) {
249 validate(form, messages -> {}, () -> uploadpage(form.dictId));
250 verifyToken(() -> uploadpage(form.dictId));
251 return charMappingService.getCharMappingFile(form.dictId).map(file -> {
252 try (InputStream inputStream = form.charMappingFile.getInputStream()) {
253 file.update(inputStream);
254 } catch (final IOException e) {
255 throwValidationError(messages -> messages.addErrorsFailedToUploadMappingFile(GLOBAL), () -> {
256 return redirectWith(getClass(), moreUrl("uploadpage/" + form.dictId));
257 });
258 }
259 saveInfo(messages -> messages.addSuccessUploadMappingFile(GLOBAL));
260 return redirectWith(getClass(), moreUrl("uploadpage/" + form.dictId));
261 }).orElseGet(() -> {
262 throwValidationError(messages -> messages.addErrorsFailedToUploadMappingFile(GLOBAL), () -> uploadpage(form.dictId));
263 return null;
264 });
265
266 }
267
268
269
270
271 @Execute
272 public HtmlResponse create(final CreateForm form) {
273 verifyCrudMode(form.crudMode, CrudMode.CREATE, form.dictId);
274 validate(form, messages -> {}, () -> asEditHtml());
275 verifyToken(() -> asEditHtml());
276 createCharMappingItem(form, () -> asEditHtml()).ifPresent(
277 entity -> {
278 try {
279 charMappingService.store(form.dictId, entity);
280 saveInfo(messages -> messages.addSuccessCrudCreateCrudTable(GLOBAL));
281 } catch (final Exception e) {
282 throwValidationError(messages -> messages.addErrorsCrudFailedToCreateCrudTable(GLOBAL, buildThrowableMessage(e)),
283 () -> asEditHtml());
284 }
285 }).orElse(() -> {
286 throwValidationError(messages -> messages.addErrorsCrudFailedToCreateInstance(GLOBAL), () -> asEditHtml());
287 });
288 return redirectWith(getClass(), moreUrl("list/1").params("dictId", form.dictId));
289 }
290
291 @Execute
292 public HtmlResponse update(final EditForm form) {
293 verifyCrudMode(form.crudMode, CrudMode.EDIT, form.dictId);
294 validate(form, messages -> {}, () -> asEditHtml());
295 verifyToken(() -> asEditHtml());
296 createCharMappingItem(form, () -> asEditHtml()).ifPresent(
297 entity -> {
298 try {
299 charMappingService.store(form.dictId, entity);
300 saveInfo(messages -> messages.addSuccessCrudUpdateCrudTable(GLOBAL));
301 } catch (final Exception e) {
302 throwValidationError(messages -> messages.addErrorsCrudFailedToUpdateCrudTable(GLOBAL, buildThrowableMessage(e)),
303 () -> asEditHtml());
304 }
305 }).orElse(() -> {
306 throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, form.getDisplayId()), () -> asEditHtml());
307 });
308 return redirectWith(getClass(), moreUrl("list/1").params("dictId", form.dictId));
309 }
310
311 @Execute
312 public HtmlResponse delete(final EditForm form) {
313 verifyCrudMode(form.crudMode, CrudMode.DETAILS, form.dictId);
314 validate(form, messages -> {}, () -> asDetailsHtml());
315 verifyToken(() -> asDetailsHtml());
316 charMappingService
317 .getCharMappingItem(form.dictId, form.id)
318 .ifPresent(
319 entity -> {
320 try {
321 charMappingService.delete(form.dictId, entity);
322 saveInfo(messages -> messages.addSuccessCrudDeleteCrudTable(GLOBAL));
323 } catch (final Exception e) {
324 throwValidationError(
325 messages -> messages.addErrorsCrudFailedToDeleteCrudTable(GLOBAL, buildThrowableMessage(e)),
326 () -> asEditHtml());
327 }
328 })
329 .orElse(() -> {
330 throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, form.getDisplayId()),
331 () -> asDetailsHtml());
332 });
333 return redirectWith(getClass(), moreUrl("list/1").params("dictId", form.dictId));
334 }
335
336
337
338
339
340 private static OptionalEntity<CharMappingItem> getEntity(final CreateForm form) {
341 switch (form.crudMode) {
342 case CrudMode.CREATE:
343 final CharMappingItempping/CharMappingItem.html#CharMappingItem">CharMappingItem entity = new CharMappingItem(0, StringUtil.EMPTY_STRINGS, StringUtil.EMPTY);
344 return OptionalEntity.of(entity);
345 case CrudMode.EDIT:
346 if (form instanceof EditForm) {
347 return ComponentUtil.getComponent(CharMappingService.class).getCharMappingItem(form.dictId, ((EditForm) form).id);
348 }
349 break;
350 default:
351 break;
352 }
353 return OptionalEntity.empty();
354 }
355
356 protected OptionalEntity<CharMappingItem> createCharMappingItem(final CreateForm form, final VaErrorHook hook) {
357 try {
358 return createCharMappingItem(this, form, hook);
359 } catch (final ValidationErrorException e) {
360 saveToken();
361 throw e;
362 }
363 }
364
365 public static OptionalEntity<CharMappingItem> createCharMappingItem(final FessBaseAction action, final CreateForm form,
366 final VaErrorHook hook) {
367 return getEntity(form).map(entity -> {
368 final String[] newInputs = splitLine(form.inputs);
369 validateMappingString(action, newInputs, "inputs", hook);
370 entity.setNewInputs(newInputs);
371 final String newOutput = form.output;
372 entity.setNewOutput(newOutput);
373 return entity;
374 });
375 }
376
377
378
379
380 protected void verifyCrudMode(final int crudMode, final int expectedMode, final String dictId) {
381 if (crudMode != expectedMode) {
382 throwValidationError(messages -> {
383 messages.addErrorsCrudInvalidMode(GLOBAL, String.valueOf(expectedMode), String.valueOf(crudMode));
384 }, () -> asListHtml(dictId));
385 }
386 }
387
388 private static void validateMappingString(final FessBaseAction action, final String[] values, final String propertyName,
389 final VaErrorHook hook) {
390 if (values.length == 0) {
391 return;
392 }
393 for (final String value : values) {
394 if (value.indexOf(',') >= 0) {
395 action.throwValidationError(messages -> {
396 messages.addErrorsInvalidStrIsIncluded(propertyName, value, ",");
397 }, hook);
398 }
399 if (value.indexOf("=>") >= 0) {
400 action.throwValidationError(messages -> {
401 messages.addErrorsInvalidStrIsIncluded(propertyName, value, "=>");
402 }, hook);
403 }
404 }
405 }
406
407 private static String[] splitLine(final String value) {
408 if (StringUtil.isBlank(value)) {
409 return StringUtil.EMPTY_STRINGS;
410 }
411 final String[] values = value.split("[\r\n]");
412 final List<String> list = new ArrayList<>(values.length);
413 for (final String line : values) {
414 if (StringUtil.isNotBlank(line)) {
415 list.add(line.trim());
416 }
417 }
418 return list.toArray(new String[list.size()]);
419 }
420
421
422
423
424
425 protected HtmlResponse asDictIndexHtml() {
426 return redirect(AdminDictAction.class);
427 }
428
429 private HtmlResponse asListHtml(final String dictId) {
430 return asHtml(path_AdminDictMapping_AdminDictMappingJsp).renderWith(data -> {
431 RenderDataUtil.register(data, "charMappingItemItems", charMappingService.getCharMappingList(dictId, charMappingPager));
432 }).useForm(SearchForm.class, setup -> {
433 setup.setup(form -> {
434 copyBeanToBean(charMappingPager, form, op -> op.include("id"));
435 });
436 });
437 }
438
439 private HtmlResponse asEditHtml() {
440 return asHtml(path_AdminDictMapping_AdminDictMappingEditJsp);
441 }
442
443 private HtmlResponse asDetailsHtml() {
444 return asHtml(path_AdminDictMapping_AdminDictMappingDetailsJsp);
445 }
446
447 }