From 6fe29e4319d4a10cf187495dadd96484147e4aaf Mon Sep 17 00:00:00 2001 From: Marvin Scham Date: Wed, 28 Jun 2023 02:57:13 +0200 Subject: [PATCH] Added missing buy logic --- src/main/java/de/rwu/easydrop/core/Core.java | 26 ++++--- .../data/connector/SQLiteConnector.java | 7 +- .../service/processing/OfferProvisioner.java | 69 +++++++++++++++---- 3 files changed, 76 insertions(+), 26 deletions(-) diff --git a/src/main/java/de/rwu/easydrop/core/Core.java b/src/main/java/de/rwu/easydrop/core/Core.java index 6e6111b..45f01ef 100644 --- a/src/main/java/de/rwu/easydrop/core/Core.java +++ b/src/main/java/de/rwu/easydrop/core/Core.java @@ -9,6 +9,8 @@ import org.slf4j.LoggerFactory; import org.sqlite.SQLiteDataSource; import de.rwu.easydrop.api.client.DataSourceFactory; +import de.rwu.easydrop.api.client.PurchaserFactory; +import de.rwu.easydrop.api.client.SellerFactory; import de.rwu.easydrop.api.dto.OfferDTO; import de.rwu.easydrop.data.connector.OfferPersistenceInterface; import de.rwu.easydrop.data.connector.ProductPersistenceInterface; @@ -56,12 +58,16 @@ public final class Core { LOGGER.info("Preparing..."); DataSourceFactory dataSourceFactory = new DataSourceFactory(config); - ProductPersistenceInterface pdb = new SQLiteConnector(new SQLiteDataSource()); - OfferPersistenceInterface odb = new SQLiteConnector(new SQLiteDataSource()); - ProductRetriever retriever = new ProductRetriever(dataSourceFactory, pdb); + SellerFactory sellerFactory = new SellerFactory(config); + PurchaserFactory purchaserFactory = new PurchaserFactory(config); + + ProductPersistenceInterface productDB = new SQLiteConnector(new SQLiteDataSource()); + OfferPersistenceInterface offerDB = new SQLiteConnector(new SQLiteDataSource()); + TransactionPersistenceInterface txDB = new SQLiteConnector(new SQLiteDataSource()); + + ProductRetriever retriever = new ProductRetriever(dataSourceFactory, productDB); CatalogueRetriever catRetriever = new CatalogueRetriever(pConfig, retriever); - CatalogueWriter catWriter = new CatalogueWriter(pdb); - TransactionPersistenceInterface tpi = new SQLiteConnector(new SQLiteDataSource()); + CatalogueWriter catWriter = new CatalogueWriter(productDB); LOGGER.info("Loading catalogues"); catRetriever.loadCatalogues(); @@ -70,17 +76,17 @@ public final class Core { LOGGER.info("Creating offers"); OfferIdentifier ident = new OfferIdentifier(); + OfferProvisioner provis = new OfferProvisioner(offerDB, sellerFactory, purchaserFactory); List identifiedOffers = ident.runIdentifier(pCats); - OfferProvisioner provis = new OfferProvisioner(odb); provis.runProvisioner(identifiedOffers); LOGGER.info("Creating transactions"); - TransactionHandler txHandler = new TransactionHandler(tpi); - for (OfferDTO o : odb.getOffers()) { + TransactionHandler txHandler = new TransactionHandler(txDB); + for (OfferDTO o : offerDB.getOffers()) { txHandler.turnOfferToTransaction(OfferMapper.mapOfferFromDTO(o)); } - tpi.outputTransactionsToLog(); - tpi.outputSummaryToLog(); + txDB.outputTransactionsToLog(); + txDB.outputSummaryToLog(); } } diff --git a/src/main/java/de/rwu/easydrop/data/connector/SQLiteConnector.java b/src/main/java/de/rwu/easydrop/data/connector/SQLiteConnector.java index fff1cc8..3b110b5 100644 --- a/src/main/java/de/rwu/easydrop/data/connector/SQLiteConnector.java +++ b/src/main/java/de/rwu/easydrop/data/connector/SQLiteConnector.java @@ -319,9 +319,12 @@ public final class SQLiteConnector implements sb.append("\n"); sb.append(String.format("%3d", ++index)); sb.append(String.format(" - %s - ", resultSet.getString("transactionTime"))); - sb.append(String.format("+%s (%s) - %s", + sb.append(String.format("+%s (%s) - ", FormattingUtil.formatEuro(resultSet.getDouble("earnings")), - FormattingUtil.formatEuro(resultSet.getDouble("volume")), + FormattingUtil.formatEuro(resultSet.getDouble("volume")))); + sb.append(String.format("[%s to %s] %s", + resultSet.getString("sourceWebshop"), + resultSet.getString("targetWebshop"), resultSet.getString(OFFERID_COL_NAME))); } } diff --git a/src/main/java/de/rwu/easydrop/service/processing/OfferProvisioner.java b/src/main/java/de/rwu/easydrop/service/processing/OfferProvisioner.java index 5c1847e..384fdc7 100644 --- a/src/main/java/de/rwu/easydrop/service/processing/OfferProvisioner.java +++ b/src/main/java/de/rwu/easydrop/service/processing/OfferProvisioner.java @@ -2,21 +2,27 @@ package de.rwu.easydrop.service.processing; import java.util.List; +import de.rwu.easydrop.api.client.AmazonPurchaser; import de.rwu.easydrop.api.client.AmazonSeller; +import de.rwu.easydrop.api.client.EbayPurchaser; import de.rwu.easydrop.api.client.EbaySeller; +import de.rwu.easydrop.api.client.PurchaserFactory; +import de.rwu.easydrop.api.client.SellerFactory; 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; +import lombok.Data; +/** + * Offer provisioner. + * + * @since 0.3.0 + */ +@Data public class OfferProvisioner { - /** - * Config. - */ - private Config config; /** * Writes offers into persistence. */ @@ -30,7 +36,20 @@ public class OfferProvisioner { * Is the API for selling products on Ebay. */ private EbaySeller ebaySeller; + /** + * Is the API for buying products on Amazon. + */ + private AmazonPurchaser amazonPurchaser; + /** + * Is the API for buying products on Ebay. + */ + private EbayPurchaser ebayPurchaser; + /** + * Puts up the sell order. + * + * @param offer + */ private void toSeller(final Offer offer) throws IllegalArgumentException { if (offer.getTargetProduct().getDataOrigin() == Webshop.EBAY) { this.ebaySeller.sellProduct(ProductMapper.mapProductToDTO(offer.getTargetProduct())); @@ -43,20 +62,41 @@ public class OfferProvisioner { } + /** + * Puts up the buy order. + * + * @param offer + */ + private void toBuyer(final Offer offer) throws IllegalArgumentException { + if (offer.getSourceProduct().getDataOrigin() == Webshop.EBAY) { + this.ebayPurchaser.purchaseProduct( + ProductMapper.mapProductToDTO(offer.getSourceProduct())); + + } else if (offer.getTargetProduct().getDataOrigin().equals(Webshop.AMAZON)) { + this.amazonPurchaser.purchaseProduct( + ProductMapper.mapProductToDTO(offer.getSourceProduct())); + } else { + throw new IllegalArgumentException("Unsupported target plattform"); + } + + } + /** * Is the class for placing orders on a target platform. * - * @param db Persistence Interface + * @param db Persistence Interface + * @param newSellerFactory Seller Factory + * @param newPurchaserFactory Purchaser Factory */ - public OfferProvisioner(final OfferPersistenceInterface db) { + public OfferProvisioner( + final OfferPersistenceInterface db, + final SellerFactory newSellerFactory, + final PurchaserFactory newPurchaserFactory) { 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")); + this.ebaySeller = newSellerFactory.createEbaySeller(); + this.amazonSeller = newSellerFactory.createAmazonSeller(); + this.ebayPurchaser = newPurchaserFactory.createEbayPurchaser(); + this.amazonPurchaser = newPurchaserFactory.createAmazonPurchaser(); } /** @@ -74,6 +114,7 @@ public class OfferProvisioner { + newOffer.getTargetProduct().getProductId()); this.toSeller(newOffer); + this.toBuyer(newOffer); newOffer.setOfferId(newOfferId); offerWriter.writeOfferToPersistence(newOffer); }