Skip to content

Commit

Permalink
RSWW-134 set up basic transports import
Browse files Browse the repository at this point in the history
transport courses and transport id are not working correctly
  • Loading branch information
Danzigerrr committed Jun 3, 2024
1 parent a23ac73 commit 7d313c4
Show file tree
Hide file tree
Showing 13 changed files with 757 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

@Component
@RequiredArgsConstructor
public class Bootstrap implements CommandLineRunner {
public class HotelBootstrap implements CommandLineRunner {
private final HotelParser hotelParser;
private final RoomParser roomParser;
private final ResourceLoader resourceLoader;
Expand All @@ -26,7 +26,7 @@ public class Bootstrap implements CommandLineRunner {

@Override
public void run(String... args) throws IOException {
Logger logger = Logger.getLogger("Bootstrap");
Logger logger = Logger.getLogger("TransportBootstrap");

Resource hotelCsvFile = resourceLoader.getResource("classpath:initData/hotels.csv");
Resource hotelRoomsCsvFile = resourceLoader.getResource("classpath:initData/hotel_rooms.csv");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class RoomParser {
private final HotelCsvReader hotelCsvReader;

public void importRooms(Resource resource, List<Hotel> hotels) {
Logger logger = Logger.getLogger("Bootstrap | Rooms");
Logger logger = Logger.getLogger("TransportBootstrap | Rooms");
RoomCapacityCalculator capacityCalculator = new RoomCapacityCalculator();
try (BufferedReader br = new BufferedReader(new InputStreamReader(resource.getInputStream()))) {
String line;
Expand Down
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");
}
}
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));
}
}
Loading

0 comments on commit 7d313c4

Please sign in to comment.