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 6f700cb..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 @@ -106,6 +128,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/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 703dd87..689b8ac 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 a FROM Accidents a WHERE a.roadCondition = ?") + List findByRoadCondition(@Param("roadCondition") int roadCondition); + + @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); - // declare your query methods for default and if you want to execute any custom queries use @Query annotation. + @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 63a47da..d60efe8 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(Accidents 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..ae118d9 --- /dev/null +++ b/src/main/java/com/epam/entities/Accidents.java @@ -0,0 +1,86 @@ +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; + + @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; + } + + public void setTime(String time) { + this.time = 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; + } + + @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 8665e8b..949f50e 100644 --- a/src/main/java/com/epam/processor/AccidentDBServiceImpl.java +++ b/src/main/java/com/epam/processor/AccidentDBServiceImpl.java @@ -1,38 +1,60 @@ 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 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 { - 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 List getAllAccidentsByRoadCondition(int roadCondition) { + return accidentRepository.findByRoadCondition(roadCondition); } - public Iterable getAllAccidentsByRoadCondition() { - // TODO Auto-generated method stub - return null; + public List 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 List 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 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(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(accident); } - - } diff --git a/src/main/resources/spring/spring-config.xml b/src/main/resources/spring/spring-config.xml index 0d2761a..3d89940 100644 --- a/src/main/resources/spring/spring-config.xml +++ b/src/main/resources/spring/spring-config.xml @@ -1,7 +1,11 @@ + 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" >