mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2025-01-26 18:11:29 +01:00
Removed redundant ManageUtils class
This commit is contained in:
parent
1efa9f7f9d
commit
efe4251359
@ -1,12 +1,14 @@
|
||||
package com.djrapitops.plan.command.commands.manage;
|
||||
|
||||
import com.djrapitops.plan.api.exceptions.database.DBException;
|
||||
import com.djrapitops.plan.api.exceptions.database.DBInitException;
|
||||
import com.djrapitops.plan.system.database.DBSystem;
|
||||
import com.djrapitops.plan.system.database.databases.Database;
|
||||
import com.djrapitops.plan.system.database.databases.sql.SQLiteDB;
|
||||
import com.djrapitops.plan.system.settings.Permissions;
|
||||
import com.djrapitops.plan.system.settings.locale.Locale;
|
||||
import com.djrapitops.plan.system.settings.locale.Msg;
|
||||
import com.djrapitops.plan.utilities.ManageUtils;
|
||||
import com.djrapitops.plan.utilities.FormatUtils;
|
||||
import com.djrapitops.plugin.api.utility.log.Log;
|
||||
import com.djrapitops.plugin.command.CommandNode;
|
||||
import com.djrapitops.plugin.command.CommandType;
|
||||
@ -15,10 +17,13 @@ import com.djrapitops.plugin.task.AbsRunnable;
|
||||
import com.djrapitops.plugin.task.RunnableFactory;
|
||||
import com.djrapitops.plugin.utilities.Verify;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* This manage subcommand is used to backup a database to a .db file.
|
||||
* This command is used to backup a database to a .db file.
|
||||
*
|
||||
* @author Rsl1122
|
||||
* @since 2.3.0
|
||||
@ -61,7 +66,7 @@ public class ManageBackupCommand extends CommandNode {
|
||||
try {
|
||||
Log.debug("Backup", "Start");
|
||||
sender.sendMessage(Locale.get(Msg.MANAGE_INFO_START).parse());
|
||||
ManageUtils.backup(args[0], database);
|
||||
createNewBackup(args[0], database);
|
||||
sender.sendMessage(Locale.get(Msg.MANAGE_INFO_COPY_SUCCESS).toString());
|
||||
} catch (Exception e) {
|
||||
Log.toLog(ManageBackupCommand.class, e);
|
||||
@ -73,4 +78,31 @@ public class ManageBackupCommand extends CommandNode {
|
||||
}
|
||||
}).runTaskAsynchronously();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new backup sqlite file with the data of given database.
|
||||
*
|
||||
* @param dbName Name of database (mysql/sqlite)
|
||||
* @param copyFromDB Database you want to backup.
|
||||
*/
|
||||
private void createNewBackup(String dbName, Database copyFromDB) throws SQLException {
|
||||
SQLiteDB backupDB = null;
|
||||
try {
|
||||
String timeStamp = FormatUtils.formatTimeStampISO8601NoClock(System.currentTimeMillis());
|
||||
String fileName = dbName + "-backup-" + timeStamp;
|
||||
backupDB = new SQLiteDB(fileName);
|
||||
Collection<UUID> uuids = copyFromDB.fetch().getSavedUUIDs();
|
||||
if (uuids.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
backupDB.init();
|
||||
copyFromDB.backup().backup(backupDB);
|
||||
} catch (DBException e) {
|
||||
Log.toLog(this.getClass(), e);
|
||||
} finally {
|
||||
if (backupDB != null) {
|
||||
backupDB.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -5,7 +5,6 @@ import com.djrapitops.plan.system.database.databases.Database;
|
||||
import com.djrapitops.plan.system.settings.Permissions;
|
||||
import com.djrapitops.plan.system.settings.locale.Locale;
|
||||
import com.djrapitops.plan.system.settings.locale.Msg;
|
||||
import com.djrapitops.plan.utilities.ManageUtils;
|
||||
import com.djrapitops.plugin.api.utility.log.Log;
|
||||
import com.djrapitops.plugin.command.CommandNode;
|
||||
import com.djrapitops.plugin.command.CommandType;
|
||||
@ -72,7 +71,7 @@ public class ManageMoveCommand extends CommandNode {
|
||||
try {
|
||||
sender.sendMessage(Locale.get(Msg.MANAGE_INFO_START).parse());
|
||||
|
||||
ManageUtils.clearAndCopy(toDatabase, fromDatabase);
|
||||
fromDatabase.backup().backup(toDatabase);
|
||||
|
||||
sender.sendMessage(Locale.get(Msg.MANAGE_INFO_MOVE_SUCCESS).toString());
|
||||
|
||||
|
@ -7,7 +7,6 @@ import com.djrapitops.plan.system.database.databases.sql.SQLiteDB;
|
||||
import com.djrapitops.plan.system.settings.Permissions;
|
||||
import com.djrapitops.plan.system.settings.locale.Locale;
|
||||
import com.djrapitops.plan.system.settings.locale.Msg;
|
||||
import com.djrapitops.plan.utilities.ManageUtils;
|
||||
import com.djrapitops.plugin.api.utility.log.Log;
|
||||
import com.djrapitops.plugin.command.CommandNode;
|
||||
import com.djrapitops.plugin.command.CommandType;
|
||||
@ -85,7 +84,7 @@ public class ManageRestoreCommand extends CommandNode {
|
||||
|
||||
sender.sendMessage(Locale.get(Msg.MANAGE_INFO_START).parse());
|
||||
|
||||
ManageUtils.clearAndCopy(database, backupDB);
|
||||
database.backup().restore(backupDB);
|
||||
|
||||
sender.sendMessage(Locale.get(Msg.MANAGE_INFO_COPY_SUCCESS).toString());
|
||||
} catch (Exception e) {
|
||||
|
@ -1,6 +1,5 @@
|
||||
package com.djrapitops.plan.system.database.databases.sql.operation;
|
||||
|
||||
import com.djrapitops.plan.api.exceptions.database.DBException;
|
||||
import com.djrapitops.plan.system.database.databases.Database;
|
||||
import com.djrapitops.plan.system.database.databases.operation.BackupOperations;
|
||||
import com.djrapitops.plan.system.database.databases.sql.SQLDB;
|
||||
@ -24,7 +23,7 @@ public class SQLBackupOps extends SQLOps implements BackupOperations {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void restore(Database fromDatabase) throws DBException, SQLException {
|
||||
public void restore(Database fromDatabase) throws SQLException {
|
||||
fromDatabase.backup().backup(db);
|
||||
}
|
||||
}
|
||||
|
@ -1,62 +0,0 @@
|
||||
package com.djrapitops.plan.utilities;
|
||||
|
||||
import com.djrapitops.plan.api.exceptions.database.DBException;
|
||||
import com.djrapitops.plan.system.database.databases.Database;
|
||||
import com.djrapitops.plan.system.database.databases.sql.SQLiteDB;
|
||||
import com.djrapitops.plugin.api.utility.log.Log;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* @author Rsl1122
|
||||
*/
|
||||
public class ManageUtils {
|
||||
|
||||
/**
|
||||
* Constructor used to hide the public constructor
|
||||
*/
|
||||
private ManageUtils() {
|
||||
throw new IllegalStateException("Utility class");
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new backup sqlite file with the data of copyFromDB.
|
||||
*
|
||||
* @param dbName Name of database (mysql/sqlite)
|
||||
* @param copyFromDB Database you want to backup.
|
||||
*/
|
||||
public static void backup(String dbName, Database copyFromDB) throws SQLException {
|
||||
SQLiteDB backupDB = null;
|
||||
try {
|
||||
String timeStamp = new Date().toString().substring(4, 10).replace(" ", "-");
|
||||
String fileName = dbName + "-backup-" + timeStamp;
|
||||
backupDB = new SQLiteDB(fileName);
|
||||
Collection<UUID> uuids = copyFromDB.fetch().getSavedUUIDs();
|
||||
if (uuids.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
backupDB.init();
|
||||
clearAndCopy(backupDB, copyFromDB);
|
||||
} catch (DBException e) {
|
||||
Log.toLog(ManageUtils.class, e);
|
||||
} finally {
|
||||
if (backupDB != null) {
|
||||
backupDB.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears a database and copies data from other database to that database.
|
||||
*
|
||||
* @param clearAndCopyToDB Database that will be cleared data will be copied
|
||||
* to.
|
||||
* @param copyFromDB Database where data will be copied from
|
||||
*/
|
||||
public static void clearAndCopy(Database clearAndCopyToDB, Database copyFromDB) throws SQLException, DBException {
|
||||
copyFromDB.backup().backup(clearAndCopyToDB);
|
||||
}
|
||||
}
|
@ -23,7 +23,8 @@ import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.mockito.Mockito.doCallRealMethod;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
|
||||
/**
|
||||
* Mocking Utility for Bukkit version of Plan.
|
||||
@ -59,7 +60,7 @@ public class BukkitMockUtil extends MockUtil {
|
||||
}
|
||||
|
||||
public BukkitMockUtil withDataFolder(File tempFolder) {
|
||||
when(planMock.getDataFolder()).thenReturn(tempFolder);
|
||||
doReturn(tempFolder).when(planMock).getDataFolder();
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -75,7 +76,7 @@ public class BukkitMockUtil extends MockUtil {
|
||||
try {
|
||||
File pluginYml = getFile("/plugin.yml");
|
||||
PluginDescriptionFile description = new PluginDescriptionFile(new FileInputStream(pluginYml));
|
||||
when(planMock.getDescription()).thenReturn(description);
|
||||
doReturn(description).when(planMock).getDescription();
|
||||
} catch (FileNotFoundException | InvalidDescriptionException e) {
|
||||
System.out.println("Error while setting plugin description");
|
||||
}
|
||||
@ -98,7 +99,6 @@ public class BukkitMockUtil extends MockUtil {
|
||||
doReturn(TestConstants.BUKKIT_MAX_PLAYERS).when(serverMock).getMaxPlayers();
|
||||
FakeConsoleCmdSender sender = new FakeConsoleCmdSender();
|
||||
doReturn(sender).when(serverMock).getConsoleSender();
|
||||
when(serverMock.getConsoleSender()).thenReturn(sender);
|
||||
|
||||
BukkitScheduler bukkitScheduler = Mockito.mock(BukkitScheduler.class);
|
||||
doReturn(bukkitScheduler).when(serverMock).getScheduler();
|
||||
|
Loading…
Reference in New Issue
Block a user