1 changed files with 45 additions and 0 deletions
@ -0,0 +1,45 @@ |
|||
package com.crawler.controller; |
|||
|
|||
import com.crawler.crawler.Crawler; |
|||
import com.crawler.exception.handler.ExceptionHandlerFactory; |
|||
import com.crawler.model.CrawlerConfig; |
|||
import com.crawler.model.CrawlerData; |
|||
import com.crawler.view.CrawlerView; |
|||
|
|||
import java.util.List; |
|||
|
|||
public class CrawlerController { |
|||
private Crawler crawler; |
|||
private CrawlerView view; |
|||
private ExceptionHandlerFactory exceptionHandlerFactory; |
|||
|
|||
public CrawlerController(Crawler crawler) { |
|||
this.crawler = crawler; |
|||
this.view = new CrawlerView(); |
|||
this.exceptionHandlerFactory = ExceptionHandlerFactory.getInstance(); |
|||
} |
|||
|
|||
public void setConfig(CrawlerConfig config) { |
|||
crawler.setConfig(config); |
|||
} |
|||
|
|||
public void execute() { |
|||
view.showStartMessage(crawler.getCrawlerName()); |
|||
|
|||
try { |
|||
List<CrawlerData> dataList = crawler.crawl(); |
|||
view.showData(dataList); |
|||
view.showSuccessMessage(dataList.size()); |
|||
|
|||
if (!dataList.isEmpty()) { |
|||
com.crawler.command.CacheCommand.saveDataWithPrompt(dataList, crawler.getCrawlerName()); |
|||
} |
|||
} catch (Exception e) { |
|||
exceptionHandlerFactory.handleException(e, view); |
|||
} |
|||
} |
|||
|
|||
public List<CrawlerData> crawl() { |
|||
return crawler.crawl(); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue