116 lines
3.9 KiB
Java
116 lines
3.9 KiB
Java
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.service.mapping.ProductMapper;
|
|
import de.rwu.easydrop.service.writer.OfferWriter;
|
|
import de.rwu.easydrop.util.FormattingUtil;
|
|
import lombok.Data;
|
|
|
|
/**
|
|
* Offer provisioner.
|
|
*
|
|
* @since 0.3.0
|
|
*/
|
|
@Data
|
|
public class OfferProvisioner {
|
|
/**
|
|
* 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;
|
|
/**
|
|
* 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 {
|
|
switch (offer.getTargetProduct().getDataOrigin()) {
|
|
case EBAY -> this.ebaySeller.sellProduct(
|
|
ProductMapper.mapProductToDTO(offer.getTargetProduct()));
|
|
case AMAZON -> this.amazonSeller.sellProduct(
|
|
ProductMapper.mapProductToDTO(offer.getTargetProduct()));
|
|
default -> throw new IllegalArgumentException("Unsupported source plattform");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Puts up the buy order.
|
|
*
|
|
* @param offer
|
|
*/
|
|
private void toBuyer(final Offer offer) throws IllegalArgumentException {
|
|
switch (offer.getSourceProduct().getDataOrigin()) {
|
|
case EBAY -> ebayPurchaser.purchaseProduct(
|
|
ProductMapper.mapProductToDTO(offer.getSourceProduct()));
|
|
case AMAZON -> amazonPurchaser.purchaseProduct(
|
|
ProductMapper.mapProductToDTO(offer.getSourceProduct()));
|
|
default -> throw new IllegalArgumentException("Unsupported target plattform");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Is the class for placing orders on a target platform.
|
|
*
|
|
* @param db Persistence Interface
|
|
* @param newSellerFactory Seller Factory
|
|
* @param newPurchaserFactory Purchaser Factory
|
|
*/
|
|
public OfferProvisioner(
|
|
final OfferPersistenceInterface db,
|
|
final SellerFactory newSellerFactory,
|
|
final PurchaserFactory newPurchaserFactory) {
|
|
this.offerWriter = new OfferWriter(db);
|
|
this.ebaySeller = newSellerFactory.createEbaySeller();
|
|
this.amazonSeller = newSellerFactory.createAmazonSeller();
|
|
this.ebayPurchaser = newPurchaserFactory.createEbayPurchaser();
|
|
this.amazonPurchaser = newPurchaserFactory.createAmazonPurchaser();
|
|
}
|
|
|
|
/**
|
|
* 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);
|
|
this.toBuyer(newOffer);
|
|
newOffer.setOfferId(newOfferId);
|
|
offerWriter.writeOfferToPersistence(newOffer);
|
|
}
|
|
}
|
|
}
|