From 18661ece28152840672b4579f6a2de83adbcd66f Mon Sep 17 00:00:00 2001 From: Pankaj Yadav Date: Mon, 12 Apr 2021 22:44:04 +0530 Subject: [PATCH 1/4] Adds and uses lombok dependency --- pom.xml | 15 ++ .../controller/TeamController.java | 16 +- .../ipldashboard/data/MatchDataProcessor.java | 36 +---- .../ipldashboard/data/MatchInput.java | 147 +++++------------- .../javabrains/ipldashboard/model/Match.java | 97 ++---------- .../javabrains/ipldashboard/model/Team.java | 51 +----- 6 files changed, 79 insertions(+), 283 deletions(-) diff --git a/pom.xml b/pom.xml index c680840..7222c10 100644 --- a/pom.xml +++ b/pom.xml @@ -30,6 +30,12 @@ spring-boot-starter-web + + org.projectlombok + lombok + true + + org.springframework.boot spring-boot-devtools @@ -51,6 +57,7 @@ spring-batch-test test + @@ -58,6 +65,14 @@ org.springframework.boot spring-boot-maven-plugin + + + + org.projectlombok + lombok + + + diff --git a/src/main/java/io/javabrains/ipldashboard/controller/TeamController.java b/src/main/java/io/javabrains/ipldashboard/controller/TeamController.java index 8c3f4c1..b2bf09f 100644 --- a/src/main/java/io/javabrains/ipldashboard/controller/TeamController.java +++ b/src/main/java/io/javabrains/ipldashboard/controller/TeamController.java @@ -3,6 +3,7 @@ import java.time.LocalDate; import java.util.List; +import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @@ -16,16 +17,11 @@ @RestController @CrossOrigin +@RequiredArgsConstructor public class TeamController { - private TeamRepository teamRepository; - private MatchRepository matchRepository; - - public TeamController(TeamRepository teamRepository, MatchRepository matchRepository) { - this.teamRepository = teamRepository; - this.matchRepository = matchRepository; - } - + private final TeamRepository teamRepository; + private final MatchRepository matchRepository; @GetMapping("/team") public Iterable getAllTeam() { @@ -35,9 +31,7 @@ public Iterable getAllTeam() { @GetMapping("/team/{teamName}") public Team getTeam(@PathVariable String teamName) { Team team = this.teamRepository.findByTeamName(teamName); - team.setMatches(matchRepository.findLatestMatchesbyTeam(teamName,4)); - - return team; + return team.toBuilder().matches(matchRepository.findLatestMatchesbyTeam(teamName,4)).build(); } @GetMapping("/team/{teamName}/matches") diff --git a/src/main/java/io/javabrains/ipldashboard/data/MatchDataProcessor.java b/src/main/java/io/javabrains/ipldashboard/data/MatchDataProcessor.java index 260c2a3..5ae1426 100644 --- a/src/main/java/io/javabrains/ipldashboard/data/MatchDataProcessor.java +++ b/src/main/java/io/javabrains/ipldashboard/data/MatchDataProcessor.java @@ -16,41 +16,7 @@ public class MatchDataProcessor implements ItemProcessor { @Override public Match process(final MatchInput matchInput) throws Exception { - - Match match = new Match(); - match.setId(Long.parseLong(matchInput.getId())); - match.setCity(matchInput.getCity()); - - match.setDate(LocalDate.parse(matchInput.getDate())); - - match.setPlayerOfMatch(matchInput.getPlayer_of_match()); - match.setVenue(matchInput.getVenue()); - - // Set Team 1 and Team 2 depending on the innings order - String firstInningsTeam, secondInningsTeam; - - if ("bat".equals(matchInput.getToss_decision())) { - firstInningsTeam = matchInput.getToss_winner(); - secondInningsTeam = matchInput.getToss_winner().equals(matchInput.getTeam1()) - ? matchInput.getTeam2() : matchInput.getTeam1(); - - } else { - secondInningsTeam = matchInput.getToss_winner(); - firstInningsTeam = matchInput.getToss_winner().equals(matchInput.getTeam1()) - ? matchInput.getTeam2() : matchInput.getTeam1(); - } - match.setTeam1(firstInningsTeam); - match.setTeam2(secondInningsTeam); - - match.setTossWinner(matchInput.getToss_winner()); - match.setTossDecision(matchInput.getToss_decision()); - match.setMatchWinner(matchInput.getWinner()); - match.setResult(matchInput.getResult()); - match.setResultMargin(matchInput.getResult_margin()); - match.setUmpire1(matchInput.getUmpire1()); - match.setUmpire2(matchInput.getUmpire2()); - - return match; + return matchInput.toMatch(); } } \ No newline at end of file diff --git a/src/main/java/io/javabrains/ipldashboard/data/MatchInput.java b/src/main/java/io/javabrains/ipldashboard/data/MatchInput.java index ba52eab..6b0497f 100644 --- a/src/main/java/io/javabrains/ipldashboard/data/MatchInput.java +++ b/src/main/java/io/javabrains/ipldashboard/data/MatchInput.java @@ -1,5 +1,17 @@ package io.javabrains.ipldashboard.data; +import io.javabrains.ipldashboard.model.Match; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.time.LocalDate; + +@AllArgsConstructor +@NoArgsConstructor +@Builder +@Data public class MatchInput { private String id; private String city; @@ -18,110 +30,37 @@ public class MatchInput { private String method; private String umpire1; private String umpire2; - - public String getId() { - return id; - } - public void setId(String id) { - this.id = id; - } - public String getCity() { - return city; - } - public void setCity(String city) { - this.city = city; - } - public String getDate() { - return date; - } - public void setDate(String date) { - this.date = date; - } - public String getPlayer_of_match() { - return player_of_match; - } - public void setPlayer_of_match(String player_of_match) { - this.player_of_match = player_of_match; - } - public String getVenue() { - return venue; - } - public void setVenue(String venue) { - this.venue = venue; - } - public String getNeutral_venue() { - return neutral_venue; - } - public void setNeutral_venue(String neutral_venue) { - this.neutral_venue = neutral_venue; - } - public String getTeam1() { - return team1; - } - public void setTeam1(String team1) { - this.team1 = team1; - } - public String getTeam2() { - return team2; - } - public void setTeam2(String team2) { - this.team2 = team2; - } - public String getToss_winner() { - return toss_winner; - } - public void setToss_winner(String toss_winner) { - this.toss_winner = toss_winner; - } - public String getToss_decision() { - return toss_decision; - } - public void setToss_decision(String toss_decision) { - this.toss_decision = toss_decision; - } - public String getWinner() { - return winner; - } - public void setWinner(String winner) { - this.winner = winner; - } - public String getResult() { - return result; - } - public void setResult(String result) { - this.result = result; - } - public String getResult_margin() { - return result_margin; - } - public void setResult_margin(String result_margin) { - this.result_margin = result_margin; - } - public String getEliminator() { - return eliminator; - } - public void setEliminator(String eliminator) { - this.eliminator = eliminator; - } - public String getMethod() { - return method; - } - public void setMethod(String method) { - this.method = method; - } - public String getUmpire1() { - return umpire1; - } - public void setUmpire1(String umpire1) { - this.umpire1 = umpire1; - } - public String getUmpire2() { - return umpire2; - } - public void setUmpire2(String umpire2) { - this.umpire2 = umpire2; - } - + public Match toMatch() { + String firstInningsTeam, secondInningsTeam; + + if ("bat".equals(this.getToss_decision())) { + firstInningsTeam = this.getToss_winner(); + secondInningsTeam = this.getToss_winner().equals(this.getTeam1()) + ? this.getTeam2() : this.getTeam1(); + } else { + secondInningsTeam = this.getToss_winner(); + firstInningsTeam = this.getToss_winner().equals(this.getTeam1()) + ? this.getTeam2() : this.getTeam1(); + } + + return Match.builder() + .id(Long.parseLong(this.getId())) + .city(this.getCity()) + .date(LocalDate.parse(this.getDate())) + .playerOfMatch(this.getPlayer_of_match()) + .venue(this.getVenue()) + .team1(firstInningsTeam) + .team2(secondInningsTeam) + .tossWinner(this.getToss_winner()) + .tossDecision(this.getToss_decision()) + .matchWinner(this.getWinner()) + .result(this.getWinner()) + .result(this.getResult()) + .resultMargin(this.getResult_margin()) + .umpire1(this.getUmpire1()) + .umpire2(this.getUmpire2()) + .build(); + } } diff --git a/src/main/java/io/javabrains/ipldashboard/model/Match.java b/src/main/java/io/javabrains/ipldashboard/model/Match.java index 7f3efc6..c52c29d 100644 --- a/src/main/java/io/javabrains/ipldashboard/model/Match.java +++ b/src/main/java/io/javabrains/ipldashboard/model/Match.java @@ -1,11 +1,20 @@ package io.javabrains.ipldashboard.model; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + import java.time.LocalDate; import javax.persistence.Entity; import javax.persistence.Id; @Entity +@Builder +@Getter +@NoArgsConstructor +@AllArgsConstructor public class Match { @Id @@ -23,92 +32,4 @@ public class Match { private String resultMargin; private String umpire1; private String umpire2; - - public long getId() { - return id; - } - public void setId(long id) { - this.id = id; - } - public String getCity() { - return city; - } - public void setCity(String city) { - this.city = city; - } - public LocalDate getDate() { - return date; - } - public void setDate(LocalDate date) { - this.date = date; - } - public String getPlayerOfMatch() { - return playerOfMatch; - } - public void setPlayerOfMatch(String playerOfMatch) { - this.playerOfMatch = playerOfMatch; - } - public String getVenue() { - return venue; - } - public void setVenue(String venue) { - this.venue = venue; - } - public String getTeam1() { - return team1; - } - public void setTeam1(String team1) { - this.team1 = team1; - } - public String getTeam2() { - return team2; - } - public void setTeam2(String team2) { - this.team2 = team2; - } - public String getTossWinner() { - return tossWinner; - } - public void setTossWinner(String tossWinner) { - this.tossWinner = tossWinner; - } - public String getTossDecision() { - return tossDecision; - } - public void setTossDecision(String tossDecision) { - this.tossDecision = tossDecision; - } - public String getMatchWinner() { - return matchWinner; - } - public void setMatchWinner(String matchWinner) { - this.matchWinner = matchWinner; - } - public String getResult() { - return result; - } - public void setResult(String result) { - this.result = result; - } - public String getResultMargin() { - return resultMargin; - } - public void setResultMargin(String resultMargin) { - this.resultMargin = resultMargin; - } - public String getUmpire1() { - return umpire1; - } - public void setUmpire1(String umpire1) { - this.umpire1 = umpire1; - } - public String getUmpire2() { - return umpire2; - } - public void setUmpire2(String umpire2) { - this.umpire2 = umpire2; - } - - - } diff --git a/src/main/java/io/javabrains/ipldashboard/model/Team.java b/src/main/java/io/javabrains/ipldashboard/model/Team.java index 4d1c1ea..050aeba 100644 --- a/src/main/java/io/javabrains/ipldashboard/model/Team.java +++ b/src/main/java/io/javabrains/ipldashboard/model/Team.java @@ -1,5 +1,7 @@ package io.javabrains.ipldashboard.model; +import lombok.*; + import java.util.List; import javax.persistence.Entity; @@ -9,6 +11,10 @@ import javax.persistence.Transient; @Entity +@Builder(toBuilder = true) +@Data +@NoArgsConstructor +@AllArgsConstructor public class Team { @Id @@ -21,53 +27,8 @@ public class Team { @Transient private List matches; - - public long getId() { - return id; - } - public void setId(long id) { - this.id = id; - } - public String getTeamName() { - return teamName; - } - public void setTeamName(String teamName) { - this.teamName = teamName; - } - public long getTotalMatches() { - return totalMatches; - } - public void setTotalMatches(long totalMatches) { - this.totalMatches = totalMatches; - } - public long getTotalWins() { - return totalWins; - } - public void setTotalWins(long totalWins) { - this.totalWins = totalWins; - } public Team(String teamName, long totalMatches) { this.teamName = teamName; this.totalMatches = totalMatches; } - @Override - public String toString() { - return "Team [teamName=" + teamName + ", totalMatches=" + totalMatches + ", totalWins=" + totalWins + "]"; - } - public Team() { - - } - public List getMatches() { - return matches; - } - public void setMatches(List matches) { - this.matches = matches; - } - - - - - - - } From dd21730aef85d1d91d6737d5b93c2ca1dd037254 Mon Sep 17 00:00:00 2001 From: Pankaj Yadav Date: Tue, 13 Apr 2021 17:19:12 +0530 Subject: [PATCH 2/4] removes conversion from MatchInput to match --- .../ipldashboard/data/MatchDataProcessor.java | 43 ++++++++++++++----- .../ipldashboard/data/MatchInput.java | 32 -------------- 2 files changed, 33 insertions(+), 42 deletions(-) diff --git a/src/main/java/io/javabrains/ipldashboard/data/MatchDataProcessor.java b/src/main/java/io/javabrains/ipldashboard/data/MatchDataProcessor.java index 5ae1426..7be9a39 100644 --- a/src/main/java/io/javabrains/ipldashboard/data/MatchDataProcessor.java +++ b/src/main/java/io/javabrains/ipldashboard/data/MatchDataProcessor.java @@ -1,22 +1,45 @@ package io.javabrains.ipldashboard.data; -import java.time.LocalDate; -import java.time.LocalDateTime; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - +import io.javabrains.ipldashboard.model.Match; import org.springframework.batch.item.ItemProcessor; -import io.javabrains.ipldashboard.model.Match; +import java.time.LocalDate; public class MatchDataProcessor implements ItemProcessor { - private static final Logger log = LoggerFactory.getLogger(MatchDataProcessor.class); - @Override public Match process(final MatchInput matchInput) throws Exception { - return matchInput.toMatch(); + String firstInningsTeam, secondInningsTeam; + + if ("bat".equals(matchInput.getToss_decision())) { + firstInningsTeam = matchInput.getToss_winner(); + secondInningsTeam = matchInput.getToss_winner().equals(matchInput.getTeam1()) + ? matchInput.getTeam2() : matchInput.getTeam1(); + + } else { + secondInningsTeam = matchInput.getToss_winner(); + firstInningsTeam = matchInput.getToss_winner().equals(matchInput.getTeam1()) + ? matchInput.getTeam2() : matchInput.getTeam1(); + } + + return Match.builder() + .id(Long.parseLong(matchInput.getId())) + .city(matchInput.getCity()) + .date(LocalDate.parse(matchInput.getDate())) + .playerOfMatch(matchInput.getPlayer_of_match()) + .venue(matchInput.getVenue()) + .team1(firstInningsTeam) + .team2(secondInningsTeam) + .tossWinner(matchInput.getToss_winner()) + .tossDecision(matchInput.getToss_decision()) + .matchWinner(matchInput.getWinner()) + .result(matchInput.getWinner()) + .result(matchInput.getResult()) + .resultMargin(matchInput.getResult_margin()) + .umpire1(matchInput.getUmpire1()) + .umpire2(matchInput.getUmpire2()) + .build(); + } } \ No newline at end of file diff --git a/src/main/java/io/javabrains/ipldashboard/data/MatchInput.java b/src/main/java/io/javabrains/ipldashboard/data/MatchInput.java index 6b0497f..658fecb 100644 --- a/src/main/java/io/javabrains/ipldashboard/data/MatchInput.java +++ b/src/main/java/io/javabrains/ipldashboard/data/MatchInput.java @@ -31,36 +31,4 @@ public class MatchInput { private String umpire1; private String umpire2; - public Match toMatch() { - String firstInningsTeam, secondInningsTeam; - - if ("bat".equals(this.getToss_decision())) { - firstInningsTeam = this.getToss_winner(); - secondInningsTeam = this.getToss_winner().equals(this.getTeam1()) - ? this.getTeam2() : this.getTeam1(); - - } else { - secondInningsTeam = this.getToss_winner(); - firstInningsTeam = this.getToss_winner().equals(this.getTeam1()) - ? this.getTeam2() : this.getTeam1(); - } - - return Match.builder() - .id(Long.parseLong(this.getId())) - .city(this.getCity()) - .date(LocalDate.parse(this.getDate())) - .playerOfMatch(this.getPlayer_of_match()) - .venue(this.getVenue()) - .team1(firstInningsTeam) - .team2(secondInningsTeam) - .tossWinner(this.getToss_winner()) - .tossDecision(this.getToss_decision()) - .matchWinner(this.getWinner()) - .result(this.getWinner()) - .result(this.getResult()) - .resultMargin(this.getResult_margin()) - .umpire1(this.getUmpire1()) - .umpire2(this.getUmpire2()) - .build(); - } } From 9d8a536a95fee3d8255fa1154f1156fba105c889 Mon Sep 17 00:00:00 2001 From: Pankaj Yadav Date: Tue, 13 Apr 2021 17:20:38 +0530 Subject: [PATCH 3/4] removes unused imports --- .../ipldashboard/data/BatchConfig.java | 39 ++++----- .../JobCompletionNotificationListener.java | 79 +++++++++---------- .../ipldashboard/data/MatchInput.java | 3 - .../javabrains/ipldashboard/model/Match.java | 3 +- .../javabrains/ipldashboard/model/Team.java | 14 ++-- 5 files changed, 65 insertions(+), 73 deletions(-) diff --git a/src/main/java/io/javabrains/ipldashboard/data/BatchConfig.java b/src/main/java/io/javabrains/ipldashboard/data/BatchConfig.java index bb64c68..773af0d 100644 --- a/src/main/java/io/javabrains/ipldashboard/data/BatchConfig.java +++ b/src/main/java/io/javabrains/ipldashboard/data/BatchConfig.java @@ -1,7 +1,6 @@ package io.javabrains.ipldashboard.data; -import javax.sql.DataSource; - +import io.javabrains.ipldashboard.model.Match; import org.springframework.batch.core.Job; import org.springframework.batch.core.Step; import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; @@ -19,15 +18,15 @@ import org.springframework.context.annotation.Configuration; import org.springframework.core.io.ClassPathResource; -import io.javabrains.ipldashboard.model.Match; +import javax.sql.DataSource; @Configuration @EnableBatchProcessing public class BatchConfig { - private final String[] FIELD_NAMES = new String[] { "id", "city", "date", "player_of_match", "venue", + private final String[] FIELD_NAMES = new String[]{"id", "city", "date", "player_of_match", "venue", "neutral_venue", "team1", "team2", "toss_winner", "toss_decision", "winner", "result", "result_margin", - "eliminator", "method", "umpire1", "umpire2" }; + "eliminator", "method", "umpire1", "umpire2"}; @Autowired public JobBuilderFactory jobBuilderFactory; @@ -55,30 +54,32 @@ public MatchDataProcessor processor() { public JdbcBatchItemWriter writer(DataSource dataSource) { return new JdbcBatchItemWriterBuilder() .itemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>()) - .sql("INSERT INTO match (id, city, date, player_of_match, venue, team1, team2, toss_winner, toss_decision, match_winner, result, result_margin, umpire1, umpire2) " - + " VALUES (:id, :city, :date, :playerOfMatch, :venue, :team1, :team2, :tossWinner, :tossDecision, :matchWinner, :result, :resultMargin, :umpire1, :umpire2)") + .sql("INSERT INTO match (id, city, date, player_of_match, venue, team1, team2, toss_winner, toss_decision, match_winner, result, " + + "result_margin, umpire1, umpire2) " + + " VALUES (:id, :city, :date, :playerOfMatch, :venue, :team1, :team2, :tossWinner, :tossDecision, :matchWinner, :result, " + + ":resultMargin, :umpire1, :umpire2)") .dataSource(dataSource).build(); } @Bean public Job importUserJob(JobCompletionNotificationListener listener, Step step1) { return jobBuilderFactory - .get("importUserJob") - .incrementer(new RunIdIncrementer()) - .listener(listener) - .flow(step1) - .end() - .build(); + .get("importUserJob") + .incrementer(new RunIdIncrementer()) + .listener(listener) + .flow(step1) + .end() + .build(); } @Bean public Step step1(JdbcBatchItemWriter writer) { return stepBuilderFactory - .get("step1") - .chunk(10) - .reader(reader()) - .processor(processor()) - .writer(writer) - .build(); + .get("step1") + .chunk(10) + .reader(reader()) + .processor(processor()) + .writer(writer) + .build(); } } \ No newline at end of file diff --git a/src/main/java/io/javabrains/ipldashboard/data/JobCompletionNotificationListener.java b/src/main/java/io/javabrains/ipldashboard/data/JobCompletionNotificationListener.java index 4dc77ef..277c094 100644 --- a/src/main/java/io/javabrains/ipldashboard/data/JobCompletionNotificationListener.java +++ b/src/main/java/io/javabrains/ipldashboard/data/JobCompletionNotificationListener.java @@ -1,21 +1,18 @@ package io.javabrains.ipldashboard.data; -import java.util.HashMap; -import java.util.Map; - -import javax.persistence.EntityManager; -import javax.transaction.Transactional; - +import io.javabrains.ipldashboard.model.Team; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.batch.core.BatchStatus; import org.springframework.batch.core.JobExecution; import org.springframework.batch.core.listener.JobExecutionListenerSupport; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Component; -import io.javabrains.ipldashboard.model.Team; +import javax.persistence.EntityManager; +import javax.transaction.Transactional; +import java.util.HashMap; +import java.util.Map; @Component public class JobCompletionNotificationListener extends JobExecutionListenerSupport { @@ -29,38 +26,38 @@ public JobCompletionNotificationListener(EntityManager em) { this.em = em; } - @Override - @Transactional - public void afterJob(JobExecution jobExecution) { - if(jobExecution.getStatus() == BatchStatus.COMPLETED) { - log.info("!!! JOB FINISHED! Time to verify the results"); - - Map teamData = new HashMap<>(); - - em.createQuery("select m.team1, count(*) from Match m group by m.team1", Object[].class) - .getResultList() - .stream() - .map(e -> new Team((String) e[0], (long) e[1])) - .forEach(team -> teamData.put(team.getTeamName(), team)); - - em.createQuery("select m.team2, count(*) from Match m group by m.team2", Object[].class) - .getResultList() - .stream() - .forEach(e -> { - Team team = teamData.get((String) e[0]); - team.setTotalMatches(team.getTotalMatches() + (long) e[1]); - }); - - em.createQuery("select m.matchWinner, count(*) from Match m group by m.matchWinner", Object[].class) - .getResultList() - .stream() - .forEach(e -> { - Team team = teamData.get((String) e[0]); - if (team != null) team.setTotalWins((long) e[1]); - }); - - teamData.values().forEach(team -> em.persist(team)); - teamData.values().forEach(team -> System.out.println(team)); + @Override + @Transactional + public void afterJob(JobExecution jobExecution) { + if (jobExecution.getStatus() == BatchStatus.COMPLETED) { + log.info("!!! JOB FINISHED! Time to verify the results"); + + Map teamData = new HashMap<>(); + + em.createQuery("select m.team1, count(*) from Match m group by m.team1", Object[].class) + .getResultList() + .stream() + .map(e -> new Team((String) e[0], (long) e[1])) + .forEach(team -> teamData.put(team.getTeamName(), team)); + + em.createQuery("select m.team2, count(*) from Match m group by m.team2", Object[].class) + .getResultList() + .stream() + .forEach(e -> { + Team team = teamData.get((String) e[0]); + team.setTotalMatches(team.getTotalMatches() + (long) e[1]); + }); + + em.createQuery("select m.matchWinner, count(*) from Match m group by m.matchWinner", Object[].class) + .getResultList() + .stream() + .forEach(e -> { + Team team = teamData.get((String) e[0]); + if (team != null) team.setTotalWins((long) e[1]); + }); + + teamData.values().forEach(team -> em.persist(team)); + teamData.values().forEach(team -> System.out.println(team)); + } } - } } \ No newline at end of file diff --git a/src/main/java/io/javabrains/ipldashboard/data/MatchInput.java b/src/main/java/io/javabrains/ipldashboard/data/MatchInput.java index 658fecb..7648d34 100644 --- a/src/main/java/io/javabrains/ipldashboard/data/MatchInput.java +++ b/src/main/java/io/javabrains/ipldashboard/data/MatchInput.java @@ -1,13 +1,10 @@ package io.javabrains.ipldashboard.data; -import io.javabrains.ipldashboard.model.Match; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; -import java.time.LocalDate; - @AllArgsConstructor @NoArgsConstructor @Builder diff --git a/src/main/java/io/javabrains/ipldashboard/model/Match.java b/src/main/java/io/javabrains/ipldashboard/model/Match.java index c52c29d..596319d 100644 --- a/src/main/java/io/javabrains/ipldashboard/model/Match.java +++ b/src/main/java/io/javabrains/ipldashboard/model/Match.java @@ -5,10 +5,9 @@ import lombok.Getter; import lombok.NoArgsConstructor; -import java.time.LocalDate; - import javax.persistence.Entity; import javax.persistence.Id; +import java.time.LocalDate; @Entity @Builder diff --git a/src/main/java/io/javabrains/ipldashboard/model/Team.java b/src/main/java/io/javabrains/ipldashboard/model/Team.java index 050aeba..ac8850f 100644 --- a/src/main/java/io/javabrains/ipldashboard/model/Team.java +++ b/src/main/java/io/javabrains/ipldashboard/model/Team.java @@ -1,15 +1,13 @@ package io.javabrains.ipldashboard.model; -import lombok.*; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; +import javax.persistence.*; import java.util.List; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.Transient; - @Entity @Builder(toBuilder = true) @Data @@ -23,7 +21,7 @@ public class Team { private String teamName; private long totalMatches; private long totalWins; - + @Transient private List matches; From acc1f750c27fc79e8a7c838236b8de24e236653c Mon Sep 17 00:00:00 2001 From: Pankaj Yadav Date: Tue, 13 Apr 2021 17:25:01 +0530 Subject: [PATCH 4/4] reverts formatting changes --- .../ipldashboard/data/BatchConfig.java | 39 +++++----- .../JobCompletionNotificationListener.java | 78 ++++++++++--------- 2 files changed, 59 insertions(+), 58 deletions(-) diff --git a/src/main/java/io/javabrains/ipldashboard/data/BatchConfig.java b/src/main/java/io/javabrains/ipldashboard/data/BatchConfig.java index 773af0d..bb64c68 100644 --- a/src/main/java/io/javabrains/ipldashboard/data/BatchConfig.java +++ b/src/main/java/io/javabrains/ipldashboard/data/BatchConfig.java @@ -1,6 +1,7 @@ package io.javabrains.ipldashboard.data; -import io.javabrains.ipldashboard.model.Match; +import javax.sql.DataSource; + import org.springframework.batch.core.Job; import org.springframework.batch.core.Step; import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; @@ -18,15 +19,15 @@ import org.springframework.context.annotation.Configuration; import org.springframework.core.io.ClassPathResource; -import javax.sql.DataSource; +import io.javabrains.ipldashboard.model.Match; @Configuration @EnableBatchProcessing public class BatchConfig { - private final String[] FIELD_NAMES = new String[]{"id", "city", "date", "player_of_match", "venue", + private final String[] FIELD_NAMES = new String[] { "id", "city", "date", "player_of_match", "venue", "neutral_venue", "team1", "team2", "toss_winner", "toss_decision", "winner", "result", "result_margin", - "eliminator", "method", "umpire1", "umpire2"}; + "eliminator", "method", "umpire1", "umpire2" }; @Autowired public JobBuilderFactory jobBuilderFactory; @@ -54,32 +55,30 @@ public MatchDataProcessor processor() { public JdbcBatchItemWriter writer(DataSource dataSource) { return new JdbcBatchItemWriterBuilder() .itemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>()) - .sql("INSERT INTO match (id, city, date, player_of_match, venue, team1, team2, toss_winner, toss_decision, match_winner, result, " + - "result_margin, umpire1, umpire2) " - + " VALUES (:id, :city, :date, :playerOfMatch, :venue, :team1, :team2, :tossWinner, :tossDecision, :matchWinner, :result, " + - ":resultMargin, :umpire1, :umpire2)") + .sql("INSERT INTO match (id, city, date, player_of_match, venue, team1, team2, toss_winner, toss_decision, match_winner, result, result_margin, umpire1, umpire2) " + + " VALUES (:id, :city, :date, :playerOfMatch, :venue, :team1, :team2, :tossWinner, :tossDecision, :matchWinner, :result, :resultMargin, :umpire1, :umpire2)") .dataSource(dataSource).build(); } @Bean public Job importUserJob(JobCompletionNotificationListener listener, Step step1) { return jobBuilderFactory - .get("importUserJob") - .incrementer(new RunIdIncrementer()) - .listener(listener) - .flow(step1) - .end() - .build(); + .get("importUserJob") + .incrementer(new RunIdIncrementer()) + .listener(listener) + .flow(step1) + .end() + .build(); } @Bean public Step step1(JdbcBatchItemWriter writer) { return stepBuilderFactory - .get("step1") - .chunk(10) - .reader(reader()) - .processor(processor()) - .writer(writer) - .build(); + .get("step1") + .chunk(10) + .reader(reader()) + .processor(processor()) + .writer(writer) + .build(); } } \ No newline at end of file diff --git a/src/main/java/io/javabrains/ipldashboard/data/JobCompletionNotificationListener.java b/src/main/java/io/javabrains/ipldashboard/data/JobCompletionNotificationListener.java index 277c094..d6a96ae 100644 --- a/src/main/java/io/javabrains/ipldashboard/data/JobCompletionNotificationListener.java +++ b/src/main/java/io/javabrains/ipldashboard/data/JobCompletionNotificationListener.java @@ -1,6 +1,11 @@ package io.javabrains.ipldashboard.data; -import io.javabrains.ipldashboard.model.Team; +import java.util.HashMap; +import java.util.Map; + +import javax.persistence.EntityManager; +import javax.transaction.Transactional; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.batch.core.BatchStatus; @@ -9,10 +14,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; -import javax.persistence.EntityManager; -import javax.transaction.Transactional; -import java.util.HashMap; -import java.util.Map; +import io.javabrains.ipldashboard.model.Team; @Component public class JobCompletionNotificationListener extends JobExecutionListenerSupport { @@ -26,38 +28,38 @@ public JobCompletionNotificationListener(EntityManager em) { this.em = em; } - @Override - @Transactional - public void afterJob(JobExecution jobExecution) { - if (jobExecution.getStatus() == BatchStatus.COMPLETED) { - log.info("!!! JOB FINISHED! Time to verify the results"); - - Map teamData = new HashMap<>(); - - em.createQuery("select m.team1, count(*) from Match m group by m.team1", Object[].class) - .getResultList() - .stream() - .map(e -> new Team((String) e[0], (long) e[1])) - .forEach(team -> teamData.put(team.getTeamName(), team)); - - em.createQuery("select m.team2, count(*) from Match m group by m.team2", Object[].class) - .getResultList() - .stream() - .forEach(e -> { - Team team = teamData.get((String) e[0]); - team.setTotalMatches(team.getTotalMatches() + (long) e[1]); - }); - - em.createQuery("select m.matchWinner, count(*) from Match m group by m.matchWinner", Object[].class) - .getResultList() - .stream() - .forEach(e -> { - Team team = teamData.get((String) e[0]); - if (team != null) team.setTotalWins((long) e[1]); - }); - - teamData.values().forEach(team -> em.persist(team)); - teamData.values().forEach(team -> System.out.println(team)); - } + @Override + @Transactional + public void afterJob(JobExecution jobExecution) { + if(jobExecution.getStatus() == BatchStatus.COMPLETED) { + log.info("!!! JOB FINISHED! Time to verify the results"); + + Map teamData = new HashMap<>(); + + em.createQuery("select m.team1, count(*) from Match m group by m.team1", Object[].class) + .getResultList() + .stream() + .map(e -> new Team((String) e[0], (long) e[1])) + .forEach(team -> teamData.put(team.getTeamName(), team)); + + em.createQuery("select m.team2, count(*) from Match m group by m.team2", Object[].class) + .getResultList() + .stream() + .forEach(e -> { + Team team = teamData.get((String) e[0]); + team.setTotalMatches(team.getTotalMatches() + (long) e[1]); + }); + + em.createQuery("select m.matchWinner, count(*) from Match m group by m.matchWinner", Object[].class) + .getResultList() + .stream() + .forEach(e -> { + Team team = teamData.get((String) e[0]); + if (team != null) team.setTotalWins((long) e[1]); + }); + + teamData.values().forEach(team -> em.persist(team)); + teamData.values().forEach(team -> System.out.println(team)); } + } } \ No newline at end of file