Пример #1
0
  /**
   * Takes the provided array of objects and stores the contents in the back-end database. The array
   * is expected to contain the following objects:
   *
   * <ul>
   *   <li>0 - Severity (String)
   *   <li>1 - PSE node name (String)
   *   <li>2 - Link ID (String)
   *   <li>3 - Received Error Message (String)
   *   <li>4 - Date received from the PSE (String)
   *   <li>5 - The universally unique identifier of the message (String)
   * </ul>
   *
   * @param dataToStore An object array in the format mentioned above.
   */
  public void storeDataInDb(Object[] dataToStore) {
    final int WORKED_OK = 1;
    final int DISCOVERED_NEW_LINK = 2;
    final int DISCOVERED_DUPLICATES = 3;
    final int ERROR_ENCOUNTERED = 4;
    final int NO_ENTRIES_FOUND = 5;
    final int DISCOVERED_LINK_MESSAGE = 6;

    DbInterface dbI = new DbInterface(originatingPort);
    int x = dbI.storeMessage(formattedMessage);
    switch (x) {
      case WORKED_OK:
        {
          // storeMessage worked without any problems.
          log.finest("Message saved in the database with a UUID of: " + formattedMessage[5]);
          String msg = (String) formattedMessage[3];
          Object[] linkState = new Object[3];
          linkState[0] = formattedMessage[1];
          linkState[1] = formattedMessage[2];
          linkState[2] = new Integer(LINK_STATE_UNKNOWN);
          if (msg.equals("LINK DOWN")) {
            linkState[2] = new Integer(LINK_STATE_DOWN);
            parent.setLinkData(linkState);
          }
          if (msg.equals("LINK UP")) {
            linkState[2] = new Integer(LINK_STATE_UP);
            parent.setLinkData(linkState);
          }
          break;
        }
      case DISCOVERED_NEW_LINK:
        {
          // storeMessage detected that this link was from a new link not in the database.
          String msg = (String) formattedMessage[3];
          if (msg.equals("LINK DOWN")) {
            parent.addNewLink(
                (String) formattedMessage[1], (String) formattedMessage[2], LINK_STATE_DOWN);
          } else if (msg.equals("LINK UP")) {
            parent.addNewLink(
                (String) formattedMessage[1], (String) formattedMessage[2], LINK_STATE_UP);
          } else {
            parent.addNewLink(
                (String) formattedMessage[1], (String) formattedMessage[2], LINK_STATE_UNKNOWN);
          }
          break;
        }
      case DISCOVERED_DUPLICATES:
        {
          // storeMessage detected duplicates in the database. Generate a warning to the user.
          log.severe(
              "Duplicates in the index of tblCodexLinks were detected while trying to save the received message.");
          System.err.println(
              "Duplicates were detected in the index of tblCodexLinks. Your database may be corrupt.");
          break;
        }
      case ERROR_ENCOUNTERED:
        {
          // storeMessage encountered an exception
          log.warning(
              "storeMessage() encountered an exception. The message it was trying to save may not have been entered into the database.");
          System.err.println(
              "An exception was detected while trying to save the message in the database. The data may not have been saved, please check the logs for more information.");
          break;
        }
      case DISCOVERED_LINK_MESSAGE:
        {
          String msg = (String) formattedMessage[3];
          if (msg.equals("LINK DOWN")) {
            parent.addNewLink(
                (String) formattedMessage[1], (String) formattedMessage[2], LINK_STATE_DOWN);
          } else if (msg.equals("LINK UP")) {
            parent.addNewLink(
                (String) formattedMessage[1], (String) formattedMessage[2], LINK_STATE_UP);
          } else {
            parent.addNewLink(
                (String) formattedMessage[1], (String) formattedMessage[2], LINK_STATE_UNKNOWN);
          }
          break;
        }
      default:
        {
          // storeMessage returned a value that wasn't recognised
          log.warning("storeMessage returned an unexpected value: " + x);
          break;
        }
    }
  }