Registered plan import command

This commit is contained in:
Risto Lahtela 2020-06-21 20:05:47 +03:00
parent ea73dec439
commit 56341465d9
4 changed files with 51 additions and 108 deletions

View File

@ -21,6 +21,7 @@ import com.djrapitops.plan.commands.use.Arguments;
import com.djrapitops.plan.commands.use.CMDSender;
import com.djrapitops.plan.commands.use.CommandWithSubcommands;
import com.djrapitops.plan.commands.use.Subcommand;
import com.djrapitops.plan.gathering.importing.ImportSystem;
import com.djrapitops.plan.settings.locale.Locale;
import com.djrapitops.plan.utilities.logging.ErrorContext;
import com.djrapitops.plan.utilities.logging.ErrorLogger;
@ -40,6 +41,7 @@ public class PlanCommand {
private final Locale locale;
private final ColorScheme colors;
private final Confirmation confirmation;
private final ImportSystem importSystem;
private final LinkCommands linkCommands;
private final RegistrationCommands registrationCommands;
private final PluginStatusCommands statusCommands;
@ -53,6 +55,7 @@ public class PlanCommand {
Locale locale,
ColorScheme colors,
Confirmation confirmation,
ImportSystem importSystem,
LinkCommands linkCommands,
RegistrationCommands registrationCommands,
PluginStatusCommands statusCommands,
@ -64,6 +67,7 @@ public class PlanCommand {
this.locale = locale;
this.colors = colors;
this.confirmation = confirmation;
this.importSystem = importSystem;
this.linkCommands = linkCommands;
this.registrationCommands = registrationCommands;
this.statusCommands = statusCommands;
@ -104,6 +108,7 @@ public class PlanCommand {
.subcommand(databaseCommand())
.subcommand(exportCommand())
.subcommand(importCommand())
.exceptionHandler(this::handleException)
.build();
}
@ -344,6 +349,18 @@ public class PlanCommand {
.build();
}
private Subcommand importCommand() {
List<String> importerNames = importSystem.getImporterNames();
if (importerNames.isEmpty()) return null;
return Subcommand.builder()
.aliases("import")
.requirePermission("plan.data.import")
.optionalArgument("import kind", importerNames.toString())
.description("Import data.")
.inDepthDescription("Performs an import to load data into the database.")
.onCommand(dataUtilityCommands::onImport)
.build();
}
private Subcommand jsonCommand() {
return Subcommand.builder()

View File

@ -20,6 +20,8 @@ import com.djrapitops.plan.commands.use.Arguments;
import com.djrapitops.plan.commands.use.CMDSender;
import com.djrapitops.plan.delivery.export.Exporter;
import com.djrapitops.plan.exceptions.ExportException;
import com.djrapitops.plan.gathering.importing.ImportSystem;
import com.djrapitops.plan.gathering.importing.importers.Importer;
import com.djrapitops.plan.identification.ServerInfo;
import com.djrapitops.plan.processing.Processing;
import com.djrapitops.plan.settings.config.PlanConfig;
@ -34,6 +36,7 @@ import com.djrapitops.plan.storage.database.queries.objects.UserIdentifierQuerie
import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.function.Consumer;
@ -45,6 +48,7 @@ public class DataUtilityCommands {
private final DBSystem dbSystem;
private final ServerInfo serverInfo;
private final Exporter exporter;
private final ImportSystem importSystem;
private final Processing processing;
@Inject
@ -54,6 +58,7 @@ public class DataUtilityCommands {
DBSystem dbSystem,
ServerInfo serverInfo,
Exporter exporter,
ImportSystem importSystem,
Processing processing
) {
this.locale = locale;
@ -61,6 +66,7 @@ public class DataUtilityCommands {
this.dbSystem = dbSystem;
this.serverInfo = serverInfo;
this.exporter = exporter;
this.importSystem = importSystem;
this.processing = processing;
}
@ -148,4 +154,29 @@ public class DataUtilityCommands {
sender.send(" §c✕: §f" + failed);
}
}
public void onImport(CMDSender sender, Arguments arguments) {
String importKind = arguments.get(0)
.orElseThrow(() -> new IllegalArgumentException("Accepts following as import kind: " + importSystem.getImporterNames()));
ensureDatabaseIsOpen();
findAndProcessImporter(sender, importKind);
}
private void findAndProcessImporter(CMDSender sender, String importKind) {
Optional<Importer> foundImporter = importSystem.getImporter(importKind);
if (foundImporter.isPresent()) {
Importer importer = foundImporter.get();
processing.submitNonCritical(() -> {
sender.send(locale.getString(ManageLang.PROGRESS_START));
importer.processImport();
sender.send(locale.getString(ManageLang.PROGRESS_SUCCESS));
});
} else {
sender.send(locale.getString(ManageLang.FAIL_IMPORTER_NOT_FOUND, importKind));
}
}
}

View File

@ -1,107 +0,0 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.commands.subcommands.manage;
import com.djrapitops.plan.gathering.importing.ImportSystem;
import com.djrapitops.plan.gathering.importing.importers.Importer;
import com.djrapitops.plan.processing.Processing;
import com.djrapitops.plan.settings.Permissions;
import com.djrapitops.plan.settings.locale.Locale;
import com.djrapitops.plan.settings.locale.lang.CmdHelpLang;
import com.djrapitops.plan.settings.locale.lang.CommandLang;
import com.djrapitops.plan.settings.locale.lang.DeepHelpLang;
import com.djrapitops.plan.settings.locale.lang.ManageLang;
import com.djrapitops.plan.storage.database.DBSystem;
import com.djrapitops.plan.storage.database.Database;
import com.djrapitops.plugin.command.CommandNode;
import com.djrapitops.plugin.command.CommandType;
import com.djrapitops.plugin.command.Sender;
import com.djrapitops.plugin.utilities.Verify;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.Arrays;
import java.util.Optional;
/**
* This manage SubCommand is used to import data from 3rd party plugins.
*
* @author Rsl1122
*/
@Singleton
public class ManageImportCommand extends CommandNode {
private final Locale locale;
private final DBSystem dbSystem;
private final Processing processing;
private final ImportSystem importSystem;
@Inject
public ManageImportCommand(
Locale locale,
DBSystem dbSystem,
Processing processing,
ImportSystem importSystem
) {
super("import", Permissions.MANAGE.getPermission(), CommandType.CONSOLE);
this.locale = locale;
this.dbSystem = dbSystem;
this.processing = processing;
this.importSystem = importSystem;
setArguments("<plugin>/list", "[import args]");
setShortHelp(locale.getString(CmdHelpLang.MANAGE_IMPORT));
setInDepthHelp(locale.getArray(DeepHelpLang.MANAGE_IMPORT));
}
@Override
public void onCommand(Sender sender, String commandLabel, String[] args) {
Verify.isTrue(args.length >= 1,
() -> new IllegalArgumentException(locale.getString(CommandLang.FAIL_REQ_ARGS, "1+", Arrays.toString(this.getArguments()))));
String importArg = args[0];
if ("list".equals(importArg)) {
sender.sendMessage(locale.getString(ManageLang.IMPORTERS));
importSystem.getImporterNames().forEach(name -> sender.sendMessage("- " + name));
return;
}
Database.State dbState = dbSystem.getDatabase().getState();
if (dbState != Database.State.OPEN) {
sender.sendMessage(locale.getString(CommandLang.FAIL_DATABASE_NOT_OPEN, dbState.name()));
return;
}
findAndProcessImporter(sender, importArg);
}
private void findAndProcessImporter(Sender sender, String importArg) {
Optional<Importer> foundImporter = importSystem.getImporter(importArg);
if (foundImporter.isPresent()) {
Importer importer = foundImporter.get();
processing.submitNonCritical(() -> {
sender.sendMessage(locale.getString(ManageLang.PROGRESS_START));
importer.processImport();
sender.sendMessage(locale.getString(ManageLang.PROGRESS_SUCCESS));
});
} else {
sender.sendMessage(locale.getString(ManageLang.FAIL_IMPORTER_NOT_FOUND, importArg));
}
}
}

View File

@ -124,7 +124,9 @@ public class CommandWithSubcommands extends Subcommand {
}
public Builder subcommand(Subcommand subcommand) {
if (subcommand != null) {
command.subcommands.add(subcommand);
}
return this;
}