package Database;

import DataClasses.*;

import java.sql.ResultSet;
import java.util.*;

public class DatabaseHandler {

    public static void insertTitle(String title, String subTitle, String Url) {
        HashMap<String, String> values = new HashMap<>();
        values.put("title", title);
        values.put("subtitle", subTitle);
        values.put("url", Url);

        SqlHandler.insert("Titles", values);
    }

    public static int getTitleId(String title) {
        // TODO : return the actual id
        SqlHandler.select("Titles", new String[] {"id"}, "");

        return 1;
    }

    public static void updateTitle(int id) {
        String condition = "id=" + id;
        HashMap<String, String> values = new HashMap<>();
        SqlHandler.upadte("Titles", values, condition);
    }

    // will search the db for titles with the same words in the same order
    // will return <found title length> / <title length>
    public static double[] searchTitle(String[] title) {
        ResultSet rs = SqlHandler.select("Titles", new String [] {"headline"}, "headline LIKE");

        return null;
    }

    public static String processCrawlRequest(Map<Integer, Article> request, String domain) {

        return "";
    }

    public static String processMarkRequest(Map<Integer, Article> request, String domain) {

        return "";
    }

}

@@ -51,25 +51,78 @@
 ava.sql.
-ResultSet
+Array;%0Aimport java.sql.ResultSet;%0Aimport java.sql.SQLException
 ;%0Aimport
@@ -1004,16 +1004,26 @@
 tic 
-d
+Map%3CString,D
 ouble
-%5B%5D
+%3E
  sea
@@ -1039,33 +1039,111 @@
 String%5B%5D title) 
-%7B
+throws SQLException %7B%0A        HashMap%3CString, Double%3E values = new HashMap%3C%3E();
 %0A        ResultS
@@ -1192,13 +1192,17 @@
 ring
- 
 %5B%5D
- 
 %7B
+%22id%22, 
 %22hea
@@ -1232,28 +1232,607 @@
 %22);%0A
-%0A        return null
+        double max = 0;%0A        String id = %22%22;%0A        while(rs.next()) %7B%0A            String%5B%5D dbTitle = (String%5B%5D) rs.getArray(%22headline%22).getArray();%0A            int index = 0;%0A            for (int i = 0; i %3C title.length; i++) %7B%0A                if (dbTitle%5Bi%5D.equals(title%5Bindex%5D))%0A                    index++;%0A            %7D%0A            double reliability = index / title.length;%0A            if (max %3C reliability) %7B%0A                max = reliability;%0A                id = rs.getString(%22id%22);%0A            %7D%0A            values.put(rs.getString(%22id%22), reliability);%0A        %7D%0A        return values
 ;%0A  

write handle title