View Javadoc
1   /*
2    * Copyright 2012-2025 CodeLibs Project and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.codelibs.fess.app.web.admin.joblog;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import org.codelibs.fess.Constants;
22  import org.codelibs.fess.annotation.Secured;
23  import org.codelibs.fess.app.pager.JobLogPager;
24  import org.codelibs.fess.app.service.JobLogService;
25  import org.codelibs.fess.app.web.CrudMode;
26  import org.codelibs.fess.app.web.base.FessAdminAction;
27  import org.codelibs.fess.util.RenderDataUtil;
28  import org.lastaflute.web.Execute;
29  import org.lastaflute.web.response.HtmlResponse;
30  import org.lastaflute.web.response.render.RenderData;
31  import org.lastaflute.web.ruts.process.ActionRuntime;
32  
33  import jakarta.annotation.Resource;
34  
35  /**
36   * Admin action for Job Log.
37   *
38   */
39  public class AdminJoblogAction extends FessAdminAction {
40  
41      /**
42       * Default constructor.
43       */
44      public AdminJoblogAction() {
45          super();
46      }
47  
48      /** The role name for job log administration. */
49      public static final String ROLE = "admin-joblog";
50  
51      // ===================================================================================
52      //                                                                           Attribute
53      //                                                                           =========
54      /** Service for job log operations. */
55      @Resource
56      private JobLogService jobLogService;
57  
58      /** Pager for job log list pagination. */
59      @Resource
60      private JobLogPager jobLogPager;
61  
62      // ===================================================================================
63      //                                                                               Hook
64      //                                                                              ======
65      /**
66       * Sets up HTML data for rendering, including help link.
67       *
68       * @param runtime the action runtime
69       */
70      @Override
71      protected void setupHtmlData(final ActionRuntime runtime) {
72          super.setupHtmlData(runtime);
73          runtime.registerData("helpLink", systemHelper.getHelpLink(fessConfig.getOnlineHelpNameJoblog()));
74      }
75  
76      /**
77       * Returns the action role for this admin action.
78       *
79       * @return the role name
80       */
81      @Override
82      protected String getActionRole() {
83          return ROLE;
84      }
85  
86      // ===================================================================================
87      //                                                                      Search Execute
88      //                                                                      ==============
89      /**
90       * Displays the job log list page.
91       *
92       * @param form the search form
93       * @return HTML response for the list page
94       */
95      @Execute
96      @Secured({ ROLE, ROLE + VIEW })
97      public HtmlResponse index(final SearchForm form) {
98          saveToken();
99          return asListHtml();
100     }
101 
102     /**
103      * Displays the job log list with pagination.
104      *
105      * @param pageNumber the page number
106      * @param form the search form
107      * @return HTML response for the list page
108      */
109     @Execute
110     @Secured({ ROLE, ROLE + VIEW })
111     public HtmlResponse list(final Integer pageNumber, final SearchForm form) {
112         saveToken();
113         jobLogPager.setCurrentPageNumber(pageNumber);
114         return asHtml(path_AdminJoblog_AdminJoblogJsp).renderWith(data -> {
115             searchPaging(data, form);
116         });
117     }
118 
119     /**
120      * Searches job logs based on the form criteria.
121      *
122      * @param form the search form
123      * @return HTML response for the search results
124      */
125     @Execute
126     @Secured({ ROLE, ROLE + VIEW })
127     public HtmlResponse search(final SearchForm form) {
128         saveToken();
129         copyBeanToBean(form, jobLogPager, op -> op.exclude(Constants.PAGER_CONVERSION_RULE));
130         return asHtml(path_AdminJoblog_AdminJoblogJsp).renderWith(data -> {
131             searchPaging(data, form);
132         });
133     }
134 
135     /**
136      * Resets the search criteria and displays the default list.
137      *
138      * @param form the search form
139      * @return HTML response for the reset list
140      */
141     @Execute
142     @Secured({ ROLE, ROLE + VIEW })
143     public HtmlResponse reset(final SearchForm form) {
144         saveToken();
145         jobLogPager.clear();
146         return asHtml(path_AdminJoblog_AdminJoblogJsp).renderWith(data -> {
147             searchPaging(data, form);
148         });
149     }
150 
151     /**
152      * Returns to the job log list page.
153      *
154      * @param form the search form
155      * @return HTML response for the list page
156      */
157     @Execute
158     @Secured({ ROLE, ROLE + VIEW })
159     public HtmlResponse back(final SearchForm form) {
160         saveToken();
161         return asHtml(path_AdminJoblog_AdminJoblogJsp).renderWith(data -> {
162             searchPaging(data, form);
163         });
164     }
165 
166     /**
167      * Sets up data for search result pagination.
168      *
169      * @param data the render data
170      * @param form the search form
171      */
172     protected void searchPaging(final RenderData data, final SearchForm form) {
173         RenderDataUtil.register(data, "jobLogItems", jobLogService.getJobLogList(jobLogPager)); // page navi
174 
175         // restore from pager
176         copyBeanToBean(jobLogPager, form, op -> op.include("id"));
177     }
178 
179     // ===================================================================================
180     //                                                                        Edit Execute
181     //                                                                        ============
182     // -----------------------------------------------------
183     //                                            Entry Page
184     //                                            ----------
185 
186     // -----------------------------------------------------
187     //                                               Details
188     //                                               -------
189     /**
190      * Displays the job log details page.
191      *
192      * @param crudMode the CRUD mode
193      * @param id the job log ID
194      * @return HTML response for the details page
195      */
196     @Execute
197     @Secured({ ROLE, ROLE + VIEW })
198     public HtmlResponse details(final int crudMode, final String id) {
199         verifyCrudMode(crudMode, CrudMode.DETAILS, this::asListHtml);
200         saveToken();
201         return asHtml(path_AdminJoblog_AdminJoblogDetailsJsp).useForm(EditForm.class, op -> {
202             op.setup(form -> {
203                 jobLogService.getJobLog(id).ifPresent(entity -> {
204                     copyBeanToBean(entity, form, copyOp -> {
205                         copyOp.excludeNull();
206                     });
207                     form.crudMode = crudMode;
208                 }).orElse(() -> {
209                     throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id), this::asListHtml);
210                 });
211             });
212         });
213     }
214 
215     // -----------------------------------------------------
216     //                                         Actually Crud
217     //                                         -------------
218     /**
219      * Deletes a job log.
220      *
221      * @param form the edit form
222      * @return HTML response after deletion
223      */
224     @Execute
225     @Secured({ ROLE })
226     public HtmlResponse delete(final EditForm form) {
227         verifyCrudMode(form.crudMode, CrudMode.DETAILS, this::asListHtml);
228         validate(form, messages -> {}, this::asDetailsHtml);
229         verifyToken(this::asDetailsHtml);
230         final String id = form.id;
231         jobLogService.getJobLog(id).alwaysPresent(entity -> {
232             jobLogService.delete(entity);
233             saveInfo(messages -> messages.addSuccessCrudDeleteCrudTable(GLOBAL));
234         });
235         return redirect(getClass());
236     }
237 
238     /**
239      * Deletes all job logs.
240      *
241      * @return HTML response after deletion
242      */
243     @Execute
244     @Secured({ ROLE })
245     public HtmlResponse deleteall() {
246         verifyToken(this::asListHtml);
247         final List<String> jobStatusList = new ArrayList<>();
248         jobStatusList.add(Constants.OK);
249         jobStatusList.add(Constants.FAIL);
250         jobLogService.deleteByJobStatus(jobStatusList);
251         jobLogPager.clear();
252         saveInfo(messages -> messages.addSuccessJobLogDeleteAll(GLOBAL));
253         return redirect(getClass());
254     }
255 
256     // ===================================================================================
257     //                                                                        Assist Logic
258     //                                                                        ============
259 
260     // ===================================================================================
261     //                                                                        Small Helper
262     //                                                                        ============
263     //                                                                              JSP
264     //                                                                           =========
265 
266     /**
267      * Returns HTML response for the list page.
268      *
269      * @return HTML response for the list page
270      */
271     private HtmlResponse asListHtml() {
272         return asHtml(path_AdminJoblog_AdminJoblogJsp).renderWith(data -> {
273             RenderDataUtil.register(data, "jobLogItems", jobLogService.getJobLogList(jobLogPager)); // page navi
274         }).useForm(SearchForm.class, setup -> {
275             setup.setup(form -> {
276                 copyBeanToBean(jobLogPager, form, op -> op.include("id"));
277             });
278         });
279     }
280 
281     /**
282      * Returns HTML response for the details page.
283      *
284      * @return HTML response for the details page
285      */
286     private HtmlResponse asDetailsHtml() {
287         return asHtml(path_AdminJoblog_AdminJoblogDetailsJsp);
288     }
289 }