Example usage for com.google.common.collect BiMap toString

List of usage examples for com.google.common.collect BiMap toString

Introduction

In this page you can find the example usage for com.google.common.collect BiMap toString.

Prototype

public String toString() 

Source Link

Document

Returns a string representation of the object.

Usage

From source file:ca.wumbo.wdl.Start.java

/**
 * Takes the info in a command line parser and pushes in the game.
 * /* ww w. jav  a  2 s  .  c o m*/
 * @param clp
 *       The command line parser.
 * 
 * @throws SQLException
 *       If there is any SQL errors.
 *  
 * @throws JAXBException
 *      If there is a parsing error.
 *  
 * @throws IOException 
 *       If an IO error occured or a file couldn't be found. This works for
 *       both the XML and log files currently.
 * 
 * @throws MissingGameInDatabaseException
 *       If the game is not in the database.
 * 
 * @throws RuntimeException
 *       If some unexpected exception occurs while closing.
 */
private static void pushGameLogToDatabase(CommandLineParser clp)
        throws JAXBException, SQLException, IOException, MissingGameInDatabaseException {
    assert clp != null;

    Parser parser = new Parser();

    log.trace("Reading in log files and map IDs.");
    String logName = "";
    try {
        for (Pair<String, Integer> fileAndMap : clp.getLogFiles()) {
            logName = fileAndMap.getFirst();
            int mapId = fileAndMap.getSecond();
            log.debug("Reading in {} (id = {}).", logName, mapId);
            parser.addRoundPath(logName, mapId);
        }
    } catch (IOException e) {
        log.error("Error reading the a log file: {}", logName);
        throw e;
    }

    log.trace("Connecting to database to push in log information.");
    try (LogEventSubmitter connection = new LogEventSubmitter()) {
        int dbGameId = clp.getGameId();
        int redTeamId = clp.getRedTeamId();
        int blueTeamId = clp.getBlueTeamId();
        BiMap<Integer, String> idToPlayerMap = HashBiMap.create();
        idToPlayerMap.putAll(clp.getPlayers());
        log.debug("Pushing in game ID = {} for (red ID = {}, blue ID = {}).", dbGameId, redTeamId, blueTeamId);
        log.trace("PlayerMap = {}", idToPlayerMap.toString());
        connection.pushEventsToDatabase(dbGameId, parser, idToPlayerMap, redTeamId, blueTeamId);
    } catch (FileNotFoundException e) {
        throw new IOException(e);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:ca.wumbo.wdl.database.LogEventSubmitter.java

/**
 * Adds all the data to insert into the database into the statement in a
 * batch format such that at the end of everything, the commands can all
 * be sent as one./*from  w w w  .j av  a2 s .c om*/
 * 
 * @param rp
 *       The stat round parser to extract events from.
 * 
 * @param idToPlayerMap
 *       A map of id to player names that will be used for matching log
 *       names to a player. 
 * 
 * @param roundId
 *       The round ID of the event data.
 * 
 * @param redTeamId
 *       The ID of the red team.
 * 
 * @throws SQLException
 *       If there is any exception calling the query or the database fails
 *       to create the necessary rows.
 */
private void pushEventsBatch(RoundParser rp, BiMap<Integer, String> idToPlayerMap, int roundId, int redTeamId,
        int blueTeamId) throws SQLException {
    assert rp != null;
    assert idToPlayerMap != null;
    assert idToPlayerMap.size() > 0;
    assert roundId >= 0;
    assert redTeamId >= 0;
    assert blueTeamId >= 0;

    try (Statement stmt = connection.createStatement()) {
        int eventLogId = -1;

        // Put in the header and remember the event log ID.
        String query = rp.getHeaderData().getSqlInsertionQuery("wdl", "event_logs", roundId, redTeamId,
                blueTeamId);
        log.trace(">>> {}", query);
        stmt.executeUpdate(query, Statement.RETURN_GENERATED_KEYS);
        ResultSet rs = stmt.getGeneratedKeys();
        if (!rs.next()) {
            throw new SQLException(
                    "Unexpected end of result set, should have gotten back the key for the event log ID.");
        }
        eventLogId = rs.getInt(1);
        log.trace("Event log ID retrieved: {}", eventLogId);
        if (eventLogId == -1) {
            throw new SQLException("Commit did not yield an event log.");
        }

        // Everything from now on in can go into a single batch command.
        // Put in the players.
        log.debug("Inserting players into event player table.");
        BiMap<String, Integer> playerToIdMap = idToPlayerMap.inverse();
        log.trace("PlayerToIdMap = {}", playerToIdMap.toString());
        for (Player player : rp.getPlayers()) {
            // Getting the player ID shouldn't fail because we've checked
            // previously to make sure the player name and ID are valid.
            String playerLogName = player.getName();
            int playerId = playerToIdMap.get(playerLogName);
            int colorNumber = player.getTeam().ordinal();
            String insertQuery = String.format("INSERT INTO `%s`.`%s`(%s) VALUES(%d, %d, \"%s\", %d)", "wdl",
                    "event_players", "fk_event_log_id, fk_player_id, player_log_name, color_number", eventLogId,
                    playerId, playerLogName, colorNumber);
            log.trace(">>> {}", insertQuery);
            stmt.addBatch(insertQuery);
        }
        log.debug("Committing player queries...");
        stmt.executeBatch();
        log.debug("Committed player batch events.");

        // Put in the events.
        log.debug("Inserting events into event table.");
        for (Event e : rp.getEvents()) {
            String queryStr = e.getSqlInsertionQuery("wdl", "events", idToPlayerMap.inverse(), eventLogId);
            log.trace(">>> {}", queryStr);
            stmt.addBatch(queryStr);
        }

        // Everything is in, deploy it now.
        log.debug("Committing event queries...");
        stmt.executeBatch();
        log.debug("All event query events done.");
    }
}