diff --git a/pom.xml b/pom.xml index c680840..6c22c2c 100644 --- a/pom.xml +++ b/pom.xml @@ -15,6 +15,7 @@ Demo project for Spring Boot 11 + 1.2.0.Final @@ -51,6 +52,17 @@ spring-batch-test test + + org.mapstruct + mapstruct + ${org.mapstruct.version} + + + org.mapstruct + mapstruct-processor + ${org.mapstruct.version} + provided + diff --git a/src/main/java/io/javabrains/ipldashboard/data/MatchDataProcessor.java b/src/main/java/io/javabrains/ipldashboard/data/MatchDataProcessor.java index 260c2a3..102b70e 100644 --- a/src/main/java/io/javabrains/ipldashboard/data/MatchDataProcessor.java +++ b/src/main/java/io/javabrains/ipldashboard/data/MatchDataProcessor.java @@ -1,11 +1,7 @@ package io.javabrains.ipldashboard.data; -import java.time.LocalDate; -import java.time.LocalDateTime; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; - import org.springframework.batch.item.ItemProcessor; import io.javabrains.ipldashboard.model.Match; @@ -16,41 +12,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 MatchMapper.INSTANCE.toMatch(matchInput); } } \ No newline at end of file diff --git a/src/main/java/io/javabrains/ipldashboard/data/MatchMapper.java b/src/main/java/io/javabrains/ipldashboard/data/MatchMapper.java new file mode 100644 index 0000000..e2c1ca9 --- /dev/null +++ b/src/main/java/io/javabrains/ipldashboard/data/MatchMapper.java @@ -0,0 +1,57 @@ +package io.javabrains.ipldashboard.data; + +import java.time.LocalDate; + +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; +import org.mapstruct.Mappings; +import org.mapstruct.Named; +import org.mapstruct.factory.Mappers; + +import io.javabrains.ipldashboard.model.Match; + +@Mapper(componentModel = "spring") +public interface MatchMapper { + public static final MatchMapper INSTANCE = Mappers.getMapper(MatchMapper.class); + String ELECTED_TO_BAT = "bat"; + + @Mappings({ @Mapping(source = "date", target = "date", qualifiedByName = "dateConvert"), + @Mapping(source = "player_of_match", target = "playerOfMatch"), + @Mapping(source = "matchInput", target = "team1", qualifiedByName = "firstInningsTeam"), + @Mapping(source = "matchInput", target = "team2", qualifiedByName = "secondInningsTeam"), + @Mapping(source = "toss_winner", target = "tossWinner"), + @Mapping(source = "toss_decision", target = "tossDecision"), + @Mapping(source = "winner", target = "matchWinner"), + @Mapping(source = "result_margin", target = "resultMargin") }) + public Match toMatch(MatchInput matchInput); + + @Named("dateConvert") + default LocalDate dateConvert(String date) { + return LocalDate.parse(date); + } + + @Named("firstInningsTeam") + default String firstInningsTeam(MatchInput matchInput) { + String firstInningsTeam; + if (ELECTED_TO_BAT.equals(matchInput.getToss_decision())) { + firstInningsTeam = matchInput.getToss_winner(); + } else { + firstInningsTeam = matchInput.getToss_winner().equals(matchInput.getTeam1()) ? matchInput.getTeam2() + : matchInput.getTeam1(); + } + return firstInningsTeam; + } + + @Named("secondInningsTeam") + default String secondInningsTeam(MatchInput matchInput) { + String secondInningsTeam; + + if (ELECTED_TO_BAT.equals(matchInput.getToss_decision())) { + secondInningsTeam = matchInput.getToss_winner().equals(matchInput.getTeam1()) ? matchInput.getTeam2() + : matchInput.getTeam1(); + } else { + secondInningsTeam = matchInput.getToss_winner(); + } + return secondInningsTeam; + } +}