Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Homework 5 commit #47

Open
wants to merge 4 commits into
base: homework-5-preparation
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.iml
target/*
/target/
/target/
2 changes: 2 additions & 0 deletions down.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
call mvn -f pom.xml dependency:copy-dependencies
@pause
20 changes: 20 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,26 @@
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!--<dependencies>-->
<!--<dependency>-->
Expand Down
19 changes: 10 additions & 9 deletions src/main/java/com/epam/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,17 @@ public static void main(String[] args) {
SpringApplication.run(Main.class);
}

@Bean
// @Bean
public CommandLineRunner demo(AccidentService repository) {
AccidentsDataLoader loader = new AccidentsDataLoader();
List<RoadAccident> accidents = loader.loadRoadAccidents("src/main/resources/DfTRoadSafety_Accidents_2009.csv");
return (args) -> {
accidents.forEach(accident -> repository.save(accident));
System.out.println(repository.findAll().iterator().next());
System.out.println(repository.getAllAccidentsByRoadSurfaceCondition("Dry").iterator().next());
System.out.println(repository.getAllAccidentsByWeatherConditionAndYear("Fine no high winds", 2009).iterator().next());
};
// AccidentsDataLoader loader = new AccidentsDataLoader();
// List<RoadAccident> accidents = loader.loadRoadAccidents("src/main/resources/DfTRoadSafety_Accidents_2009.csv");
// return (args) -> {
// accidents.forEach(accident -> repository.save(accident));
// System.out.println(repository.findAll().iterator().next());
// System.out.println(repository.getAllAccidentsByRoadSurfaceCondition("Dry").iterator().next());
// System.out.println(repository.getAllAccidentsByWeatherConditionAndYear("Fine no high winds", 2009).iterator().next());
// };
return null;
}


Expand Down
10 changes: 9 additions & 1 deletion src/main/java/com/epam/db/HsqlInit.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ public void initTablesFromFiles(Connection connection){
try {
String workingDir = System.getProperty("user.dir");

connection.prepareStatement("DROP TABLE IF EXISTS accident_severity").execute();
connection.prepareStatement("DROP TABLE IF EXISTS district_authority").execute();
connection.prepareStatement("DROP TABLE IF EXISTS light_condition").execute();
connection.prepareStatement("DROP TABLE IF EXISTS police_force").execute();
connection.prepareStatement("DROP TABLE IF EXISTS road_surface").execute();
connection.prepareStatement("DROP TABLE IF EXISTS weather_conditions").execute();
connection.prepareStatement("DROP TABLE IF EXISTS accidents").execute();

connection.prepareStatement("CREATE TEXT TABLE accident_severity(\n" +
" code INTEGER NOT NULL PRIMARY KEY \n" +
" ,label VARCHAR(70) NOT NULL\n" +
Expand Down Expand Up @@ -62,7 +70,7 @@ public void initTablesFromFiles(Connection connection){
" ,Accident_Severity INTEGER NOT NULL\n" +
" ,Number_of_Vehicles INTEGER NOT NULL\n" +
" ,Number_of_Casualties INTEGER NOT NULL\n" +
" ,Date DATE NOT NULL\n" +
" ,Date VARCHAR(15) NOT NULL\n" +
" ,Day_of_Week INTEGER NOT NULL\n" +
" ,Time VARCHAR(15) NOT NULL\n" +
" ,Local_Authority_District INTEGER NOT NULL\n" +
Expand Down
51 changes: 26 additions & 25 deletions src/main/java/com/epam/dbservice/AccidentService.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
package com.epam.dbservice;

import java.util.Date;

import com.epam.data.RoadAccident;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface AccidentService extends CrudRepository<RoadAccident, String> {

// scenario 1
RoadAccident findOne(String accidentId);

// scenario 2
Iterable<RoadAccident> getAllAccidentsByRoadSurfaceCondition(String roadSurfaceCondition);

// scenario 3
Iterable<RoadAccident> getAllAccidentsByWeatherConditionAndYear(String weatherCondition,int year);

// scenario 4
Iterable<RoadAccident> getAllAccidentsByDate(Date date);


}
package com.epam.dbservice;

import java.util.Date;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import com.epam.h5.entity.Accidents;

@Repository
public interface AccidentService extends CrudRepository<Accidents, String> {

// scenario 1
Accidents findOne(String accidentId);

// scenario 2
Iterable<Accidents> getAllAccidentsByRoadSurfaceCondition(String roadSurfaceCondition);

// scenario 3
Iterable<Accidents> getAllAccidentsByWeatherConditionAndPoliceForce(String weatherCondition,int policeForce);

// scenario 4
Iterable<Accidents> getAllAccidentsByDate(Date date);


}
32 changes: 32 additions & 0 deletions src/main/java/com/epam/h5/DBOperator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.epam.h5;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.epam.h5.dao.AccidentRepository;
import com.epam.h5.entity.Accidents;

@Component
public class DBOperator {

@Resource
AccidentRepository dao;

public Accidents findOne(String accidentId){
return dao.findOne(accidentId);
}

// public Iterable<Accidents> getAllAccidentsByRoadSurfaceCondition(String roadSurfaceCondition){
// return dao.getAllAccidentsByRoadSurfaceCondition(roadSurfaceCondition);
// }
//
// public Iterable<Accidents> getAllAccidentsByWeatherConditionAndPoliceForce(String weatherCondition,int policeForce){
// return dao.getAllAccidentsByWeatherConditionAndPoliceForce(weatherCondition, policeForce);
// }
//
// public Iterable<Accidents> getAllAccidentsByDate(Date date){
// return dao.getAllAccidentsByDate(date);
// }

}
30 changes: 30 additions & 0 deletions src/main/java/com/epam/h5/dao/AccidentRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.epam.h5.dao;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.epam.h5.entity.Accidents;

@Repository
public interface AccidentRepository extends JpaRepository<Accidents, String> {

// scenario 1
// @Query(value = "SELECT * FROM Accidents WHERE accidentId = :accidentId")
Accidents findOne(@Param("accidentId") String accidentId);

// scenario 2
// @Query(value = "SELECT * FROM Accidents WHERE roadSurfaceCondition = :roadSurfaceCondition")
// Iterable<Accidents> getAllAccidentsByRoadSurfaceCondition(String roadSurfaceCondition);

// scenario 3
// @Query(value = "SELECT * FROM Accidents WHERE weatherCondition = :weatherCondition and policeForce = :policeForce")
// Iterable<Accidents> getAllAccidentsByWeatherConditionAndPoliceForce(String weatherCondition,int policeForce);

// scenario 4
// @Query(value = "SELECT * FROM Accidents WHERE date = :date")
// Iterable<Accidents> getAllAccidentsByDate(Date date);


}
185 changes: 185 additions & 0 deletions src/main/java/com/epam/h5/entity/Accidents.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
package com.epam.h5.entity;

import java.time.LocalDate;

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 accidentId;

@Column(name = "Longitude")
private float longitude;

@Column(name = "Latitude")
private float latitude;

@Column(name = "Police_Force")
private String policeForce;

@Column(name = "Accident_Severity")
private String accidentSeverity;

@Column(name = "Number_of_Vehicles")
private int numberOfVehicles;

@Column(name = "Number_of_Casualties")
private int numberOfCasualties;

@Column(name = "Date")
private LocalDate date;

@Column(name = "Day_of_Week")
private int dayOfWeek;

@Column(name = "Time")
private String time;

@Column(name = "Local_Authority_District")
private int districtAuthority;

@Column(name = "Light_Conditions")
private int lightCondition;

@Column(name = "Weather_Conditions")
private int weatherCondition;

@Column(name = "Road_Surface_Conditions")
private int roadSurfaceCondition;

public Accidents() {}

public String getAccidentId() {
return accidentId;
}

public void setAccidentId(String accidentId) {
this.accidentId = accidentId;
}

public float getLongitude() {
return longitude;
}

public void setLongitude(float longitude) {
this.longitude = longitude;
}

public float getLatitude() {
return latitude;
}

public void setLatitude(float latitude) {
this.latitude = latitude;
}

public String getPoliceForce() {
return policeForce;
}

public void setPoliceForce(String policeForce) {
this.policeForce = policeForce;
}

public String getAccidentSeverity() {
return accidentSeverity;
}

public void setAccidentSeverity(String accidentSeverity) {
this.accidentSeverity = accidentSeverity;
}

public int getNumberOfVehicles() {
return numberOfVehicles;
}

public void setNumberOfVehicles(int numberOfVehicles) {
this.numberOfVehicles = numberOfVehicles;
}

public int getNumberOfCasualties() {
return numberOfCasualties;
}

public void setNumberOfCasualties(int numberOfCasualties) {
this.numberOfCasualties = numberOfCasualties;
}

public LocalDate getDate() {
return date;
}

public void setDate(LocalDate date) {
this.date = date;
}

public int getDayOfWeek() {
return dayOfWeek;
}

public void setDayOfWeek(int dayOfWeek) {
this.dayOfWeek = dayOfWeek;
}

public String getTime() {
return time;
}

public void setTime(String time) {
this.time = time;
}

public int getDistrictAuthority() {
return districtAuthority;
}

public void setDistrictAuthority(int districtAuthority) {
this.districtAuthority = districtAuthority;
}

public int getLightCondition() {
return lightCondition;
}

public void setLightCondition(int lightCondition) {
this.lightCondition = lightCondition;
}

public int getWeatherCondition() {
return weatherCondition;
}

public void setWeatherCondition(int weatherCondition) {
this.weatherCondition = weatherCondition;
}

public int getRoadSurfaceCondition() {
return roadSurfaceCondition;
}

public void setRoadSurfaceCondition(int roadSurfaceCondition) {
this.roadSurfaceCondition = roadSurfaceCondition;
}

@Override
public String toString() {
return "Accidents [accidentId=" + accidentId + ", longitude="
+ longitude + ", latitude=" + latitude + ", policeForce="
+ policeForce + ", accidentSeverity=" + accidentSeverity
+ ", numberOfVehicles=" + numberOfVehicles
+ ", numberOfCasualties=" + numberOfCasualties + ", date="
+ date + ", dayOfWeek=" + dayOfWeek + ", time=" + time
+ ", districtAuthority=" + districtAuthority
+ ", lightCondition=" + lightCondition + ", weatherCondition="
+ weatherCondition + ", roadSurfaceCondition="
+ roadSurfaceCondition + "]";
}

}
Loading