Added utility structures + tests
This commit is contained in:
@@ -52,7 +52,6 @@ public final class Config {
|
||||
* Returns current config instance.
|
||||
*
|
||||
* @return Config instance
|
||||
* @throws ConfigurationException
|
||||
*/
|
||||
public static Config getInstance() {
|
||||
if (instance == null) {
|
||||
|
||||
142
src/main/java/de/rwu/easydrop/util/ProductsConfig.java
Normal file
142
src/main/java/de/rwu/easydrop/util/ProductsConfig.java
Normal file
@@ -0,0 +1,142 @@
|
||||
package de.rwu.easydrop.util;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import javax.naming.ConfigurationException;
|
||||
|
||||
import com.jayway.jsonpath.JsonPath;
|
||||
import com.jayway.jsonpath.JsonPathException;
|
||||
|
||||
import de.rwu.easydrop.model.Product;
|
||||
import de.rwu.easydrop.model.ProductCatalogue;
|
||||
|
||||
/**
|
||||
* Reads the user-specified catalogue of products.
|
||||
*
|
||||
* @since 0.2.0
|
||||
*/
|
||||
public final class ProductsConfig {
|
||||
/**
|
||||
* Products config location.
|
||||
*/
|
||||
private String configLocation = "config/products-config.json";
|
||||
|
||||
/**
|
||||
* @return the configLocation
|
||||
*/
|
||||
public String getConfigLocation() {
|
||||
return configLocation;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param newConfigLocation the configLocation to set
|
||||
*/
|
||||
public void setConfigLocation(final String newConfigLocation) {
|
||||
this.configLocation = newConfigLocation;
|
||||
}
|
||||
|
||||
/**
|
||||
* Holds the configured products.
|
||||
*/
|
||||
private List<ProductCatalogue> productCatalogues;
|
||||
|
||||
/**
|
||||
* @return the product catalogues
|
||||
*/
|
||||
public List<ProductCatalogue> getProductCatalogues() {
|
||||
return productCatalogues;
|
||||
}
|
||||
|
||||
/**
|
||||
* Singleton instance.
|
||||
*/
|
||||
private static ProductsConfig instance = null;
|
||||
|
||||
/**
|
||||
* Private constructor to prevent external instantiation.
|
||||
*/
|
||||
private ProductsConfig() {
|
||||
productCatalogues = new ArrayList<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns current products config instance.
|
||||
*
|
||||
* @return Products Config instance
|
||||
*/
|
||||
public static ProductsConfig getInstance() {
|
||||
if (instance == null) {
|
||||
instance = new ProductsConfig();
|
||||
}
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads user-specified configuration into productCatalogues attribute.
|
||||
*
|
||||
* @throws ConfigurationException
|
||||
*/
|
||||
public void loadConfig() throws ConfigurationException {
|
||||
try {
|
||||
File jsonFile = new File(configLocation).getAbsoluteFile();
|
||||
ArrayList<HashMap<String, Object>> jsonProducts = JsonPath.read(jsonFile, "$.products");
|
||||
|
||||
setProductCatalogues(jsonProducts);
|
||||
} catch (IOException e) {
|
||||
throw new ConfigurationException("Couldn't load required products config file");
|
||||
} catch (JsonPathException e) {
|
||||
throw new ConfigurationException("Products config is empty or malformed");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads catalogues from JSON.
|
||||
*
|
||||
* @param jsonCatalogues
|
||||
*/
|
||||
private void setProductCatalogues(final ArrayList<HashMap<String, Object>> jsonCatalogues) {
|
||||
for (HashMap<String, Object> productCatalogue : jsonCatalogues) {
|
||||
String name = productCatalogue.get("name").toString();
|
||||
String desc = productCatalogue.get("description").toString();
|
||||
String identifiers = productCatalogue.get("identifiers").toString();
|
||||
|
||||
ProductCatalogue pCat = new ProductCatalogue(name, desc);
|
||||
|
||||
addProductsToCatalogue(identifiers, pCat);
|
||||
productCatalogues.add(pCat);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads products from JSON.
|
||||
*
|
||||
* @param jsonIdentifiers
|
||||
* @param pCat
|
||||
*/
|
||||
private void addProductsToCatalogue(final String jsonIdentifiers, final ProductCatalogue pCat) {
|
||||
ArrayList<HashMap<String, Object>> identifiers = JsonPath.read(jsonIdentifiers, "$");
|
||||
|
||||
for (HashMap<String, Object> product : identifiers) {
|
||||
String dataOrigin = product.keySet().iterator().next();
|
||||
String identifier = product.get(dataOrigin).toString();
|
||||
|
||||
Product newProduct = new Product();
|
||||
newProduct.setDataOrigin(dataOrigin);
|
||||
newProduct.setProductId(identifier);
|
||||
|
||||
pCat.addProduct(newProduct);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets the contained product catalogues.
|
||||
*/
|
||||
public void reset() {
|
||||
productCatalogues = new ArrayList<>();
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
/**
|
||||
* General utility such as formatting helpers.
|
||||
*
|
||||
* TODO implement
|
||||
* @since 0.1.0
|
||||
*/
|
||||
package de.rwu.easydrop.util;
|
||||
|
||||
Reference in New Issue
Block a user