Wrote OfferIdentifier and updated ProductRetriever

This commit is contained in:
Leonie Eitze
2023-06-09 17:23:28 +02:00
parent 9906ede6ec
commit c89c8974cf
5 changed files with 89 additions and 15 deletions

View File

@@ -3,3 +3,4 @@ AMAZON_API_URL=
AMAZON_API_KEY= AMAZON_API_KEY=
EBAY_API_URL= EBAY_API_URL=
EBAY_API_KEY= EBAY_API_KEY=
WEBSHOPS=amazon,ebay

View File

@@ -1,6 +1,13 @@
package de.rwu.easydrop.core; package de.rwu.easydrop.core;
import de.rwu.easydrop.model.Offer; import de.rwu.easydrop.model.Offer;
import de.rwu.easydrop.model.Product;
import de.rwu.easydrop.model.ProductCatalogue;
import de.rwu.easydrop.service.retriever.OfferRetriever;
import de.rwu.easydrop.service.retriever.ProductRetriever;
import de.rwu.easydrop.util.Config;
import javax.naming.ConfigurationException;
import java.util.List; import java.util.List;
import java.util.ArrayList; import java.util.ArrayList;
@@ -8,11 +15,14 @@ import java.util.ArrayList;
public class OfferIdentifier { public class OfferIdentifier {
String[] webshops;
OfferRetriever offerRetriever;
ProductRetriever productRetriever;
public OfferIdentifier() throws ConfigurationException {
public OfferIdentifier() { Config config = Config.getInstance();
this.webshops = config.getProperty("WEBSHOPS").split(",");
} }
public List<Offer> runIdentifier() { public List<Offer> runIdentifier() {
@@ -23,8 +33,43 @@ public class OfferIdentifier {
* wenn ja, muss er die Liste um diese Angebote kürzen * wenn ja, muss er die Liste um diese Angebote kürzen
* *
*/ */
return new ArrayList<Offer>();
// Just an example here taken from demo.products-config.json; should be generic and configurable either via
// JSON or the database later.
ProductCatalogue catalogue = new ProductCatalogue("Gigabyte GeForce RTX 3060", "Very epic GPU");
for(String webshop: this.webshops) {
// The product retriever should know what API with which ProductID to query; high-level components should not know about such
// things.
Product product = this.productRetriever.getProductFromDataSource(webshop, catalogue.getProductName());
if(product != null) {
catalogue.addProduct(product);
}
}
List<Offer> identifiedOffers = new ArrayList<>();
// Here we would call the price function that decides if is feasible to dropship the product and if, at which
// margin
// Offer possibleOffer = GenerateOfferFromCatalogue(catalogue);
// if(possibleOffer != null) {
// identifiedOffers.add(possibleOffer);
// }
List<Offer> newOffers = new ArrayList<>();
List<Offer> existingOffers = offerRetriever.loadOffers();
for(Offer identifiedOffer: identifiedOffers) {
boolean isNew = true;
for(Offer existingOffer: existingOffers) {
if(existingOffer.getSourceProduct().getProductId().equals(identifiedOffer.getSourceProduct().getProductId())) {
isNew = false;
break;
}
}
if(isNew) {
newOffers.add(identifiedOffer);
}
}
return newOffers;
} }

View File

@@ -23,11 +23,6 @@ public class Offer {
*/ */
Product saleProduct; Product saleProduct;
/*
* Boolean to check if sourceProduct is still available on source website
*/
boolean availability;
/* /*
* Date of the creation of the offer * Date of the creation of the offer
* NOTE: Use Timestamp? https://docs.oracle.com/javase/8/docs/api/java/sql/Timestamp.html * NOTE: Use Timestamp? https://docs.oracle.com/javase/8/docs/api/java/sql/Timestamp.html
@@ -44,9 +39,4 @@ public class Offer {
*/ */
Date checkDate; Date checkDate;
} }

View File

@@ -0,0 +1,14 @@
package de.rwu.easydrop.service.retriever;
import de.rwu.easydrop.model.Offer;
import java.util.ArrayList;
import java.util.List;
public class OfferRetriever {
public List<Offer> loadOffers() {
return new ArrayList<>();
}
}

View File

@@ -81,4 +81,28 @@ public class ProductRetriever {
return product; return product;
} }
/**
* Retrieves a product from an API by name of the API so that high-level components do not need to be extended
* with exact function call names if we extend the list of webshops.
*
* @param dataSourceName Data source name, e.g. amazon
* @param productName Product name, translated to the correct product ID for the data source
* @return Product from that data source or null if data source not available
*/
public Product getProductFromDataSource(final String dataSourceName, final String productName) {
switch(dataSourceName.toLowerCase()) {
case "amazon":
// TODO: Translation from productName to productId (Amazon) needed
return getProductFromAmazon(productName);
case "ebay":
// TODO: Translation from productName to productId (eBay) needed
return getProductFromEbay(productName);
case "persistence":
// TODO: Translation from productName to productId (persistence layer) needed
return getProductFromPersistence(productName);
default:
return null;
}
}
} }