From e1d7becc2e6eb7ea300886581b7e877431463132 Mon Sep 17 00:00:00 2001 From: localhorst Date: Sun, 10 May 2020 21:23:44 +0200 Subject: [PATCH 1/8] added yaml dump&parse class --- prototype/build.gradle | 1 + prototype/eCommerce_config.yml | 3 + .../org/hso/ecommerce/app/Application.java | 14 ++- .../hso/ecommerce/app/config/AppSettings.java | 89 +++++++++++++++++ .../hso/ecommerce/app/config/YAMLData.java | 99 +++++++++++++++++++ 5 files changed, 205 insertions(+), 1 deletion(-) create mode 100644 prototype/eCommerce_config.yml create mode 100644 prototype/src/main/java/org/hso/ecommerce/app/config/AppSettings.java create mode 100644 prototype/src/main/java/org/hso/ecommerce/app/config/YAMLData.java diff --git a/prototype/build.gradle b/prototype/build.gradle index 2769783..63716e3 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.28.0' + implementation 'org.yaml:snakeyaml:1.26' testCompile("org.springframework.boot:spring-boot-starter-test") } diff --git a/prototype/eCommerce_config.yml b/prototype/eCommerce_config.yml new file mode 100644 index 0000000..9b146c6 --- /dev/null +++ b/prototype/eCommerce_config.yml @@ -0,0 +1,3 @@ +!!org.hso.ecommerce.app.config.YAMLData {companyName: newCommerce GmbH, installationName: Fast-Web-Shop, + numberOfStorageSpaces: 128, parcelServiceApiURL: 'https://dhl.api.ecommerce.mosad.xyz', + parcelServiceName: DHL International GmbH} diff --git a/prototype/src/main/java/org/hso/ecommerce/app/Application.java b/prototype/src/main/java/org/hso/ecommerce/app/Application.java index 8b45a22..51c45af 100644 --- a/prototype/src/main/java/org/hso/ecommerce/app/Application.java +++ b/prototype/src/main/java/org/hso/ecommerce/app/Application.java @@ -1,11 +1,13 @@ package org.hso.ecommerce.app; +import org.hso.ecommerce.app.config.AppSettings; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.domain.EntityScan; import org.springframework.context.annotation.ComponentScan; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; + @SpringBootApplication @ComponentScan(basePackages = {"org.hso.ecommerce"}) @EntityScan("org.hso.ecommerce") @@ -13,7 +15,17 @@ import org.springframework.data.jpa.repository.config.EnableJpaRepositories; public class Application { public static void main(String[] args) { - SpringApplication.run(Application.class, args); + + AppSettings appSettings = AppSettings.getInstance(); // This does not belongs here + + //appSettings.writeDemoConfig(); + + appSettings.readConfig(); // This does not belongs here either + + + + + //SpringApplication.run(Application.class, args); } } 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..fff755a --- /dev/null +++ b/prototype/src/main/java/org/hso/ecommerce/app/config/AppSettings.java @@ -0,0 +1,89 @@ +package org.hso.ecommerce.app.config; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.constructor.Constructor; + +public class AppSettings { + private static AppSettings conf; + + private AppSettings() { + } + + public static AppSettings getInstance() { + if (AppSettings.conf == null) { + AppSettings.conf = new AppSettings(); + } + return AppSettings.conf; + } + + public void writeDemoConfig() { + + Yaml yaml = new Yaml(); + + YAMLData data = new YAMLData(); + + data.setInstallationName("Fast-Web-Shop"); + data.setCompanyName("newCommerce GmbH"); + //data.setCompanyAddress(new Address("Kupfergraben", "6", "10117", "Berlin", "Germany")); + data.setNumberOfStorageSpaces(128); + + /* + + List suppliers = new ArrayList(); + suppliers.add(new Supplier("Reichelt elektronik GmbH & Co. KG", "d41d8cd98f00b204e9800998ecf8427e", + "https://reichelt.api.ecommerce.mosad.xyz", + new Address("Elektronikring", "1", "26452", "Sande", "Germany"), 4)); + suppliers.add(new 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); + + */ + + data.setParcelServiceName("DHL International GmbH"); + data.setParcelServiceApiURL("https://dhl.api.ecommerce.mosad.xyz"); + + FileWriter writer; + try { + writer = new FileWriter("./eCommerce_config.yml"); + yaml.dump(data, writer); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + + public void readConfig() { +/* + Yaml yaml = new Yaml(new Constructor(YAMLData.class)); + InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("./eCommerce_config.yml"); + YAMLData data = yaml.load(inputStream); + */ + String input = "!!org.hso.ecommerce.app.config.YAMLData {companyName: newCommerce GmbH, installationName: Fast-Web-Shop,\n" + + " numberOfStorageSpaces: 128, parcelServiceApiURL: 'https://dhl.api.ecommerce.mosad.xyz',\n" + + " parcelServiceName: DHL International GmbH}\n" + + ""; + + Yaml yaml = new Yaml(new Constructor(YAMLData.class)); + + + YAMLData data = yaml.load(input); + + System.out.println(data.getCompanyName()); + + } + +} \ 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..bea290a --- /dev/null +++ b/prototype/src/main/java/org/hso/ecommerce/app/config/YAMLData.java @@ -0,0 +1,99 @@ +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(String streetName, String houseNumer, String zipCode, String cityName, String countryName) { + this.streetName = streetName; + this.houseNumber = houseNumer; + this.zipCode = zipCode; + this.cityName = cityName; + this.countryName = countryName; + } + } + + public static class Supplier { + + public String name; + public String id; + public String apiURL; + public Address companyAddress; + public int deliveryTime; + + public Supplier(String name, String id, String apiURL, Address companyAddress, int deliveryTime) { + this.name = name; + this.id = id; + this.apiURL = apiURL; + this.companyAddress = companyAddress; + this.deliveryTime = deliveryTime; + } + + } + */ +} From bba4173269484be9aead77920738f8cf1d3b1f0a Mon Sep 17 00:00:00 2001 From: localhorst Date: Sun, 10 May 2020 22:19:58 +0200 Subject: [PATCH 2/8] fixed inputstream once more --- prototype/eCommerce_config.yml | 11 +++++-- .../org/hso/ecommerce/app/Application.java | 5 +-- .../hso/ecommerce/app/config/AppSettings.java | 33 ++++++++++--------- .../hso/ecommerce/app/config/YAMLData.java | 10 +++--- 4 files changed, 31 insertions(+), 28 deletions(-) diff --git a/prototype/eCommerce_config.yml b/prototype/eCommerce_config.yml index 9b146c6..e0bb4cd 100644 --- a/prototype/eCommerce_config.yml +++ b/prototype/eCommerce_config.yml @@ -1,3 +1,8 @@ -!!org.hso.ecommerce.app.config.YAMLData {companyName: newCommerce GmbH, installationName: Fast-Web-Shop, - numberOfStorageSpaces: 128, parcelServiceApiURL: 'https://dhl.api.ecommerce.mosad.xyz', - parcelServiceName: DHL International GmbH} +!!org.hso.ecommerce.app.config.YAMLData +companyAddress: {cityName: Berlin, countryName: Germany, houseNumber: '6', streetName: Kupfergraben, + zipCode: '10117'} +companyName: newCommerce GmbH +installationName: Fast-Web-Shop +numberOfStorageSpaces: 128 +parcelServiceApiURL: https://dhl.api.ecommerce.mosad.xyz +parcelServiceName: DHL International GmbH diff --git a/prototype/src/main/java/org/hso/ecommerce/app/Application.java b/prototype/src/main/java/org/hso/ecommerce/app/Application.java index 51c45af..3bcb8e1 100644 --- a/prototype/src/main/java/org/hso/ecommerce/app/Application.java +++ b/prototype/src/main/java/org/hso/ecommerce/app/Application.java @@ -18,13 +18,10 @@ public class Application { AppSettings appSettings = AppSettings.getInstance(); // This does not belongs here - //appSettings.writeDemoConfig(); + appSettings.writeDemoConfig(); appSettings.readConfig(); // This does not belongs here either - - - //SpringApplication.run(Application.class, args); } 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 fff755a..402f7ce 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 @@ -11,6 +11,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; +import org.hso.ecommerce.app.config.YAMLData.Address; import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.constructor.Constructor; @@ -35,7 +36,7 @@ public class AppSettings { data.setInstallationName("Fast-Web-Shop"); data.setCompanyName("newCommerce GmbH"); - //data.setCompanyAddress(new Address("Kupfergraben", "6", "10117", "Berlin", "Germany")); + data.setCompanyAddress(new Address("Kupfergraben", "6", "10117", "Berlin", "Germany")); data.setNumberOfStorageSpaces(128); /* @@ -67,23 +68,23 @@ public class AppSettings { } public void readConfig() { -/* + Yaml yaml = new Yaml(new Constructor(YAMLData.class)); - InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("./eCommerce_config.yml"); - YAMLData data = yaml.load(inputStream); - */ - String input = "!!org.hso.ecommerce.app.config.YAMLData {companyName: newCommerce GmbH, installationName: Fast-Web-Shop,\n" + - " numberOfStorageSpaces: 128, parcelServiceApiURL: 'https://dhl.api.ecommerce.mosad.xyz',\n" + - " parcelServiceName: DHL International GmbH}\n" + - ""; + + InputStream inputStream; + try { + inputStream = new FileInputStream("./eCommerce_config.yml"); + + YAMLData data = yaml.load(inputStream); + + System.out.println(data.getCompanyName()); + + } catch (FileNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + - Yaml yaml = new Yaml(new Constructor(YAMLData.class)); - - - YAMLData data = yaml.load(input); - - System.out.println(data.getCompanyName()); - } } \ 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 bea290a..1069d02 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 @@ -7,7 +7,7 @@ public class YAMLData { private String installationName; private String companyName; - //private Address companyAddress; + private Address companyAddress; private int numberOfStorageSpaces; //private List suppliers; private String parcelServiceName; @@ -26,14 +26,14 @@ public class YAMLData { 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; } @@ -60,7 +60,7 @@ public class YAMLData { public void setParcelServiceApiURL(String parcelServiceApiURL) { this.parcelServiceApiURL = parcelServiceApiURL; } - /* + public static class Address { public String streetName; @@ -77,7 +77,7 @@ public class YAMLData { this.countryName = countryName; } } - +/* public static class Supplier { public String name; From e1c00eca8f55c040c3dee29e710b0bf230d6ebf5 Mon Sep 17 00:00:00 2001 From: Seil0 Date: Tue, 16 Jun 2020 18:52:33 +0200 Subject: [PATCH 3/8] fix ApplicationSettings.readConfig() --- .../org/hso/ecommerce/app/Application.java | 11 +-- .../org/hso/ecommerce/app/InitController.java | 21 +++++ .../hso/ecommerce/app/config/AppSettings.java | 83 ++++++++----------- .../hso/ecommerce/app/config/YAMLData.java | 57 +++++++++++-- 4 files changed, 107 insertions(+), 65 deletions(-) create mode 100644 prototype/src/main/java/org/hso/ecommerce/app/InitController.java diff --git a/prototype/src/main/java/org/hso/ecommerce/app/Application.java b/prototype/src/main/java/org/hso/ecommerce/app/Application.java index 3bcb8e1..8b45a22 100644 --- a/prototype/src/main/java/org/hso/ecommerce/app/Application.java +++ b/prototype/src/main/java/org/hso/ecommerce/app/Application.java @@ -1,13 +1,11 @@ package org.hso.ecommerce.app; -import org.hso.ecommerce.app.config.AppSettings; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.domain.EntityScan; import org.springframework.context.annotation.ComponentScan; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; - @SpringBootApplication @ComponentScan(basePackages = {"org.hso.ecommerce"}) @EntityScan("org.hso.ecommerce") @@ -15,14 +13,7 @@ import org.springframework.data.jpa.repository.config.EnableJpaRepositories; public class Application { public static void main(String[] args) { - - AppSettings appSettings = AppSettings.getInstance(); // This does not belongs here - - appSettings.writeDemoConfig(); - - appSettings.readConfig(); // This does not belongs here either - - //SpringApplication.run(Application.class, args); + SpringApplication.run(Application.class, args); } } diff --git a/prototype/src/main/java/org/hso/ecommerce/app/InitController.java b/prototype/src/main/java/org/hso/ecommerce/app/InitController.java new file mode 100644 index 0000000..9860b95 --- /dev/null +++ b/prototype/src/main/java/org/hso/ecommerce/app/InitController.java @@ -0,0 +1,21 @@ +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 402f7ce..c7da089 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 @@ -1,90 +1,77 @@ package org.hso.ecommerce.app.config; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.FileWriter; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; +import java.io.*; import java.util.ArrayList; import java.util.List; -import java.util.Map; import org.hso.ecommerce.app.config.YAMLData.Address; +import org.springframework.stereotype.Component; import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.constructor.Constructor; +@Component public class AppSettings { - private static AppSettings conf; + private static AppSettings instance; private AppSettings() { + // constructor } public static AppSettings getInstance() { - if (AppSettings.conf == null) { - AppSettings.conf = new AppSettings(); + if (instance == null) { + instance = new AppSettings(); } - return AppSettings.conf; + return instance; } public void writeDemoConfig() { - - Yaml yaml = new Yaml(); - YAMLData data = new YAMLData(); - data.setInstallationName("Fast-Web-Shop"); data.setCompanyName("newCommerce GmbH"); - data.setCompanyAddress(new Address("Kupfergraben", "6", "10117", "Berlin", "Germany")); + data.setCompanyAddress(new Address( + "Kupfergraben", + "6", + "10117", + "Berlin", + "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 Supplier("Reichelt elektronik GmbH & Co. KG", "d41d8cd98f00b204e9800998ecf8427e", - "https://reichelt.api.ecommerce.mosad.xyz", - new Address("Elektronikring", "1", "26452", "Sande", "Germany"), 4)); - suppliers.add(new 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); - - */ - data.setParcelServiceName("DHL International GmbH"); data.setParcelServiceApiURL("https://dhl.api.ecommerce.mosad.xyz"); - FileWriter writer; - try { - writer = new FileWriter("./eCommerce_config.yml"); + try (FileWriter writer = new FileWriter("./eCommerce_config.yml")) { + Yaml yaml = new Yaml(); yaml.dump(data, writer); } catch (IOException e) { - // TODO Auto-generated catch block e.printStackTrace(); } } - public void readConfig() { + public YAMLData readConfig() { + YAMLData data = new YAMLData(); - Yaml yaml = new Yaml(new Constructor(YAMLData.class)); + try (InputStream inputStream = new FileInputStream("./eCommerce_config.yml")) { + Yaml yaml = new Yaml(new Constructor(YAMLData.class)); + data = yaml.load(inputStream); - InputStream inputStream; - try { - inputStream = new FileInputStream("./eCommerce_config.yml"); - - YAMLData data = yaml.load(inputStream); - - System.out.println(data.getCompanyName()); - - } catch (FileNotFoundException e) { - // TODO Auto-generated catch block + System.out.println(data.getCompanyName()); + } catch (IOException e) { e.printStackTrace(); } - + return data; } } \ 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 1069d02..3bfd1d0 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,8 +1,5 @@ package org.hso.ecommerce.app.config; -import java.util.List; - - public class YAMLData { private String installationName; @@ -26,14 +23,12 @@ public class YAMLData { 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; } @@ -69,13 +64,57 @@ public class YAMLData { public String cityName; public String countryName; - public Address(String streetName, String houseNumer, String zipCode, String cityName, String countryName) { + public Address() { + // needed by snakeyaml + } + + public Address(String streetName, String houseNumber, String zipCode, String cityName, String countryName) { this.streetName = streetName; - this.houseNumber = houseNumer; + 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 { @@ -86,6 +125,10 @@ public class YAMLData { public Address companyAddress; public int deliveryTime; + public Supplier() { + // needed by snakeyaml + } + public Supplier(String name, String id, String apiURL, Address companyAddress, int deliveryTime) { this.name = name; this.id = id; From 774e62f8923a474fd7975fb67c3d5f328a570bcf Mon Sep 17 00:00:00 2001 From: Seil0 Date: Tue, 16 Jun 2020 19:19:58 +0200 Subject: [PATCH 4/8] 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() { From d5825ba7a0388afd545e20b7ac999ddd423631b8 Mon Sep 17 00:00:00 2001 From: Seil0 Date: Tue, 16 Jun 2020 20:42:40 +0200 Subject: [PATCH 5/8] use @Autowired to get AppSettings Object --- .../hso/ecommerce/app/config/AppSettings.java | 34 +++++++++---------- .../ecommerce/components/SlotInitializer.java | 5 ++- 2 files changed, 21 insertions(+), 18 deletions(-) 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 0f5d40d..fe02203 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 @@ -11,18 +11,18 @@ import org.yaml.snakeyaml.constructor.Constructor; import javax.annotation.PostConstruct; -@Component +@Component("appSettings") public class AppSettings { - private static YAMLData data; + private 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; + private String installationName; + private String companyName; + private Address companyAddress; + private int numberOfStorageSpaces; + private List suppliers; + private String parcelServiceName; + private String parcelServiceApiURL; @PostConstruct /** @@ -119,35 +119,35 @@ public class AppSettings { return data; } - public static YAMLData getData() { + public YAMLData getData() { return data; } - public static String getInstallationName() { + public String getInstallationName() { return installationName; } - public static String getCompanyName() { + public String getCompanyName() { return companyName; } - public static Address getCompanyAddress() { + public Address getCompanyAddress() { return companyAddress; } - public static int getNumberOfStorageSpaces() { + public int getNumberOfStorageSpaces() { return numberOfStorageSpaces; } - public static List getSuppliers() { + public List getSuppliers() { return suppliers; } - public static String getParcelServiceName() { + public String getParcelServiceName() { return parcelServiceName; } - public static String getParcelServiceApiURL() { + public String getParcelServiceApiURL() { return parcelServiceApiURL; } } \ No newline at end of file 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 ba0e2a8..ae9f572 100644 --- a/prototype/src/main/java/org/hso/ecommerce/components/SlotInitializer.java +++ b/prototype/src/main/java/org/hso/ecommerce/components/SlotInitializer.java @@ -14,10 +14,13 @@ public class SlotInitializer { @Autowired private final SlotRepository slotRepository = null; - private final int NUM_SLOTS = AppSettings.getNumberOfStorageSpaces(); + @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(); From 8c0652b26b33334499145092933bef7742fa345e Mon Sep 17 00:00:00 2001 From: Seil0 Date: Wed, 17 Jun 2020 22:32:48 +0200 Subject: [PATCH 6/8] exit if the settings file has not been found * use a final variable for the settings file name --- .../java/org/hso/ecommerce/app/config/AppSettings.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) 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 fe02203..552a13c 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 @@ -15,6 +15,7 @@ import javax.annotation.PostConstruct; public class AppSettings { private YAMLData data; + private final String configFile = "eCommerce_config.yml"; private String installationName; private String companyName; @@ -90,7 +91,7 @@ public class AppSettings { data.setParcelServiceName("DHL International GmbH"); data.setParcelServiceApiURL("https://dhl.api.ecommerce.mosad.xyz"); - try (FileWriter writer = new FileWriter("./eCommerce_config.yml")) { + try (FileWriter writer = new FileWriter("./" + configFile)) { Yaml yaml = new Yaml(); yaml.dump(data, writer); } catch (IOException e) { @@ -106,14 +107,16 @@ public class AppSettings { public YAMLData readConfig() { YAMLData data = new YAMLData(); - try (InputStream inputStream = new FileInputStream("./eCommerce_config.yml")) { + 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 \"eCommerce_config.yml\" has not been found, please create a valid Configuration file."); + 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; From ea0c8c45d6c6f4a8997195f42b710060c99158ac Mon Sep 17 00:00:00 2001 From: Seil0 Date: Thu, 18 Jun 2020 14:04:54 +0200 Subject: [PATCH 7/8] rename eCommerce_config.yml to config.yml * if no config file is present, create a default config file --- prototype/config.yml | 21 +++++++ prototype/eCommerce_config.yml | 21 ------- .../hso/ecommerce/app/config/AppSettings.java | 55 ++++++++++--------- 3 files changed, 51 insertions(+), 46 deletions(-) create mode 100644 prototype/config.yml delete mode 100644 prototype/eCommerce_config.yml diff --git a/prototype/config.yml b/prototype/config.yml new file mode 100644 index 0000000..5012f67 --- /dev/null +++ b/prototype/config.yml @@ -0,0 +1,21 @@ +!!org.hso.ecommerce.app.config.YAMLData +companyAddress: {cityName: Musterstadt, countryName: Germany, houseNumber: '1', streetName: Musterstraße, + zipCode: '12345'} +companyName: eCommerce Shop UG +installationName: eCommerce +numberOfStorageSpaces: 128 +parcelServiceApiURL: http://[::1]:8082/ +parcelServiceName: Parcel Service +suppliers: +- apiURL: http://[::1]:8081/bank/ + companyAddress: {cityName: Musterstadt, countryName: Germany, houseNumber: '2', + streetName: Musterstraße, zipCode: '12345'} + deliveryTime: 4 + id: d41d8cd98f00b204e9800998ecf8427e + name: Bank of Chees +- apiURL: http://[::1]:8081/mda/ + companyAddress: {cityName: Musterstadt, countryName: Germany, houseNumber: '3', + streetName: Musterstraße, zipCode: '12345'} + deliveryTime: 5 + id: 18a17da5bac1cf00551b08c3e98720f5 + name: MDA diff --git a/prototype/eCommerce_config.yml b/prototype/eCommerce_config.yml deleted file mode 100644 index c46e0cc..0000000 --- a/prototype/eCommerce_config.yml +++ /dev/null @@ -1,21 +0,0 @@ -!!org.hso.ecommerce.app.config.YAMLData -companyAddress: {cityName: Berlin, countryName: Germany, houseNumber: '6', streetName: Kupfergraben, - zipCode: '10117'} -companyName: newCommerce GmbH -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/config/AppSettings.java b/prototype/src/main/java/org/hso/ecommerce/app/config/AppSettings.java index 552a13c..625ed65 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 @@ -15,7 +15,7 @@ import javax.annotation.PostConstruct; public class AppSettings { private YAMLData data; - private final String configFile = "eCommerce_config.yml"; + private final String configFile = "config.yml"; private String installationName; private String companyName; @@ -24,11 +24,11 @@ public class AppSettings { private List suppliers; private String parcelServiceName; private String parcelServiceApiURL; - - @PostConstruct + /** * on initialization read the config and store the data in static objects */ + @PostConstruct public void init() { data = readConfig(); @@ -44,52 +44,52 @@ public class AppSettings { } /** - * write a demo config file + * write the default config file */ - public void writeDemoConfig() { + public void writeDefaultConfig() { YAMLData data = new YAMLData(); - data.setInstallationName("Fast-Web-Shop"); - data.setCompanyName("newCommerce GmbH"); + data.setInstallationName("eCommerce"); + data.setCompanyName("eCommerce Shop UG"); data.setCompanyAddress(new Address( - "Kupfergraben", - "6", - "10117", - "Berlin", + "Musterstraße", + "1", + "12345", + "Musterstadt", "Germany" )); data.setNumberOfStorageSpaces(128); List suppliers = new ArrayList<>(); suppliers.add(new YAMLData.Supplier( - "Reichelt elektronik GmbH & Co. KG", + "Bank of Chees", "d41d8cd98f00b204e9800998ecf8427e", - "https://reichelt.api.ecommerce.mosad.xyz", + "http://[::1]:8081/bank/", 4, new Address( - "Elektronikring", - "1", - "26452", - "Sande", + "Musterstraße", + "2", + "12345", + "Musterstadt", "Germany" ) )); suppliers.add(new YAMLData.Supplier( - "Conrad Electronic SE", + "MDA", "18a17da5bac1cf00551b08c3e98720f5", - "https://conrad.api.ecommerce.mosad.xyz", + "http://[::1]:8081/mda/", 5, new Address( - "Klaus-Conrad-Straße", - "1", - "92240", - "Hirschau", + "Musterstraße", + "3", + "12345", + "Musterstadt", "Germany" ) )); data.setSuppliers(suppliers); - data.setParcelServiceName("DHL International GmbH"); - data.setParcelServiceApiURL("https://dhl.api.ecommerce.mosad.xyz"); + data.setParcelServiceName("Parcel Service"); + data.setParcelServiceApiURL("http://[::1]:8082/"); try (FileWriter writer = new FileWriter("./" + configFile)) { Yaml yaml = new Yaml(); @@ -107,6 +107,11 @@ public class AppSettings { 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); From f4aae9c5811f1a6ce573cd5ee88fde4c533e3be3 Mon Sep 17 00:00:00 2001 From: Seil0 Date: Thu, 18 Jun 2020 14:07:59 +0200 Subject: [PATCH 8/8] ignore config.yml --- prototype/.gitignore | 2 ++ prototype/config.yml | 21 --------------------- 2 files changed, 2 insertions(+), 21 deletions(-) delete mode 100644 prototype/config.yml 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/config.yml b/prototype/config.yml deleted file mode 100644 index 5012f67..0000000 --- a/prototype/config.yml +++ /dev/null @@ -1,21 +0,0 @@ -!!org.hso.ecommerce.app.config.YAMLData -companyAddress: {cityName: Musterstadt, countryName: Germany, houseNumber: '1', streetName: Musterstraße, - zipCode: '12345'} -companyName: eCommerce Shop UG -installationName: eCommerce -numberOfStorageSpaces: 128 -parcelServiceApiURL: http://[::1]:8082/ -parcelServiceName: Parcel Service -suppliers: -- apiURL: http://[::1]:8081/bank/ - companyAddress: {cityName: Musterstadt, countryName: Germany, houseNumber: '2', - streetName: Musterstraße, zipCode: '12345'} - deliveryTime: 4 - id: d41d8cd98f00b204e9800998ecf8427e - name: Bank of Chees -- apiURL: http://[::1]:8081/mda/ - companyAddress: {cityName: Musterstadt, countryName: Germany, houseNumber: '3', - streetName: Musterstraße, zipCode: '12345'} - deliveryTime: 5 - id: 18a17da5bac1cf00551b08c3e98720f5 - name: MDA