/** Inserts a row in the newly created table for the authenticated user. */
  private static void insertData(String tableId) throws IOException {
    Sql sql =
        fusiontables
            .query()
            .sql(
                "INSERT INTO "
                    + tableId
                    + " (Text,Number,Location,Date) "
                    + "VALUES ("
                    + "'Google Inc', "
                    + "1, "
                    + "'1600 Amphitheatre Parkway Mountain View, "
                    + "CA 94043, USA','"
                    + new DateTime(new Date())
                    + "')");

    try {
      sql.execute();
    } catch (IllegalArgumentException e) {
      // For google-api-services-fusiontables-v1-rev1-1.7.2-beta this exception will always
      // been thrown.
      // Please see issue 545: JSON response could not be deserialized to Sqlresponse.class
      // http://code.google.com/p/google-api-java-client/issues/detail?id=545
    }
  }
  /**
   * @param tableId
   * @throws IOException
   */
  private static void showRows(String tableId) throws IOException {
    View.header("Showing Rows From Table");

    Sql sql = fusiontables.query().sql("SELECT Text,Number,Location,Date FROM " + tableId);

    try {
      sql.execute();
    } catch (IllegalArgumentException e) {
      // For google-api-services-fusiontables-v1-rev1-1.7.2-beta this exception will always
      // been thrown.
      // Please see issue 545: JSON response could not be deserialized to Sqlresponse.class
      // http://code.google.com/p/google-api-java-client/issues/detail?id=545
    }
  }
  //	----------------------------------------------------------------------------------------------------------------
  //      Method:     AddNewEntry
  //      Inputs:	    RF Data Entry (JSON)
  //     Outputs:	    Success = TRUE / Failure = FALSE
  // Description:     Insert new data to table  (executes SQL command)
  //	----------------------------------------------------------------------------------------------------------------
  public boolean AddNewEntry(String json, boolean fusion_tables) {
    boolean status; // Return status (success / failure)

    try // Try to get JSON, and save data to database
    { //
      Gson gson = new GsonBuilder().create(); // Create Gson builder
      RFData RFMember = gson.fromJson(json, RFData.class); // Convert from JSON to RFData
      if (RFMember.XbeeID != -1) // If not default then save data
      {
        // Print debug information to port
        System.out.println(
            "Insert New RF Data into Table - XbeeID: "
                + RFMember.XbeeID
                + ", RSSI: "
                + RFMember.RSSI);

        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String sql_string; // Build up SQL string
        sql_string = "INSERT INTO RF_Fields ("; // Insert SQL statement, Table: RF_Fields
        sql_string += "intXbeeID,"; // Field: intXbeeID
        sql_string += "intDeviceID,"; // Field: intDeviceID
        sql_string += "fltRSSI,"; // Field: fltRSSI
        sql_string += "fltLatitude,"; // Field: fltLatitude
        sql_string += "fltLongitude,"; // Field: fltLongitude
        sql_string += "fltYaw,"; // Field: fltYaw
        sql_string += "fltPitch,"; // Field: fltPitch
        sql_string += "fltRoll,"; // Field: fltRoll
        sql_string += "dtSampleDate) "; // Field: dtSampleDate
        sql_string += "VALUES ("; // Values indetifier
        sql_string += RFMember.XbeeID + ","; // Value: XbeeID
        sql_string += RFMember.DeviceID + ","; // Value: DeviceID
        sql_string += RFMember.RSSI + ","; // Value: RSSI
        sql_string += RFMember.Latitude + ","; // Value: Latitude
        sql_string += RFMember.Longitude + ","; // Value: Longitude
        sql_string += RFMember.Yaw + ","; // Value: Yaw
        sql_string += RFMember.Pitch + ","; // Value: Pitch
        sql_string += RFMember.Roll + ","; // Value: Roll
        sql_string += "'" + ft.format(RFMember.SampleDate) + "')";
        System.out.println("SQL: " + sql_string); // Debug print the SQL statement
        //
        Statement stmt = conn.createStatement(); // Build SQL statement
        stmt.execute(sql_string); // Execute the SQL statement
        stmt.close(); // Close the statement
        status = true; // Success

        if (fusion_tables) // Only add to the fusion tables if the flag
        { // is set
          // Add to fusion table
          String tableId = GetTableId("RF Field Data"); // Get the "Customer Data" Table
          sql_string = "INSERT INTO " + tableId + " ("; // Insert SQL statement, Table: Table ID
          sql_string += "XbeeID,"; // Field: intXbeeID
          sql_string += "DeviceID,"; // Field: intDeviceID
          sql_string += "RSSI,"; // Field: fltRSSI
          sql_string += "Location,"; // Field: fltLatitude
          sql_string += "Longitude,"; // Field: fltLongitude
          sql_string += "Yaw,"; // Field: fltYaw
          sql_string += "Pitch,"; // Field: fltPitch
          sql_string += "Roll,"; // Field: fltRoll
          sql_string += "SampleDate) "; // Field: dtSampleDate
          sql_string += "VALUES ("; // Values indetifier
          sql_string += RFMember.XbeeID + ","; // Value: XbeeID
          sql_string += RFMember.DeviceID + ","; // Value: DeviceID
          sql_string += RFMember.RSSI + ","; // Value: RSSI
          sql_string += RFMember.Latitude + ","; // Value: Latitude
          sql_string += RFMember.Longitude + ","; // Value: Longitude
          sql_string += RFMember.Yaw + ","; // Value: Yaw
          sql_string += RFMember.Pitch + ","; // Value: Pitch
          sql_string += RFMember.Roll + ","; // Value: Roll
          sql_string += "'" + ft.format(RFMember.SampleDate) + "')";
          Sql sql = FusionTables.query().sql(sql_string); // Build Fusion Query
          // Try and execute the SQL command
          try //
          { //
            sql.executeAndDownloadTo(System.out); // Execute command, stream to the system.out
          } //
          catch (IllegalArgumentException e) //
          { //
          } //
        }
      } //
      else //
      { //
        System.err.println("AddNewEntry: Invalid JSON data"); // Print the exception data and exit
        status = false; // Failure, invalid JSON or data
      }
    } //
    catch (Exception e) // Exception processing:
    { //
      System.err.println("AddNewEntry: " + e.getMessage()); // Print the exception data and exit
      status = false; // Failure
    } //
    return status; // Return status
  }