diff --git a/prototype/.gitignore b/prototype/.gitignore index 52b353d..0fadc96 100644 --- a/prototype/.gitignore +++ b/prototype/.gitignore @@ -5,3 +5,5 @@ e-commerce.db ./e-commerce.iml ./e-commerce.ipr ./e-commerce.iws + +config.yml diff --git a/prototype/build.gradle b/prototype/build.gradle index c8e14cb..8d138b9 100644 --- a/prototype/build.gradle +++ b/prototype/build.gradle @@ -28,6 +28,7 @@ dependencies { implementation 'com.github.gwenn:sqlite-dialect:0.1.0' implementation 'org.springframework.boot:spring-boot-devtools' implementation 'org.xerial:sqlite-jdbc:3.31.1' + implementation 'org.yaml:snakeyaml:1.26' testCompile("org.springframework.boot:spring-boot-starter-test") } diff --git a/prototype/src/main/java/org/hso/ecommerce/app/config/AppSettings.java b/prototype/src/main/java/org/hso/ecommerce/app/config/AppSettings.java new file mode 100644 index 0000000..625ed65 --- /dev/null +++ b/prototype/src/main/java/org/hso/ecommerce/app/config/AppSettings.java @@ -0,0 +1,161 @@ +package org.hso.ecommerce.app.config; + +import java.io.*; +import java.util.ArrayList; +import java.util.List; + +import org.hso.ecommerce.app.config.YAMLData.Address; +import org.springframework.stereotype.Component; +import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.constructor.Constructor; + +import javax.annotation.PostConstruct; + +@Component("appSettings") +public class AppSettings { + + private YAMLData data; + private final String configFile = "config.yml"; + + private String installationName; + private String companyName; + private Address companyAddress; + private int numberOfStorageSpaces; + private List suppliers; + private String parcelServiceName; + private String parcelServiceApiURL; + + /** + * on initialization read the config and store the data in static objects + */ + @PostConstruct + public void init() { + data = readConfig(); + + installationName = data.getInstallationName(); + companyName = data.getCompanyName(); + companyAddress = data.getCompanyAddress(); + numberOfStorageSpaces = data.getNumberOfStorageSpaces(); + suppliers = data.getSuppliers(); + parcelServiceName = data.getParcelServiceName(); + parcelServiceApiURL = data.getParcelServiceApiURL(); + + System.out.println("Initialised Settings!"); + } + + /** + * write the default config file + */ + public void writeDefaultConfig() { + YAMLData data = new YAMLData(); + data.setInstallationName("eCommerce"); + data.setCompanyName("eCommerce Shop UG"); + data.setCompanyAddress(new Address( + "Musterstraße", + "1", + "12345", + "Musterstadt", + "Germany" + )); + data.setNumberOfStorageSpaces(128); + + List suppliers = new ArrayList<>(); + suppliers.add(new YAMLData.Supplier( + "Bank of Chees", + "d41d8cd98f00b204e9800998ecf8427e", + "http://[::1]:8081/bank/", + 4, + new Address( + "Musterstraße", + "2", + "12345", + "Musterstadt", + "Germany" + ) + )); + suppliers.add(new YAMLData.Supplier( + "MDA", + "18a17da5bac1cf00551b08c3e98720f5", + "http://[::1]:8081/mda/", + 5, + new Address( + "Musterstraße", + "3", + "12345", + "Musterstadt", + "Germany" + ) + )); + data.setSuppliers(suppliers); + + data.setParcelServiceName("Parcel Service"); + data.setParcelServiceApiURL("http://[::1]:8082/"); + + try (FileWriter writer = new FileWriter("./" + configFile)) { + Yaml yaml = new Yaml(); + yaml.dump(data, writer); + } catch (IOException e) { + e.printStackTrace(); + } + + } + + /** + * read a config file named "eCommerce_config.yml" from the applications root directory + * @return the settings as YAMLData object + */ + public YAMLData readConfig() { + YAMLData data = new YAMLData(); + + File file = new File("./" + configFile); + if (!file.exists()) { + writeDefaultConfig(); + } + + try (InputStream inputStream = new FileInputStream("./" + configFile)) { + Yaml yaml = new Yaml(new Constructor(YAMLData.class)); + data = yaml.load(inputStream); + } catch (FileNotFoundException e) { + System.err.println("The file \"" + configFile + "\" has not been found, please create a valid Configuration file."); + e.printStackTrace(); + System.exit(1); + } catch (IOException e) { + e.printStackTrace(); + System.exit(1); + } + + return data; + } + + public YAMLData getData() { + return data; + } + + public String getInstallationName() { + return installationName; + } + + public String getCompanyName() { + return companyName; + } + + public Address getCompanyAddress() { + return companyAddress; + } + + public int getNumberOfStorageSpaces() { + return numberOfStorageSpaces; + } + + public List getSuppliers() { + return suppliers; + } + + public String getParcelServiceName() { + return parcelServiceName; + } + + public String getParcelServiceApiURL() { + return parcelServiceApiURL; + } +} \ No newline at end of file diff --git a/prototype/src/main/java/org/hso/ecommerce/app/config/YAMLData.java b/prototype/src/main/java/org/hso/ecommerce/app/config/YAMLData.java new file mode 100644 index 0000000..eb0dd60 --- /dev/null +++ b/prototype/src/main/java/org/hso/ecommerce/app/config/YAMLData.java @@ -0,0 +1,183 @@ +package org.hso.ecommerce.app.config; + +import java.util.List; + +public class YAMLData { + + private String installationName; + private String companyName; + private Address companyAddress; + private int numberOfStorageSpaces; + private List suppliers; + private String parcelServiceName; + private String parcelServiceApiURL; + + + public String getInstallationName() { + return installationName; + } + public void setInstallationName(String installationName) { + this.installationName = installationName; + } + public String getCompanyName() { + return companyName; + } + public void setCompanyName(String companyName) { + this.companyName = companyName; + } + public Address getCompanyAddress() { + return companyAddress; + } + public void setCompanyAddress(Address companyAddress) { + this.companyAddress = companyAddress; + } + public int getNumberOfStorageSpaces() { + return numberOfStorageSpaces; + } + public void setNumberOfStorageSpaces(int numberOfStorageSpaces) { + this.numberOfStorageSpaces = numberOfStorageSpaces; + } + public List getSuppliers() { + return suppliers; + } + public void setSuppliers(List suppliers) { + this.suppliers = suppliers; + } + public String getParcelServiceName() { + return parcelServiceName; + } + public void setParcelServiceName(String parcelServiceName) { + this.parcelServiceName = parcelServiceName; + } + public String getParcelServiceApiURL() { + return parcelServiceApiURL; + } + public void setParcelServiceApiURL(String parcelServiceApiURL) { + this.parcelServiceApiURL = parcelServiceApiURL; + } + + public static class Address { + + public String streetName; + public String houseNumber; + public String zipCode; + public String cityName; + public String countryName; + + public Address() { + // needed by snakeyaml + } + + public Address(String streetName, String houseNumber, String zipCode, String cityName, String countryName) { + this.streetName = streetName; + this.houseNumber = houseNumber; + this.zipCode = zipCode; + this.cityName = cityName; + this.countryName = countryName; + } + + public String getStreetName() { + return streetName; + } + + public void setStreetName(String streetName) { + this.streetName = streetName; + } + + public String getHouseNumber() { + return houseNumber; + } + + public void setHouseNumber(String houseNumber) { + this.houseNumber = houseNumber; + } + + public String getZipCode() { + return zipCode; + } + + public void setZipCode(String zipCode) { + this.zipCode = zipCode; + } + + public String getCityName() { + return cityName; + } + + public void setCityName(String cityName) { + this.cityName = cityName; + } + + public String getCountryName() { + return countryName; + } + + public void setCountryName(String countryName) { + this.countryName = countryName; + } + } + + public static class Supplier { + + public String name; + public String id; + public String apiURL; + public int deliveryTime; + public Address companyAddress; + + + public Supplier() { + // needed by snakeyaml + } + + public Supplier(String name, String id, String apiURL, int deliveryTime, Address companyAddress) { + this.name = name; + this.id = id; + this.apiURL = apiURL; + this.deliveryTime = deliveryTime; + this.companyAddress = companyAddress; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getApiURL() { + return apiURL; + } + + public void setApiURL(String apiURL) { + this.apiURL = apiURL; + } + + public int getDeliveryTime() { + return deliveryTime; + } + + public void setDeliveryTime(int deliveryTime) { + this.deliveryTime = deliveryTime; + } + + public Address getCompanyAddress() { + return companyAddress; + } + + public void setCompanyAddress(Address companyAddress) { + this.companyAddress = companyAddress; + } + + } + +} diff --git a/prototype/src/main/java/org/hso/ecommerce/components/SlotInitializer.java b/prototype/src/main/java/org/hso/ecommerce/components/SlotInitializer.java index 72c4f8a..df32144 100644 --- a/prototype/src/main/java/org/hso/ecommerce/components/SlotInitializer.java +++ b/prototype/src/main/java/org/hso/ecommerce/components/SlotInitializer.java @@ -1,5 +1,6 @@ package org.hso.ecommerce.components; +import org.hso.ecommerce.app.config.AppSettings; import org.hso.ecommerce.entities.warehouse.Slot; import org.hso.ecommerce.repos.warehouse.SlotRepository; import org.springframework.beans.factory.annotation.Autowired; @@ -13,18 +14,22 @@ public class SlotInitializer { @Autowired private final SlotRepository slotRepository = null; - // TODO: use values form cfg. - private final int NUM_SLOTS = 50; + @Autowired + private final AppSettings appSettings = null; + + @PostConstruct + public void init() { + int NUM_SLOTS = appSettings.getNumberOfStorageSpaces(); + + for (int i = 1; i <= NUM_SLOTS; i++) { + if (!slotRepository.findBySlotNum(i).isPresent()) { + Slot slotAdded = new Slot(); + slotAdded.slotNum = i; + slotRepository.save(slotAdded); + + System.out.println("Added Slot " + i + " to DB"); + } + } + } - @PostConstruct - public void init() { - for (int i = 1; i <= NUM_SLOTS; i++) { - if (!slotRepository.findBySlotNum(i).isPresent()) { - Slot slotAdded = new Slot(); - slotAdded.slotNum = i; - slotRepository.save(slotAdded); - System.out.println("Added Slot " + i + " to DB"); - } - } - } }