Connected application components
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package de.rwu.easydrop.service.mapping;
|
||||
|
||||
import de.rwu.easydrop.api.dto.OfferDTO;
|
||||
|
||||
import de.rwu.easydrop.model.Offer;
|
||||
|
||||
/**
|
||||
@@ -31,6 +30,10 @@ public final class OfferMapper {
|
||||
*/
|
||||
public static Offer mapOfferFromDTO(final OfferDTO dto) {
|
||||
Offer offer = new Offer();
|
||||
offer.setOfferId(dto.getOfferId());
|
||||
offer.setSourceProduct(ProductMapper.mapProductFromDTO(dto.getSourceProduct()));
|
||||
offer.setTargetProduct(ProductMapper.mapProductFromDTO(dto.getTargetProduct()));
|
||||
offer.setLastUpdate(dto.getLastUpdate());
|
||||
|
||||
return offer;
|
||||
}
|
||||
@@ -42,7 +45,11 @@ public final class OfferMapper {
|
||||
* @return OfferDTO
|
||||
*/
|
||||
public static OfferDTO mapOfferToDTO(final Offer offer) {
|
||||
OfferDTO dto = new OfferDTO(offer.getOfferId());
|
||||
OfferDTO dto = new OfferDTO();
|
||||
dto.setOfferId(offer.getOfferId());
|
||||
dto.setSourceProduct(ProductMapper.mapProductToDTO(offer.getSourceProduct()));
|
||||
dto.setTargetProduct(ProductMapper.mapProductToDTO(offer.getTargetProduct()));
|
||||
dto.setLastUpdate(offer.getLastUpdate());
|
||||
|
||||
return dto;
|
||||
}
|
||||
|
||||
@@ -39,6 +39,7 @@ public final class ProductMapper {
|
||||
product.setDeliveryPrice(dto.getDeliveryPrice());
|
||||
product.setMerchant(dto.getMerchant());
|
||||
product.setProductId(dto.getProductId());
|
||||
product.setLastUpdate(dto.getLastUpdate());
|
||||
|
||||
return product;
|
||||
}
|
||||
@@ -56,6 +57,7 @@ public final class ProductMapper {
|
||||
dto.setCurrentPrice(product.getCurrentPrice());
|
||||
dto.setDeliveryPrice(product.getDeliveryPrice());
|
||||
dto.setMerchant(product.getMerchant());
|
||||
dto.setLastUpdate(product.getLastUpdate());
|
||||
|
||||
return dto;
|
||||
}
|
||||
|
||||
@@ -1,56 +1,56 @@
|
||||
package de.rwu.easydrop.service.processing;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import de.rwu.easydrop.exception.InvalidCatalogueException;
|
||||
import de.rwu.easydrop.exception.InvalidOfferException;
|
||||
import de.rwu.easydrop.model.Offer;
|
||||
import de.rwu.easydrop.model.Product;
|
||||
import de.rwu.easydrop.model.ProductCatalogue;
|
||||
import de.rwu.easydrop.model.ProductPair;
|
||||
import de.rwu.easydrop.util.FormattingUtil;
|
||||
import de.rwu.easydrop.util.Timestamp;
|
||||
|
||||
/**
|
||||
* Creates dropshipping orders based on price margin.
|
||||
*
|
||||
* @since 0.3.0
|
||||
*/
|
||||
public final class OrderManager {
|
||||
|
||||
public class OfferIdentifier {
|
||||
/**
|
||||
* Temporary logging instance.
|
||||
* Logging instance.
|
||||
*/
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(OrderManager.class);
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(OfferIdentifier.class);
|
||||
|
||||
/**
|
||||
* Private constructor to prevent unwanted instantiation.
|
||||
* runIdentifier calls the price function that decides
|
||||
* if it is feasible to dropship products.
|
||||
*
|
||||
* @throws UnsupportedOperationException always
|
||||
* @param pCats Product catalogues
|
||||
* @return Identified offers
|
||||
*/
|
||||
private OrderManager() throws UnsupportedOperationException {
|
||||
throw new UnsupportedOperationException("This is a stateless class, don't instantiate it.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates orders for products with sufficient margin.
|
||||
*
|
||||
* @param pCats Product Catalogues
|
||||
*/
|
||||
public static void createOrders(final List<ProductCatalogue> pCats) {
|
||||
public List<Offer> runIdentifier(final List<ProductCatalogue> pCats) {
|
||||
List<Offer> identifiedOffers = new ArrayList<>();
|
||||
for (ProductCatalogue pCat : pCats) {
|
||||
ProductPair pair = getHighestMarginProducts(pCat);
|
||||
|
||||
// #12: Create actual orders/transactions, remove logger
|
||||
// Call price finder for all catalogue
|
||||
ProductPair pair = getHighestMarginProducts(pCat);
|
||||
Offer possibleOffer = new Offer();
|
||||
possibleOffer.setLastUpdate(Timestamp.now());
|
||||
possibleOffer.setSourceProduct(pair.getProduct1());
|
||||
possibleOffer.setTargetProduct(pair.getProduct2());
|
||||
identifiedOffers.add(possibleOffer);
|
||||
|
||||
double margin = pair.getProduct2().getCurrentPrice()
|
||||
- pair.getProduct1().getCurrentPrice();
|
||||
String marginFormatted = FormattingUtil.formatEuro(margin);
|
||||
LOGGER.info("{}: Margin {} ({} to {})",
|
||||
LOGGER.info("\n Identified Offer: {} ({} to {}) with margin {} ",
|
||||
pCat.getProductName(),
|
||||
marginFormatted,
|
||||
pair.getProduct1().getDataOrigin(),
|
||||
pair.getProduct2().getDataOrigin());
|
||||
pair.getProduct2().getDataOrigin(),
|
||||
marginFormatted);
|
||||
}
|
||||
|
||||
return identifiedOffers;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -79,7 +79,7 @@ public final class OrderManager {
|
||||
}
|
||||
|
||||
if (cheapestProduct.getCurrentPrice() == mostExpensiveProduct.getCurrentPrice()) {
|
||||
throw new InvalidCatalogueException("Price margin is zero!");
|
||||
throw new InvalidOfferException("Price margin is zero!");
|
||||
}
|
||||
|
||||
return new ProductPair(cheapestProduct, mostExpensiveProduct);
|
||||
@@ -0,0 +1,81 @@
|
||||
package de.rwu.easydrop.service.processing;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import de.rwu.easydrop.api.client.AmazonSeller;
|
||||
import de.rwu.easydrop.api.client.EbaySeller;
|
||||
import de.rwu.easydrop.data.connector.OfferPersistenceInterface;
|
||||
import de.rwu.easydrop.model.Offer;
|
||||
import de.rwu.easydrop.model.Webshop;
|
||||
import de.rwu.easydrop.service.mapping.ProductMapper;
|
||||
import de.rwu.easydrop.service.writer.OfferWriter;
|
||||
import de.rwu.easydrop.util.Config;
|
||||
import de.rwu.easydrop.util.FormattingUtil;
|
||||
|
||||
public class OfferProvisioner {
|
||||
/**
|
||||
* Config.
|
||||
*/
|
||||
private Config config;
|
||||
/**
|
||||
* Writes offers into persistence.
|
||||
*/
|
||||
private OfferWriter offerWriter;
|
||||
|
||||
/**
|
||||
* Is the API for selling products on Amazon.
|
||||
*/
|
||||
private AmazonSeller amazonSeller;
|
||||
/**
|
||||
* Is the API for selling products on Ebay.
|
||||
*/
|
||||
private EbaySeller ebaySeller;
|
||||
|
||||
private void toSeller(final Offer offer) throws IllegalArgumentException {
|
||||
if (offer.getTargetProduct().getDataOrigin() == Webshop.eBay) {
|
||||
this.ebaySeller.sellProduct(ProductMapper.mapProductToDTO(offer.getTargetProduct()));
|
||||
|
||||
} else if (offer.getTargetProduct().getDataOrigin().equals(Webshop.Amazon)) {
|
||||
this.amazonSeller.sellProduct(ProductMapper.mapProductToDTO(offer.getTargetProduct()));
|
||||
} else {
|
||||
throw new IllegalArgumentException("Unsupported target plattform");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Is the class for placing orders on a target platform.
|
||||
*
|
||||
* @param db Persistence Interface
|
||||
*/
|
||||
public OfferProvisioner(final OfferPersistenceInterface db) {
|
||||
this.offerWriter = new OfferWriter(db);
|
||||
this.config = Config.getInstance();
|
||||
this.amazonSeller = new AmazonSeller(
|
||||
config.getProperty("AMAZON_API_URL"),
|
||||
config.getProperty("AMAZON_API_KEY"));
|
||||
this.ebaySeller = new EbaySeller(
|
||||
config.getProperty("EBAY_API_URL"),
|
||||
config.getProperty("EBAY_API_KEY"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Method for placing orders on a target platform.
|
||||
*
|
||||
* @param offersToProvision
|
||||
*/
|
||||
public final void runProvisioner(final List<Offer> offersToProvision) {
|
||||
for (Offer newOffer : offersToProvision) {
|
||||
String newOfferId = FormattingUtil.removeSpaces(
|
||||
newOffer.getSourceProduct().getDataOrigin().toString()
|
||||
+ newOffer.getTargetProduct().getDataOrigin().toString()
|
||||
+ "_"
|
||||
+ newOffer.getSourceProduct().getProductId()
|
||||
+ newOffer.getTargetProduct().getProductId());
|
||||
|
||||
this.toSeller(newOffer);
|
||||
newOffer.setOfferId(newOfferId);
|
||||
offerWriter.writeOfferToPersistence(newOffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,9 @@ import java.util.List;
|
||||
|
||||
import javax.naming.ConfigurationException;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import de.rwu.easydrop.model.Product;
|
||||
import de.rwu.easydrop.model.ProductCatalogue;
|
||||
import de.rwu.easydrop.util.ProductsConfig;
|
||||
@@ -17,6 +20,11 @@ import lombok.Data;
|
||||
*/
|
||||
@Data
|
||||
public class CatalogueRetriever {
|
||||
/**
|
||||
* Logging instance.
|
||||
*/
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(CatalogueRetriever.class);
|
||||
|
||||
/**
|
||||
* User-configured products.
|
||||
*/
|
||||
@@ -56,15 +64,14 @@ public class CatalogueRetriever {
|
||||
pCat.getProductName(), pCat.getDescription());
|
||||
|
||||
for (Product product : pCat.getProducts()) {
|
||||
Product newProduct = new Product();
|
||||
|
||||
newProduct = productRetriever.getProductFromWebshop(product.getDataOrigin(),
|
||||
Product newProduct = productRetriever.getProductFromWebshop(product.getDataOrigin(),
|
||||
product.getProductId());
|
||||
|
||||
newProductCatalogue.addProduct(newProduct);
|
||||
}
|
||||
|
||||
productCatalogues.add(newProductCatalogue);
|
||||
LOGGER.info("\nLoaded Catalogue: \n" + newProductCatalogue.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,40 @@
|
||||
package de.rwu.easydrop.service.retriever;
|
||||
|
||||
public class OfferRetriever {
|
||||
import de.rwu.easydrop.api.dto.OfferDTO;
|
||||
import de.rwu.easydrop.data.connector.OfferPersistenceInterface;
|
||||
import de.rwu.easydrop.model.Offer;
|
||||
import de.rwu.easydrop.service.mapping.OfferMapper;
|
||||
|
||||
/**
|
||||
* Retrieves offer information from different sources.
|
||||
*
|
||||
* @since 0.3.0
|
||||
*/
|
||||
public class OfferRetriever {
|
||||
/**
|
||||
* Persistence interface.
|
||||
*/
|
||||
private OfferPersistenceInterface persistence;
|
||||
|
||||
/**
|
||||
* Creates an Offer Retriever.
|
||||
*
|
||||
* @param db Persistence Interface
|
||||
*/
|
||||
public OfferRetriever(final OfferPersistenceInterface db) {
|
||||
this.persistence = db;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves an offer from persistence.
|
||||
*
|
||||
* @param offerId
|
||||
* @return Offer from persistence
|
||||
*/
|
||||
public Offer getOfferFromPersistence(final String offerId) {
|
||||
OfferPersistenceInterface src = persistence;
|
||||
|
||||
OfferDTO dto = src.getOfferDTOById(offerId);
|
||||
return OfferMapper.mapOfferFromDTO(dto);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +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.data.connector.ProductPersistenceInterface;
|
||||
import de.rwu.easydrop.model.Product;
|
||||
import de.rwu.easydrop.model.Webshop;
|
||||
import de.rwu.easydrop.service.mapping.ProductMapper;
|
||||
@@ -20,19 +20,20 @@ public class ProductRetriever {
|
||||
* Data source factory.
|
||||
*/
|
||||
private DataSourceFactory dataSourceFactory;
|
||||
|
||||
/**
|
||||
* @param newDataSourceFactory the WebshopFactory to set
|
||||
* Persistence interface.
|
||||
*/
|
||||
public void setWebshopFactory(final DataSourceFactory newDataSourceFactory) {
|
||||
this.dataSourceFactory = newDataSourceFactory;
|
||||
}
|
||||
private ProductPersistenceInterface persistence;
|
||||
|
||||
/**
|
||||
* @param newDataSourceFactory
|
||||
* @param newPersistence
|
||||
*/
|
||||
public ProductRetriever(final DataSourceFactory newDataSourceFactory) {
|
||||
this.setWebshopFactory(newDataSourceFactory);
|
||||
public ProductRetriever(
|
||||
final DataSourceFactory newDataSourceFactory,
|
||||
final ProductPersistenceInterface newPersistence) {
|
||||
this.dataSourceFactory = newDataSourceFactory;
|
||||
this.persistence = newPersistence;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -74,9 +75,7 @@ public class ProductRetriever {
|
||||
* @return Product from persistence
|
||||
*/
|
||||
public Product getProductFromPersistence(final String productId) {
|
||||
AbstractProductPersistence src = dataSourceFactory.createProductPersistenceDataSource();
|
||||
|
||||
ProductDTO dto = src.getProductDTOById(productId);
|
||||
ProductDTO dto = persistence.getProductDTOById(productId);
|
||||
Product product = ProductMapper.mapProductFromDTO(dto);
|
||||
ProductValidator.validate(product);
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package de.rwu.easydrop.service.validation;
|
||||
|
||||
import de.rwu.easydrop.exception.InvalidOfferException;
|
||||
import de.rwu.easydrop.model.Offer;
|
||||
|
||||
/**
|
||||
@@ -8,7 +9,7 @@ import de.rwu.easydrop.model.Offer;
|
||||
* @since 0.2.0
|
||||
*/
|
||||
public final class OfferValidator {
|
||||
/**
|
||||
/**
|
||||
* Private constructor to prevent unwanted instantiation.
|
||||
*
|
||||
* @throws UnsupportedOperationException always
|
||||
@@ -16,13 +17,19 @@ public final class OfferValidator {
|
||||
private OfferValidator() throws UnsupportedOperationException {
|
||||
throw new UnsupportedOperationException("This is a validator class, don't instantiate it.");
|
||||
}
|
||||
/**
|
||||
|
||||
/**
|
||||
* Makes sure an Offer does not contain invalid information.
|
||||
*
|
||||
* @param offer the Offer
|
||||
*/
|
||||
public static void validate(final Offer offer) { }
|
||||
public static void validate(final Offer offer) {
|
||||
try {
|
||||
if (offer.getOfferId().equals("")) {
|
||||
throw new InvalidOfferException("Offer ID cannot be empty");
|
||||
}
|
||||
} catch (NullPointerException e) {
|
||||
throw new InvalidOfferException("Required information is missing in the offer", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ 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.data.connector.ProductPersistenceInterface;
|
||||
import de.rwu.easydrop.model.Product;
|
||||
import de.rwu.easydrop.model.ProductCatalogue;
|
||||
import de.rwu.easydrop.service.mapping.ProductMapper;
|
||||
@@ -18,14 +18,14 @@ public final class CatalogueWriter {
|
||||
/**
|
||||
* Holds a persistence reference.
|
||||
*/
|
||||
private AbstractProductPersistence persistence;
|
||||
private ProductPersistenceInterface persistence;
|
||||
|
||||
/**
|
||||
* Creates new instance.
|
||||
*
|
||||
* @param newPersistence
|
||||
*/
|
||||
public CatalogueWriter(final AbstractProductPersistence newPersistence) {
|
||||
public CatalogueWriter(final ProductPersistenceInterface newPersistence) {
|
||||
persistence = newPersistence;
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ public final class CatalogueWriter {
|
||||
ProductValidator.validate(product);
|
||||
ProductDTO dto = ProductMapper.mapProductToDTO(product);
|
||||
|
||||
persistence.saveProduct(dto);
|
||||
persistence.writeProduct(dto);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,21 +1,20 @@
|
||||
package de.rwu.easydrop.service.writer;
|
||||
|
||||
import de.rwu.easydrop.api.dto.OfferDTO;
|
||||
import de.rwu.easydrop.data.connector.AbstractOfferPersistence;
|
||||
import de.rwu.easydrop.data.connector.OfferPersistenceInterface;
|
||||
import de.rwu.easydrop.model.Offer;
|
||||
import de.rwu.easydrop.service.mapping.OfferMapper;
|
||||
import de.rwu.easydrop.service.validation.OfferValidator;
|
||||
|
||||
public class OfferWriter {
|
||||
/**
|
||||
* Persistence.
|
||||
*/
|
||||
private AbstractOfferPersistence persistence;
|
||||
private OfferPersistenceInterface persistence;
|
||||
|
||||
/**
|
||||
* @param newPersistence the persistence to set
|
||||
*/
|
||||
public void setPersistence(final AbstractOfferPersistence newPersistence) {
|
||||
public OfferWriter(final OfferPersistenceInterface newPersistence) {
|
||||
this.persistence = newPersistence;
|
||||
}
|
||||
|
||||
@@ -25,9 +24,8 @@ public class OfferWriter {
|
||||
* @param offer
|
||||
*/
|
||||
public void writeOfferToPersistence(final Offer offer) {
|
||||
OfferValidator.validate(offer);
|
||||
OfferDTO dto = OfferMapper.mapOfferToDTO(offer);
|
||||
|
||||
persistence.saveOffer(dto);
|
||||
persistence.writeOffer(dto);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package de.rwu.easydrop.service.writer;
|
||||
|
||||
import de.rwu.easydrop.api.dto.ProductDTO;
|
||||
import de.rwu.easydrop.data.connector.AbstractProductPersistence;
|
||||
import de.rwu.easydrop.data.connector.ProductPersistenceInterface;
|
||||
import de.rwu.easydrop.model.Product;
|
||||
import de.rwu.easydrop.service.mapping.ProductMapper;
|
||||
import de.rwu.easydrop.service.validation.ProductValidator;
|
||||
@@ -15,12 +15,12 @@ public class ProductWriter {
|
||||
/**
|
||||
* Persistence.
|
||||
*/
|
||||
private AbstractProductPersistence persistence;
|
||||
private ProductPersistenceInterface persistence;
|
||||
|
||||
/**
|
||||
* @param newPersistence the persistence to set
|
||||
*/
|
||||
public void setPersistence(final AbstractProductPersistence newPersistence) {
|
||||
public ProductWriter(final ProductPersistenceInterface newPersistence) {
|
||||
this.persistence = newPersistence;
|
||||
}
|
||||
|
||||
@@ -33,6 +33,6 @@ public class ProductWriter {
|
||||
ProductValidator.validate(product);
|
||||
ProductDTO dto = ProductMapper.mapProductToDTO(product);
|
||||
|
||||
persistence.saveProduct(dto);
|
||||
persistence.writeProduct(dto);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user