/*
  * Convenience method for displaying output when debugging.
  */
 private void displayRow(Table tbl) {
   final TableIterator<Row> itr = tableAPI.tableIterator(tbl.createPrimaryKey(), null, null);
   while (itr.hasNext()) {
     System.out.println(itr.next());
   }
   itr.close();
 }
  private void deleteExistingData() {

    /* Get an iterator over all the primary keys in the table. */
    final TableIterator<PrimaryKey> itr =
        tableAPI.tableKeysIterator(table.createPrimaryKey(), null, null);

    /* Delete each row from the table. */
    long cnt = 0;
    while (itr.hasNext()) {
      tableAPI.delete(itr.next(), null, null);
      cnt++;
    }
    itr.close();
    System.out.println(cnt + " records deleted");
  }
  private void addRow() {

    final Row row = table.createRow();

    /* Generate zipcode */
    final StringBuilder zipBuf = new StringBuilder();
    for (int i = 0; i < 5; i++) {
      final int zipDigit = secureRandom.nextInt(10);
      zipBuf.append(zipDigit);
    }
    final String zipStr = zipBuf.toString();
    row.put("zipcode", zipStr);

    /* Generate ssn */
    final StringBuilder ssnBuf = new StringBuilder();
    for (int i = 0; i < 9; i++) {
      final int ssnDigit = secureRandom.nextInt(10);
      ssnBuf.append(ssnDigit);
    }
    row.put("ssn", Long.parseLong(ssnBuf.toString()));

    /* Generate lastname */
    final int lastnameIndx = secureRandom.nextInt(LAST_NAMES.length);
    row.put("lastname", LAST_NAMES[lastnameIndx]);

    /* Generate firstname and gender */
    int firstnameIndx = secureRandom.nextInt(FIRST_NAMES_FEMALE.length);
    String firstname = FIRST_NAMES_FEMALE[firstnameIndx];
    String gender = "female";
    final int maleFemale = secureRandom.nextInt(2);
    if (maleFemale == 0) {
      firstnameIndx = secureRandom.nextInt(FIRST_NAMES_MALE.length);
      firstname = FIRST_NAMES_MALE[firstnameIndx];
      gender = "male";
    }
    row.put("firstname", firstname);
    row.putEnum("gender", gender);

    /* Generate license id as String, but store it as BINARY */
    final StringBuilder licenseBuf = new StringBuilder();
    for (int i = 0; i < 9; i++) {
      if (i > 0) {
        final int licenseDigit = secureRandom.nextInt(10);
        licenseBuf.append(String.valueOf(licenseDigit));
      } else {
        licenseBuf.append("S");
      }
    }
    row.putFixed("license", (licenseBuf.toString()).getBytes());

    /* Generate phoneinfo */
    final MapValue phoneMap = row.putMap("phoneinfo");
    for (int i = 0; i < PHONE_TYPE.length; i++) {
      final String phonetype = PHONE_TYPE[i];

      final int areaCodeIndx = secureRandom.nextInt(AREA_CODES.length);
      final int exchangeIndx = secureRandom.nextInt(EXCHANGES.length);
      final String areacode = AREA_CODES[areaCodeIndx];
      final String exchange = EXCHANGES[exchangeIndx];

      final StringBuilder phoneBuf = new StringBuilder();
      phoneBuf.append(areacode);
      phoneBuf.append("-");
      phoneBuf.append(exchange);
      phoneBuf.append("-");

      for (int j = 0; j < 4; j++) {
        final int phoneDigit = secureRandom.nextInt(10);
        phoneBuf.append(phoneDigit);
      }
      final String phone = phoneBuf.toString();
      phoneMap.put(phonetype, phone);
    }

    final RecordValue addressRec = row.putRecord("address");

    final int streetNumber = secureRandom.nextInt(99999);
    final int streetNameIndx = secureRandom.nextInt(STREET_NAMES.length);
    final int unitNumber = secureRandom.nextInt(11) - 1;
    final int cityNameIndx = secureRandom.nextInt(CITY_NAMES.length);
    final int stateIndx = secureRandom.nextInt(STATE_ABBREVIATIONS.length);

    addressRec.put("number", streetNumber);
    addressRec.put("street", STREET_NAMES[streetNameIndx]);
    addressRec.put("unit", unitNumber);
    addressRec.put("city", CITY_NAMES[cityNameIndx]);
    addressRec.put("state", STATE_ABBREVIATIONS[stateIndx]);
    addressRec.put("zip", Integer.parseInt(zipStr));

    /* Generate vehicleinfo */
    final ArrayValue vehicleArray = row.putArray("vehicleinfo");

    final int nVehicles = 1 + secureRandom.nextInt(3);

    for (int i = 0; i < nVehicles; i++) {

      final RecordValue vehicleRec = vehicleArray.addRecord();

      final int typeIndx = secureRandom.nextInt(TYPES.length);
      final int makeIndx = secureRandom.nextInt(MAKES.length);
      final int classIndx = secureRandom.nextInt(CLASSES.length);
      final int colorIndx = secureRandom.nextInt(COLORS.length);

      final String type = TYPES[typeIndx];
      final String make = MAKES[makeIndx];
      final String vClass = CLASSES[classIndx];
      final String color = COLORS[colorIndx];

      String[] models = MODELS_FORD_AUTO;
      float valueMult = 2.0f;

      if ("suv".equals(type)) {
        valueMult = 4.0f;
        if ("Chrysler".equals(make)) {
          models = MODELS_CHRYSLER_SUV;
        } else if ("GM".equals(make)) {
          models = MODELS_GM_SUV;
        } else {
          /* Default to make "Ford" */
          models = MODELS_FORD_SUV;
        }
      } else if ("truck".equals(type)) {
        valueMult = 3.0f;
        if ("Chrysler".equals(make)) {
          models = MODELS_CHRYSLER_TRUCK;
        } else if ("GM".equals(make)) {
          models = MODELS_GM_TRUCK;
        } else {
          /* Default to make "Ford" */
          models = MODELS_FORD_TRUCK;
        }
      } else {
        /* Default to type "auto" */
        if ("Chrysler".equals(make)) {
          models = MODELS_CHRYSLER_AUTO;
        } else if ("GM".equals(make)) {
          models = MODELS_GM_AUTO;
        }
      }
      final int modelIndx = secureRandom.nextInt(models.length);
      final String model = models[modelIndx];

      final float baseValue = 10371.59f;
      final float deltaValue = secureRandom.nextFloat();
      final float value = (valueMult * baseValue) + deltaValue;

      final double tax = TAX_RATE * value;

      boolean paid = true;
      final int paidOrNot = secureRandom.nextInt(2);
      if (paidOrNot == 0) {
        paid = false;
      }

      vehicleRec.put("type", type);
      vehicleRec.put("make", make);
      vehicleRec.put("model", model);
      vehicleRec.put("class", vClass);
      vehicleRec.put("color", color);
      vehicleRec.put("value", value);
      vehicleRec.put("tax", tax);
      vehicleRec.put("paid", paid);
    }

    /* Handle duplicates. */
    final PrimaryKey dupKey = row.createPrimaryKey();
    final Row dupRow = tableAPI.get(dupKey, null);
    if (dupRow != null) {
      /* Change the ssn */
      int newSsnDigit = secureRandom.nextInt(10);
      final StringBuilder newSsnBuf = new StringBuilder(newSsnDigit);
      for (int i = 0; i < 8; i++) {
        newSsnDigit = secureRandom.nextInt(10);
        newSsnBuf.append(newSsnDigit);
      }
      row.put("ssn", Long.parseLong(newSsnBuf.toString()));
    }

    /* Finally, write the row created above to the table */
    tableAPI.putIfAbsent(row, null, null);
  }