153 lines
3.6 KiB
Java
153 lines
3.6 KiB
Java
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 String installationName;
|
|
private String companyName;
|
|
private Address companyAddress;
|
|
private int numberOfStorageSpaces;
|
|
private List<YAMLData.Supplier> suppliers;
|
|
private String parcelServiceName;
|
|
private 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");
|
|
data.setCompanyName("newCommerce GmbH");
|
|
data.setCompanyAddress(new Address(
|
|
"Kupfergraben",
|
|
"6",
|
|
"10117",
|
|
"Berlin",
|
|
"Germany"
|
|
));
|
|
data.setNumberOfStorageSpaces(128);
|
|
|
|
List<YAMLData.Supplier> 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");
|
|
|
|
try (FileWriter writer = new FileWriter("./eCommerce_config.yml")) {
|
|
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();
|
|
|
|
try (InputStream inputStream = new FileInputStream("./eCommerce_config.yml")) {
|
|
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.");
|
|
e.printStackTrace();
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
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;
|
|
}
|
|
} |