Merge remote-tracking branch 'origin/#70-OfferUpdater' into issue#12-#64-Core
This commit is contained in:
@@ -1,23 +1,81 @@
|
|||||||
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 java.util.List;
|
import java.util.List;
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.DriverManager;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.sql.Statement;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
public class OfferReviewer {
|
public class OfferReviewer {
|
||||||
|
|
||||||
public OfferReviewer(/*OfferReader/retriever for database? */){
|
/**
|
||||||
|
* Check all Offers and compare them with the API.
|
||||||
}
|
* @return list of all items that need to be changed
|
||||||
|
|
||||||
public List<Offer> runReviewer() {
|
|
||||||
/*
|
|
||||||
* Liest eingestellte Angebote in der Datenbank
|
|
||||||
* Prüft Zielplattformen der SourceProducts, ob diese noch verfügbar sind (Issue#12) bzw. ob sie sich im Preis geändert haben
|
|
||||||
* Gibt Liste zurück von Offers, die geändert werden müssen, wird übergeben an OfferUpdater mit availability true oder false
|
|
||||||
*/
|
*/
|
||||||
return new ArrayList<Offer>();
|
public List<Offer> checkOffer(/*OfferReader/retriever for database? */) {
|
||||||
|
|
||||||
|
Connection connection = null;
|
||||||
|
Statement statement = null;
|
||||||
|
ResultSet resultSet = null;
|
||||||
|
List<Offer> changedOffers = new ArrayList<>();
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Establish the database connection
|
||||||
|
connection = DriverManager.getConnection("jdbc:sqlite:persistence.db");
|
||||||
|
|
||||||
|
// Create a SQL statement
|
||||||
|
statement = connection.createStatement();
|
||||||
|
|
||||||
|
// Execute the query to retrieve the entries
|
||||||
|
String query = "SELECT sourceProduct, saleProduct, creationDate, upDate, "
|
||||||
|
+ "checkDate, offerId FROM table";
|
||||||
|
resultSet = statement.executeQuery(query);
|
||||||
|
|
||||||
|
// Process the retrieved entries
|
||||||
|
while (resultSet.next()) {
|
||||||
|
Product sourceProduct = (Product) resultSet.getObject("sourceProduct");
|
||||||
|
Product saleProduct = (Product) resultSet.getObject("saleProduct");
|
||||||
|
java.sql.Date creationDate = resultSet.getDate("creationDate");
|
||||||
|
Date updateDate = resultSet.getDate("upDate");
|
||||||
|
Date checkDate = resultSet.getDate("checkDate");
|
||||||
|
String offerId = resultSet.getString("offerId");
|
||||||
|
|
||||||
|
// Call the API to get the current price
|
||||||
|
double apiPrice = getPriceFromAPI(sourceProduct);
|
||||||
|
|
||||||
|
// Compare the prices
|
||||||
|
if (saleProduct.getCurrentPrice() != apiPrice) {
|
||||||
|
// Price has changed, create a Product object and add it to the changedProducts list
|
||||||
|
Offer offer = new Offer();
|
||||||
|
changedOffers.add(offer);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
if (resultSet != null) {
|
||||||
|
resultSet.close();
|
||||||
|
}
|
||||||
|
if (statement != null) {
|
||||||
|
statement.close();
|
||||||
|
}
|
||||||
|
if (connection != null) {
|
||||||
|
connection.close();
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new ArrayList<Offer>();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,21 +1,110 @@
|
|||||||
package de.rwu.easydrop.core;
|
package de.rwu.easydrop.core;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.DriverManager;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.SQLException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import de.rwu.easydrop.model.Offer;
|
import de.rwu.easydrop.model.Offer;
|
||||||
|
import de.rwu.easydrop.model.Product;
|
||||||
|
|
||||||
|
|
||||||
public class OfferUpdater {
|
public class OfferUpdater {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* a.
|
||||||
|
*/
|
||||||
public OfferUpdater() {
|
public OfferUpdater() {
|
||||||
|
|
||||||
|
OfferReviewer offerReviewer = new OfferReviewer();
|
||||||
|
List<Offer> changedOffers = offerReviewer.checkOffer();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void runUpdater(List<Offer> offersToUpdate) {
|
/**
|
||||||
/*
|
* A.
|
||||||
* Bekommt vom OfferReviewer eine Liste mit zu ändernden Offers übergeben.
|
* @param offersToUpdate
|
||||||
* Bei availability=false löscht der Updater das Angebot aus der Datenbank und von der Zielplattform.
|
|
||||||
* Der Updater ändert die geänderten Parameter in der Liste in der Datenbank und in der Zielplattform ab.
|
|
||||||
* Er ändert das upDate in der Datenbank nach der Änderung.
|
|
||||||
*/
|
*/
|
||||||
|
public void runUpdater(final List<Offer> offersToUpdate) {
|
||||||
|
Connection connection = null;
|
||||||
|
PreparedStatement deleteStatement = null;
|
||||||
|
PreparedStatement insertStatement = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Establish the database connection
|
||||||
|
connection = DriverManager.getConnection("jdbc:sqlite:persistence.db");
|
||||||
|
|
||||||
|
// Disable auto-commit to perform a transaction
|
||||||
|
connection.setAutoCommit(false);
|
||||||
|
|
||||||
|
// Prepare the DELETE statement to remove the existing entries
|
||||||
|
String deleteQuery = "DELETE FROM your_table WHERE product_id = ?";
|
||||||
|
deleteStatement = connection.prepareStatement(deleteQuery);
|
||||||
|
|
||||||
|
// Prepare the INSERT statement to add the new entries
|
||||||
|
String insertQuery = "INSERT INTO your_table (product_id, product_name, price)"
|
||||||
|
+ "VALUES (?, ?, ?)";
|
||||||
|
insertStatement = connection.prepareStatement(insertQuery);
|
||||||
|
|
||||||
|
// Retrieve the existing entries from the database
|
||||||
|
List<Product> existingProducts = retrieveExistingProducts(connection);
|
||||||
|
|
||||||
|
// Delete the existing entries that are not present in the changedProducts list
|
||||||
|
for (Product existingProduct : existingProducts) {
|
||||||
|
if (!changedOffers.(existingProduct)) {
|
||||||
|
deleteStatement.setString(1, existingProduct.getProductId());
|
||||||
|
deleteStatement.executeUpdate();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Insert the new entries or update the existing entries
|
||||||
|
for (Product changedOffers : offersToUpdate) {
|
||||||
|
if (existingProducts.contains(changedOffers)) {
|
||||||
|
// Update the existing entry with the new data
|
||||||
|
// You need to modify the update query and statement based on your requirements
|
||||||
|
// Here's an example of updating the price of an existing entry
|
||||||
|
String updateQuery = "UPDATE table SET currentPrice = ? WHERE offerId = ?";
|
||||||
|
PreparedStatement updateStatement = connection.prepareStatement(updateQuery);
|
||||||
|
updateStatement.setDouble(1, changedOffers.getCurrentPrice());
|
||||||
|
updateStatement.setString(2, changedOffers.getProductId());
|
||||||
|
updateStatement.executeUpdate();
|
||||||
|
updateStatement.close();
|
||||||
|
} else {
|
||||||
|
// Insert the new entry
|
||||||
|
insertStatement.setString(1, changedOffers.getProductId());
|
||||||
|
insertStatement.setString(2, changedOffers.getMerchant());
|
||||||
|
insertStatement.setDouble(3, changedOffers.getCurrentPrice());
|
||||||
|
insertStatement.executeUpdate();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Commit the transaction
|
||||||
|
connection.commit();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
// Rollback the transaction in case of an exception
|
||||||
|
if (connection != null) {
|
||||||
|
try {
|
||||||
|
connection.rollback();
|
||||||
|
} catch (SQLException rollbackException) {
|
||||||
|
rollbackException.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
// Close the resources (deleteStatement, insertStatement, connection) in a finally block
|
||||||
|
try {
|
||||||
|
if (deleteStatement != null) {
|
||||||
|
deleteStatement.close();
|
||||||
|
}
|
||||||
|
if (insertStatement != null) {
|
||||||
|
insertStatement.close();
|
||||||
|
}
|
||||||
|
if (connection != null) {
|
||||||
|
connection.close();
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user