feature/config #78

Merged
CodeSteak merged 10 commits from feature/config into master 2020-06-18 14:09:47 +02:00
5 changed files with 365 additions and 13 deletions

View File

@ -5,3 +5,5 @@ e-commerce.db
./e-commerce.iml
./e-commerce.ipr
./e-commerce.iws
config.yml

View File

@ -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")
}

View File

@ -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<YAMLData.Supplier> 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<YAMLData.Supplier> 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();

Vlcht besser als private/public static final String in der Klasse.

wollen wir wirklich "./eCommerce_config.yml" als Dateinamen? Hat merkwürdiges Caseing (underscore und Groß/klein-schreibung.).

Vlcht besser als private/public static final String in der Klasse. wollen wir wirklich "./eCommerce_config.yml" als Dateinamen? Hat merkwürdiges Caseing (underscore und Groß/klein-schreibung.).
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.");

Führt nach im catch(...) Fall evtl zu null pointer deref. Vlcht im catch leere werte setzen oder app beenden.

Führt nach im catch(...) Fall evtl zu null pointer deref. Vlcht im catch leere werte setzen oder app beenden.
Outdated
Review

Wollen wir mit leeren Werten starten oder beenden? Ich hab jetzt mal ein System.exit() eingebaut.

Wollen wir mit leeren Werten starten oder beenden? Ich hab jetzt mal ein `System.exit()` eingebaut.
e.printStackTrace();
System.exit(1);
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}

In den Getter ist nicht sichergestellt, dass die werte schon initialisiert sind. Andere Komponenten könnten schon vorher PostConstruct ausgeführt haben.

In den Getter ist nicht sichergestellt, dass die werte schon initialisiert sind. Andere Komponenten könnten schon vorher PostConstruct ausgeführt haben.
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<YAMLData.Supplier> getSuppliers() {
return suppliers;
}
public String getParcelServiceName() {
return parcelServiceName;
}
public String getParcelServiceApiURL() {
return parcelServiceApiURL;
}
}

View File

@ -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<Supplier> 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<Supplier> getSuppliers() {
return suppliers;
}
public void setSuppliers(List<Supplier> 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;
}
}
}

View File

@ -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

Muss in init() eingelesen werden wegen obigen Problem

Muss in init() eingelesen werden wegen obigen Problem
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");
}
}
}
}