Skip to content

Commit

Permalink
- WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
derreisende77 committed Aug 11, 2024
1 parent e989777 commit 856d93a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ class SeenHistoryController : AutoCloseable {

@Throws(SQLException::class)
fun removeDuplicates() {
val auto_commit = connection!!.autoCommit
val prevAutoCommitState = connection!!.autoCommit
connection!!.autoCommit = false
try {
connection!!.createStatement().use { statement ->
Expand All @@ -291,7 +291,7 @@ class SeenHistoryController : AutoCloseable {
connection!!.rollback()
throw e
}
connection!!.autoCommit = auto_commit
connection!!.autoCommit = prevAutoCommitState
}

@Throws(SQLException::class)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package mediathek.gui.actions.optimize_history_db;

import mediathek.config.Konstanten;
import mediathek.controller.history.SeenHistoryController;
import mediathek.mainwindow.MediathekGui;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.sql.SQLException;

public class OptimizeHistoryDbAction extends AbstractAction {
private final MediathekGui mediathekGui;
private static final Logger logger = LogManager.getLogger();

public OptimizeHistoryDbAction(MediathekGui mediathekGui) {
this.mediathekGui = mediathekGui;
putValue(NAME, "History-Datenbank optimieren...");
}

@Override
public void actionPerformed(ActionEvent e) {
try (SeenHistoryController controller = new SeenHistoryController()) {
var dupes = controller.checkDuplicates();
var numDuplicates = dupes.total() - dupes.distinct();
logger.trace("{} duplicates found in history", numDuplicates);
if (numDuplicates == 0) {
//no duplicates
var msg = "Es sind keine Duplikate vorhanden.\nEine Optimierung ist nicht notwendig.";
JOptionPane.showMessageDialog(mediathekGui, msg, Konstanten.PROGRAMMNAME, JOptionPane.INFORMATION_MESSAGE);
}
else {
var msg = String.format("Es wurden %d Duplikate gefunden.\nMöchten Sie diese bereinigen?", numDuplicates);
var answer = JOptionPane.showOptionDialog(mediathekGui, msg, Konstanten.PROGRAMMNAME,
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);
if (answer == JOptionPane.YES_OPTION) {
controller.removeDuplicates();
dupes = controller.checkDuplicates();
numDuplicates = dupes.total() - dupes.distinct();
logger.trace("{} duplicates found in history after cleanup", numDuplicates);
msg = String.format("Datenbank wurde bereinigt.\n%d Duplikate blieben übrig.", numDuplicates);
JOptionPane.showMessageDialog(mediathekGui, msg, Konstanten.PROGRAMMNAME, JOptionPane.INFORMATION_MESSAGE);
}
}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(mediathekGui, ex.getMessage(), Konstanten.PROGRAMMNAME, JOptionPane.ERROR_MESSAGE);
}
}
}
34 changes: 2 additions & 32 deletions src/main/java/mediathek/mainwindow/MediathekGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import mediathek.gui.actions.import_actions.ImportOldAbosAction;
import mediathek.gui.actions.import_actions.ImportOldBlacklistAction;
import mediathek.gui.actions.import_actions.ImportOldReplacementListAction;
import mediathek.gui.actions.optimize_history_db.OptimizeHistoryDbAction;
import mediathek.gui.dialog.DialogBeenden;
import mediathek.gui.dialog.LoadFilmListDialog;
import mediathek.gui.dialogEinstellungen.DialogEinstellungen;
Expand Down Expand Up @@ -54,12 +55,10 @@

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.NoSuchElementException;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -994,38 +993,9 @@ private void handleFilmlistWriteStopEvent(FilmListWriteStopEvent e) {
SwingUtilities.invokeLater(() -> loadFilmListAction.setEnabled(true));
}

public class OptimizeHistoryDbAction extends AbstractAction {
public OptimizeHistoryDbAction()
{
putValue(NAME, "History-Datenbank optimieren...");
}

@Override
public void actionPerformed(ActionEvent e) {
try (SeenHistoryController controller = new SeenHistoryController()) {
var dupes = controller.checkDuplicates();
var numDuplicates = dupes.total() - dupes.distinct();
logger.trace("{} duplicates found in history", numDuplicates);
var msg = String.format("Es wurden %d Duplikate gefunden.\nMöchten Sie diese bereinigen?", numDuplicates);
var answer = JOptionPane.showOptionDialog(MediathekGui.this, msg, Konstanten.PROGRAMMNAME,
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);
if (answer == JOptionPane.YES_OPTION) {
controller.removeDuplicates();
dupes = controller.checkDuplicates();
numDuplicates = dupes.total() - dupes.distinct();
logger.trace("{} duplicates found in history after cleanup", numDuplicates);
msg = String.format("Datenbank wurde bereinigt.\n%d Duplikate blieben übrig.", numDuplicates);
JOptionPane.showMessageDialog(MediathekGui.this, msg, Konstanten.PROGRAMMNAME, JOptionPane.INFORMATION_MESSAGE);
}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(MediathekGui.this, ex.getMessage(), Konstanten.PROGRAMMNAME, JOptionPane.ERROR_MESSAGE);
}
}
}

private void createHelperToolsEntries() {
var menu = new JMenu("Hilfsmittel");
menu.add(new OptimizeHistoryDbAction());
menu.add(new OptimizeHistoryDbAction(this));
jMenuHilfe.add(menu);
}

Expand Down

0 comments on commit 856d93a

Please sign in to comment.