From 42f4559077c356b26ccb49b31d2cded853d135a4 Mon Sep 17 00:00:00 2001 From: northzzn Date: Sun, 22 May 2016 20:33:52 +0800 Subject: [PATCH 1/4] Homework#5 --- pom.xml | 5 ++ .../dbrepositories/AccidentRepository.java | 24 ++++++- .../com/epam/dbservice/AccidentService.java | 30 ++++---- src/main/java/com/epam/entities/Accident.java | 7 -- .../java/com/epam/entities/Accidents.java | 70 +++++++++++++++++++ .../epam/processor/AccidentDBServiceImpl.java | 66 ++++++++++++----- src/main/resources/spring/spring-config.xml | 11 ++- 7 files changed, 170 insertions(+), 43 deletions(-) delete mode 100644 src/main/java/com/epam/entities/Accident.java create mode 100644 src/main/java/com/epam/entities/Accidents.java diff --git a/pom.xml b/pom.xml index 6f700cb..2078f3c 100644 --- a/pom.xml +++ b/pom.xml @@ -106,6 +106,11 @@ 1.12.1.RELEASE + + org.hibernate.javax.persistence + hibernate-jpa-2.0-api + 1.0.0.Final + diff --git a/src/main/java/com/epam/dbrepositories/AccidentRepository.java b/src/main/java/com/epam/dbrepositories/AccidentRepository.java index 703dd87..43c8ec8 100644 --- a/src/main/java/com/epam/dbrepositories/AccidentRepository.java +++ b/src/main/java/com/epam/dbrepositories/AccidentRepository.java @@ -1,7 +1,27 @@ package com.epam.dbrepositories; +import java.util.Date; +import java.util.List; + +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.CrudRepository; +import org.springframework.data.repository.query.Param; +import org.springframework.stereotype.Repository; + +import com.epam.entities.Accidents; + @Repository -public interface AccidentRepository extends CrudRepository { +public interface AccidentRepository extends CrudRepository { + + // declare your query methods for default and if you want to execute any + // custom queries use @Query annotation. + @Query(value = "SELECT * FROM Accidents WHERE roadCondition := roadCondition") + List findByRoadCondition(@Param("roadCondition") int roadCondition); + + @Query(value = "SELECT * FROM Accidents WHERE weatherConditions = :weatherConditions AND to_char(a.date, 'YYYY') = :year") + List findByWeatherConditionsAndYear(@Param("weatherConditions") int weatherConditions, + @Param("year") String year); - // declare your query methods for default and if you want to execute any custom queries use @Query annotation. + @Query(value = "SELECT * FROM accidents WHERE date = :date") + List findByDate(@Param("date") Date date); } diff --git a/src/main/java/com/epam/dbservice/AccidentService.java b/src/main/java/com/epam/dbservice/AccidentService.java index 63a47da..3ee49e1 100644 --- a/src/main/java/com/epam/dbservice/AccidentService.java +++ b/src/main/java/com/epam/dbservice/AccidentService.java @@ -2,22 +2,24 @@ import java.util.Date; -import com.epam.data.RoadAccident; +import com.epam.entities.Accidents; public interface AccidentService { - + // scenario 1 - RoadAccident findOne(String accidentId); - - // scenario 2 - Iterable getAllAccidentsByRoadCondition(); - - // scenario 3 - Iterable getAllAccidentsByWeatherConditionAndYear(String weatherCondition,String year); - - // scenario 4 - Iterable getAllAccidentsByDate(Date date); - - Boolean update(RoadAccident roadAccident); + Accidents findOne(String accidentId); + + // scenario 2 + Iterable getAllAccidentsByRoadCondition(int roadCondition); + + // scenario 3 + Iterable getAllAccidentsByWeatherConditionAndYear(int weatherCondition, String year); + + // scenario 4 + Iterable getAllAccidentsByDate(Date date); + + Accidents update(Accidents roadAccident); + // scenario 4 + void updateTime(Iterable accidents); } diff --git a/src/main/java/com/epam/entities/Accident.java b/src/main/java/com/epam/entities/Accident.java deleted file mode 100644 index 60ee338..0000000 --- a/src/main/java/com/epam/entities/Accident.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.epam.entities; - -@Entity -@Table(name="Accidents") -public class Accident { - -} diff --git a/src/main/java/com/epam/entities/Accidents.java b/src/main/java/com/epam/entities/Accidents.java new file mode 100644 index 0000000..a9146e0 --- /dev/null +++ b/src/main/java/com/epam/entities/Accidents.java @@ -0,0 +1,70 @@ +package com.epam.entities; + +import java.sql.Date; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Table(name = "accidents") +public class Accidents { + + @Id + @Column(name = "Accident_Index") + private String accidentIndex; + + @Column(name = "Road_Surface_Conditions") + private int roadCondition; + + @Column(name = "Weather_Conditions") + private int weatherConditions; + + @Column(name = "Date") + private Date date; + + public String getTime() { + return time; + } + + public void setTime(String time) { + this.time = time; + } + + @Column(name = "Time") + private String time; + + public String getAccidentIndex() { + return accidentIndex; + } + + public void setAccidentIndex(String accidentIndex) { + this.accidentIndex = accidentIndex; + } + + public int getRoadCondition() { + return roadCondition; + } + + public void setRoadCondition(int roadCondition) { + this.roadCondition = roadCondition; + } + + public int getWeatherConditions() { + return weatherConditions; + } + + public void setWeatherConditions(int weatherConditions) { + this.weatherConditions = weatherConditions; + } + + public Date getDate() { + return date; + } + + public void setDate(Date date) { + this.date = date; + } + +} diff --git a/src/main/java/com/epam/processor/AccidentDBServiceImpl.java b/src/main/java/com/epam/processor/AccidentDBServiceImpl.java index 8665e8b..6cf15a3 100644 --- a/src/main/java/com/epam/processor/AccidentDBServiceImpl.java +++ b/src/main/java/com/epam/processor/AccidentDBServiceImpl.java @@ -1,38 +1,66 @@ package com.epam.processor; +import java.util.Calendar; import java.util.Date; +import java.util.List; -import com.epam.data.RoadAccident; +import org.springframework.beans.factory.annotation.Autowired; + +import com.epam.dbrepositories.AccidentRepository; import com.epam.dbservice.AccidentService; +import com.epam.entities.Accidents; public class AccidentDBServiceImpl implements AccidentService { - public RoadAccident findOne(String accidentId) { - // TODO Auto-generated method stub - return null; + @Autowired + private AccidentRepository accidentRepository; + + public Accidents findOne(String accidentId) { + return accidentRepository.findOne(accidentId); + } + + public Iterable getAllAccidentsByRoadCondition(int roadCondition) { + return accidentRepository.findByRoadCondition(roadCondition); } - public Iterable getAllAccidentsByRoadCondition() { - // TODO Auto-generated method stub - return null; + public Iterable getAllAccidentsByWeatherConditionAndYear(int weatherCondition, String year) { + return accidentRepository.findByWeatherConditionsAndYear(weatherCondition, year); } - public Iterable getAllAccidentsByWeatherConditionAndYear( - String weatherCondition, String year) { - // TODO Auto-generated method stub - return null; + public Iterable getAllAccidentsByDate(Date date) { + return accidentRepository.findByDate(date); } - public Iterable getAllAccidentsByDate(Date date) { - // TODO Auto-generated method stub - + public Accidents update(Accidents roadAccident) { + return accidentRepository.save(roadAccident); } - public Boolean update(RoadAccident roadAccident) { - // TODO Auto-generated method stub - return null; + public void updateTime(Date date) { + List list = accidentRepository.findByDate(date); + for (Accidents accident : list){ + + } + } + + public static String TIME_MORNING = "MORNING"; + public static String TIME_AFTERNOON = "AFTERNOON"; + public static String TIME_EVENING = "EVENING"; + public static String TIME_NIGHT = "NIGHT"; + + public void updateTime(Iterable accidents) { + Calendar calendar=Calendar.getInstance(); + for (Accidents accident : accidents){ + calendar.setTime(accident.getDate()); + if (calendar.get(Calendar.HOUR_OF_DAY) >= 18) { + accident.setTime(TIME_NIGHT); + } else if (calendar.get(Calendar.HOUR_OF_DAY) >= 12) { + accident.setTime(TIME_AFTERNOON); + } else if (calendar.get(Calendar.HOUR_OF_DAY) >= 6) { + accident.setTime(TIME_MORNING); + } else { + accident.setTime(TIME_EVENING); + } + } } - - } diff --git a/src/main/resources/spring/spring-config.xml b/src/main/resources/spring/spring-config.xml index 0d2761a..1a076d8 100644 --- a/src/main/resources/spring/spring-config.xml +++ b/src/main/resources/spring/spring-config.xml @@ -1,11 +1,20 @@ + xmlns:jpa="http://www.springframework.org/schema/data/jpa" + xsi:schemaLocation="http://www.springframework.org/schema/beans + http://www.springframework.org/schema/beans/spring-beans-3.0.xsd + http://www.springframework.org/schema/data/jpa + http://www.springframework.org/schema/data/jpa/spring-jpa.xsd" > + + + + + From 5bb29ecd67cee6d2756795bf35f6b30394b07ff3 Mon Sep 17 00:00:00 2001 From: northzzn Date: Sun, 22 May 2016 20:37:20 +0800 Subject: [PATCH 2/4] Homework#5 --- .../com/epam/processor/AccidentDBServiceImpl.java | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/epam/processor/AccidentDBServiceImpl.java b/src/main/java/com/epam/processor/AccidentDBServiceImpl.java index 6cf15a3..af73418 100644 --- a/src/main/java/com/epam/processor/AccidentDBServiceImpl.java +++ b/src/main/java/com/epam/processor/AccidentDBServiceImpl.java @@ -19,15 +19,15 @@ public Accidents findOne(String accidentId) { return accidentRepository.findOne(accidentId); } - public Iterable getAllAccidentsByRoadCondition(int roadCondition) { + public List getAllAccidentsByRoadCondition(int roadCondition) { return accidentRepository.findByRoadCondition(roadCondition); } - public Iterable getAllAccidentsByWeatherConditionAndYear(int weatherCondition, String year) { + public List getAllAccidentsByWeatherConditionAndYear(int weatherCondition, String year) { return accidentRepository.findByWeatherConditionsAndYear(weatherCondition, year); } - public Iterable getAllAccidentsByDate(Date date) { + public List getAllAccidentsByDate(Date date) { return accidentRepository.findByDate(date); } @@ -35,13 +35,6 @@ public Accidents update(Accidents roadAccident) { return accidentRepository.save(roadAccident); } - public void updateTime(Date date) { - List list = accidentRepository.findByDate(date); - for (Accidents accident : list){ - - } - } - public static String TIME_MORNING = "MORNING"; public static String TIME_AFTERNOON = "AFTERNOON"; public static String TIME_EVENING = "EVENING"; From 266b28f230dbaf8e2b433525d0ccc0772da89f09 Mon Sep 17 00:00:00 2001 From: northzzn Date: Sun, 22 May 2016 20:46:33 +0800 Subject: [PATCH 3/4] Homework#5 --- src/main/java/com/epam/processor/AccidentDBServiceImpl.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/com/epam/processor/AccidentDBServiceImpl.java b/src/main/java/com/epam/processor/AccidentDBServiceImpl.java index af73418..fb18d0b 100644 --- a/src/main/java/com/epam/processor/AccidentDBServiceImpl.java +++ b/src/main/java/com/epam/processor/AccidentDBServiceImpl.java @@ -54,6 +54,7 @@ public void updateTime(Iterable accidents) { accident.setTime(TIME_EVENING); } } + accidentRepository.save(accidents); } } From f5313610a1538a37ff62867a00165b2ccf628421 Mon Sep 17 00:00:00 2001 From: northzzn Date: Wed, 25 May 2016 22:19:30 +0800 Subject: [PATCH 4/4] hw#5 --- .travis.yml | 3 -- pom.xml | 22 +++++++++ src/main/java/com/epam/Main.java | 49 ++++++++++++++----- .../dbrepositories/AccidentRepository.java | 6 +-- .../com/epam/dbservice/AccidentService.java | 2 +- .../java/com/epam/entities/Accidents.java | 22 +++++++-- .../epam/processor/AccidentDBServiceImpl.java | 28 +++++------ src/main/resources/spring/spring-config.xml | 5 -- 8 files changed, 97 insertions(+), 40 deletions(-) delete mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 9bcf999..0000000 --- a/.travis.yml +++ /dev/null @@ -1,3 +0,0 @@ -language: java -jdk: - - oraclejdk8 diff --git a/pom.xml b/pom.xml index 2078f3c..a37642c 100644 --- a/pom.xml +++ b/pom.xml @@ -19,7 +19,29 @@ + + + org.springframework.boot + spring-boot-starter-parent + 1.3.5.RELEASE + + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + org.springframework.boot + spring-boot-devtools + true + + + com.h2database + h2 + runtime + com.fasterxml.jackson.core jackson-annotations diff --git a/src/main/java/com/epam/Main.java b/src/main/java/com/epam/Main.java index d27c742..e68b712 100644 --- a/src/main/java/com/epam/Main.java +++ b/src/main/java/com/epam/Main.java @@ -1,23 +1,50 @@ package com.epam; -import com.epam.data.AccidentsDataLoader; -import com.epam.data.RoadAccident; - import java.io.IOException; -import java.util.List; +import java.sql.Date; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.ApplicationContext; +import com.epam.dbrepositories.AccidentRepository; +import com.epam.dbservice.AccidentService; +import com.epam.entities.Accidents; +import com.epam.processor.AccidentDBServiceImpl; /** * Created by Tkachi on 2016/4/3. */ +@SpringBootApplication public class Main { - private static final String ACCIDENTS_CSV = "src/main/resources/DfTRoadSafety_Accidents_2010.csv"; - - - public static void main(String[] args) throws IOException { - AccidentsDataLoader accidentsDataLoader = new AccidentsDataLoader(); - List accidents = accidentsDataLoader.loadRoadAccidents(ACCIDENTS_CSV); - } + private static Long oneHour = 60 * 60 * 1000L; + + public static void main(String[] args) { + SpringApplication springApplication = new SpringApplication(); + ApplicationContext ctx = SpringApplication.run(Main.class, args); + AccidentRepository accRepository = (AccidentRepository) ctx.getBean(AccidentRepository.class); + AccidentService accService = (AccidentDBServiceImpl) ctx.getBean(AccidentDBServiceImpl.class); + + save(accService); + + //Scenario 1. Find all the accidents by ID(Note: We can use findOne method which will accept the Accident ID as PK). + System.out.println("Scenario 1 output: find index 2:: " + accService.findOne("2")); + //Scenario 2. Find all the accidents count groupby all roadsurface conditions . + System.out.println("Scenario 2 output: find by road condition 3:: " + accService.getAllAccidentsByRoadCondition(3)); + //Scenario 3. Find all the accidents count groupby accident year and weather condition .( For eg: in year 2009 we need to know the number of accidents based on each weather condition). + System.out.println("Scenario 3 output: find by weather condition 4 and year 1970:: " + accService.getAllAccidentsByWeatherConditionAndYear(4, "1970")); + //Scenario 4. On a given date, fetch all the accidents and update the Time based on the below rules + accService.updateTime(accService.findOne("1")); + System.out.println("Scenario 4 output: update time :: " + accService.findOne("1")); + } + + private static void save(AccidentService accImpl) { + accImpl.update(new Accidents("1", 1, 1, new Date(1 * oneHour))); + accImpl.update(new Accidents("2", 2, 2, new Date(7 * oneHour))); + accImpl.update(new Accidents("3", 3, 3, new Date(13 * oneHour))); + accImpl.update(new Accidents("4", 4, 4, new Date(19 * oneHour))); + } } diff --git a/src/main/java/com/epam/dbrepositories/AccidentRepository.java b/src/main/java/com/epam/dbrepositories/AccidentRepository.java index 43c8ec8..689b8ac 100644 --- a/src/main/java/com/epam/dbrepositories/AccidentRepository.java +++ b/src/main/java/com/epam/dbrepositories/AccidentRepository.java @@ -15,13 +15,13 @@ public interface AccidentRepository extends CrudRepository { // declare your query methods for default and if you want to execute any // custom queries use @Query annotation. - @Query(value = "SELECT * FROM Accidents WHERE roadCondition := roadCondition") + @Query(value = "SELECT a FROM Accidents a WHERE a.roadCondition = ?") List findByRoadCondition(@Param("roadCondition") int roadCondition); - @Query(value = "SELECT * FROM Accidents WHERE weatherConditions = :weatherConditions AND to_char(a.date, 'YYYY') = :year") + @Query(value = "SELECT a FROM Accidents a WHERE a.weatherConditions = ? AND to_char(a.date, 'YYYY') = ?") List findByWeatherConditionsAndYear(@Param("weatherConditions") int weatherConditions, @Param("year") String year); - @Query(value = "SELECT * FROM accidents WHERE date = :date") + @Query(value = "SELECT a FROM Accidents a WHERE a.date = ?") List findByDate(@Param("date") Date date); } diff --git a/src/main/java/com/epam/dbservice/AccidentService.java b/src/main/java/com/epam/dbservice/AccidentService.java index 3ee49e1..d60efe8 100644 --- a/src/main/java/com/epam/dbservice/AccidentService.java +++ b/src/main/java/com/epam/dbservice/AccidentService.java @@ -21,5 +21,5 @@ public interface AccidentService { Accidents update(Accidents roadAccident); // scenario 4 - void updateTime(Iterable accidents); + void updateTime(Accidents accidents); } diff --git a/src/main/java/com/epam/entities/Accidents.java b/src/main/java/com/epam/entities/Accidents.java index a9146e0..ae118d9 100644 --- a/src/main/java/com/epam/entities/Accidents.java +++ b/src/main/java/com/epam/entities/Accidents.java @@ -24,6 +24,20 @@ public class Accidents { @Column(name = "Date") private Date date; + @Column(name = "Time") + private String time; + + public Accidents() { + + } + + public Accidents(String accidentIndex, int roadCondition, int weatherConditions, Date date) { + this.accidentIndex = accidentIndex; + this.roadCondition = roadCondition; + this.weatherConditions = weatherConditions; + this.date = date; + } + public String getTime() { return time; } @@ -32,9 +46,6 @@ public void setTime(String time) { this.time = time; } - @Column(name = "Time") - private String time; - public String getAccidentIndex() { return accidentIndex; } @@ -67,4 +78,9 @@ public void setDate(Date date) { this.date = date; } + @Override + public String toString() { + return "Accident -- " + " accidentIndex:" + accidentIndex + " roadCondition:" + roadCondition + + " weatherConditions:" + weatherConditions + " date:" + date + " time:" + time; + } } diff --git a/src/main/java/com/epam/processor/AccidentDBServiceImpl.java b/src/main/java/com/epam/processor/AccidentDBServiceImpl.java index fb18d0b..949f50e 100644 --- a/src/main/java/com/epam/processor/AccidentDBServiceImpl.java +++ b/src/main/java/com/epam/processor/AccidentDBServiceImpl.java @@ -5,11 +5,13 @@ import java.util.List; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; import com.epam.dbrepositories.AccidentRepository; import com.epam.dbservice.AccidentService; import com.epam.entities.Accidents; +@Service public class AccidentDBServiceImpl implements AccidentService { @Autowired @@ -40,21 +42,19 @@ public Accidents update(Accidents roadAccident) { public static String TIME_EVENING = "EVENING"; public static String TIME_NIGHT = "NIGHT"; - public void updateTime(Iterable accidents) { - Calendar calendar=Calendar.getInstance(); - for (Accidents accident : accidents){ - calendar.setTime(accident.getDate()); - if (calendar.get(Calendar.HOUR_OF_DAY) >= 18) { - accident.setTime(TIME_NIGHT); - } else if (calendar.get(Calendar.HOUR_OF_DAY) >= 12) { - accident.setTime(TIME_AFTERNOON); - } else if (calendar.get(Calendar.HOUR_OF_DAY) >= 6) { - accident.setTime(TIME_MORNING); - } else { - accident.setTime(TIME_EVENING); - } + public void updateTime(Accidents accident) { + Calendar calendar = Calendar.getInstance(); + calendar.setTime(accident.getDate()); + if (calendar.get(Calendar.HOUR_OF_DAY) >= 18) { + accident.setTime(TIME_NIGHT); + } else if (calendar.get(Calendar.HOUR_OF_DAY) >= 12) { + accident.setTime(TIME_AFTERNOON); + } else if (calendar.get(Calendar.HOUR_OF_DAY) >= 6) { + accident.setTime(TIME_MORNING); + } else { + accident.setTime(TIME_EVENING); } - accidentRepository.save(accidents); + accidentRepository.save(accident); } } diff --git a/src/main/resources/spring/spring-config.xml b/src/main/resources/spring/spring-config.xml index 1a076d8..3d89940 100644 --- a/src/main/resources/spring/spring-config.xml +++ b/src/main/resources/spring/spring-config.xml @@ -12,9 +12,4 @@ - - - - -