Did Checkstyle improvements on OfferProv + Ident

This commit is contained in:
Leonie Eitze
2023-06-26 10:03:03 +02:00
parent 399951c43d
commit e3377c7b59
2 changed files with 90 additions and 66 deletions

View File

@@ -20,19 +20,31 @@ public class OfferIdentifier {
* Logger for main process. * Logger for main process.
*/ */
private static final Logger LOGGER = LoggerFactory.getLogger(OfferIdentifier.class); private static final Logger LOGGER = LoggerFactory.getLogger(OfferIdentifier.class);
/**
OfferRetriever offerRetriever; * OfferRetriever gets the offer from persistence.
*/
private OfferRetriever offerRetriever;
/**
* OfferIdentifier identifies offers that are
* feasible to place on a target platform based on
* their margin.
* @throws ConfigurationException
*/
public OfferIdentifier() throws ConfigurationException{ public OfferIdentifier() throws ConfigurationException{
this.offerRetriever = new OfferRetriever(); this.offerRetriever = new OfferRetriever();
} }
public List<Offer> runIdentifier(List<ProductCatalogue> pCats) { /**
* runIdentifier calls the price function that decides
* if it is feasible to dropship products and if so,
* at which margin.
* @param pCats
* @return newOffers
*/
public List<Offer> runIdentifier(final List<ProductCatalogue> pCats) {
List<Offer> identifiedOffers = new ArrayList<>(); List<Offer> identifiedOffers = new ArrayList<>();
// Here we call the price function that decides if is feasible to dropship the product and if, at which
// margin
for (ProductCatalogue pCat : pCats) { for (ProductCatalogue pCat : pCats) {
try{ try {
// Call price finder for all catalogue // Call price finder for all catalogue
ProductPair pair = OrderManager.getHighestMarginProducts(pCat); ProductPair pair = OrderManager.getHighestMarginProducts(pCat);
Offer possibleOffer = new Offer(); Offer possibleOffer = new Offer();
@@ -41,34 +53,37 @@ public class OfferIdentifier {
possibleOffer.setSaleProduct(pair.getProduct2()); possibleOffer.setSaleProduct(pair.getProduct2());
identifiedOffers.add(possibleOffer); identifiedOffers.add(possibleOffer);
LOGGER.info( LOGGER.info(
"Identified offer " + "Identified offer "
pair.getProduct1().getProductId() + +
" -> " + pair.getProduct1().getProductId()
+
" -> "
+
pair.getProduct2().getProductId() pair.getProduct2().getProductId()
); );
// Following fields will be set if offer confirmed // Following fields will be set if offer confirmed
// creationDate, offerId // creationDate, offerId
// Following fields will be set if offer needs update // Following fields will be set if offer needs update
// upDate // upDate
} }
catch(InvalidCatalogueException e) { catch (InvalidCatalogueException e) {
// if no margin, getHighestMarginProducts will throw // if no margin, getHighestMarginProducts will throw
System.out.print("product has no margin"); System.out.print("product has no margin");
} }
} }
List<Offer> newOffers = new ArrayList<>(); List<Offer> newOffers = new ArrayList<>();
List<Offer> existingOffers = offerRetriever.loadOffers(); List<Offer> existingOffers = offerRetriever.loadOffers();
for(Offer identifiedOffer: identifiedOffers) { for (Offer identifiedOffer: identifiedOffers) {
boolean isNew = true; boolean isNew = true;
for(Offer existingOffer: existingOffers) { for (Offer existingOffer: existingOffers) {
if(existingOffer.getSourceProduct().getProductId().equals(identifiedOffer.getSourceProduct().getProductId())) { if (
existingOffer.getSourceProduct().getProductId().
equals(identifiedOffer.getSourceProduct().getProductId())) {
isNew = false; isNew = false;
break; break;
} }
} }
if(isNew) { if (isNew) {
newOffers.add(identifiedOffer); newOffers.add(identifiedOffer);
} }
} }

View File

@@ -1,6 +1,5 @@
package de.rwu.easydrop.core; package de.rwu.easydrop.core;
import java.io.IOException;
import java.util.List; import java.util.List;
import de.rwu.easydrop.util.Config; import de.rwu.easydrop.util.Config;
@@ -14,61 +13,72 @@ import de.rwu.easydrop.api.client.EbaySeller;
import de.rwu.easydrop.api.dto.ProductDTO; import de.rwu.easydrop.api.dto.ProductDTO;
public class OfferProvisioner { public class OfferProvisioner {
/**
Config config; * Config.
ProductRetriever productRetriever; */
private Config config;
/**
* Gets the products from persistence.
*/
private ProductRetriever productRetriever;
// ProductWriter // ProductWriter
AbstractProductPersistence db; /**
OfferWriter offerWriter; * Is the product databank.
AmazonSeller amazonSeller; */
EbaySeller ebaySeller; private AbstractProductPersistence db;
/**
* Writes offers into persistence.
*/
private OfferWriter offerWriter;
private void toSeller(Offer offer) throws IllegalArgumentException { /**
// TODO dataOrigin should use the webshop enum * Is the API for selling products on Amazon.
if(offer.getSaleProduct().getDataOrigin().toLowerCase().equals("ebay")){ */
this.ebaySeller.sellProduct(new ProductDTO( private AmazonSeller amazonSeller;
offer.getSaleProduct().getProductId(), /**
"Amazon") * Is the API for selling products on Ebay.
); */
private EbaySeller ebaySeller;
} else if (offer.getSaleProduct().getDataOrigin().toLowerCase().equals("Amazon")) { private void toSeller(final Offer offer) throws IllegalArgumentException {
this.amazonSeller.sellProduct(new ProductDTO( // TODO dataOrigin should use the webshop enum
if (offer.getSaleProduct().getDataOrigin().toLowerCase().equals("ebay")) {
this.ebaySeller.sellProduct(new ProductDTO(
offer.getSaleProduct().getProductId(), offer.getSaleProduct().getProductId(),
"eBay") "Amazon"));
);
} } else if (offer.getSaleProduct().getDataOrigin().toLowerCase().equals("Amazon")) {
else { this.amazonSeller.sellProduct(new ProductDTO(
throw new IllegalArgumentException("Unsupported target plattform"); offer.getSaleProduct().getProductId(),
} "eBay"));
} else {
throw new IllegalArgumentException("Unsupported target plattform");
}
} }
/**
* Is the class for placing orders on a target platform.
*/
public OfferProvisioner(/* OfferWriter for database? */) {
public OfferProvisioner(/*OfferWriter for database? */) {
//in die Klasse hardcoden, "webshop" nicht verwenden
//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.offerWriter = new OfferWriter();
this.config = Config.getInstance(); this.config = Config.getInstance();
this.amazonSeller = new AmazonSeller( this.amazonSeller = new AmazonSeller(
config.getProperty("AMAZON_API_URL"), config.getProperty("AMAZON_API_URL"),
config.getProperty("AMAZON_API_KEY") config.getProperty("AMAZON_API_KEY"));
);
this.ebaySeller = new EbaySeller( this.ebaySeller = new EbaySeller(
config.getProperty("EBAY_API_URL"), config.getProperty("EBAY_API_URL"),
config.getProperty("EBAY_API_KEY") config.getProperty("EBAY_API_KEY"));
);
} }
public void runProvisioner(List<Offer> offersToProvision) { /**
/* * Method for placing orders on a target platform.
* Bekommt vom Identifier eine Liste mit Angeboten, die er einstellen soll auf Zielplattformen * @param offersToProvision
* Stellt Angebote bei der Zielplattform ein */
* Schreibt eingestellte Angebote in die Datenbank public final void runProvisioner(final List<Offer> offersToProvision) {
*/
for(Offer newOffer: offersToProvision){ for (Offer newOffer: offersToProvision){
try { try {
this.toSeller(newOffer); this.toSeller(newOffer);
@@ -77,8 +87,7 @@ public class OfferProvisioner {
// "duplicate" the product with dataOrigin new platform and merchant = "me" // "duplicate" the product with dataOrigin new platform and merchant = "me"
try { try {
offerWriter.writeOfferToPersistence(newOffer); offerWriter.writeOfferToPersistence(newOffer);
} } catch (Exception e) {
catch (Exception e){
System.out.println("Could not write to persistence"); System.out.println("Could not write to persistence");
} }
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {