fix ApplicationSettings.readConfig()
This commit is contained in:
parent
d121532fba
commit
e1c00eca8f
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
||||
}
|
||||
}
|
|
@ -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<YAMLData.Supplier> suppliers = new ArrayList<YAMLData.Supplier>();
|
||||
// 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<Supplier> suppliers = new ArrayList<Supplier>();
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
|
|
Reference in New Issue