package kr.co.swh.lecture.opensource.project.discode;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.google.gson.Gson;

public class MakeSentimentalDictionary {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Connection connection = null;
		try {
			Class.forName("com.mysql.cj.jdbc.Driver");
			connection = DriverManager.getConnection("jdbc:mysql://dev-swh.gq:3306/market?serverTimezone=UTC","root","swhacademy!");
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			
		Gson gson = new Gson();
		CloseableHttpClient client = HttpClients.createDefault();
		HttpResponse response = null;
		String responseString = "";
		HttpGet requestGet = new HttpGet("https://raw.githubusercontent.com/park1200656/KnuSentiLex/master/data/SentiWord_info.json");
		try {
			response = client.execute(requestGet);
			responseString = EntityUtils.toString(response.getEntity());
			SentimentalDictionary[] value = gson.fromJson(responseString, SentimentalDictionary[].class);
			for(SentimentalDictionary dic : value) {
				String insertTableSQL = "INSERT INTO SENTIMENTAL_DIC"
						+ "(word_root, polarity) VALUES"
						+ "(?,?)";
				PreparedStatement preparedStatementInsert = connection.prepareStatement(insertTableSQL);
				preparedStatementInsert.setString(1, dic.getWord_root());
				preparedStatementInsert.setInt(2, Integer.parseInt(dic.getPolarity()));
				preparedStatementInsert.executeUpdate();
			}
		} catch (IOException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
