From 774e62f8923a474fd7975fb67c3d5f328a570bcf Mon Sep 17 00:00:00 2001 From: Seil0 Date: Tue, 16 Jun 2020 19:19:58 +0200 Subject: [PATCH] add suppliers to YAMLData, store data in AppSettings() --- prototype/eCommerce_config.yml | 13 ++ .../org/hso/ecommerce/app/InitController.java | 21 --- .../hso/ecommerce/app/config/AppSettings.java | 120 ++++++++++++++---- .../hso/ecommerce/app/config/YAMLData.java | 55 +++++++- .../ecommerce/components/SlotInitializer.java | 4 +- 5 files changed, 161 insertions(+), 52 deletions(-) delete mode 100644 prototype/src/main/java/org/hso/ecommerce/app/InitController.java diff --git a/prototype/eCommerce_config.yml b/prototype/eCommerce_config.yml index e0bb4cd..c46e0cc 100644 --- a/prototype/eCommerce_config.yml +++ b/prototype/eCommerce_config.yml @@ -6,3 +6,16 @@ installationName: Fast-Web-Shop numberOfStorageSpaces: 128 parcelServiceApiURL: https://dhl.api.ecommerce.mosad.xyz parcelServiceName: DHL International GmbH +suppliers: +- apiURL: https://reichelt.api.ecommerce.mosad.xyz + companyAddress: {cityName: Sande, countryName: Germany, houseNumber: '1', streetName: Elektronikring, + zipCode: '26452'} + deliveryTime: 4 + id: d41d8cd98f00b204e9800998ecf8427e + name: Reichelt elektronik GmbH & Co. KG +- apiURL: https://conrad.api.ecommerce.mosad.xyz + companyAddress: {cityName: Hirschau, countryName: Germany, houseNumber: '1', streetName: Klaus-Conrad-Straße, + zipCode: '92240'} + deliveryTime: 5 + id: 18a17da5bac1cf00551b08c3e98720f5 + name: Conrad Electronic SE diff --git a/prototype/src/main/java/org/hso/ecommerce/app/InitController.java b/prototype/src/main/java/org/hso/ecommerce/app/InitController.java deleted file mode 100644 index 9860b95..0000000 --- a/prototype/src/main/java/org/hso/ecommerce/app/InitController.java +++ /dev/null @@ -1,21 +0,0 @@ -package org.hso.ecommerce.app; - -import org.hso.ecommerce.app.config.AppSettings; -import org.springframework.stereotype.Component; - -import javax.annotation.PostConstruct; - -@Component -public class InitController { - - @PostConstruct - public void init() { - System.out.println("Initialize!"); - - - AppSettings appSettings = AppSettings.getInstance(); - //appSettings.writeDemoConfig(); - appSettings.readConfig(); - - } -} 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 index c7da089..0f5d40d 100644 --- a/prototype/src/main/java/org/hso/ecommerce/app/config/AppSettings.java +++ b/prototype/src/main/java/org/hso/ecommerce/app/config/AppSettings.java @@ -9,21 +9,42 @@ import org.springframework.stereotype.Component; import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.constructor.Constructor; +import javax.annotation.PostConstruct; + @Component public class AppSettings { - private static AppSettings instance; - private AppSettings() { - // constructor - } - - public static AppSettings getInstance() { - if (instance == null) { - instance = new AppSettings(); - } - return instance; + private static YAMLData data; + + private static String installationName; + private static String companyName; + private static Address companyAddress; + private static int numberOfStorageSpaces; + private static List suppliers; + private static String parcelServiceName; + private static String parcelServiceApiURL; + + @PostConstruct + /** + * on initialization read the config and store the data in static objects + */ + 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 a demo config file + */ public void writeDemoConfig() { YAMLData data = new YAMLData(); data.setInstallationName("Fast-Web-Shop"); @@ -36,16 +57,35 @@ public class AppSettings { "Germany" )); data.setNumberOfStorageSpaces(128); - -// List suppliers = new ArrayList(); -// suppliers.add(new YAMLData.Supplier("Reichelt elektronik GmbH & Co. KG", "d41d8cd98f00b204e9800998ecf8427e", -// "https://reichelt.api.ecommerce.mosad.xyz", -// new Address("Elektronikring", "1", "26452", "Sande", "Germany"), 4)); -// suppliers.add(new YAMLData.Supplier("Conrad Electronic SE", "18a17da5bac1cf00551b08c3e98720f5", -// "https://conrad.api.ecommerce.mosad.xyz", -// new Address("Klaus-Conrad-Straße", "1", "92240", "Hirschau", "Germany"), 5)); -// -// data.setSuppliers(suppliers); + + List suppliers = new ArrayList<>(); + suppliers.add(new YAMLData.Supplier( + "Reichelt elektronik GmbH & Co. KG", + "d41d8cd98f00b204e9800998ecf8427e", + "https://reichelt.api.ecommerce.mosad.xyz", + 4, + new Address( + "Elektronikring", + "1", + "26452", + "Sande", + "Germany" + ) + )); + suppliers.add(new YAMLData.Supplier( + "Conrad Electronic SE", + "18a17da5bac1cf00551b08c3e98720f5", + "https://conrad.api.ecommerce.mosad.xyz", + 5, + new Address( + "Klaus-Conrad-Straße", + "1", + "92240", + "Hirschau", + "Germany" + ) + )); + data.setSuppliers(suppliers); data.setParcelServiceName("DHL International GmbH"); data.setParcelServiceApiURL("https://dhl.api.ecommerce.mosad.xyz"); @@ -59,14 +99,19 @@ public class AppSettings { } + /** + * 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(); try (InputStream inputStream = new FileInputStream("./eCommerce_config.yml")) { Yaml yaml = new Yaml(new Constructor(YAMLData.class)); data = yaml.load(inputStream); - - System.out.println(data.getCompanyName()); + } catch (FileNotFoundException e) { + System.err.println("The file \"eCommerce_config.yml\" has not been found, please create a valid Configuration file."); + e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } @@ -74,4 +119,35 @@ public class AppSettings { return data; } + public static YAMLData getData() { + return data; + } + + public static String getInstallationName() { + return installationName; + } + + public static String getCompanyName() { + return companyName; + } + + public static Address getCompanyAddress() { + return companyAddress; + } + + public static int getNumberOfStorageSpaces() { + return numberOfStorageSpaces; + } + + public static List getSuppliers() { + return suppliers; + } + + public static String getParcelServiceName() { + return parcelServiceName; + } + + public static 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 index 3bfd1d0..eb0dd60 100644 --- a/prototype/src/main/java/org/hso/ecommerce/app/config/YAMLData.java +++ b/prototype/src/main/java/org/hso/ecommerce/app/config/YAMLData.java @@ -1,12 +1,14 @@ 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 List suppliers; private String parcelServiceName; private String parcelServiceApiURL; @@ -35,14 +37,12 @@ public class YAMLData { 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; } @@ -116,27 +116,68 @@ public class YAMLData { this.countryName = countryName; } } -/* + public static class Supplier { public String name; public String id; public String apiURL; - public Address companyAddress; public int deliveryTime; + public Address companyAddress; + public Supplier() { // needed by snakeyaml } - public Supplier(String name, String id, String apiURL, Address companyAddress, int deliveryTime) { + 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 ce8c39b..ba0e2a8 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,8 +14,7 @@ public class SlotInitializer { @Autowired private final SlotRepository slotRepository = null; - // TODO: use values form cfg. - private final int NUM_SLOTS = 50; + private final int NUM_SLOTS = AppSettings.getNumberOfStorageSpaces(); @PostConstruct public void init() {