adds deletion of all games and creation of bd backup

This commit is contained in:
mikael.lantz 2020-12-16 10:59:24 +01:00
parent ed565cd9fe
commit 49f6fd2671
6 changed files with 115 additions and 27 deletions

View File

@ -23,7 +23,7 @@ import se.lantz.util.ExceptionHandler;
public class DbConnector
{
public static final String DB_NAME = "pcusb.db";
private static final String COMMA = "\",\"";
private static final Logger logger = LoggerFactory.getLogger(DbConnector.class);
private List<String> columnList = new ArrayList<>();
@ -58,7 +58,7 @@ public class DbConnector
try
{
// db parameters
String url = "jdbc:sqlite:pcusb.db";
String url = "jdbc:sqlite:" + DB_NAME;
// create a connection to the database
connection = DriverManager.getConnection(url);
@ -703,4 +703,19 @@ public class DbConnector
ExceptionHandler.handleException(e, "Could not delete game in db.");
}
}
public void deleteAllGames()
{
String sql = "DELETE FROM gameinfo;";
logger.debug("Generated DELETE String:\n{}", sql);
try (Connection conn = this.connect(); PreparedStatement pstmt = conn.prepareStatement(sql))
{
int value = pstmt.executeUpdate();
logger.debug("Executed successfully, value = {}", value);
}
catch (SQLException e)
{
ExceptionHandler.handleException(e, "Could not delete games in db.");
}
}
}

View File

