1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.codelibs.fess.app.web.admin.keymatch;
17
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.Map;
21
22 import javax.annotation.Resource;
23
24 import org.codelibs.fess.Constants;
25 import org.codelibs.fess.annotation.Secured;
26 import org.codelibs.fess.app.pager.KeyMatchPager;
27 import org.codelibs.fess.app.service.KeyMatchService;
28 import org.codelibs.fess.app.web.CrudMode;
29 import org.codelibs.fess.app.web.base.FessAdminAction;
30 import org.codelibs.fess.es.config.exentity.KeyMatch;
31 import org.codelibs.fess.helper.KeyMatchHelper;
32 import org.codelibs.fess.helper.SystemHelper;
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.HtmlResponse;
39 import org.lastaflute.web.response.render.RenderData;
40 import org.lastaflute.web.ruts.process.ActionRuntime;
41
42
43
44
45
46 public class AdminKeymatchAction extends FessAdminAction {
47
48 public static final String ROLE = "admin-keymatch";
49
50
51
52
53 @Resource
54 private KeyMatchHelper keyMatchHelper;
55 @Resource
56 private KeyMatchService keyMatchService;
57 @Resource
58 private KeyMatchPager keyMatchPager;
59
60
61
62
63 @Override
64 protected void setupHtmlData(final ActionRuntime runtime) {
65 super.setupHtmlData(runtime);
66 runtime.registerData("helpLink", systemHelper.getHelpLink(fessConfig.getOnlineHelpNameKeymatch()));
67 }
68
69 @Override
70 protected String getActionRole() {
71 return ROLE;
72 }
73
74
75
76
77 @Execute
78 @Secured({ ROLE, ROLE + VIEW })
79 public HtmlResponse index(final SearchForm form) {
80 return asListHtml();
81 }
82
83 @Execute
84 @Secured({ ROLE, ROLE + VIEW })
85 public HtmlResponse list(final OptionalThing<Integer> pageNumber, final SearchForm form) {
86 pageNumber.ifPresent(num -> {
87 keyMatchPager.setCurrentPageNumber(pageNumber.get());
88 }).orElse(() -> {
89 keyMatchPager.setCurrentPageNumber(0);
90 });
91 return asHtml(path_AdminKeymatch_AdminKeymatchJsp).renderWith(data -> {
92 searchPaging(data, form);
93 });
94 }
95
96 @Execute
97 @Secured({ ROLE, ROLE + VIEW })
98 public HtmlResponse search(final SearchForm form) {
99 copyBeanToBean(form, keyMatchPager, op -> op.exclude(Constants.PAGER_CONVERSION_RULE));
100 return asHtml(path_AdminKeymatch_AdminKeymatchJsp).renderWith(data -> {
101 searchPaging(data, form);
102 });
103 }
104
105 @Execute
106 @Secured({ ROLE, ROLE + VIEW })
107 public HtmlResponse reset(final SearchForm form) {
108 keyMatchPager.clear();
109 return asHtml(path_AdminKeymatch_AdminKeymatchJsp).renderWith(data -> {
110 searchPaging(data, form);
111 });
112 }
113
114 protected void searchPaging(final RenderData data, final SearchForm form) {
115 RenderDataUtil.register(data, "keyMatchItems", keyMatchService.getKeyMatchList(keyMatchPager));
116
117
118 copyBeanToBean(keyMatchPager, form, op -> op.include("term", "query"));
119 }
120
121
122
123
124
125
126
127 @Execute
128 @Secured({ ROLE })
129 public HtmlResponse createnew() {
130 saveToken();
131 return asHtml(path_AdminKeymatch_AdminKeymatchEditJsp).useForm(CreateForm.class, op -> {
132 op.setup(form -> {
133 form.initialize();
134 form.crudMode = CrudMode.CREATE;
135 });
136 });
137 }
138
139 @Execute
140 @Secured({ ROLE })
141 public HtmlResponse edit(final EditForm form) {
142 validate(form, messages -> {}, this::asListHtml);
143 final String id = form.id;
144 keyMatchService.getKeyMatch(id).ifPresent(entity -> {
145 copyBeanToBean(entity, form, op -> {});
146 }).orElse(() -> {
147 throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id), this::asListHtml);
148 });
149 saveToken();
150 if (form.crudMode.intValue() == CrudMode.EDIT) {
151
152 form.crudMode = CrudMode.DETAILS;
153 return asDetailsHtml();
154 }
155 form.crudMode = CrudMode.EDIT;
156 return asEditHtml();
157 }
158
159
160
161
162 @Execute
163 @Secured({ ROLE, ROLE + VIEW })
164 public HtmlResponse details(final int crudMode, final String id) {
165 verifyCrudMode(crudMode, CrudMode.DETAILS);
166 saveToken();
167 final List<Map<String, Object>> docList = new ArrayList<>();
168 return asHtml(path_AdminKeymatch_AdminKeymatchDetailsJsp).useForm(EditForm.class, op -> {
169 op.setup(form -> {
170 keyMatchService.getKeyMatch(id).ifPresent(entity -> {
171 copyBeanToBean(entity, form, copyOp -> {
172 copyOp.excludeNull();
173 });
174 form.crudMode = crudMode;
175 docList.addAll(keyMatchHelper.getBoostedDocumentList(entity));
176 }).orElse(() -> {
177 throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id), this::asListHtml);
178 });
179 });
180 }).renderWith(data -> {
181 data.register("docs", docList);
182 });
183 }
184
185
186
187
188 @Execute
189 @Secured({ ROLE })
190 public HtmlResponse create(final CreateForm form) {
191 verifyCrudMode(form.crudMode, CrudMode.CREATE);
192 validate(form, messages -> {}, this::asEditHtml);
193 verifyToken(this::asEditHtml);
194 getKeyMatch(form).ifPresent(entity -> {
195 try {
196 keyMatchService.store(entity);
197 saveInfo(messages -> messages.addSuccessCrudCreateCrudTable(GLOBAL));
198 ComponentUtil.getKeyMatchHelper().update();
199 } catch (final Exception e) {
200 throwValidationError(messages -> messages.addErrorsCrudFailedToCreateCrudTable(GLOBAL, buildThrowableMessage(e)),
201 this::asEditHtml);
202 }
203 }).orElse(() -> {
204 throwValidationError(messages -> messages.addErrorsCrudFailedToCreateInstance(GLOBAL), this::asEditHtml);
205 });
206 return redirect(getClass());
207 }
208
209 @Execute
210 @Secured({ ROLE })
211 public HtmlResponse update(final EditForm form) {
212 verifyCrudMode(form.crudMode, CrudMode.EDIT);
213 validate(form, messages -> {}, this::asEditHtml);
214 verifyToken(this::asEditHtml);
215 getKeyMatch(form).ifPresent(entity -> {
216 try {
217 keyMatchService.store(entity);
218 saveInfo(messages -> messages.addSuccessCrudUpdateCrudTable(GLOBAL));
219 ComponentUtil.getKeyMatchHelper().update();
220 } catch (final Exception e) {
221 throwValidationError(messages -> messages.addErrorsCrudFailedToUpdateCrudTable(GLOBAL, buildThrowableMessage(e)),
222 this::asEditHtml);
223 }
224 }).orElse(() -> {
225 throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, form.id), this::asEditHtml);
226 });
227 return redirect(getClass());
228 }
229
230 @Execute
231 @Secured({ ROLE })
232 public HtmlResponse delete(final EditForm form) {
233 verifyCrudMode(form.crudMode, CrudMode.DETAILS);
234 validate(form, messages -> {}, this::asDetailsHtml);
235 verifyToken(this::asDetailsHtml);
236 final String id = form.id;
237 keyMatchService.getKeyMatch(id).ifPresent(entity -> {
238 try {
239 keyMatchService.delete(entity);
240 saveInfo(messages -> messages.addSuccessCrudDeleteCrudTable(GLOBAL));
241 ComponentUtil.getKeyMatchHelper().update();
242 } catch (final Exception e) {
243 throwValidationError(messages -> messages.addErrorsCrudFailedToDeleteCrudTable(GLOBAL, buildThrowableMessage(e)),
244 this::asEditHtml);
245 }
246 }).orElse(() -> {
247 throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id), this::asDetailsHtml);
248 });
249 return redirect(getClass());
250 }
251
252
253
254
255
256 public static OptionalEntity<KeyMatch> getEntity(final CreateForm form, final String username, final long currentTime) {
257 switch (form.crudMode) {
258 case CrudMode.CREATE:
259 return OptionalEntity.of(new KeyMatch()).map(entity -> {
260 entity.setCreatedBy(username);
261 entity.setCreatedTime(currentTime);
262 return entity;
263 });
264 case CrudMode.EDIT:
265 if (form instanceof EditForm) {
266 return ComponentUtil.getComponent(KeyMatchService.class).getKeyMatch(((EditForm) form).id);
267 }
268 break;
269 default:
270 break;
271 }
272 return OptionalEntity.empty();
273 }
274
275 public static OptionalEntity<KeyMatch> getKeyMatch(final CreateForm form) {
276 final SystemHelper systemHelper = ComponentUtil.getSystemHelper();
277 final String username = systemHelper.getUsername();
278 final long currentTime = systemHelper.getCurrentTimeAsLong();
279 return getEntity(form, username, currentTime).map(entity -> {
280 entity.setUpdatedBy(username);
281 entity.setUpdatedTime(currentTime);
282 copyBeanToBean(form, entity, op -> op.exclude(Constants.COMMON_CONVERSION_RULE));
283 return entity;
284 });
285 }
286
287
288
289
290 protected void verifyCrudMode(final int crudMode, final int expectedMode) {
291 if (crudMode != expectedMode) {
292 throwValidationError(messages -> {
293 messages.addErrorsCrudInvalidMode(GLOBAL, String.valueOf(expectedMode), String.valueOf(crudMode));
294 }, this::asListHtml);
295 }
296 }
297
298
299
300
301
302 private HtmlResponse asListHtml() {
303 return asHtml(path_AdminKeymatch_AdminKeymatchJsp).renderWith(data -> {
304 RenderDataUtil.register(data, "keyMatchItems", keyMatchService.getKeyMatchList(keyMatchPager));
305 }).useForm(SearchForm.class, setup -> {
306 setup.setup(form -> {
307 copyBeanToBean(keyMatchPager, form, op -> op.include("term", "query"));
308 });
309 });
310 }
311
312 private HtmlResponse asEditHtml() {
313 return asHtml(path_AdminKeymatch_AdminKeymatchEditJsp);
314 }
315
316 private HtmlResponse asDetailsHtml() {
317 return asHtml(path_AdminKeymatch_AdminKeymatchDetailsJsp);
318 }
319 }