diff --git a/src/main/java/de/rwu/easydrop/core/OfferProvisioner.java b/src/main/java/de/rwu/easydrop/core/OfferProvisioner.java index 27aa676..0209812 100644 --- a/src/main/java/de/rwu/easydrop/core/OfferProvisioner.java +++ b/src/main/java/de/rwu/easydrop/core/OfferProvisioner.java @@ -1,18 +1,49 @@ package de.rwu.easydrop.core; +import java.io.IOException; import java.util.List; +import de.rwu.easydrop.util.Config; import de.rwu.easydrop.data.connector.AbstractProductPersistence; +import de.rwu.easydrop.exception.DataWriterException; import de.rwu.easydrop.model.Offer; import de.rwu.easydrop.service.retriever.ProductRetriever; import de.rwu.easydrop.service.writer.OfferWriter; +import de.rwu.easydrop.api.client.AmazonSeller; +import de.rwu.easydrop.api.client.EbaySeller; +import de.rwu.easydrop.api.dto.ProductDTO; public class OfferProvisioner { - ProductRetriever productRetriever; + Config config; + ProductRetriever productRetriever; // ProductWriter AbstractProductPersistence db; OfferWriter offerWriter; + AmazonSeller amazonSeller; + EbaySeller ebaySeller; + + private void toSeller(Offer offer) throws IllegalArgumentException { + // TODO dataOrigin should use the webshop enum + if(offer.getSaleProduct().getDataOrigin().toLowerCase().equals("ebay")){ + this.ebaySeller.sellProduct(new ProductDTO( + offer.getSaleProduct().getProductId(), + "Amazon") + ); + + } else if (offer.getSaleProduct().getDataOrigin().toLowerCase().equals("Amazon")) { + this.amazonSeller.sellProduct(new ProductDTO( + offer.getSaleProduct().getProductId(), + "eBay") + ); + } + else { + throw new IllegalArgumentException("Unsupported target plattform"); + } + + } + + public OfferProvisioner(/*OfferWriter for database? */) { @@ -20,6 +51,15 @@ public class OfferProvisioner { //oder eine Konstante anlegen die das halten würde (besser skalierbar) //die Konstante in model anlegen (in Product, Konstante valid origins) this.offerWriter = new OfferWriter(); + 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") + ); } public void runProvisioner(List offersToProvision) { @@ -28,23 +68,28 @@ public class OfferProvisioner { * Stellt Angebote bei der Zielplattform ein * Schreibt eingestellte Angebote in die Datenbank */ - for(Offer newOffer: offersToProvision){ - // if(newOffer.getSourceProduct().getDataOrigin().equals("ebay")){ - // //put newOffer on amazon - // } - //else put newOffer on eBay - - // if successfully transmitted - // add to persistence - // "duplicate" the product with dataOrigin new platform and merchant = "me" - offerWriter.writeOfferToPersistence(newOffer); + try { + this.toSeller(newOffer); + // if successfully transmitted + // add to persistence + // "duplicate" the product with dataOrigin new platform and merchant = "me" + try { + offerWriter.writeOfferToPersistence(newOffer); + } + catch (Exception e){ + System.out.println("Could not write to persistence"); + } + } catch (IllegalArgumentException e) { + System.out.println( + "Offer could not be placed, " + + newOffer.getSaleProduct().getDataOrigin() + + " is not supported" + ); + } catch (DataWriterException e) { + System.out.println("could not transmit offer"); + } } - - - - } - }