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