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