diff --git a/src/main/java/de/rwu/easydrop/core/OfferReviewer.java b/src/main/java/de/rwu/easydrop/core/OfferReviewer.java index ba9e3d4..e394093 100644 --- a/src/main/java/de/rwu/easydrop/core/OfferReviewer.java +++ b/src/main/java/de/rwu/easydrop/core/OfferReviewer.java @@ -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(); } + + + } diff --git a/src/main/java/de/rwu/easydrop/core/OfferUpdater.java b/src/main/java/de/rwu/easydrop/core/OfferUpdater.java index 33091cb..27d08a9 100644 --- a/src/main/java/de/rwu/easydrop/core/OfferUpdater.java +++ b/src/main/java/de/rwu/easydrop/core/OfferUpdater.java @@ -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 changedOffers = offerReviewer.checkOffer(); } - - public void runUpdater(List 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 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 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(); + } } } +} +