From e1d7becc2e6eb7ea300886581b7e877431463132 Mon Sep 17 00:00:00 2001 From: localhorst Date: Sun, 10 May 2020 21:23:44 +0200 Subject: [PATCH] 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; + } + + } + */ +}