|
| 1 | +package cz.muni.fi.pa165.sampledata; |
| 2 | + |
| 3 | +import cz.muni.fi.pa165.sampledata.SampleDataLoadingFacade; |
| 4 | +import cz.muni.fi.pa165.soccermanager.entity.*; |
| 5 | +import cz.muni.fi.pa165.soccermanager.enums.NationalityEnum; |
| 6 | +import cz.muni.fi.pa165.soccermanager.enums.PositionEnum; |
| 7 | +import cz.muni.fi.pa165.soccermanager.enums.StadiumEnum; |
| 8 | +import cz.muni.fi.pa165.soccermanager.service.*; |
| 9 | +import org.springframework.stereotype.Component; |
| 10 | +import org.springframework.transaction.annotation.Transactional; |
| 11 | + |
| 12 | +import java.io.IOException; |
| 13 | +import java.time.LocalDate; |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.List; |
| 16 | + |
| 17 | +/** |
| 18 | + * @author 445720 Martin Hamernik |
| 19 | + * @version 12/2/2017. |
| 20 | + */ |
| 21 | +@Component |
| 22 | +@Transactional |
| 23 | +public class SampleDataLoadingFacadeImpl implements SampleDataLoadingFacade { |
| 24 | + |
| 25 | + private final TeamService teamService; |
| 26 | + private final PlayerService playerService; |
| 27 | + private final LeagueService leagueService; |
| 28 | + private final ManagerService managerService; |
| 29 | + private final MatchService matchService; |
| 30 | + |
| 31 | + public SampleDataLoadingFacadeImpl(TeamService teamService, |
| 32 | + PlayerService playerService, |
| 33 | + LeagueService leagueService, |
| 34 | + ManagerService managerService, |
| 35 | + MatchService matchService) { |
| 36 | + this.teamService = teamService; |
| 37 | + this.playerService = playerService; |
| 38 | + this.leagueService = leagueService; |
| 39 | + this.managerService = managerService; |
| 40 | + this.matchService = matchService; |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + @SuppressWarnings("unused") |
| 45 | + public void loadData() throws IOException { |
| 46 | + List<League> leagues = new ArrayList<>(); |
| 47 | + List<Manager> managers = new ArrayList<>(); |
| 48 | + List<Player> players = new ArrayList<>(); |
| 49 | + List<Team> teams = new ArrayList<>(); |
| 50 | + List<Match> matches = new ArrayList<>(); |
| 51 | + |
| 52 | + League premierLeague = new League.LeagueBuilder("Premier League", NationalityEnum.England).build(); |
| 53 | + leagues.add(premierLeague); |
| 54 | + League ligaSantander = new League.LeagueBuilder("La Liga Santander", NationalityEnum.Spain).build(); |
| 55 | + leagues.add(ligaSantander); |
| 56 | + League bundesliga = new League.LeagueBuilder("Bundesliga", NationalityEnum.Germany).build(); |
| 57 | + leagues.add(bundesliga); |
| 58 | + |
| 59 | + Manager mourinho = new Manager. ManagerBuilder( "Jose Mourinho", NationalityEnum. Portugal, "[email protected]"). build(); |
| 60 | + managers.add(mourinho); |
| 61 | + Manager guardiola = new Manager. ManagerBuilder( "Pep Guardiola", NationalityEnum. Spain, "[email protected]"). build(); |
| 62 | + managers.add(guardiola); |
| 63 | + Manager klopp = new Manager. ManagerBuilder( "Jurgen Klopp", NationalityEnum. Germany, "[email protected]"). build(); |
| 64 | + managers.add(klopp); |
| 65 | + Manager admin = new Manager. ManagerBuilder( "admin", NationalityEnum. Yemen, "[email protected]", true). build(); |
| 66 | + managers.add(admin); |
| 67 | + |
| 68 | + Player pogba = new Player.PlayerBuilder( |
| 69 | + "Paul Pogba", |
| 70 | + PositionEnum.MIDFIELDER, |
| 71 | + NationalityEnum.France, |
| 72 | + LocalDate.of(1990,1,1)) |
| 73 | + .number(8) |
| 74 | + .passing(90) |
| 75 | + .shooting(80) |
| 76 | + .speed(70) |
| 77 | + .defence(60) |
| 78 | + .strength(90) |
| 79 | + .goalkeeping(5) |
| 80 | + .build(); |
| 81 | + players.add(pogba); |
| 82 | + |
| 83 | + Player lukaku = new Player.PlayerBuilder( |
| 84 | + "Romelu Lukaku", |
| 85 | + PositionEnum.ATTACKER, |
| 86 | + NationalityEnum.Belgium, |
| 87 | + LocalDate.of(1992,1,1)) |
| 88 | + .number(9) |
| 89 | + .passing(60) |
| 90 | + .shooting(90) |
| 91 | + .speed(60) |
| 92 | + .defence(50) |
| 93 | + .strength(95) |
| 94 | + .goalkeeping(5) |
| 95 | + .build(); |
| 96 | + players.add(lukaku); |
| 97 | + |
| 98 | + Player degea = new Player.PlayerBuilder( |
| 99 | + "David Degea", |
| 100 | + PositionEnum.GOALKEEPER, |
| 101 | + NationalityEnum.Spain, |
| 102 | + LocalDate.of(1989,1,1)) |
| 103 | + .number(1) |
| 104 | + .passing(10) |
| 105 | + .shooting(5) |
| 106 | + .speed(50) |
| 107 | + .defence(10) |
| 108 | + .strength(30) |
| 109 | + .goalkeeping(95) |
| 110 | + .build(); |
| 111 | + players.add(degea); |
| 112 | + |
| 113 | + Player debruyne = new Player.PlayerBuilder( |
| 114 | + "Kevin De Bruyne", |
| 115 | + PositionEnum.MIDFIELDER, |
| 116 | + NationalityEnum.Belgium, |
| 117 | + LocalDate.of(1990,1,1)) |
| 118 | + .number(10) |
| 119 | + .passing(95) |
| 120 | + .shooting(90) |
| 121 | + .speed(65) |
| 122 | + .defence(60) |
| 123 | + .strength(70) |
| 124 | + .goalkeeping(5) |
| 125 | + .build(); |
| 126 | + players.add(debruyne); |
| 127 | + |
| 128 | + Player aguero = new Player.PlayerBuilder( |
| 129 | + "Sergio Aguero", |
| 130 | + PositionEnum.ATTACKER, |
| 131 | + NationalityEnum.Argentina, |
| 132 | + LocalDate.of(1992,1,1)) |
| 133 | + .number(9) |
| 134 | + .passing(70) |
| 135 | + .shooting(90) |
| 136 | + .speed(80) |
| 137 | + .defence(40) |
| 138 | + .strength(70) |
| 139 | + .goalkeeping(5) |
| 140 | + .build(); |
| 141 | + players.add(aguero); |
| 142 | + |
| 143 | + Team united = new Team.TeamBuilder( |
| 144 | + "Manchester United", |
| 145 | + NationalityEnum.England, |
| 146 | + StadiumEnum.Old_Trafford, |
| 147 | + premierLeague |
| 148 | + ).build(); |
| 149 | + united.addPlayer(pogba); |
| 150 | + united.addPlayer(lukaku); |
| 151 | + united.addPlayer(degea); |
| 152 | + united.setManager(mourinho); |
| 153 | + teams.add(united); |
| 154 | + |
| 155 | + Team city = new Team.TeamBuilder( |
| 156 | + "Manchester City", |
| 157 | + NationalityEnum.England, |
| 158 | + StadiumEnum.Etihad_Stadium, |
| 159 | + premierLeague |
| 160 | + ).build(); |
| 161 | + city.addPlayer(aguero); |
| 162 | + city.addPlayer(debruyne); |
| 163 | + city.setManager(guardiola); |
| 164 | + teams.add(city); |
| 165 | + |
| 166 | + loadToDb(leagues, managers, players, teams, matches); |
| 167 | + } |
| 168 | + |
| 169 | + private void loadToDb(List<League> leagues, |
| 170 | + List<Manager> managers, |
| 171 | + List<Player> players, |
| 172 | + List<Team> teams, |
| 173 | + List<Match> matches) { |
| 174 | + |
| 175 | + for (Player player: players) { |
| 176 | + playerService.create(player); |
| 177 | + } |
| 178 | + |
| 179 | + for (Manager manager: managers) { |
| 180 | + managerService.create(manager, manager.getName()); |
| 181 | + } |
| 182 | + |
| 183 | + for (League league: leagues) { |
| 184 | + leagueService.insert(league); |
| 185 | + } |
| 186 | + |
| 187 | + for (Team team : teams) { |
| 188 | + teamService.create(team); |
| 189 | + } |
| 190 | + |
| 191 | + for (Match match: matches) { |
| 192 | + matchService.createMatch(match); |
| 193 | + } |
| 194 | + |
| 195 | + for (League league: leagueService.fetchAll()) { |
| 196 | + league.setMatches(matchService.fetchByLeague(league)); |
| 197 | + } |
| 198 | + } |
| 199 | +} |
0 commit comments