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