added offerupdater

This commit is contained in:
Alexander Maier
2023-06-27 01:48:10 +02:00
parent d0b6178d26
commit 1e5ee72594
2 changed files with 100 additions and 9 deletions

View File

@@ -59,7 +59,6 @@ public class OfferReviewer {
} catch (SQLException e) {
e.printStackTrace();
} finally {
// Close the resources (resultSet, statement, connection) in a finally block
try {
if (resultSet != null) {
resultSet.close();
@@ -76,4 +75,7 @@ public class OfferReviewer {
}
return new ArrayList<Offer>();
}
}

View File

@@ -1,21 +1,110 @@
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 de.rwu.easydrop.model.Offer;
import de.rwu.easydrop.model.Product;
public class OfferUpdater {
/**
* a.
*/
public OfferUpdater() {
OfferReviewer offerReviewer = new OfferReviewer();
List<Offer> changedOffers = offerReviewer.checkOffer();
}
public void runUpdater(List<Offer> offersToUpdate) {
/*
* Bekommt vom OfferReviewer eine Liste mit zu ändernden Offers übergeben.
* 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.
*/
/**
* A.
* @param offersToUpdate
*/
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();
}
}
}
}