View Javadoc
1   /*
2    * Copyright 2012-2021 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 javax.annotation.Resource;
22  
23  import org.codelibs.fess.Constants;
24  import org.codelibs.fess.annotation.Secured;
25  import org.codelibs.fess.app.pager.JobLogPager;
26  import org.codelibs.fess.app.service.JobLogService;
27  import org.codelibs.fess.app.web.CrudMode;
28  import org.codelibs.fess.app.web.base.FessAdminAction;
29  import org.codelibs.fess.util.RenderDataUtil;
30  import org.lastaflute.web.Execute;
31  import org.lastaflute.web.response.HtmlResponse;
32  import org.lastaflute.web.response.render.RenderData;
33  import org.lastaflute.web.ruts.process.ActionRuntime;
34  
35  /**
36   * @author shinsuke
37   * @author Shunji Makino
38   */
39  public class AdminJoblogAction extends FessAdminAction {
40  
41      public static final String ROLE = "admin-joblog";
42  
43      // ===================================================================================
44      //                                                                           Attribute
45      //                                                                           =========
46      @Resource
47      private JobLogService jobLogService;
48      @Resource
49      private JobLogPager jobLogPager;
50  
51      // ===================================================================================
52      //                                                                               Hook
53      //                                                                              ======
54      @Override
55      protected void setupHtmlData(final ActionRuntime runtime) {
56          super.setupHtmlData(runtime);
57          runtime.registerData("helpLink", systemHelper.getHelpLink(fessConfig.getOnlineHelpNameJoblog()));
58      }
59  
60      @Override
61      protected String getActionRole() {
62          return ROLE;
63      }
64  
65      // ===================================================================================
66      //                                                                      Search Execute
67      //                                                                      ==============
68      @Execute
69      @Secured({ ROLE, ROLE + VIEW })
70      public HtmlResponse index(final SearchForm form) {
71          saveToken();
72          return asListHtml();
73      }
74  
75      @Execute
76      @Secured({ ROLE, ROLE + VIEW })
77      public HtmlResponse list(final Integer pageNumber, final SearchForm form) {
78          saveToken();
79          jobLogPager.setCurrentPageNumber(pageNumber);
80          return asHtml(path_AdminJoblog_AdminJoblogJsp).renderWith(data -> {
81              searchPaging(data, form);
82          });
83      }
84  
85      @Execute
86      @Secured({ ROLE, ROLE + VIEW })
87      public HtmlResponse search(final SearchForm form) {
88          saveToken();
89          copyBeanToBean(form, jobLogPager, op -> op.exclude(Constants.PAGER_CONVERSION_RULE));
90          return asHtml(path_AdminJoblog_AdminJoblogJsp).renderWith(data -> {
91              searchPaging(data, form);
92          });
93      }
94  
95      @Execute
96      @Secured({ ROLE, ROLE + VIEW })
97      public HtmlResponse reset(final SearchForm form) {
98          saveToken();
99          jobLogPager.clear();
100         return asHtml(path_AdminJoblog_AdminJoblogJsp).renderWith(data -> {
101             searchPaging(data, form);
102         });
103     }
104 
105     @Execute
106     @Secured({ ROLE, ROLE + VIEW })
107     public HtmlResponse back(final SearchForm form) {
108         saveToken();
109         return asHtml(path_AdminJoblog_AdminJoblogJsp).renderWith(data -> {
110             searchPaging(data, form);
111         });
112     }
113 
114     protected void searchPaging(final RenderData data, final SearchForm form) {
115         RenderDataUtil.register(data, "jobLogItems", jobLogService.getJobLogList(jobLogPager)); // page navi
116 
117         // restore from pager
118         copyBeanToBean(jobLogPager, form, op -> op.include("id"));
119     }
120 
121     // ===================================================================================
122     //                                                                        Edit Execute
123     //                                                                        ============
124     // -----------------------------------------------------
125     //                                            Entry Page
126     //                                            ----------
127 
128     // -----------------------------------------------------
129     //                                               Details
130     //                                               -------
131     @Execute
132     @Secured({ ROLE, ROLE + VIEW })
133     public HtmlResponse details(final int crudMode, final String id) {
134         verifyCrudMode(crudMode, CrudMode.DETAILS);
135         saveToken();
136         return asHtml(path_AdminJoblog_AdminJoblogDetailsJsp).useForm(EditForm.class, op -> {
137             op.setup(form -> {
138                 jobLogService.getJobLog(id).ifPresent(entity -> {
139                     copyBeanToBean(entity, form, copyOp -> {
140                         copyOp.excludeNull();
141                     });
142                     form.crudMode = crudMode;
143                 }).orElse(() -> {
144                     throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id), this::asListHtml);
145                 });
146             });
147         });
148     }
149 
150     // -----------------------------------------------------
151     //                                         Actually Crud
152     //                                         -------------
153     @Execute
154     @Secured({ ROLE })
155     public HtmlResponse delete(final EditForm form) {
156         verifyCrudMode(form.crudMode, CrudMode.DETAILS);
157         validate(form, messages -> {}, this::asDetailsHtml);
158         verifyToken(this::asDetailsHtml);
159         final String id = form.id;
160         jobLogService.getJobLog(id).alwaysPresent(entity -> {
161             jobLogService.delete(entity);
162             saveInfo(messages -> messages.addSuccessCrudDeleteCrudTable(GLOBAL));
163         });
164         return redirect(getClass());
165     }
166 
167     @Execute
168     @Secured({ ROLE })
169     public HtmlResponse deleteall() {
170         verifyToken(this::asListHtml);
171         final List<String> jobStatusList = new ArrayList<>();
172         jobStatusList.add(Constants.OK);
173         jobStatusList.add(Constants.FAIL);
174         jobLogService.deleteByJobStatus(jobStatusList);
175         jobLogPager.clear();
176         saveInfo(messages -> messages.addSuccessJobLogDeleteAll(GLOBAL));
177         return redirect(getClass());
178     }
179 
180     // ===================================================================================
181     //                                                                        Assist Logic
182     //                                                                        ============
183 
184     // ===================================================================================
185     //                                                                        Small Helper
186     //                                                                        ============
187     protected void verifyCrudMode(final int crudMode, final int expectedMode) {
188         if (crudMode != expectedMode) {
189             throwValidationError(messages -> {
190                 messages.addErrorsCrudInvalidMode(GLOBAL, String.valueOf(expectedMode), String.valueOf(crudMode));
191             }, this::asListHtml);
192         }
193     }
194 
195     // ===================================================================================
196     //                                                                              JSP
197     //                                                                           =========
198 
199     private HtmlResponse asListHtml() {
200         return asHtml(path_AdminJoblog_AdminJoblogJsp).renderWith(data -> {
201             RenderDataUtil.register(data, "jobLogItems", jobLogService.getJobLogList(jobLogPager)); // page navi
202         }).useForm(SearchForm.class, setup -> {
203             setup.setup(form -> {
204                 copyBeanToBean(jobLogPager, form, op -> op.include("id"));
205             });
206         });
207     }
208 
209     private HtmlResponse asDetailsHtml() {
210         return asHtml(path_AdminJoblog_AdminJoblogDetailsJsp);
211     }
212 }