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 ;
2020-06-16 19:19:58 +02:00
import javax.annotation.PostConstruct ;
2020-06-16 20:42:40 +02:00
@Component ( " appSettings " )
2020-05-10 21:23:44 +02:00
public class AppSettings {
2020-06-16 20:42:40 +02:00
private YAMLData data ;
2020-05-10 21:23:44 +02:00
2020-06-16 20:42:40 +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 ;
2020-06-16 19:19:58 +02:00
@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! " ) ;
2020-05-10 21:23:44 +02:00
}
2020-06-16 19:19:58 +02:00
/ * *
* write a demo config file
* /
2020-05-10 21:23:44 +02:00
public void writeDemoConfig ( ) {
YAMLData data = new YAMLData ( ) ;
data . setInstallationName ( " Fast-Web-Shop " ) ;
data . setCompanyName ( " newCommerce GmbH " ) ;
2020-06-16 18:52:33 +02:00
data . setCompanyAddress ( new Address (
" Kupfergraben " ,
" 6 " ,
" 10117 " ,
" Berlin " ,
" Germany "
) ) ;
2020-05-10 21:23:44 +02:00
data . setNumberOfStorageSpaces ( 128 ) ;
2020-06-16 19:19:58 +02:00
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 ) ;
2020-05-10 21:23:44 +02:00
data . setParcelServiceName ( " DHL International GmbH " ) ;
data . setParcelServiceApiURL ( " https://dhl.api.ecommerce.mosad.xyz " ) ;
2020-06-16 18:52:33 +02:00
try ( FileWriter writer = new FileWriter ( " ./eCommerce_config.yml " ) ) {
Yaml yaml = new Yaml ( ) ;
2020-05-10 21:23:44 +02:00
yaml . dump ( data , writer ) ;
} catch ( IOException e ) {
e . printStackTrace ( ) ;
}
}
2020-06-16 19:19:58 +02:00
/ * *
* 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
2020-06-16 18:52:33 +02:00
try ( InputStream inputStream = new FileInputStream ( " ./eCommerce_config.yml " ) ) {
Yaml yaml = new Yaml ( new Constructor ( YAMLData . class ) ) ;
data = yaml . load ( inputStream ) ;
2020-06-16 19:19:58 +02:00
} catch ( FileNotFoundException e ) {
System . err . println ( " The file \" eCommerce_config.yml \" has not been found, please create a valid Configuration file. " ) ;
e . printStackTrace ( ) ;
2020-06-16 18:52:33 +02:00
} catch ( IOException e ) {
2020-05-10 22:19:58 +02:00
e . printStackTrace ( ) ;
}
2020-06-16 18:52:33 +02:00
return data ;
2020-05-10 21:23:44 +02:00
}
2020-06-16 20:42:40 +02:00
public YAMLData getData ( ) {
2020-06-16 19:19:58 +02:00
return data ;
}
2020-06-16 20:42:40 +02:00
public String getInstallationName ( ) {
2020-06-16 19:19:58 +02:00
return installationName ;
}
2020-06-16 20:42:40 +02:00
public String getCompanyName ( ) {
2020-06-16 19:19:58 +02:00
return companyName ;
}
2020-06-16 20:42:40 +02:00
public Address getCompanyAddress ( ) {
2020-06-16 19:19:58 +02:00
return companyAddress ;
}
2020-06-16 20:42:40 +02:00
public int getNumberOfStorageSpaces ( ) {
2020-06-16 19:19:58 +02:00
return numberOfStorageSpaces ;
}
2020-06-16 20:42:40 +02:00
public List < YAMLData . Supplier > getSuppliers ( ) {
2020-06-16 19:19:58 +02:00
return suppliers ;
}
2020-06-16 20:42:40 +02:00
public String getParcelServiceName ( ) {
2020-06-16 19:19:58 +02:00
return parcelServiceName ;
}
2020-06-16 20:42:40 +02:00
public String getParcelServiceApiURL ( ) {
2020-06-16 19:19:58 +02:00
return parcelServiceApiURL ;
}
2020-05-10 21:23:44 +02:00
}