@Override public void executeTask(JobExecutionContext jobExecutionContext) { playerRepository = settingsProvider.getPlayerRepository(); formerBidPlayersContainerRepository = settingsProvider.getFormerBidPlayersContainerRepository(); playerBidderManager = settingsProvider.getPlayerBidder(); List<Player> playersOnOurList = IteratorUtils.toList(playerRepository.findAll().iterator()); log("There are " + playersOnOurList.size() + " players on our list"); refreshPlayers(playersOnOurList); Date currentDate = settingsProvider.getCurrentDate().getTime(); log("Current date is : " + DugoutUtils.getDateInRomania(currentDate)); List<Player> playersWithDeadlineNotExpired = filterPlayers(playersOnOurList); log(playersWithDeadlineNotExpired.size() + " players have deadline not expired."); for (Player player : playersWithDeadlineNotExpired) { long seconds = DugoutUtils.getSecondsBetween(currentDate, player.getExpiryDate()); if (seconds <= EXPIRY_SECONDS) { if (canBidOnPlayer(player)) { long valueToBid = player.getSellingValue() + 1000; log("Bidding " + valueToBid + " on player : " + player); playerNavigator.bidOnPlayer(player); } playerNavigator.refreshPlayer(player); } } List<Player> playersToBeRemoved = ListUtils.subtract(playersOnOurList, playersWithDeadlineNotExpired); for (Player player : playersWithDeadlineNotExpired) { if (player.isOverBiddingValue()) { log("Player is over bidding value and will be removed from our list : " + player); playersToBeRemoved.add(player); } } List<Player> playersToBeSaved = ListUtils.subtract(playersOnOurList, playersToBeRemoved); log( "Saving players with deadline not expired on database : " + playersToBeSaved.size() + " players."); playerRepository.save(playersToBeSaved); if (playersToBeRemoved.size() > 0) { log("Moving " + playersToBeRemoved.size() + " to list of former bid players."); FormerBidPlayersContainer container = getFormerBidPlayersContainer(); container.addPlayers(playersToBeRemoved); formerBidPlayersContainerRepository.save(container); playerRepository.delete(playersToBeRemoved); } log("Notifying PlayerBidderManager component."); playerBidderManager.notify(playersWithDeadlineNotExpired); }
private void refreshPlayers(List<Player> players) { Date currentDate = settingsProvider.getCurrentDate().getTime(); for (Player player : players) { boolean expired = player.getExpiryDate() != null && player.getExpiryDate().before(currentDate); boolean isExpiringSoon = DugoutUtils.getSecondsBetween(player.getExpiryDate(), currentDate) <= EXPIRY_SECONDS; if (expired || isExpiringSoon) { playerNavigator.refreshPlayer(player); } } }