private CouchDbPlayer copyPlayer(IPlayer player) { if (player == null) { return null; } int playerId = player.getId(); CouchDbPlayer persistentPlayer; if (containsPlayerById(playerId)) { // The Object already exists within the session persistentPlayer = (CouchDbPlayer) db.find(CouchDbPlayer.class, String.valueOf(playerId)); } else { // A new database entry persistentPlayer = new CouchDbPlayer(); } persistentPlayer.setId(String.valueOf(playerId)); persistentPlayer.setName(player.getName()); persistentPlayer.setWinsTotal(player.getWinsTotal()); persistentPlayer.setLossesTotal(player.getLossesTotal()); // System.out.println(player.getResults()); List<IPersistentResult> list = new ArrayList<>(); for (IResult r : player.getResults()) { list.add(new CouchDbResult(r.getEnemyId(), r.getWins(), r.getLosses())); } persistentPlayer.setResults(list); return persistentPlayer; }
@Override public IPlayer getPlayerById(int id) { CouchDbPlayer p = db.find(CouchDbPlayer.class, String.valueOf(id)); if (p == null) { return null; } return copyPlayer(p); }