#53 Implemented SQLite product data persistence + tests
This commit is contained in:
@@ -1,16 +1,17 @@
|
||||
package de.rwu.easydrop.service.mapping;
|
||||
|
||||
import de.rwu.easydrop.api.dto.ProductDTO;
|
||||
|
||||
import de.rwu.easydrop.model.Product;
|
||||
|
||||
/**
|
||||
* Maps between Product, ProductDAO and ProductDTO.
|
||||
* Maps between Product, ProductDTO and ProductDTO.
|
||||
*
|
||||
* @since 0.2.0
|
||||
*
|
||||
* @see Product
|
||||
* @see ProductDTO
|
||||
* @see ProductDAO
|
||||
* @see ProductDTO
|
||||
*/
|
||||
public final class ProductMapper {
|
||||
|
||||
@@ -41,4 +42,21 @@ public final class ProductMapper {
|
||||
|
||||
return product;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a ProductDTO object from a corresponding Product.
|
||||
*
|
||||
* @param product Product
|
||||
* @return ProductDTO
|
||||
*/
|
||||
public static ProductDTO mapProductToDTO(final Product product) {
|
||||
ProductDTO dto = new ProductDTO(product.getProductId(), product.getDataOrigin());
|
||||
|
||||
dto.setAvailable(product.isAvailable());
|
||||
dto.setCurrentPrice(product.getCurrentPrice());
|
||||
dto.setDeliveryPrice(product.getDeliveryPrice());
|
||||
dto.setMerchant(product.getMerchant());
|
||||
|
||||
return dto;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import java.util.List;
|
||||
|
||||
import javax.naming.ConfigurationException;
|
||||
|
||||
import de.rwu.easydrop.exception.InvalidProductException;
|
||||
import de.rwu.easydrop.model.Product;
|
||||
import de.rwu.easydrop.model.ProductCatalogue;
|
||||
import de.rwu.easydrop.util.ProductsConfig;
|
||||
@@ -60,10 +61,12 @@ public class CatalogueRetriever {
|
||||
newProduct.setDataOrigin(product.getDataOrigin());
|
||||
newProduct.setProductId(product.getProductId());
|
||||
|
||||
if (product.getDataOrigin().equals("Amazon")) {
|
||||
if (newProduct.getDataOrigin().equals("Amazon")) {
|
||||
newProduct = productRetriever.getProductFromAmazon(product.getProductId());
|
||||
} else if (product.getDataOrigin().equals("eBay")) {
|
||||
} else if (newProduct.getDataOrigin().equals("eBay")) {
|
||||
newProduct = productRetriever.getProductFromEbay(product.getProductId());
|
||||
} else {
|
||||
throw new InvalidProductException("Product data origin is invalid");
|
||||
}
|
||||
|
||||
newProductCatalogue.addProduct(newProduct);
|
||||
|
||||
@@ -4,6 +4,7 @@ import de.rwu.easydrop.api.client.AmazonProductDataSource;
|
||||
import de.rwu.easydrop.api.client.DataSourceFactory;
|
||||
import de.rwu.easydrop.api.client.EbayItemDataSource;
|
||||
import de.rwu.easydrop.api.dto.ProductDTO;
|
||||
import de.rwu.easydrop.data.connector.AbstractProductPersistence;
|
||||
import de.rwu.easydrop.model.Product;
|
||||
import de.rwu.easydrop.service.mapping.ProductMapper;
|
||||
import de.rwu.easydrop.service.validation.ProductValidator;
|
||||
@@ -64,4 +65,20 @@ public class ProductRetriever {
|
||||
|
||||
return product;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves a product from persistence.
|
||||
*
|
||||
* @param productId
|
||||
* @return Product from persistence
|
||||
*/
|
||||
public Product getProductFromPersistence(final String productId) {
|
||||
AbstractProductPersistence src = dataSourceFactory.createProductPersistenceDataSource();
|
||||
|
||||
ProductDTO dto = src.getProductDTOById(productId);
|
||||
Product product = ProductMapper.mapProductFromDTO(dto);
|
||||
ProductValidator.validate(product);
|
||||
|
||||
return product;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
package de.rwu.easydrop.service.writer;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import de.rwu.easydrop.api.dto.ProductDTO;
|
||||
import de.rwu.easydrop.data.connector.AbstractProductPersistence;
|
||||
import de.rwu.easydrop.model.Product;
|
||||
import de.rwu.easydrop.model.ProductCatalogue;
|
||||
import de.rwu.easydrop.service.mapping.ProductMapper;
|
||||
import de.rwu.easydrop.service.validation.ProductValidator;
|
||||
|
||||
/**
|
||||
* Writes data for all products of multiple catalogues to persistence.
|
||||
*
|
||||
* @since 0.2.0
|
||||
*/
|
||||
public final class CatalogueWriter {
|
||||
/**
|
||||
* Holds a persistence reference.
|
||||
*/
|
||||
private AbstractProductPersistence persistence;
|
||||
|
||||
/**
|
||||
* Creates new instance.
|
||||
*
|
||||
* @param newPersistence
|
||||
*/
|
||||
public CatalogueWriter(final AbstractProductPersistence newPersistence) {
|
||||
persistence = newPersistence;
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes all products of specified catalogues to persistence.
|
||||
*
|
||||
* @param catalogues
|
||||
*/
|
||||
public void writeCatalogues(final List<ProductCatalogue> catalogues) {
|
||||
for (ProductCatalogue pCat : catalogues) {
|
||||
for (Product product : pCat.getProducts()) {
|
||||
ProductValidator.validate(product);
|
||||
ProductDTO dto = ProductMapper.mapProductToDTO(product);
|
||||
|
||||
persistence.saveProduct(dto);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package de.rwu.easydrop.service.writer;
|
||||
|
||||
import de.rwu.easydrop.api.dto.ProductDTO;
|
||||
import de.rwu.easydrop.data.connector.AbstractProductPersistence;
|
||||
import de.rwu.easydrop.model.Product;
|
||||
import de.rwu.easydrop.service.mapping.ProductMapper;
|
||||
import de.rwu.easydrop.service.validation.ProductValidator;
|
||||
|
||||
/**
|
||||
* Wrapper for writing product info to persistence.
|
||||
*
|
||||
* @since 0.2.0
|
||||
*/
|
||||
public class ProductWriter {
|
||||
/**
|
||||
* Persistence.
|
||||
*/
|
||||
private AbstractProductPersistence persistence;
|
||||
|
||||
/**
|
||||
* @param newPersistence the persistence to set
|
||||
*/
|
||||
public void setPersistence(final AbstractProductPersistence newPersistence) {
|
||||
this.persistence = newPersistence;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates and saves product to persistence.
|
||||
*
|
||||
* @param product
|
||||
*/
|
||||
public void writeProductToPersistence(final Product product) {
|
||||
ProductValidator.validate(product);
|
||||
ProductDTO dto = ProductMapper.mapProductToDTO(product);
|
||||
|
||||
persistence.saveProduct(dto);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
/**
|
||||
* Writes Objects to a data store.
|
||||
*
|
||||
* @since 0.2.0
|
||||
*/
|
||||
package de.rwu.easydrop.service.writer;
|
||||
Reference in New Issue
Block a user