-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RSWW-134 set up basic transports import
transport courses and transport id are not working correctly
- Loading branch information
1 parent
a23ac73
commit 7d313c4
Showing
13 changed files
with
757 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
...or/src/main/java/cloud/project/datagenerator/transports/bootstrap/TransportBootstrap.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package cloud.project.datagenerator.transports.bootstrap; | ||
|
||
import cloud.project.datagenerator.transports.bootstrap.util.LocationParser; | ||
import cloud.project.datagenerator.transports.bootstrap.util.TransportCoursesParser; | ||
import cloud.project.datagenerator.transports.domain.Location; | ||
import cloud.project.datagenerator.transports.domain.Transport; | ||
import cloud.project.datagenerator.transports.domain.TransportCourse; | ||
import cloud.project.datagenerator.transports.repositories.LocationRepository; | ||
import cloud.project.datagenerator.transports.repositories.TransportCourseRepository; | ||
import cloud.project.datagenerator.transports.repositories.TransportRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.boot.CommandLineRunner; | ||
import org.springframework.core.io.Resource; | ||
import org.springframework.core.io.ResourceLoader; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.time.LocalDateTime; | ||
import java.time.Month; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Random; | ||
import java.util.UUID; | ||
import java.util.logging.Logger; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class TransportBootstrap implements CommandLineRunner { | ||
private final LocationParser locationParser; | ||
private final TransportCoursesParser transportCoursesParser; | ||
private final ResourceLoader resourceLoader; | ||
private final TransportRepository transportRepository; | ||
private final TransportCourseRepository transportCourseRepository; | ||
private final LocationRepository locationRepository; | ||
|
||
@Override | ||
public void run(String... args) { | ||
Logger logger = Logger.getLogger("TransportBootstrap | Transports"); | ||
|
||
Resource hotelCsvResource = resourceLoader.getResource("classpath:initData/hotels.csv"); | ||
Resource hotelDepartureOptionsResource = resourceLoader.getResource("classpath:initData/hotel_departure_options.csv"); | ||
|
||
List<Location> planeArrivalLocations = locationParser.importLocationsAbroad(hotelCsvResource, "PLANE"); | ||
List<Location> busArrivalLocations = locationParser.importLocationsAbroad(hotelCsvResource, "BUS"); | ||
List<Location> departureLocations = locationParser.importLocationsPoland(hotelDepartureOptionsResource); | ||
|
||
Map<String, List<TransportCourse>> transportCoursesMap = transportCoursesParser.createTransportCourses(hotelCsvResource, hotelDepartureOptionsResource, busArrivalLocations, planeArrivalLocations, departureLocations); | ||
|
||
List<TransportCourse> planeCourses = transportCoursesMap.get("PLANE"); | ||
List<TransportCourse> busCourses = transportCoursesMap.get("BUS"); | ||
|
||
LocalDateTime bootstrapBeginDay = LocalDateTime.of(2024, Month.MAY, 1, 12, 0, 0); | ||
|
||
int randomSeed = 12345678; | ||
Random random = new Random(randomSeed); | ||
|
||
int numberOfDays = 60; | ||
|
||
// generate transport for each course and every day of two months | ||
for (int day = 0; day < numberOfDays; day++) { | ||
for (TransportCourse planeCourse : planeCourses) { | ||
int capacity = random.nextInt(8, 15); | ||
|
||
LocalDateTime departureDate = bootstrapBeginDay.plusDays(day); | ||
|
||
UUID transportId = UUID.nameUUIDFromBytes(( | ||
departureDate | ||
+ planeCourse.toString() | ||
+ capacity | ||
+ day).getBytes()); | ||
|
||
Transport transport = Transport.builder() | ||
.id(transportId) | ||
.departureDate(departureDate) | ||
.course(planeCourse) | ||
.capacity(capacity) | ||
.pricePerAdult(random.nextFloat(100, 500)) | ||
.build(); | ||
|
||
locationRepository.saveAll(List.of(planeCourse.getDepartureFrom(), planeCourse.getArrivalAt())); | ||
transportCourseRepository.save(planeCourse); | ||
transportRepository.save(transport); | ||
} | ||
|
||
for (TransportCourse busCourse : busCourses) { | ||
int capacity = random.nextInt(8, 15); | ||
LocalDateTime departureDate = bootstrapBeginDay.plusDays(day); | ||
|
||
UUID transportId = UUID.nameUUIDFromBytes(( | ||
departureDate | ||
+ busCourse.toString() | ||
+ capacity | ||
+ day).getBytes()); | ||
|
||
Transport transport = Transport.builder() | ||
.id(transportId) | ||
.course(busCourse) | ||
.departureDate(departureDate) | ||
.capacity(capacity) | ||
.pricePerAdult(random.nextFloat(50, 200)) | ||
.build(); | ||
|
||
locationRepository.saveAll(List.of(busCourse.getDepartureFrom(), busCourse.getArrivalAt())); | ||
transportCourseRepository.save(busCourse); | ||
transportRepository.save(transport); | ||
} | ||
logger.info("TransportBootstrap imported day " + (day + 1) + " out of " + numberOfDays + " days"); | ||
} | ||
logger.info("TransportBootstrap finished importing data"); | ||
} | ||
} |
102 changes: 102 additions & 0 deletions
102
...r/src/main/java/cloud/project/datagenerator/transports/bootstrap/util/LocationParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package cloud.project.datagenerator.transports.bootstrap.util; | ||
|
||
import cloud.project.datagenerator.transports.domain.Location; | ||
import org.springframework.core.io.Resource; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.UUID; | ||
|
||
@Component | ||
public class LocationParser { | ||
|
||
private final List<Location> locationsAvailableByBus = new ArrayList<>(); | ||
|
||
public LocationParser() { | ||
locationsAvailableByBus.add(new Location("Albania", "Durres")); | ||
locationsAvailableByBus.add(new Location("Turcja", "Kayseri")); | ||
locationsAvailableByBus.add(new Location("Włochy", "Apulia")); | ||
locationsAvailableByBus.add(new Location("Włochy", "Sycylia")); | ||
locationsAvailableByBus.add(new Location("Włochy", "Kalabria")); | ||
} | ||
|
||
public List<Location> importLocationsAbroad(Resource resource, String transportType) { | ||
List<Location> locations = new ArrayList<>(); | ||
|
||
try (BufferedReader br = new BufferedReader(new InputStreamReader(resource.getInputStream()))) { | ||
String line; | ||
br.readLine(); // Skip header line | ||
|
||
while ((line = br.readLine()) != null) { | ||
String[] data = line.split("\t"); | ||
String country = data[4]; | ||
String region = data[5]; | ||
Location locationDto = createNewLocation(locations, country, region, transportType); | ||
if (locationDto != null) { | ||
locations.add(locationDto); | ||
} | ||
} | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
return locations; | ||
} | ||
|
||
public List<Location> importLocationsPoland(Resource resource) { | ||
List<Location> locations = new ArrayList<>(); | ||
|
||
try (BufferedReader br = new BufferedReader(new InputStreamReader(resource.getInputStream()))) { | ||
String line; | ||
br.readLine(); // Skip header line | ||
|
||
while ((line = br.readLine()) != null) { | ||
String[] data = line.split("\t"); | ||
String region = data[1]; | ||
if (!Objects.equals(region, "Dojazd własny")) { | ||
Location locationDto = createNewLocation(locations, "Polska", region, null); | ||
if (locationDto != null) { | ||
locations.add(locationDto); | ||
} | ||
} | ||
} | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
return locations; | ||
} | ||
|
||
private Location createNewLocation(List<Location> locations, String country, String region, String transportType) { | ||
if (transportType != null && transportType.equals("BUS")) { | ||
if (!locationAvailableByBus(country, region)) { | ||
return null; | ||
} | ||
} | ||
|
||
if (locationExists(locations, country, region)) { | ||
return null; | ||
} | ||
|
||
return Location.builder() | ||
.id(UUID.nameUUIDFromBytes((country+region).getBytes())) | ||
.country(country) | ||
.region(region) | ||
.build(); | ||
} | ||
|
||
private boolean locationExists(List<Location> locations, String country, String region) { | ||
return locations.stream() | ||
.anyMatch(locationDto -> locationDto.getCountry().equals(country) && locationDto.getRegion().equals(region)); | ||
} | ||
|
||
private boolean locationAvailableByBus(String country, String region) { | ||
return locationsAvailableByBus.stream() | ||
.anyMatch(location -> location.getCountry().equals(country) && location.getRegion().equals(region)); | ||
} | ||
} |
Oops, something went wrong.