From 6985e0ea87c35e8fc1aae7d2ca5567b80cca2d11 Mon Sep 17 00:00:00 2001 From: Matthias Engelien Date: Sun, 8 Sep 2024 16:38:17 +0200 Subject: [PATCH] Modeling first attempt for DB --- .../ga/config/DataBaseConfiguration.java | 44 +++++++++++++++++++ .../de/etecture/ga/model/Appointment.java | 2 - .../java/de/etecture/ga/model/Garage.java | 31 ++++++++++--- .../de/etecture/ga/model/GarageServices.java | 9 ++++ .../java/de/etecture/ga/model/MDService.java | 1 + .../ga/service/GarageImportService.java | 5 +-- src/main/resources/application.properties | 1 + src/main/resources/schema.sql | 41 +++++++++++++++++ 8 files changed, 123 insertions(+), 11 deletions(-) create mode 100644 src/main/java/de/etecture/ga/config/DataBaseConfiguration.java create mode 100644 src/main/java/de/etecture/ga/model/GarageServices.java create mode 100644 src/main/resources/schema.sql diff --git a/src/main/java/de/etecture/ga/config/DataBaseConfiguration.java b/src/main/java/de/etecture/ga/config/DataBaseConfiguration.java new file mode 100644 index 0000000..04863bc --- /dev/null +++ b/src/main/java/de/etecture/ga/config/DataBaseConfiguration.java @@ -0,0 +1,44 @@ +package de.etecture.ga.config; + +import java.time.Duration; +import java.time.temporal.ChronoUnit; +import java.util.Arrays; +import java.util.List; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.convert.converter.Converter; +import org.springframework.data.convert.ReadingConverter; +import org.springframework.data.convert.WritingConverter; +import org.springframework.data.jdbc.repository.config.AbstractJdbcConfiguration; +import org.springframework.stereotype.Component; + + +@Component +public class DataBaseConfiguration extends AbstractJdbcConfiguration { + + + @Override + protected List userConverters() { + return Arrays.asList(new DurationToLongConverter(), new LongToDurationConverter()); + } + + @WritingConverter + public class DurationToLongConverter implements Converter { + + @Override + public Long convert(Duration duration) { + return duration.toNanos(); + } + } + + @ReadingConverter + public class LongToDurationConverter implements Converter { + + @Override + public Duration convert(Long duration) { + return Duration.of(duration, ChronoUnit.NANOS); + } + } + +} diff --git a/src/main/java/de/etecture/ga/model/Appointment.java b/src/main/java/de/etecture/ga/model/Appointment.java index 34d9559..aea27dd 100644 --- a/src/main/java/de/etecture/ga/model/Appointment.java +++ b/src/main/java/de/etecture/ga/model/Appointment.java @@ -13,8 +13,6 @@ public class Appointment { @Id private Long id; - private Garage garage; - private String serviceCode; private String serviceName; diff --git a/src/main/java/de/etecture/ga/model/Garage.java b/src/main/java/de/etecture/ga/model/Garage.java index 4afbed7..25d69a7 100644 --- a/src/main/java/de/etecture/ga/model/Garage.java +++ b/src/main/java/de/etecture/ga/model/Garage.java @@ -2,8 +2,10 @@ package de.etecture.ga.model; import java.util.ArrayList; import java.util.List; +import java.util.Set; import org.springframework.data.annotation.Id; +import org.springframework.util.Assert; import lombok.Data; @@ -12,18 +14,35 @@ public class Garage { @Id private Long id; - + private String name; - + private List appointments; - + + private Set garageServices; + public Garage addAppointment(Appointment appointment) { - - if(this.appointments == null) { + + if (this.appointments == null) { this.appointments = new ArrayList<>(); } - + this.appointments.add(appointment); return this; } + + public void addService(MDService service) { + garageServices.add(createGarageService(service)); + } + + private GarageServices createGarageService(MDService service) { + + Assert.notNull(service, "Service must not be null"); + Assert.notNull(service.getId(), "Service id, must not be null"); + + GarageServices garageService = new GarageServices(); + garageService.setMdService(service.getId()); + + return garageService; + } } diff --git a/src/main/java/de/etecture/ga/model/GarageServices.java b/src/main/java/de/etecture/ga/model/GarageServices.java new file mode 100644 index 0000000..5981b78 --- /dev/null +++ b/src/main/java/de/etecture/ga/model/GarageServices.java @@ -0,0 +1,9 @@ +package de.etecture.ga.model; + +import lombok.Data; + +@Data +public class GarageServices { + + private Long mdService; +} diff --git a/src/main/java/de/etecture/ga/model/MDService.java b/src/main/java/de/etecture/ga/model/MDService.java index 2981a4c..45d8024 100644 --- a/src/main/java/de/etecture/ga/model/MDService.java +++ b/src/main/java/de/etecture/ga/model/MDService.java @@ -17,4 +17,5 @@ public class MDService { private String name; private Duration duration; + } diff --git a/src/main/java/de/etecture/ga/service/GarageImportService.java b/src/main/java/de/etecture/ga/service/GarageImportService.java index f82a923..489d578 100644 --- a/src/main/java/de/etecture/ga/service/GarageImportService.java +++ b/src/main/java/de/etecture/ga/service/GarageImportService.java @@ -53,7 +53,7 @@ public class GarageImportService { garage.setName(getGarageNameFromFile(file)); List appointments = loadObjectsFromFile(file); - appointments.stream().map(data -> this.fromCSVData(data, garage)).filter(Objects::nonNull) + appointments.stream().map(this::fromCSVData).filter(Objects::nonNull) .forEach(garage::addAppointment); return Optional.of(garage); @@ -87,13 +87,12 @@ public class GarageImportService { } } - private Appointment fromCSVData(CSVData data, Garage forGarage) { + private Appointment fromCSVData(CSVData data) { if (data == null) return null; Appointment appointment = new Appointment(); - appointment.setGarage(forGarage); appointment.setAppointmentTime(data.APP_DATE); appointment.setServiceCode(data.SERVICE); diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 4d495be..768c859 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -5,6 +5,7 @@ spring.datasource.url=jdbc:h2:mem:etecture spring.datasource.driverClassName=org.h2.Driver spring.datasource.username=sa spring.datasource.password=password +spring.datasource.platform=h2 # init DB by script spring.sql.init.mode=always \ No newline at end of file diff --git a/src/main/resources/schema.sql b/src/main/resources/schema.sql new file mode 100644 index 0000000..9901534 --- /dev/null +++ b/src/main/resources/schema.sql @@ -0,0 +1,41 @@ + +CREATE TABLE GARAGE ( + ID BIGINT NOT NULL AUTO_INCREMENT UNIQUE, + NAME VARCHAR(200) DEFAULT NULL UNIQUE, + + PRIMARY KEY (ID) +); + + +CREATE TABLE APPOINTMENT ( + ID BIGINT NOT NULL AUTO_INCREMENT UNIQUE, + GARAGE BIGINT NOT NULL, + SERVICE_CODE VARCHAR(5) DEFAULT NULL, + SERVICE_NAME VARCHAR(50) DEFAULT NULL, + APPOINTMENT_TIME DATE DEFAULT NULL, + DURATION BIGINT NOT NULL DEFAULT 0, + + CONSTRAINT GARAGE_APPOINTMENT_IDX UNIQUE (GARAGE,APPOINTMENT_TIME), + + PRIMARY KEY (ID), + FOREIGN KEY (GARAGE) REFERENCES GARAGE(ID) +); + +CREATE TABLE MD_SERVICE ( + ID BIGINT NOT NULL AUTO_INCREMENT UNIQUE, + CODE VARCHAR(5) NOT NULL UNIQUE, + NAME VARCHAR(50) DEFAULT NULL, + DURATION INT NOT NULL, + + PRIMARY KEY (ID) +); + +CREATE TABLE GARAGE_SERVICES_MAP ( + GARAGE BIGINT NOT NULL, + MD_SERVICE BIGINT NOT NULL, + + CONSTRAINT GARAGE_SERVICE_IDX UNIQUE (GARAGE,MD_SERVICE), + + FOREIGN KEY (GARAGE) REFERENCES GARAGE(ID), + FOREIGN KEY (MD_SERVICE) REFERENCES MD_SERVICE(ID) +); \ No newline at end of file