package ru.apetrov;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.URL;

/**
 * Created by Andrey on 13.12.2017.
 */
public class JsoupParser {

    private final FilterPaterrn filter;
    private boolean endLoop;
    private JDBCStorege storege;


    public JsoupParser() {
        this.filter = new FilterPaterrn();
        this.endLoop = false;
        this.storege = new JDBCStorege();

    }

    public void loop(String url) throws IOException {
        Document document = this.getPage(url);
        do {
            this.parse(document);
            url = this.next(url);
            document = this.getPage(url);
        } while (!this.endLoop);
    }

    private Document getPage(String url) {
        Document document = null;
        try {
            document = Jsoup.parse(new URL(url), 3000);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return document;
    }

    public void parse(Document document) throws IOException {
        Element forumTable = document.select("table[class=forumTable]").first();
        Elements values = forumTable.select("tr");

        for (Element value : values) {
            Element postslisttopic = value.select("td[class=postslisttopic]").first();
            Element altCol = value.select("td[class=altCol]").first();
            Element altColCreateDate = value.select("td[class=altCol]").last();
            if (postslisttopic != null) {
                String vacancy = postslisttopic.child(0).text();
                String author = altCol.text();
                String createDate = altColCreateDate.text();
                if (this.filter.isCorrect(vacancy, createDate)) {
                    this.storege.add(vacancy,author,createDate);
                    System.out.println(vacancy);
                    System.out.println(author);
                    System.out.println(createDate + "\n");
                }
            }
        }
    }

    public String next(String url) {
        String result = url;
        Document document = this.getPage(url);
        Element sortOptions = document.select("table[class=sort_options]").last();
        Element tr = sortOptions.getElementsByTag("td").first();
        for(int i = 0; i < 10; i++) {
            if (tr.select("b").text().equals(tr.child(9).text())){
                this.endLoop=true;
                break;
            }
            if (tr.child(i).text().equals(tr.select("b").text())) {
                Element element = tr.child(++i);
                result = element.attr("href");
                break;
            }
        }
        return result;
    }
}

@@ -186,16 +186,46 @@
 net.URL;
+%0Aimport java.sql.SQLException;
 %0A%0A/**%0A *
@@ -363,42 +363,8 @@
 op;%0A
-    private JDBCStorege storege;%0A%0A
 %0A   
@@ -463,50 +463,8 @@
 lse;
-%0A        this.storege = new JDBCStorege();
 %0A%0A  
@@ -514,32 +514,91 @@
 s IOException %7B%0A
+        try(JDBCStorege storege = new JDBCStorege()) %7B%0A    
         Document
@@ -620,32 +620,36 @@
 s.getPage(url);%0A
+    
         do %7B%0A   
@@ -653,24 +653,28 @@
             
+    
 this.parse(d
@@ -680,19 +680,32 @@
 document
-);%0A
+, storege);%0A    
         
@@ -734,32 +734,36 @@
 l);%0A            
+   
+ 
 document = this.
@@ -784,16 +784,20 @@
         
+    
 %7D while 
@@ -813,16 +813,94 @@
 dLoop);%0A
+        %7D catch (SQLException e) %7B%0A            e.printStackTrace();%0A        %7D%0A
     %7D%0A%0A 
@@ -1193,16 +1193,37 @@
 document
+, JDBCStorege storege
 ) throws
@@ -1948,21 +1948,16 @@
         
-this.
 storege.
@@ -2550,9 +2550,10 @@
 ild(
-9
+10
 ).te

update AutoCloseable