use of de.Keyle.MyPet.api.repository.RepositoryInitException in project MyPet by xXKeyleXx.
the class Converter method convert.
public static boolean convert() {
if (Configuration.Repository.CONVERT_FROM.equalsIgnoreCase(Configuration.Repository.REPOSITORY_TYPE)) {
return false;
}
Repository fromRepo;
Repository toRepo;
if (Configuration.Repository.CONVERT_FROM.equalsIgnoreCase("NBT")) {
fromRepo = new NbtRepository();
} else if (Configuration.Repository.CONVERT_FROM.equalsIgnoreCase("MySQL")) {
fromRepo = new MySqlRepository();
} else if (Configuration.Repository.CONVERT_FROM.equalsIgnoreCase("MongoDB")) {
fromRepo = new MongoDbRepository();
} else if (Configuration.Repository.CONVERT_FROM.equalsIgnoreCase("SQLite")) {
fromRepo = new SqLiteRepository();
} else {
return false;
}
MyPetApi.getLogger().info("Converting from " + Configuration.Repository.CONVERT_FROM + " to " + Configuration.Repository.REPOSITORY_TYPE + "...");
try {
fromRepo.init();
} catch (RepositoryInitException e) {
return false;
}
toRepo = MyPetApi.getRepository();
List<MyPetPlayer> playerList = fromRepo.getAllMyPetPlayers();
if (toRepo instanceof NbtRepository) {
return false;
} else if (toRepo instanceof MySqlRepository) {
((MySqlRepository) toRepo).addMyPetPlayers(playerList);
} else if (toRepo instanceof SqLiteRepository) {
((SqLiteRepository) toRepo).addMyPetPlayers(playerList);
} else if (toRepo instanceof MongoDbRepository) {
HashSet<String> playerNames = new HashSet<>();
for (MyPetPlayer player : playerList) {
String playerName = player.getName();
if (playerNames.contains(playerName)) {
MyPetApi.getLogger().info("Found duplicate Player: " + player.toString());
continue;
}
playerNames.add(playerName);
((MongoDbRepository) toRepo).addMyPetPlayer(player);
}
}
List<StoredMyPet> pets = fromRepo.getAllMyPets();
if (toRepo instanceof MySqlRepository) {
((MySqlRepository) toRepo).addMyPets(pets);
} else if (toRepo instanceof SqLiteRepository) {
((SqLiteRepository) toRepo).addMyPets(pets);
} else if (toRepo instanceof MongoDbRepository) {
for (StoredMyPet pet : pets) {
((MongoDbRepository) toRepo).addMyPet(pet);
}
}
toRepo.save();
fromRepo.disable();
if (Configuration.Repository.CONVERT_FROM.equalsIgnoreCase("NBT")) {
File nbtFile = new File(MyPetApi.getPlugin().getDataFolder().getPath() + File.separator + "My.Pets");
File nbtFileOld = new File(MyPetApi.getPlugin().getDataFolder().getPath() + File.separator + "My.Pets.old");
nbtFile.renameTo(nbtFileOld);
MyPetApi.getPlugin().getConfig().set("MyPet.Repository.Type", Configuration.Repository.REPOSITORY_TYPE);
MyPetApi.getPlugin().getConfig().set("MyPet.Repository.NBT", null);
}
MyPetApi.getPlugin().getConfig().set("MyPet.Repository.ConvertFrom", "-");
MyPetApi.getPlugin().saveConfig();
MyPetApi.getLogger().info("Conversion from " + Configuration.Repository.CONVERT_FROM + " to " + Configuration.Repository.REPOSITORY_TYPE + " complete!");
return true;
}
use of de.Keyle.MyPet.api.repository.RepositoryInitException in project MyPet by xXKeyleXx.
the class SqLiteRepository method init.
@Override
public void init() throws RepositoryInitException {
try {
File dbFile = new File(MyPetApi.getPlugin().getDataFolder().getPath() + File.separator + "pets.db");
Class.forName("org.sqlite.JDBC");
connection = DriverManager.getConnection("jdbc:sqlite:" + dbFile.getAbsolutePath());
PreparedStatement statement = connection.prepareStatement("SELECT name FROM sqlite_master WHERE type='table' AND name='info';");
ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
resultSet.close();
statement = connection.prepareStatement("SELECT * FROM info;");
resultSet = statement.executeQuery();
updateStructure(resultSet);
updateInfo();
} else {
initStructure();
}
resultSet.close();
} catch (Exception e) {
e.printStackTrace();
throw new RepositoryInitException(e);
}
}
use of de.Keyle.MyPet.api.repository.RepositoryInitException in project MyPet by xXKeyleXx.
the class MySqlRepository method init.
@Override
public void init() throws RepositoryInitException {
this.dataSource = new HikariDataSource();
dataSource.setJdbcUrl("jdbc:mysql://" + Configuration.Repository.MySQL.HOST + ":" + Configuration.Repository.MySQL.PORT + "/" + Configuration.Repository.MySQL.DATABASE + (Configuration.Repository.MySQL.DATABASE.contains("?") ? "&" : "?") + "useUnicode=true&characterEncoding=" + Configuration.Repository.MySQL.CHARACTER_ENCODING);
dataSource.setUsername(Configuration.Repository.MySQL.USER);
dataSource.setPassword(Configuration.Repository.MySQL.PASSWORD);
dataSource.setMaximumPoolSize(Configuration.Repository.MySQL.POOL_SIZE);
dataSource.addDataSourceProperty("cachePrepStmts", true);
dataSource.setLeakDetectionThreshold(10000);
try (Connection connection = dataSource.getConnection();
PreparedStatement statement = connection.prepareStatement("SELECT * FROM " + Configuration.Repository.MySQL.PREFIX + "info;");
ResultSet resultSet = statement.executeQuery()) {
if (resultSet.next()) {
updateStructure(resultSet);
updateInfo();
} else {
initStructure();
}
} catch (SQLSyntaxErrorException e) {
initStructure();
} catch (Exception e) {
throw new RepositoryInitException(e);
}
}
use of de.Keyle.MyPet.api.repository.RepositoryInitException in project MyPet by xXKeyleXx.
the class MongoDbRepository method connect.
private void connect() throws RepositoryInitException {
try {
MongoClientOptions.Builder o = MongoClientOptions.builder().connectTimeout(3000);
if (Configuration.Repository.MongoDB.USER.equals("")) {
this.mongo = new MongoClient(new ServerAddress(Configuration.Repository.MongoDB.HOST, Configuration.Repository.MongoDB.PORT), o.build());
} else {
MongoCredential credentials = MongoCredential.createCredential(Configuration.Repository.MongoDB.USER, Configuration.Repository.MongoDB.DATABASE, Configuration.Repository.MongoDB.PASSWORD.toCharArray());
this.mongo = new MongoClient(new ServerAddress(Configuration.Repository.MongoDB.HOST, Configuration.Repository.MongoDB.PORT), Lists.newArrayList(credentials), o.build());
}
this.mongo.getAddress();
this.db = this.mongo.getDatabase(Configuration.Repository.MongoDB.DATABASE);
} catch (Exception e) {
throw new RepositoryInitException(e);
}
}
Aggregations