/**
* ShowCaseStandalone
* Copyright (C) 2012 Kellerkindt <copyright at kellerkindt.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.kellerkindt.scs.commands;
import java.util.logging.Level;
import org.bukkit.command.CommandSender;
import com.kellerkindt.scs.ShowCaseStandalone;
import com.kellerkindt.scs.exceptions.InsufficientPermissionException;
import com.kellerkindt.scs.exceptions.MissingOrIncorrectArgumentException;
import com.kellerkindt.scs.interfaces.ShopHandler;
import com.kellerkindt.scs.interfaces.StorageHandler;
import com.kellerkindt.scs.storage.FastFileShopStorage;
import com.kellerkindt.scs.storage.ShowCaseImport;
import com.kellerkindt.scs.utilities.Messaging;
import com.kellerkindt.scs.utilities.Properties;
import com.kellerkindt.scs.utilities.Term;
/**
* @author Sorklin <sorklin at gmail.com>
*/
public class ImportCmd extends GenericCmd {
public ImportCmd(CommandSender cs, String args[]){
super(cs, args);
this.mustBePlayer = false;
this.permission = Properties.permAdmin;
this.minArg = 2;
}
@Override
public boolean execute() throws MissingOrIncorrectArgumentException, InsufficientPermissionException {
if(errorCheck())
return true;
ShopHandler sHandler = scs.getShopHandler();
// import from showcase
if(args[1].equalsIgnoreCase("showcase")){
ShowCaseStandalone.slog(Level.INFO, "Import Showcase shops.");
ShowCaseImport si = new ShowCaseImport(scs);
if(!si.fileExists()){
Messaging.send(cs, "Could not attach to showcases.csv. Is it in your ShowCaseStandalone data folder?");
return true;
}
// load
load(sHandler, si);
}
// import from ffss
else if (args[1].equalsIgnoreCase("ffss")) {
try {
ShowCaseStandalone.slog(Level.INFO, "Import FastFileShopStorage.");
load(sHandler, new FastFileShopStorage(scs));
} catch (Throwable t) {
t.printStackTrace();
}
}
// import from sql - bukkit configuration
else if (args[1].equalsIgnoreCase("sql_bukkit")) {
try {
int count = sHandler.size();
StorageHandler storage = scs.getSQLShopStorageBukkit();
storage.load(sHandler);
Messaging.send(cs, "Imported: "+(sHandler.size() - count));
} catch (Exception e) {
Messaging.send(cs, Term.ERROR_IMPORT.get("sql_bukkit"));
}
}
// import from sql - my configuration
else if (args[1].equalsIgnoreCase("sql_scs")) {
try {
int count = sHandler.size();
StorageHandler storage = scs.getSQLShopStorageThis();
storage.load(sHandler);
Messaging.send(cs, "Imported: "+(sHandler.size() - count));
} catch (Exception e) {
Messaging.send(cs, Term.ERROR_IMPORT.get("sql_scs"));
}
}
else
throw new MissingOrIncorrectArgumentException();
return true;
}
/**
* Loads the shops from the ShopHandler
* @param shopHandler ShopHandler to put the new shops into
* @param storageHandler StorageHandler to load from
*/
private void load (ShopHandler shopHandler, StorageHandler storageHandler) {
try {
int before = shopHandler.size();
storageHandler.load(shopHandler);
String message = "Successfully loaded " + (shopHandler.size() - before) + " shop from " + storageHandler.getClass().getSimpleName();
scs.msgPlayer(player, message);
scs.log(Level.INFO, message + " by player " + player, true);
} catch (Throwable t) {
scs.msgPlayer(player, "Couldn't import shops");
t.printStackTrace();
}
}
}