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.failureurl;
17  
18  import org.codelibs.fess.Constants;
19  import org.codelibs.fess.annotation.Secured;
20  import org.codelibs.fess.app.pager.FailureUrlPager;
21  import org.codelibs.fess.app.service.FailureUrlService;
22  import org.codelibs.fess.app.web.CrudMode;
23  import org.codelibs.fess.app.web.base.FessAdminAction;
24  import org.codelibs.fess.util.RenderDataUtil;
25  import org.lastaflute.web.Execute;
26  import org.lastaflute.web.response.HtmlResponse;
27  import org.lastaflute.web.response.render.RenderData;
28  import org.lastaflute.web.ruts.process.ActionRuntime;
29  
30  import jakarta.annotation.Resource;
31  
32  /**
33   * Admin action for Failure URL management.
34   *
35   */
36  public class AdminFailureurlAction extends FessAdminAction {
37  
38      /**
39       * Default constructor.
40       */
41      public AdminFailureurlAction() {
42          super();
43      }
44  
45      /**
46       * Role name for failure URL administration.
47       */
48      public static final String ROLE = "admin-failureurl";
49  
50      // ===================================================================================
51      //                                                                           Attribute
52      //                                                                           =========
53      @Resource
54      private FailureUrlService failureUrlService;
55      @Resource
56      private FailureUrlPager failureUrlPager;
57  
58      // ===================================================================================
59      //                                                                               Hook
60      //                                                                              ======
61      @Override
62      protected void setupHtmlData(final ActionRuntime runtime) {
63          super.setupHtmlData(runtime);
64          runtime.registerData("helpLink", systemHelper.getHelpLink(fessConfig.getOnlineHelpNameFailureurl()));
65      }
66  
67      @Override
68      protected String getActionRole() {
69          return ROLE;
70      }
71  
72      // ===================================================================================
73      //                                                                      Search Execute
74      //                                                                      ==============
75      /**
76       * Displays the main failure URL management page.
77       *
78       * @return HTML response for the failure URL list page
79       */
80      @Execute
81      @Secured({ ROLE, ROLE + VIEW })
82      public HtmlResponse index() {
83          saveToken();
84          return asListHtml();
85      }
86  
87      /**
88       * Displays the failure URL list page with pagination support.
89       *
90       * @param pageNumber the page number to display
91       * @param form the search form containing filter criteria
92       * @return HTML response for the failure URL list page
93       */
94      @Execute
95      @Secured({ ROLE, ROLE + VIEW })
96      public HtmlResponse list(final Integer pageNumber, final SearchForm form) {
97          failureUrlPager.setCurrentPageNumber(pageNumber);
98          return asHtml(path_AdminFailureurl_AdminFailureurlJsp).renderWith(data -> {
99              searchPaging(data, form);
100         });
101     }
102 
103     /**
104      * Performs a search for failure URLs based on the provided criteria.
105      *
106      * @param form the search form containing filter criteria
107      * @return HTML response for the failure URL list page with search results
108      */
109     @Execute
110     @Secured({ ROLE, ROLE + VIEW })
111     public HtmlResponse search(final SearchForm form) {
112         copyBeanToBean(form, failureUrlPager, op -> op.exclude(Constants.PAGER_CONVERSION_RULE));
113         return asHtml(path_AdminFailureurl_AdminFailureurlJsp).renderWith(data -> {
114             searchPaging(data, form);
115         });
116     }
117 
118     /**
119      * Resets the search criteria and pagination state.
120      *
121      * @param form the search form to reset
122      * @return HTML response for the failure URL list page with cleared search
123      */
124     @Execute
125     @Secured({ ROLE, ROLE + VIEW })
126     public HtmlResponse reset(final SearchForm form) {
127         failureUrlPager.clear();
128         return asHtml(path_AdminFailureurl_AdminFailureurlJsp).renderWith(data -> {
129             searchPaging(data, form);
130         });
131     }
132 
133     /**
134      * Returns to the failure URL list page with the current search form.
135      *
136      * @param form the search form containing current search parameters
137      * @return HTML response for the failure URL list page
138      */
139     @Execute
140     @Secured({ ROLE, ROLE + VIEW })
141     public HtmlResponse back(final SearchForm form) {
142         return asHtml(path_AdminFailureurl_AdminFailureurlJsp).renderWith(data -> {
143             searchPaging(data, form);
144         });
145     }
146 
147     /**
148      * Sets up pagination data and restores search form values from the pager.
149      *
150      * @param data the render data to populate with failure URL items
151      * @param form the search form to restore values into
152      */
153     protected void searchPaging(final RenderData data, final SearchForm form) {
154         RenderDataUtil.register(data, "failureUrlItems", failureUrlService.getFailureUrlList(failureUrlPager)); // page navi
155 
156         // restore from pager
157         copyBeanToBean(failureUrlPager, form, op -> op.include("url", "errorCountMin", "errorCountMax", "errorName"));
158     }
159 
160     // -----------------------------------------------------
161     //                                               Details
162     //                                               -------
163     /**
164      * Displays the details of a specific failure URL record.
165      *
166      * @param crudMode the CRUD operation mode (should be DETAILS)
167      * @param id the ID of the failure URL record to display
168      * @return HTML response for the failure URL details page
169      */
170     @Execute
171     @Secured({ ROLE, ROLE + VIEW })
172     public HtmlResponse details(final int crudMode, final String id) {
173         verifyCrudMode(crudMode, CrudMode.DETAILS, this::asListHtml);
174         return asHtml(path_AdminFailureurl_AdminFailureurlDetailsJsp).useForm(EditForm.class, op -> {
175             op.setup(form -> {
176                 failureUrlService.getFailureUrl(id).ifPresent(entity -> {
177                     copyBeanToBean(entity, form, copyOp -> {
178                         copyOp.excludeNull();
179                     });
180                     form.crudMode = crudMode;
181                 }).orElse(() -> {
182                     throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id), this::asListHtml);
183                 });
184             });
185         });
186     }
187 
188     // -----------------------------------------------------
189     //                            Actually Crud (only Delete)
190     //                                         -------------
191 
192     /**
193      * Deletes a specific failure URL record.
194      *
195      * @param form the edit form containing the ID of the failure URL to delete
196      * @return HTML response redirecting to the failure URL list page
197      */
198     @Execute
199     @Secured({ ROLE })
200     public HtmlResponse delete(final EditForm form) {
201         verifyCrudMode(form.crudMode, CrudMode.DETAILS, this::asListHtml);
202         validate(form, messages -> {}, this::asDetailsHtml);
203         verifyToken(this::asDetailsHtml);
204         final String id = form.id;
205         failureUrlService.getFailureUrl(id).alwaysPresent(entity -> {
206             failureUrlService.delete(entity);
207             saveInfo(messages -> messages.addSuccessCrudDeleteCrudTable(GLOBAL));
208         });
209         return redirect(getClass());
210     }
211 
212     /**
213      * Deletes all failure URL records that match the current search criteria.
214      *
215      * @return HTML response redirecting to the failure URL list page
216      */
217     @Execute
218     @Secured({ ROLE })
219     public HtmlResponse deleteall() {
220         verifyToken(this::asListHtml);
221         failureUrlService.deleteAll(failureUrlPager);
222         failureUrlPager.clear();
223         saveInfo(messages -> messages.addSuccessFailureUrlDeleteAll(GLOBAL));
224         return redirect(getClass());
225     }
226 
227     // ===================================================================================
228     //                                                                        Small Helper
229     //                                                                        ============
230     //                                                                              JSP
231     //                                                                           =========
232 
233     private HtmlResponse asListHtml() {
234         return asHtml(path_AdminFailureurl_AdminFailureurlJsp).renderWith(data -> {
235             RenderDataUtil.register(data, "failureUrlItems", failureUrlService.getFailureUrlList(failureUrlPager)); // page navi
236         }).useForm(SearchForm.class, setup -> {
237             setup.setup(form -> {
238                 copyBeanToBean(failureUrlPager, form, op -> op.include("url", "errorCountMin", "errorCountMax", "errorName"));
239             });
240         });
241     }
242 
243     private HtmlResponse asDetailsHtml() {
244         return asHtml(path_AdminFailureurl_AdminFailureurlDetailsJsp);
245     }
246 }