import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

public class PullUp 
{
  public static void main(String[] args) 
  {
    String familyName;
    String firstName;
    Connection connection;
    DatabaseConnection databaseConnection;
    Statement personStatement;
    Statement claimantStatement;
    Statement transactionStatement;
    Statement claimHeadStatement;        
    ResultSet personResultSet;
    ResultSet claimantResultSet;
    ResultSet transactionResultSet;
    ResultSet claimHeadResultSet;
    Date startDate;
    Date endDate;
    long startTime;
    long endTime;
    
    
    familyName = new String(args[0]);
    firstName = new String(args[1]);
    
    databaseConnection = new DatabaseConnection();
    connection = databaseConnection.get();

    startDate = new Date();
    startTime = startDate.getTime();
    
    try 
    {
      personStatement = connection.createStatement();
      personResultSet = personStatement.executeQuery
      (
        "select * from person " +
        "where family_name = '" + familyName + "' and " +
        "first_name = '" + firstName + "';"
      );
      while (personResultSet.next())
      {
        claimantStatement = connection.createStatement();
        claimantResultSet = claimantStatement.executeQuery
        (
          "select * from claimant " +
          "where person_id = " + personResultSet.getInt("id") + ";"
        );
        while (claimantResultSet.next())
        {
          transactionStatement = connection.createStatement();
          transactionResultSet = transactionStatement.executeQuery
          (
            "select * from transaction " +
            "where claimant_id = " + claimantResultSet.getInt("id") + ";"
          );
          while (transactionResultSet.next())
          {
            claimHeadStatement = connection.createStatement();
            claimHeadResultSet = claimHeadStatement.executeQuery
            (
              "select * from claim_head " +
              "where " +
                "id = " + transactionResultSet.getInt("claim_head_id") + ";"
            );
            while (claimHeadResultSet.next())
            {
              System.out.println
              (
                "Family name: " + 
                personResultSet.getString("family_name") + ", " +
                "First name: " + 
                personResultSet.getString("first_name") + ", " +
                "Claim head: " + 
                claimHeadResultSet.getString("description") + ", " +
                "Amount: " + 
                transactionResultSet.getBigDecimal("amount")
              );
            }                        
          }
        }
      }
    } 
    catch (SQLException e) 
    {
        e.printStackTrace();
    }

    endDate = new Date();
    endTime = endDate.getTime();        
    System.out.println("Elapsed time: " + (endTime - startTime));

  }
}
