View Javadoc
1   /*
2    * Copyright 2012-2019 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  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  /**
35   * @author shinsuke
36   * @author Shunji Makino
37   */
38  public class AdminCrawlinginfoAction extends FessAdminAction {
39  
40      private static final Logger logger = LoggerFactory.getLogger(AdminCrawlinginfoAction.class);
41  
42      // ===================================================================================
43      //                                                                           Attribute
44      //                                                                           =========
45      @Resource
46      private CrawlingInfoService crawlingInfoService;
47      @Resource
48      private CrawlingInfoPager crawlingInfoPager;
49      @Resource
50      protected ProcessHelper processHelper;
51  
52      // ===================================================================================
53      //                                                                               Hook
54      //                                                                              ======
55      @Override
56      protected void setupHtmlData(final ActionRuntime runtime) {
57          super.setupHtmlData(runtime);
58          runtime.registerData("helpLink", systemHelper.getHelpLink(fessConfig.getOnlineHelpNameCrawlinginfo()));
59      }
60  
61      // ===================================================================================
62      //                                                                      Search Execute
63      //                                                                      ==============
64      @Execute
65      public HtmlResponse index() {
66          saveToken();
67          return asListHtml();
68      }
69  
70      @Execute
71      public HtmlResponse list(final Integer pageNumber, final SearchForm form) {
72          saveToken();
73          crawlingInfoPager.setCurrentPageNumber(pageNumber);
74          return asHtml(path_AdminCrawlinginfo_AdminCrawlinginfoJsp).renderWith(data -> {
75              searchPaging(data, form);
76          });
77      }
78  
79      @Execute
80      public HtmlResponse search(final SearchForm form) {
81          saveToken();
82          copyBeanToBean(form, crawlingInfoPager, op -> op.exclude(Constants.PAGER_CONVERSION_RULE));
83          return asHtml(path_AdminCrawlinginfo_AdminCrawlinginfoJsp).renderWith(data -> {
84              searchPaging(data, form);
85          });
86      }
87  
88      @Execute
89      public HtmlResponse reset(final SearchForm form) {
90          saveToken();
91          crawlingInfoPager.clear();
92          return asHtml(path_AdminCrawlinginfo_AdminCrawlinginfoJsp).renderWith(data -> {
93              searchPaging(data, form);
94          });
95      }
96  
97      @Execute
98      public HtmlResponse back(final SearchForm form) {
99          saveToken();
100         return asHtml(path_AdminCrawlinginfo_AdminCrawlinginfoJsp).renderWith(data -> {
101             searchPaging(data, form);
102         });
103     }
104 
105     protected void searchPaging(final RenderData data, final SearchForm form) {
106         RenderDataUtil.register(data, "crawlingInfoItems", crawlingInfoService.getCrawlingInfoList(crawlingInfoPager)); // page navi
107 
108         // restore from pager
109         copyBeanToBean(crawlingInfoPager, form, op -> op.include("sessionId"));
110     }
111 
112     // ===================================================================================
113     //                                                                        Edit Execute
114     //                                                                        ============
115 
116     // -----------------------------------------------------
117     //                                               Details
118     //                                               -------
119     @Execute
120     public HtmlResponse details(final int crudMode, final String id) {
121         verifyCrudMode(crudMode, CrudMode.DETAILS);
122         saveToken();
123         return crawlingInfoService.getCrawlingInfo(id).map(entity -> {
124             return asHtml(path_AdminCrawlinginfo_AdminCrawlinginfoDetailsJsp).useForm(EditForm.class, op -> {
125                 op.setup(form -> {
126                     copyBeanToBean(entity, form, copyOp -> {
127                         copyOp.excludeNull();
128                     });
129                     form.crudMode = crudMode;
130                 });
131             }).renderWith(data -> {
132                 RenderDataUtil.register(data, "crawlingInfoParamItems", crawlingInfoService.getCrawlingInfoParamList(id));
133                 RenderDataUtil.register(data, "running", processHelper.isProcessRunning(entity.getSessionId()));
134             });
135         }).orElseGet(() -> {
136             throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id), () -> asListHtml());
137             return null;
138         });
139     }
140 
141     @Execute
142     public HtmlResponse threaddump(final EditForm form) {
143         verifyCrudMode(form.crudMode, CrudMode.DETAILS);
144         validate(form, messages -> {}, () -> asDetailsHtml());
145         verifyToken(() -> asDetailsHtml());
146         final String id = form.id;
147         crawlingInfoService.getCrawlingInfo(id).ifPresent(entity -> {
148             try {
149                 processHelper.sendCommand(entity.getSessionId(), Constants.CRAWLER_PROCESS_COMMAND_THREAD_DUMP);
150                 saveInfo(messages -> messages.addSuccessPrintThreadDump(GLOBAL));
151             } catch (final Exception e) {
152                 logger.warn("Failed to print a thread dump.", e);
153                 throwValidationError(messages -> messages.addErrorsFailedToPrintThreadDump(GLOBAL), () -> asListHtml());
154             }
155         }).orElse(() -> {
156             throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id), () -> asListHtml());
157         });
158         return redirect(getClass());
159     }
160 
161     // -----------------------------------------------------
162     //                                         Actually Crud
163     //                                         -------------
164     @Execute
165     public HtmlResponse delete(final EditForm form) {
166         verifyCrudMode(form.crudMode, CrudMode.DETAILS);
167         validate(form, messages -> {}, () -> asDetailsHtml());
168         verifyToken(() -> asDetailsHtml());
169         final String id = form.id;
170         crawlingInfoService.getCrawlingInfo(id).alwaysPresent(entity -> {
171             crawlingInfoService.delete(entity);
172             saveInfo(messages -> messages.addSuccessCrudDeleteCrudTable(GLOBAL));
173         });
174         return redirect(getClass());
175     }
176 
177     @Execute
178     public HtmlResponse deleteall() {
179         verifyToken(() -> asListHtml());
180         crawlingInfoService.deleteOldSessions(processHelper.getRunningSessionIdSet());
181         crawlingInfoPager.clear();
182         saveInfo(messages -> messages.addSuccessCrawlingInfoDeleteAll(GLOBAL));
183         return redirect(getClass());
184     }
185 
186     // ===================================================================================
187     //                                                                        Assist Logic
188     //                                                                        ============
189 
190     // ===================================================================================
191     //                                                                        Small Helper
192     //                                                                        ============
193     protected void verifyCrudMode(final int crudMode, final int expectedMode) {
194         if (crudMode != expectedMode) {
195             throwValidationError(messages -> {
196                 messages.addErrorsCrudInvalidMode(GLOBAL, String.valueOf(expectedMode), String.valueOf(crudMode));
197             }, () -> asListHtml());
198         }
199     }
200 
201     // ===================================================================================
202     //                                                                              JSP
203     //                                                                           =========
204 
205     private HtmlResponse asListHtml() {
206         return asHtml(path_AdminCrawlinginfo_AdminCrawlinginfoJsp).renderWith(data -> {
207             RenderDataUtil.register(data, "crawlingInfoItems", crawlingInfoService.getCrawlingInfoList(crawlingInfoPager)); // page navi
208             }).useForm(SearchForm.class, setup -> {
209             setup.setup(form -> {
210                 copyBeanToBean(crawlingInfoPager, form, op -> op.include("id"));
211             });
212         });
213     }
214 
215     private HtmlResponse asDetailsHtml() {
216         return asHtml(path_AdminCrawlinginfo_AdminCrawlinginfoDetailsJsp);
217     }
218 }