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.crawlinginfo;
17  
18  import javax.annotation.Resource;
19  
20  import org.codelibs.fess.Constants;
21  import org.codelibs.fess.app.pager.CrawlingInfoPager;
22  import org.codelibs.fess.app.service.CrawlingInfoService;
23  import org.codelibs.fess.app.web.CrudMode;
24  import org.codelibs.fess.app.web.base.FessAdminAction;
25  import org.codelibs.fess.helper.ProcessHelper;
26  import org.codelibs.fess.util.RenderDataUtil;
27  import org.lastaflute.web.Execute;
28  import org.lastaflute.web.response.HtmlResponse;
29  import org.lastaflute.web.response.render.RenderData;
30  import org.lastaflute.web.ruts.process.ActionRuntime;
31  
32  /**
33   * @author shinsuke
34   * @author Shunji Makino
35   */
36  public class AdminCrawlinginfoAction extends FessAdminAction {
37  
38      // ===================================================================================
39      //                                                                           Attribute
40      //                                                                           =========
41      @Resource
42      private CrawlingInfoService crawlingInfoService;
43      @Resource
44      private CrawlingInfoPager crawlingInfoPager;
45      @Resource
46      protected ProcessHelper processHelper;
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.getOnlineHelpNameCrawlinginfo()));
55      }
56  
57      // ===================================================================================
58      //                                                                      Search Execute
59      //                                                                      ==============
60      @Execute
61      public HtmlResponse index() {
62          saveToken();
63          return asListHtml();
64      }
65  
66      @Execute
67      public HtmlResponse list(final Integer pageNumber, final SearchForm form) {
68          saveToken();
69          crawlingInfoPager.setCurrentPageNumber(pageNumber);
70          return asHtml(path_AdminCrawlinginfo_AdminCrawlinginfoJsp).renderWith(data -> {
71              searchPaging(data, form);
72          });
73      }
74  
75      @Execute
76      public HtmlResponse search(final SearchForm form) {
77          saveToken();
78          copyBeanToBean(form, crawlingInfoPager, op -> op.exclude(Constants.PAGER_CONVERSION_RULE));
79          return asHtml(path_AdminCrawlinginfo_AdminCrawlinginfoJsp).renderWith(data -> {
80              searchPaging(data, form);
81          });
82      }
83  
84      @Execute
85      public HtmlResponse reset(final SearchForm form) {
86          saveToken();
87          crawlingInfoPager.clear();
88          return asHtml(path_AdminCrawlinginfo_AdminCrawlinginfoJsp).renderWith(data -> {
89              searchPaging(data, form);
90          });
91      }
92  
93      @Execute
94      public HtmlResponse back(final SearchForm form) {
95          saveToken();
96          return asHtml(path_AdminCrawlinginfo_AdminCrawlinginfoJsp).renderWith(data -> {
97              searchPaging(data, form);
98          });
99      }
100 
101     protected void searchPaging(final RenderData data, final SearchForm form) {
102         RenderDataUtil.register(data, "crawlingInfoItems", crawlingInfoService.getCrawlingInfoList(crawlingInfoPager)); // page navi
103 
104         // restore from pager
105         copyBeanToBean(crawlingInfoPager, form, op -> op.include("sessionId"));
106     }
107 
108     // ===================================================================================
109     //                                                                        Edit Execute
110     //                                                                        ============
111 
112     // -----------------------------------------------------
113     //                                               Details
114     //                                               -------
115     @Execute
116     public HtmlResponse details(final int crudMode, final String id) {
117         verifyCrudMode(crudMode, CrudMode.DETAILS);
118         saveToken();
119         return asHtml(path_AdminCrawlinginfo_AdminCrawlinginfoDetailsJsp).useForm(EditForm.class, op -> {
120             op.setup(form -> {
121                 crawlingInfoService.getCrawlingInfo(id).ifPresent(entity -> {
122                     copyBeanToBean(entity, form, copyOp -> {
123                         copyOp.excludeNull();
124                     });
125                     form.crudMode = crudMode;
126                 }).orElse(() -> {
127                     throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id), () -> asListHtml());
128                 });
129             });
130         }).renderWith(data -> {
131             RenderDataUtil.register(data, "crawlingInfoParamItems", crawlingInfoService.getCrawlingInfoParamList(id));
132         });
133     }
134 
135     // -----------------------------------------------------
136     //                                         Actually Crud
137     //                                         -------------
138     @Execute
139     public HtmlResponse delete(final EditForm form) {
140         verifyCrudMode(form.crudMode, CrudMode.DETAILS);
141         validate(form, messages -> {}, () -> asDetailsHtml());
142         verifyToken(() -> asDetailsHtml());
143         final String id = form.id;
144         crawlingInfoService.getCrawlingInfo(id).alwaysPresent(entity -> {
145             crawlingInfoService.delete(entity);
146             saveInfo(messages -> messages.addSuccessCrudDeleteCrudTable(GLOBAL));
147         });
148         return redirect(getClass());
149     }
150 
151     @Execute
152     public HtmlResponse deleteall() {
153         verifyToken(() -> asListHtml());
154         crawlingInfoService.deleteOldSessions(processHelper.getRunningSessionIdSet());
155         crawlingInfoPager.clear();
156         saveInfo(messages -> messages.addSuccessCrawlingInfoDeleteAll(GLOBAL));
157         return redirect(getClass());
158     }
159 
160     // ===================================================================================
161     //                                                                        Assist Logic
162     //                                                                        ============
163 
164     // ===================================================================================
165     //                                                                        Small Helper
166     //                                                                        ============
167     protected void verifyCrudMode(final int crudMode, final int expectedMode) {
168         if (crudMode != expectedMode) {
169             throwValidationError(messages -> {
170                 messages.addErrorsCrudInvalidMode(GLOBAL, String.valueOf(expectedMode), String.valueOf(crudMode));
171             }, () -> asListHtml());
172         }
173     }
174 
175     // ===================================================================================
176     //                                                                              JSP
177     //                                                                           =========
178 
179     private HtmlResponse asListHtml() {
180         return asHtml(path_AdminCrawlinginfo_AdminCrawlinginfoJsp).renderWith(data -> {
181             RenderDataUtil.register(data, "crawlingInfoItems", crawlingInfoService.getCrawlingInfoList(crawlingInfoPager)); // page navi
182             }).useForm(SearchForm.class, setup -> {
183             setup.setup(form -> {
184                 copyBeanToBean(crawlingInfoPager, form, op -> op.include("id"));
185             });
186         });
187     }
188 
189     private HtmlResponse asDetailsHtml() {
190         return asHtml(path_AdminCrawlinginfo_AdminCrawlinginfoDetailsJsp);
191     }
192 }