package amazon_product_metadata_parser.output;

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

import amazon_product_metadata_parser.dto.ProductDTO;

/**
 * Outputs product data to the SQL Server 2012 database "AmazonData" using Windows authentication.
 */
@SuppressWarnings("SpellCheckingInspection")
public class SQLServer2012Output implements Output {

  private static final String SQL_SERVER_DRIVER_CLASS_NAME =
      "com.microsoft.sqlserver.jdbc.SQLServerDriver";
  private static final String PC_NAME_WINDOWS_ENVIRONMENT_VARIABLE = "COMPUTERNAME";
  private final String serverUrl;
  private final String pcName = System.getenv(PC_NAME_WINDOWS_ENVIRONMENT_VARIABLE);
  private final String     databaseName;
  private final JDBCStatementsToExecute statementsToExecute;
  private       Connection          conn;
  private int insertStatementsExecuted = 0;
  private int rowsAffected = 0;

  public SQLServer2012Output(String databaseName, JDBCStatementsToExecute statementsToExecute)
      throws SQLException {
    this.databaseName = databaseName;
    this.statementsToExecute = statementsToExecute;
    serverUrl = buildServerURL();
  }

  //  url = "jdbc:sqlserver://MYPC\\SQLEXPRESS;databaseName=MYDB;integratedSecurity=true";
  private String buildServerURL() {
    return "jdbc:sqlserver://"
           + pcName
           + ";databaseName="
           + databaseName
           + ";integratedSecurity=true";
  }

  @Override
  public void open() throws IOException {
    try {
      Class.forName(SQL_SERVER_DRIVER_CLASS_NAME);
      conn = DriverManager.getConnection(serverUrl);
      System.out.println("Connected to SQL Server Database '" + databaseName + "'");
    } catch (ClassNotFoundException | SQLException e) {
      throw new IOException(e);
    }
  }

  @Override
  public void execute(ProductDTO productDTO) throws IOException {
    statementsToExecute.executeQueries(productDTO);
  }

  @Override
  public void close() throws IOException {
    try {
      if (conn != null) {
        conn.close();
        System.out.println("Closed connection to '" + databaseName + "'");
      }
    } catch (SQLException e) {
      throw new IOException(e);
    }
  }
}

@@ -41,16 +41,40 @@
 utput;%0A%0A
+import java.io.Console;%0A
 import j
@@ -89,24 +89,24 @@
 OException;%0A
-
 import java.
@@ -183,16 +183,62 @@
 ption;%0A%0A
+import amazon_product_metadata_parser.Parser;%0A
 import a
@@ -938,25 +938,16 @@
 nection 
-         
 conn;%0A  
@@ -1503,24 +1503,24 @@
 atabaseName%0A
-
            +
@@ -1549,24 +1549,1022 @@
 true%22;%0A  %7D%0A%0A
+  public static void main(String%5B%5D args) throws SQLException, IOException %7B%0A    System.out.printf(%22This program parses the 'amazon-meta.txt' file and inserts the data into %22%0A                      + %22a SQL Server 2012 database.%25nIt assumes you have set up the appropriate%22%0A                      + %22 stored procedures and schema in a database named 'AmazonProductMetadata'%22%0A                      + %22.%25n%22);%0A    System.out.println(%22Press enter the path to the 'amazon-meta.txt' file to proceed or %22%0A                       + %22press Ctrl+C to exit%22);%0A    Console console = System.console();%0A    String response = console.readLine();%0A    switch (response) %7B%0A      case %22quit%22:%0A        // Fall through%0A      case %22cancel%22:%0A        // Fall through%0A      case %22exit%22:%0A        // Fall through%0A        break;%0A      default:%0A        Parser parser = new Parser(response, new SQLServer2012Output(%22AmazonProductMetadata%22, new%0A            JDBCStatementsToExecuteImplExample()));%0A        parser.parse();%0A    %7D%0A%0A  %7D%0A%0A
   @Override%0A

add psvm to SQLServer2012Output.java