Webshops refactoring + checkstyle
This commit is contained in:
@@ -9,6 +9,7 @@ import java.net.URL;
|
||||
|
||||
import de.rwu.easydrop.api.dto.ProductDTO;
|
||||
import de.rwu.easydrop.exception.DataSourceException;
|
||||
import de.rwu.easydrop.model.Webshop;
|
||||
import de.rwu.easydrop.util.FormattingUtil;
|
||||
|
||||
/**
|
||||
@@ -23,7 +24,7 @@ public abstract class AbstractDataSource implements DataSource {
|
||||
*
|
||||
* @return Data source name
|
||||
*/
|
||||
protected abstract String getDataOrigin();
|
||||
protected abstract Webshop getDataOrigin();
|
||||
|
||||
/**
|
||||
* Returns the data source's API key.
|
||||
@@ -48,7 +49,7 @@ public abstract class AbstractDataSource implements DataSource {
|
||||
public ProductDTO getProductDTOById(final String productIdentifier)
|
||||
throws IllegalArgumentException {
|
||||
StringBuilder response = new StringBuilder();
|
||||
String dataOrigin = getDataOrigin();
|
||||
Webshop dataOrigin = getDataOrigin();
|
||||
String apiKey = getApiKey();
|
||||
ProductDTO product = new ProductDTO(productIdentifier, dataOrigin);
|
||||
|
||||
@@ -72,20 +73,16 @@ public abstract class AbstractDataSource implements DataSource {
|
||||
+ responseCode);
|
||||
}
|
||||
|
||||
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
response.append(line);
|
||||
}
|
||||
reader.close();
|
||||
|
||||
// FIXME: Mock not complete. Data is missing
|
||||
// FIXME response can be empty
|
||||
String data;
|
||||
if (response.toString().isEmpty()){
|
||||
if (response.toString().isEmpty()) {
|
||||
data = "{}";
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
data = response.toString();
|
||||
}
|
||||
buildProductDTO(product, data);
|
||||
|
||||
@@ -7,6 +7,7 @@ import java.net.URL;
|
||||
|
||||
import de.rwu.easydrop.api.dto.ProductDTO;
|
||||
import de.rwu.easydrop.exception.DataWriterException;
|
||||
import de.rwu.easydrop.model.Webshop;
|
||||
import de.rwu.easydrop.util.FormattingUtil;
|
||||
|
||||
/**
|
||||
@@ -28,7 +29,7 @@ public abstract class AbstractDataWriter {
|
||||
*
|
||||
* @return Data target API name
|
||||
*/
|
||||
protected abstract String getDataTarget();
|
||||
protected abstract Webshop getDataTarget();
|
||||
|
||||
/**
|
||||
* Creates an URL object to connect to the API with.
|
||||
@@ -47,7 +48,7 @@ public abstract class AbstractDataWriter {
|
||||
*/
|
||||
protected void sendPutRequest(final ProductDTO dto, final String apiType) {
|
||||
String apiKey = getApiKey();
|
||||
String dataTarget = getDataTarget();
|
||||
Webshop dataTarget = getDataTarget();
|
||||
|
||||
if (!dataTarget.equals(dto.getDataOrigin())) {
|
||||
throw new DataWriterException(
|
||||
|
||||
@@ -8,6 +8,7 @@ import com.jayway.jsonpath.PathNotFoundException;
|
||||
import com.jayway.jsonpath.ReadContext;
|
||||
|
||||
import de.rwu.easydrop.api.dto.ProductDTO;
|
||||
import de.rwu.easydrop.model.Webshop;
|
||||
|
||||
/**
|
||||
* Interface to an Amazon data source.
|
||||
@@ -18,7 +19,7 @@ public final class AmazonProductDataSource extends AbstractDataSource {
|
||||
/**
|
||||
* Name of this data source.
|
||||
*/
|
||||
private static final String DATA_ORIGIN = "Amazon";
|
||||
private static final Webshop DATA_ORIGIN = Webshop.Amazon;
|
||||
/**
|
||||
* Base URL to the Amazon data source.
|
||||
*/
|
||||
@@ -82,7 +83,7 @@ public final class AmazonProductDataSource extends AbstractDataSource {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getDataOrigin() {
|
||||
protected Webshop getDataOrigin() {
|
||||
return DATA_ORIGIN;
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,8 @@ package de.rwu.easydrop.api.client;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
|
||||
import de.rwu.easydrop.model.Webshop;
|
||||
|
||||
/**
|
||||
* Sends a buy request to the Amazon API.
|
||||
*
|
||||
@@ -12,7 +14,7 @@ public final class AmazonPurchaser extends AbstractPurchaser {
|
||||
/**
|
||||
* Name of this data source.
|
||||
*/
|
||||
private static final String DATA_TARGET = "Amazon";
|
||||
private static final Webshop DATA_TARGET = Webshop.Amazon;
|
||||
/**
|
||||
* Base URL to the Amazon Purchase API.
|
||||
*/
|
||||
@@ -61,7 +63,7 @@ public final class AmazonPurchaser extends AbstractPurchaser {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getDataTarget() {
|
||||
protected Webshop getDataTarget() {
|
||||
return DATA_TARGET;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@ package de.rwu.easydrop.api.client;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
|
||||
import de.rwu.easydrop.model.Webshop;
|
||||
|
||||
/**
|
||||
* Sends a sell request to the Amazon API.
|
||||
*
|
||||
@@ -12,7 +14,7 @@ public final class AmazonSeller extends AbstractSeller {
|
||||
/**
|
||||
* Name of this data source.
|
||||
*/
|
||||
private static final String DATA_TARGET = "Amazon";
|
||||
private static final Webshop DATA_TARGET = Webshop.Amazon;
|
||||
/**
|
||||
* Base URL to the Amazon Purchase API.
|
||||
*/
|
||||
@@ -61,7 +63,7 @@ public final class AmazonSeller extends AbstractSeller {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getDataTarget() {
|
||||
protected Webshop getDataTarget() {
|
||||
return DATA_TARGET;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import com.jayway.jsonpath.PathNotFoundException;
|
||||
import com.jayway.jsonpath.ReadContext;
|
||||
|
||||
import de.rwu.easydrop.api.dto.ProductDTO;
|
||||
import de.rwu.easydrop.model.Webshop;
|
||||
|
||||
/**
|
||||
* Interface to an eBay data source.
|
||||
@@ -18,7 +19,7 @@ public final class EbayItemDataSource extends AbstractDataSource {
|
||||
/**
|
||||
* Name of this data source.
|
||||
*/
|
||||
private static final String DATA_ORIGIN = "eBay";
|
||||
private static final Webshop DATA_ORIGIN = Webshop.eBay;
|
||||
/**
|
||||
* Base URL to the eBay data source.
|
||||
*/
|
||||
@@ -77,7 +78,7 @@ public final class EbayItemDataSource extends AbstractDataSource {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getDataOrigin() {
|
||||
protected Webshop getDataOrigin() {
|
||||
return DATA_ORIGIN;
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,8 @@ package de.rwu.easydrop.api.client;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
|
||||
import de.rwu.easydrop.model.Webshop;
|
||||
|
||||
/**
|
||||
* Sends a buy request to the eBay API.
|
||||
*
|
||||
@@ -12,7 +14,7 @@ public final class EbayPurchaser extends AbstractPurchaser {
|
||||
/**
|
||||
* Name of this data source.
|
||||
*/
|
||||
private static final String DATA_TARGET = "eBay";
|
||||
private static final Webshop DATA_TARGET = Webshop.eBay;
|
||||
/**
|
||||
* Base URL to the eBay Purchase API.
|
||||
*/
|
||||
@@ -46,7 +48,7 @@ public final class EbayPurchaser extends AbstractPurchaser {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getDataTarget() {
|
||||
protected Webshop getDataTarget() {
|
||||
return DATA_TARGET;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@ package de.rwu.easydrop.api.client;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
|
||||
import de.rwu.easydrop.model.Webshop;
|
||||
|
||||
/**
|
||||
* Sends a sell request to the eBay API.
|
||||
*
|
||||
@@ -12,7 +14,7 @@ public final class EbaySeller extends AbstractSeller {
|
||||
/**
|
||||
* Name of this data source.
|
||||
*/
|
||||
private static final String DATA_TARGET = "eBay";
|
||||
private static final Webshop DATA_TARGET = Webshop.eBay;
|
||||
/**
|
||||
* Base URL to the eBay Purchase API.
|
||||
*/
|
||||
@@ -46,7 +48,7 @@ public final class EbaySeller extends AbstractSeller {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getDataTarget() {
|
||||
protected Webshop getDataTarget() {
|
||||
return DATA_TARGET;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,31 +14,32 @@ public class OfferDTO {
|
||||
*/
|
||||
private String offerId;
|
||||
|
||||
/*
|
||||
* The product that our software buys
|
||||
/**
|
||||
* The product that our software buys.
|
||||
*/
|
||||
Product sourceProduct;
|
||||
private Product sourceProduct;
|
||||
|
||||
/*
|
||||
* The product that our software sells
|
||||
/**
|
||||
* The product that our software sells.
|
||||
*/
|
||||
Product saleProduct;
|
||||
private Product saleProduct;
|
||||
|
||||
/*
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
Date creationDate;
|
||||
private Date creationDate;
|
||||
|
||||
/*
|
||||
* Date of last update of the offer on the destination website (API)
|
||||
/**
|
||||
* Date of last update of the offer on the destination website (API).
|
||||
*/
|
||||
Date upDate;
|
||||
private Date upDate;
|
||||
|
||||
/*
|
||||
* Date of last check if offer is still valid
|
||||
/**
|
||||
* Date of last check if offer is still valid.
|
||||
*/
|
||||
Date checkDate;
|
||||
private Date checkDate;
|
||||
|
||||
/**
|
||||
* Creates OfferDTO instance.
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package de.rwu.easydrop.api.dto;
|
||||
|
||||
import de.rwu.easydrop.model.Webshop;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
@@ -10,9 +11,9 @@ import lombok.Data;
|
||||
@Data
|
||||
public class ProductDTO {
|
||||
/**
|
||||
* Data source platform, like "Amazon".
|
||||
* Data source platform, like Amazon.
|
||||
*/
|
||||
private String dataOrigin;
|
||||
private Webshop dataOrigin;
|
||||
|
||||
/**
|
||||
* Platform internal product identifier.
|
||||
@@ -45,7 +46,7 @@ public class ProductDTO {
|
||||
* @param newProductId Internal Product indetifier
|
||||
* @param newDataOrigin Data Origin
|
||||
*/
|
||||
public ProductDTO(final String newProductId, final String newDataOrigin) {
|
||||
public ProductDTO(final String newProductId, final Webshop newDataOrigin) {
|
||||
this.productId = newProductId;
|
||||
this.dataOrigin = newDataOrigin;
|
||||
}
|
||||
|
||||
@@ -7,32 +7,39 @@ import javax.naming.ConfigurationException;
|
||||
import de.rwu.easydrop.model.Offer;
|
||||
import de.rwu.easydrop.model.ProductCatalogue;
|
||||
|
||||
/**
|
||||
* The application core.
|
||||
*
|
||||
* @since 0.3.0
|
||||
*/
|
||||
public class Core {
|
||||
|
||||
/**
|
||||
* Offer identifier.
|
||||
*/
|
||||
private OfferIdentifier ident;
|
||||
/**
|
||||
* Offer provisioner.
|
||||
*/
|
||||
private OfferProvisioner provis;
|
||||
private OfferReviewer review;
|
||||
private OfferUpdater update;
|
||||
|
||||
|
||||
public Core() throws ConfigurationException{
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @throws ConfigurationException
|
||||
*/
|
||||
public Core() throws ConfigurationException {
|
||||
this.ident = new OfferIdentifier();
|
||||
this.provis = new OfferProvisioner();
|
||||
this.review = new OfferReviewer();
|
||||
this.update = new OfferUpdater();
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void runCore(List<ProductCatalogue> pCats){
|
||||
/**
|
||||
* Runs the core.
|
||||
*
|
||||
* @param pCats
|
||||
*/
|
||||
public void runCore(final List<ProductCatalogue> pCats) {
|
||||
List<Offer> identifiedOffers = ident.runIdentifier(pCats);
|
||||
provis.runProvisioner(identifiedOffers);
|
||||
List<Offer> updatingOffers = review.runReviewer();
|
||||
update.runUpdater(updatingOffers);
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -14,7 +14,6 @@ import de.rwu.easydrop.service.retriever.OfferRetriever;
|
||||
import de.rwu.easydrop.service.processing.OrderManager;
|
||||
import de.rwu.easydrop.exception.InvalidCatalogueException;
|
||||
|
||||
|
||||
public class OfferIdentifier {
|
||||
/**
|
||||
* Logger for main process.
|
||||
@@ -24,13 +23,15 @@ public class OfferIdentifier {
|
||||
* 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();
|
||||
}
|
||||
|
||||
@@ -38,6 +39,7 @@ public class OfferIdentifier {
|
||||
* runIdentifier calls the price function that decides
|
||||
* if it is feasible to dropship products and if so,
|
||||
* at which margin.
|
||||
*
|
||||
* @param pCats
|
||||
* @return newOffers
|
||||
*/
|
||||
@@ -59,30 +61,19 @@ public class OfferIdentifier {
|
||||
+
|
||||
" -> "
|
||||
+
|
||||
pair.getProduct2().getProductId()
|
||||
);
|
||||
pair.getProduct2().getProductId());
|
||||
// Following fields will be set if offer confirmed
|
||||
// creationDate, offerId
|
||||
// Following fields will be set if offer needs update
|
||||
// upDate
|
||||
}
|
||||
catch (InvalidCatalogueException e) {
|
||||
} catch (InvalidCatalogueException e) {
|
||||
// if no margin, getHighestMarginProducts will throw
|
||||
System.out.print("product has no margin");
|
||||
}
|
||||
}
|
||||
List<Offer> newOffers = new ArrayList<>();
|
||||
List<Offer> existingOffers = offerRetriever.loadOffers();
|
||||
for (Offer identifiedOffer: identifiedOffers) {
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -2,30 +2,20 @@ package de.rwu.easydrop.core;
|
||||
|
||||
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;
|
||||
import de.rwu.easydrop.exception.DataWriterException;
|
||||
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;
|
||||
|
||||
public class OfferProvisioner {
|
||||
/**
|
||||
* Config.
|
||||
*/
|
||||
private Config config;
|
||||
/**
|
||||
* Gets the products from persistence.
|
||||
*/
|
||||
private ProductRetriever productRetriever;
|
||||
// ProductWriter
|
||||
/**
|
||||
* Is the product databank.
|
||||
*/
|
||||
private AbstractProductPersistence db;
|
||||
/**
|
||||
* Writes offers into persistence.
|
||||
*/
|
||||
@@ -41,16 +31,11 @@ public class OfferProvisioner {
|
||||
private EbaySeller ebaySeller;
|
||||
|
||||
private void toSeller(final 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"));
|
||||
if (offer.getSaleProduct().getDataOrigin() == Webshop.eBay) {
|
||||
this.ebaySeller.sellProduct(ProductMapper.mapProductToDTO(offer.getSaleProduct()));
|
||||
|
||||
} else if (offer.getSaleProduct().getDataOrigin().toLowerCase().equals("amazon")) {
|
||||
this.amazonSeller.sellProduct(new ProductDTO(
|
||||
offer.getSaleProduct().getProductId(),
|
||||
"eBay"));
|
||||
} else if (offer.getSaleProduct().getDataOrigin().equals(Webshop.Amazon)) {
|
||||
this.amazonSeller.sellProduct(ProductMapper.mapProductToDTO(offer.getSaleProduct()));
|
||||
} else {
|
||||
throw new IllegalArgumentException("Unsupported target plattform");
|
||||
}
|
||||
@@ -74,11 +59,12 @@ public class OfferProvisioner {
|
||||
|
||||
/**
|
||||
* Method for placing orders on a target platform.
|
||||
*
|
||||
* @param offersToProvision
|
||||
*/
|
||||
public final void runProvisioner(final List<Offer> offersToProvision) {
|
||||
|
||||
for (Offer newOffer: offersToProvision){
|
||||
for (Offer newOffer : offersToProvision) {
|
||||
|
||||
try {
|
||||
this.toSeller(newOffer);
|
||||
@@ -92,10 +78,9 @@ public class OfferProvisioner {
|
||||
}
|
||||
} catch (IllegalArgumentException e) {
|
||||
System.out.println(
|
||||
"Offer could not be placed, " +
|
||||
newOffer.getSaleProduct().getDataOrigin() +
|
||||
" is not supported"
|
||||
);
|
||||
"Offer could not be placed, "
|
||||
+ newOffer.getSaleProduct().getDataOrigin()
|
||||
+ " is not supported");
|
||||
} catch (DataWriterException e) {
|
||||
System.out.println("could not transmit offer");
|
||||
}
|
||||
|
||||
6
src/main/java/de/rwu/easydrop/core/package-info.java
Normal file
6
src/main/java/de/rwu/easydrop/core/package-info.java
Normal file
@@ -0,0 +1,6 @@
|
||||
/**
|
||||
* EasyDrop's core.
|
||||
*
|
||||
* @since 0.3.0
|
||||
*/
|
||||
package de.rwu.easydrop.core;
|
||||
@@ -3,11 +3,6 @@ package de.rwu.easydrop.data.connector;
|
||||
import de.rwu.easydrop.api.dto.OfferDTO;
|
||||
|
||||
public abstract class AbstractOfferPersistence {
|
||||
/**
|
||||
* Data origin.
|
||||
*/
|
||||
public static final String DATA_ORIGIN = "Persistence";
|
||||
|
||||
/**
|
||||
* Writes a ProductDTO to persistence.
|
||||
*
|
||||
@@ -17,9 +12,11 @@ public abstract class AbstractOfferPersistence {
|
||||
|
||||
/**
|
||||
* Gets a OfferDTO from persistence.
|
||||
*
|
||||
* @param offerId
|
||||
* @return Offer data transfer object
|
||||
*/
|
||||
//@Override
|
||||
public abstract OfferDTO getOfferDTOById(String OfferId);
|
||||
public abstract OfferDTO getOfferDTOById(String offerId);
|
||||
|
||||
/**
|
||||
* Deletes all data from persistence.
|
||||
|
||||
@@ -11,6 +11,7 @@ import org.sqlite.SQLiteDataSource;
|
||||
|
||||
import de.rwu.easydrop.api.dto.ProductDTO;
|
||||
import de.rwu.easydrop.exception.PersistenceException;
|
||||
import de.rwu.easydrop.model.Webshop;
|
||||
|
||||
/**
|
||||
* Allows connecting to a SQLite Database.
|
||||
@@ -18,11 +19,6 @@ import de.rwu.easydrop.exception.PersistenceException;
|
||||
* @since 0.2.0
|
||||
*/
|
||||
public final class SQLiteConnector extends AbstractProductPersistence {
|
||||
/**
|
||||
* Data origin.
|
||||
*/
|
||||
private static final String DATA_ORIGIN = "SQLite";
|
||||
|
||||
/**
|
||||
* SQLite Database.
|
||||
*/
|
||||
@@ -95,7 +91,7 @@ public final class SQLiteConnector extends AbstractProductPersistence {
|
||||
PreparedStatement statement = connection.prepareStatement(query)) {
|
||||
int index = 0;
|
||||
|
||||
statement.setString(++index, dto.getDataOrigin());
|
||||
statement.setString(++index, dto.getDataOrigin().toString());
|
||||
statement.setString(++index, dto.getProductId());
|
||||
statement.setDouble(++index, dto.getCurrentPrice());
|
||||
statement.setString(++index, dto.getMerchant());
|
||||
@@ -120,8 +116,9 @@ public final class SQLiteConnector extends AbstractProductPersistence {
|
||||
|
||||
try (ResultSet resultSet = statement.executeQuery()) {
|
||||
if (resultSet.next()) {
|
||||
dto = new ProductDTO(resultSet.getString("productId"),
|
||||
resultSet.getString("dataOrigin"));
|
||||
Webshop newShop = Webshop.fromString(resultSet.getString("dataOrigin"));
|
||||
|
||||
dto = new ProductDTO(resultSet.getString("productId"), newShop);
|
||||
dto.setCurrentPrice(resultSet.getDouble("currentPrice"));
|
||||
dto.setMerchant(resultSet.getString("merchant"));
|
||||
dto.setDeliveryPrice(resultSet.getDouble("deliveryPrice"));
|
||||
@@ -149,8 +146,9 @@ public final class SQLiteConnector extends AbstractProductPersistence {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getDataOrigin() {
|
||||
return DATA_ORIGIN;
|
||||
protected Webshop getDataOrigin() {
|
||||
throw new UnsupportedOperationException(
|
||||
this.getClass().getName() + " doesn't support getDataOrigin");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -10,23 +10,10 @@ import lombok.Data;
|
||||
*/
|
||||
@Data
|
||||
public class Product {
|
||||
/**
|
||||
* Constants for data source/destination platforms.
|
||||
*/
|
||||
public enum Webshop {
|
||||
/**
|
||||
* Amazon.
|
||||
*/
|
||||
AMAZON,
|
||||
/**Ebay.
|
||||
*/
|
||||
EBAY
|
||||
}
|
||||
|
||||
/**
|
||||
* Data source platform, like "Amazon".
|
||||
*/
|
||||
private String dataOrigin;
|
||||
private Webshop dataOrigin;
|
||||
|
||||
/**
|
||||
* Platform internal product identifier.
|
||||
|
||||
@@ -5,11 +5,27 @@ package de.rwu.easydrop.model;
|
||||
*/
|
||||
public enum Webshop {
|
||||
/**
|
||||
* Amazon.
|
||||
* Amazon Product API.
|
||||
*/
|
||||
Amazon,
|
||||
/**
|
||||
* eBay.
|
||||
* eBay Item API.
|
||||
*/
|
||||
eBay
|
||||
eBay;
|
||||
|
||||
/**
|
||||
* Attempts to derive a webshop value from a string.
|
||||
*
|
||||
* @param str String
|
||||
* @return webshop
|
||||
* @throws IllegalArgumentException
|
||||
*/
|
||||
public static Webshop fromString(final String str) {
|
||||
for (Webshop shop : Webshop.values()) {
|
||||
if (shop.toString().equalsIgnoreCase(str)) {
|
||||
return shop;
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException(String.format("No webshop called {} found", str));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,6 @@ import de.rwu.easydrop.model.Offer;
|
||||
*
|
||||
* @see Offer
|
||||
* @see OfferDTO
|
||||
* @see OfferDTO
|
||||
*/
|
||||
public final class OfferMapper {
|
||||
|
||||
@@ -39,7 +38,7 @@ public final class OfferMapper {
|
||||
/**
|
||||
* Creates an OfferDTO object from a corresponding offer.
|
||||
*
|
||||
* @param Offer offer
|
||||
* @param offer
|
||||
* @return OfferDTO
|
||||
*/
|
||||
public static OfferDTO mapOfferToDTO(final Offer offer) {
|
||||
|
||||
@@ -5,7 +5,6 @@ import java.util.List;
|
||||
|
||||
import javax.naming.ConfigurationException;
|
||||
|
||||
import de.rwu.easydrop.exception.InvalidProductException;
|
||||
import de.rwu.easydrop.model.Product;
|
||||
import de.rwu.easydrop.model.ProductCatalogue;
|
||||
import de.rwu.easydrop.util.ProductsConfig;
|
||||
@@ -58,16 +57,9 @@ public class CatalogueRetriever {
|
||||
|
||||
for (Product product : pCat.getProducts()) {
|
||||
Product newProduct = new Product();
|
||||
newProduct.setDataOrigin(product.getDataOrigin());
|
||||
newProduct.setProductId(product.getProductId());
|
||||
|
||||
if (newProduct.getDataOrigin().equals("Amazon")) {
|
||||
newProduct = productRetriever.getProductFromAmazon(product.getProductId());
|
||||
} else if (newProduct.getDataOrigin().equals("eBay")) {
|
||||
newProduct = productRetriever.getProductFromEbay(product.getProductId());
|
||||
} else {
|
||||
throw new InvalidProductException("Product data origin is invalid");
|
||||
}
|
||||
newProduct = productRetriever.getProductFromWebshop(product.getDataOrigin(),
|
||||
product.getProductId());
|
||||
|
||||
newProductCatalogue.addProduct(newProduct);
|
||||
}
|
||||
|
||||
@@ -1,14 +1,5 @@
|
||||
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<>();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import de.rwu.easydrop.api.client.EbayItemDataSource;
|
||||
import de.rwu.easydrop.api.dto.ProductDTO;
|
||||
import de.rwu.easydrop.data.connector.AbstractProductPersistence;
|
||||
import de.rwu.easydrop.model.Product;
|
||||
import de.rwu.easydrop.model.Webshop;
|
||||
import de.rwu.easydrop.service.mapping.ProductMapper;
|
||||
import de.rwu.easydrop.service.validation.ProductValidator;
|
||||
|
||||
@@ -21,9 +22,9 @@ public class ProductRetriever {
|
||||
private DataSourceFactory dataSourceFactory;
|
||||
|
||||
/**
|
||||
* @param newDataSourceFactory the dataSourceFactory to set
|
||||
* @param newDataSourceFactory the WebshopFactory to set
|
||||
*/
|
||||
public void setDataSourceFactory(final DataSourceFactory newDataSourceFactory) {
|
||||
public void setWebshopFactory(final DataSourceFactory newDataSourceFactory) {
|
||||
this.dataSourceFactory = newDataSourceFactory;
|
||||
}
|
||||
|
||||
@@ -31,7 +32,7 @@ public class ProductRetriever {
|
||||
* @param newDataSourceFactory
|
||||
*/
|
||||
public ProductRetriever(final DataSourceFactory newDataSourceFactory) {
|
||||
this.setDataSourceFactory(newDataSourceFactory);
|
||||
this.setWebshopFactory(newDataSourceFactory);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -83,21 +84,21 @@ public class ProductRetriever {
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves a product from an API by name of the API so that high-level components do not need to be extended
|
||||
* 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
|
||||
* @param shop Data source name, e.g. Amazon
|
||||
* @param productIdentifier 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(Product.webshop dataSourceName, final String productName) {
|
||||
switch(dataSourceName) {
|
||||
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);
|
||||
public Product getProductFromWebshop(final Webshop shop, final String productIdentifier) {
|
||||
switch (shop) {
|
||||
case Amazon:
|
||||
return getProductFromAmazon(productIdentifier);
|
||||
case eBay:
|
||||
return getProductFromEbay(productIdentifier);
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
package de.rwu.easydrop.service.validation;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import de.rwu.easydrop.exception.InvalidProductException;
|
||||
import de.rwu.easydrop.model.Product;
|
||||
|
||||
@@ -31,26 +28,8 @@ public final class ProductValidator {
|
||||
if (product.getCurrentPrice() == 0.00) {
|
||||
throw new InvalidProductException("Current price cannot be 0.00");
|
||||
}
|
||||
if (!isInValidDataOrigins(product.getDataOrigin())) {
|
||||
throw new InvalidProductException("Unknown data source");
|
||||
}
|
||||
if (product.getProductId().equals("")) {
|
||||
throw new InvalidProductException("Product ID cannot be empty");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether a dataOrigin is within the set of valid ones.
|
||||
*
|
||||
* @param dataOrigin like "Amazon"
|
||||
* @return true if valid
|
||||
*/
|
||||
public static boolean isInValidDataOrigins(final String dataOrigin) {
|
||||
Set<String> validOrigins = new HashSet<>();
|
||||
|
||||
validOrigins.add("Amazon");
|
||||
validOrigins.add("eBay");
|
||||
|
||||
return validOrigins.contains(dataOrigin);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ import com.jayway.jsonpath.JsonPathException;
|
||||
|
||||
import de.rwu.easydrop.model.Product;
|
||||
import de.rwu.easydrop.model.ProductCatalogue;
|
||||
import de.rwu.easydrop.model.Webshop;
|
||||
|
||||
/**
|
||||
* Reads the user-specified catalogue of products.
|
||||
@@ -122,8 +123,8 @@ public final class ProductsConfig {
|
||||
ArrayList<HashMap<String, Object>> identifiers = JsonPath.read(jsonIdentifiers, "$");
|
||||
|
||||
for (HashMap<String, Object> product : identifiers) {
|
||||
String dataOrigin = product.keySet().iterator().next();
|
||||
String identifier = product.get(dataOrigin).toString();
|
||||
Webshop dataOrigin = Webshop.fromString(product.keySet().iterator().next());
|
||||
String identifier = product.get(dataOrigin.toString()).toString();
|
||||
|
||||
Product newProduct = new Product();
|
||||
newProduct.setDataOrigin(dataOrigin);
|
||||
|
||||
Reference in New Issue
Block a user