private static void saveRecordToMap(Record record) {
   if (recordMap.get(record.getRecordType()) == null) {
     ArrayList<Record> recordList = new ArrayList<>();
     recordList.add(record);
     recordMap.put(record.getRecordType(), recordList);
   } else {
     ArrayList<Record> recordList = recordMap.get(record.getRecordType());
     recordList.add(record);
   }
 }
  public static void beginParsing() throws IOException {
    recordMap = new HashMap<>();
    tableMap = new Maps().generateTableMap();
    File vaxFile = new File(FILE_PATH + File.separator + FILE_NAME);

    BufferedReader theFile = new BufferedReader(new FileReader(vaxFile));
    String line = "";
    int lineNum = 0;
    while ((line = theFile.readLine()) != null) {
      lineNum++;
      parseLine(lineNum, line);
    }

    // Close the BufferedReader.
    if (theFile != null) {
      theFile.close();
    }
    // Print map contents:
    writeToLog("\n--------------- PARSING FINISHED ---------------\n");

    // writeToFile("Record Map values: ");
    for (Entry<Integer, ArrayList<Record>> entry : recordMap.entrySet()) {
      // writeToFile("\nRecord type: " + entry.getKey());
      for (Record record : entry.getValue()) {
        /*writeToFile("\tType: " + record.getRecordType() + " Action: "
        + record.getActionCode() + " Ref: " + record.getReferenceId());*/
        // + " Record: " + record.getText());
        String action = "";
        switch (record.getActionCode()) {
          case 51:
            action = "INSERT";
            break;
          case 53:
            action = "UPDATE";
            break;
          case 54:
            action = "DELETE";
            break;
          default:
            action = "NOTRECOGNIZED";
            break;
        }
        writeToFile(
            tableMap.get(record.getRecordType()) + "," + action + "," + record.getReferenceId());
      }
    }

    // Close Buffered Writers.
    if (theLog != null) {
      theLog.close();
    }
    if (theOutputFile != null) {
      theOutputFile.close();
    }
  }
  public static void parseLine(int lineNum, String line) throws IOException {
    MessageHeader msgHdr = new MessageHeader(line.substring(0, 12));
    MessageTypeHeader msgTypeHdr = new MessageTypeHeader(line.substring(12, 104));

    int declaredMessageLength = msgHdr.getMessageLength();
    int declaredRecordDataLength = msgTypeHdr.getRecordDataLength();

    // Validation to ensure the line contents match the lengths declaration.
    // 12 chars before the message length declaration.
    // 104 chars before the record data length declaration.
    if (declaredMessageLength + 12 == line.length()
        && declaredRecordDataLength + 104 == line.length()) {

      writeToLog(lineNum + ": " + "Declared lengths match the actual line length.");

      int currPos = 104;
      while (true) {

        RecordHeader rcdHdr = new RecordHeader(line.substring(currPos, currPos + 20));
        int recordLength = rcdHdr.getRecordLength();

        currPos = currPos + 20;

        Record record;

        if (keyMap.get(rcdHdr.getRecordType()) != null) {
          // String recordText = line.substring(currPos - 1, currPos + recordLength - 1);
          String recordText = line.substring(currPos, currPos + recordLength);
          String recordReferenceId = "|";

          // Build the reference identifier based on the record keys defined in the Key Map
          for (Key key : keyMap.get(rcdHdr.getRecordType())) {
            recordReferenceId =
                recordReferenceId
                    + recordText.substring(
                        key.getKeyPosition(), key.getKeyPosition() + key.getKeyLength())
                    + "|";
          }

          record =
              new Record(
                  rcdHdr.getRecordType(),
                  recordReferenceId,
                  rcdHdr.getRecordActionCode(),
                  recordText);
          writeToLog(record.getText().length() + ": " + record.getText());
          saveRecordToMap(record);

        } else {
          writeToLog("Record type " + rcdHdr.getRecordType() + " is not supported.");
        }

        currPos = currPos + recordLength;

        if (currPos >= line.length()) {
          break;
        }
      }

    } else {
      writeToLog(
          lineNum
              + ": "
              + "Line is invalid. Declared message lengths do not match the actual length.");
    }
  }