@ -29,7 +29,7 @@ public class MainPanel extends JPanel
});
uiModel.addDuplicateGameListener(e -> showDuplicateDialog(e.getNewValue().toString()));
uiModel.addRequireFieldsListener(e -> showRequiredFieldsDialog((List<String>)e.getNewValue()));
uiModel.addRequireFieldsListener(e -> showRequiredFieldsDialog((List<String>) e.getNewValue()));
}
private JSplitPane getSplitPane()
@ -106,7 +106,7 @@ public class MainPanel extends JPanel
{
getListPanel().addNewGame();
}
public void deleteCurrentGame()
{
if (getListPanel().getSelectedIndexInList() > -1)
@ -121,7 +121,40 @@ public class MainPanel extends JPanel
}
}
}
public void deleteAllGames()
{
int value = showDeleteAllGamesDialog();
if (value == JOptionPane.YES_OPTION)
{
uiModel.deleteAllGames();
repaintAfterModifications();
SwingUtilities.invokeLater(() -> getListPanel().setSelectedIndexInList(0));
}
}
public void backupDb()
{
String backupFolderName = uiModel.backupDb();
String message = "";
if (backupFolderName != null)
{
message = "Backup saved in the folder " + backupFolderName;
JOptionPane.showMessageDialog(MainPanel.this,
message,
"Backup db",
JOptionPane.INFORMATION_MESSAGE);
}
else
{
message = "Could not create a backup.";
JOptionPane.showMessageDialog(MainPanel.this,
message,
"Backup db",
JOptionPane.ERROR_MESSAGE);
}
}
int showDeleteDialog()
{
String message = "Do you want to delete " + uiModel.getInfoModel().getTitle() + " from the database?";
@ -129,14 +162,20 @@ public class MainPanel extends JPanel
{
message = "Do you want to delete the new game entry?";
}
return JOptionPane.showConfirmDialog(MainPanel.this,
return JOptionPane
.showConfirmDialog(MainPanel.this, message, "Delete", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
}
int showDeleteAllGamesDialog()
{
String message = "Do you want to delete all games from the database? A backup will added to the backups folder before deleting.\nCover, screenshot and game files will not be deleted.";
return JOptionPane.showConfirmDialog(MainPanel.this,
message,
"Delete",
"Delete all games",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
}
public void repaintAfterModifications()
{
this.invalidate();

View File

@ -180,10 +180,8 @@ public class MenuManager
private JMenuItem getBackupDbItem()
{
backupDbItem = new JMenuItem("Backup database...");
backupDbItem.addActionListener(e -> {
});
backupDbItem = new JMenuItem("Backup database");
backupDbItem.addActionListener(e -> mainWindow.getMainPanel().backupDb());
return backupDbItem;
}
@ -198,10 +196,8 @@ public class MenuManager
private JMenuItem getCreateEmptyDbItem()
{
createEmptyDbItem = new JMenuItem("Create empty database...");
createEmptyDbItem.addActionListener(e -> {
//TODO
});
createEmptyDbItem = new JMenuItem("Delete all games");
createEmptyDbItem.addActionListener(e -> mainWindow.getMainPanel().deleteAllGames());
return createEmptyDbItem;
}

View File

@ -50,7 +50,7 @@ public class ExportManager
{
for (GameDetails gameDetails : gameDetailsList)
{
uiModel.exportGame(gameDetails, targetDir, this.favFormat, infoBuilder);
uiModel.exportGameInfoFile(gameDetails, targetDir, this.favFormat, infoBuilder);
}
}

View File

@ -135,9 +135,9 @@ public class MainViewModel extends AbstractModel
return returnList;
}
public void exportGame(GameDetails gameDetails, File targetDir, boolean favFormat, StringBuilder infoBuilder)
public void exportGameInfoFile(GameDetails gameDetails, File targetDir, boolean favFormat, StringBuilder infoBuilder)
{
fileManager.exportGame(gameDetails, targetDir, favFormat, infoBuilder);
fileManager.exportGameInfoFile(gameDetails, targetDir, favFormat, infoBuilder);
}
public InfoModel getInfoModel()
@ -320,6 +320,21 @@ public class MainViewModel extends AbstractModel
reloadCurrentGameView();
}
}
public void deleteAllGames()
{
if (backupDb() != null)
{
dbConnector.deleteAllGames();
//Reload the current view
reloadCurrentGameView();
}
}
public String backupDb()
{
return FileManager.backupDb();
}
private List<String> validateRequiredFields()
{

View File

@ -12,6 +12,8 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Properties;
import java.util.stream.Collectors;
@ -22,6 +24,7 @@ import javax.imageio.ImageIO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import se.lantz.db.DbConnector;
import se.lantz.model.InfoModel;
import se.lantz.model.data.GameDetails;
@ -30,6 +33,7 @@ public class FileManager
private static final String GAMES = "./games/";
private static final String SCREENS = "./screens/";
private static final String COVERS = "./covers/";
private static final String BACKUP = "./backup/";
private static final Logger logger = LoggerFactory.getLogger(FileManager.class);
@ -231,23 +235,22 @@ public class FileManager
return newNameString;
}
public void exportGame(GameDetails gameDetails, File targetDir, boolean favFormat, StringBuilder infoBuilder)
public void exportGameInfoFile(GameDetails gameDetails, File targetDir, boolean favFormat, StringBuilder infoBuilder)
{
try
{
String fileName = generateFileNameFromTitle(gameDetails.getTitle());
String filename = generateFileNameFromTitle(gameDetails.getTitle());
infoBuilder.append("Creating game info file for " + gameDetails.getTitle() + "\n");
String filename = fileName;
if (favFormat)
{
filename = fileName + ".tsg";
filename = filename + ".tsg";
}
else
{
//Add -ms to comply with the maxi game tool.
filename = fileName + "-ms.tsg";
filename = filename + "-ms.tsg";
}
writeGameInfoFile(filename, targetDir, gameDetails, favFormat);
}
@ -257,9 +260,6 @@ public class FileManager
logger.error(message, e);
infoBuilder.append(message);
}
//TODO copy the other files into the right location
}
public void writeGameInfoFile(String fileName, File targetDir, GameDetails gameDetails, boolean favFormat)
@ -361,4 +361,27 @@ public class FileManager
}
return fileProperties;
}
public static String backupDb()
{
//TODO: Copy screens, covers, games also to the backup folder
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss");
LocalDateTime now = LocalDateTime.now();
//String for current date and time
String dateAndTime = dtf.format(now);
File outputFolder = new File(BACKUP + "/" + dateAndTime + "/");
try
{
File dbFile = new File("./" + DbConnector.DB_NAME);
Files.createDirectories(outputFolder.toPath());
Path targetFile = outputFolder.toPath().resolve(DbConnector.DB_NAME);
Files.copy(dbFile.toPath(), targetFile);
}
catch (IOException e)
{
ExceptionHandler.handleException(e, "Could not create backup of Db");
return null;
}
return dateAndTime;
}
}