This repository has been archived on 2020-08-02. You can view files and clone it, but cannot push or open issues or pull requests.
e-commerce/web_backend/src/main/java/org/hso/ecommerce/app/config/AppSettings.java

161 lines
3.7 KiB
Java
Raw Normal View History

2020-05-10 21:23:44 +02:00
package org.hso.ecommerce.app.config;
2020-06-16 18:52:33 +02:00
import java.io.*;
2020-05-10 21:23:44 +02:00
import java.util.ArrayList;
import java.util.List;
2020-05-10 22:19:58 +02:00
import org.hso.ecommerce.app.config.YAMLData.Address;
2020-06-16 18:52:33 +02:00
import org.springframework.stereotype.Component;
2020-05-10 21:23:44 +02:00
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;
import javax.annotation.PostConstruct;
@Component("appSettings")
2020-05-10 21:23:44 +02:00
public class AppSettings {
private YAMLData data;
private final String configFile = "config.yml";
2020-05-10 21:23:44 +02:00
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!");
2020-05-10 21:23:44 +02:00
}
/**
* write the default config file
*/
public void writeDefaultConfig() {
2020-05-10 21:23:44 +02:00
YAMLData data = new YAMLData();
data.setInstallationName("eCommerce");
data.setCompanyName("eCommerce Shop UG");
2020-06-16 18:52:33 +02:00
data.setCompanyAddress(new Address(
"Musterstraße",
"1",
"12345",
"Musterstadt",
2020-06-16 18:52:33 +02:00
"Germany"
));
2020-05-10 21:23:44 +02:00
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);
2020-05-10 21:23:44 +02:00
data.setParcelServiceName("Parcel Service");
data.setParcelServiceApiURL("http://[::1]:8082/");
2020-05-10 21:23:44 +02:00
try (FileWriter writer = new FileWriter("./" + configFile)) {
2020-06-16 18:52:33 +02:00
Yaml yaml = new Yaml();
2020-05-10 21:23:44 +02:00
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
*/
2020-06-16 18:52:33 +02:00
public YAMLData readConfig() {
YAMLData data = new YAMLData();
2020-05-10 22:19:58 +02:00
File file = new File("./" + configFile);
if (!file.exists()) {
writeDefaultConfig();
}
try (InputStream inputStream = new FileInputStream("./" + configFile)) {
2020-06-16 18:52:33 +02:00
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.");
e.printStackTrace();
System.exit(1);
2020-06-16 18:52:33 +02:00
} catch (IOException e) {
2020-05-10 22:19:58 +02:00
e.printStackTrace();
System.exit(1);
2020-05-10 22:19:58 +02:00
}
2020-06-16 18:52:33 +02:00
return data;
2020-05-10 21:23:44 +02:00
}
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;
}
2020-05-10 21:23:44 +02:00
}