Search in sources :

Example 1 with RepositoryInitException

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;
}
Also used : MySqlRepository(de.Keyle.MyPet.repository.types.MySqlRepository) MongoDbRepository(de.Keyle.MyPet.repository.types.MongoDbRepository) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) NbtRepository(de.Keyle.MyPet.repository.types.NbtRepository) Repository(de.Keyle.MyPet.api.repository.Repository) SqLiteRepository(de.Keyle.MyPet.repository.types.SqLiteRepository) MongoDbRepository(de.Keyle.MyPet.repository.types.MongoDbRepository) NbtRepository(de.Keyle.MyPet.repository.types.NbtRepository) MySqlRepository(de.Keyle.MyPet.repository.types.MySqlRepository) RepositoryInitException(de.Keyle.MyPet.api.repository.RepositoryInitException) SqLiteRepository(de.Keyle.MyPet.repository.types.SqLiteRepository) File(java.io.File) StoredMyPet(de.Keyle.MyPet.api.entity.StoredMyPet) HashSet(java.util.HashSet)

Example 2 with RepositoryInitException

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);
    }
}
Also used : RepositoryInitException(de.Keyle.MyPet.api.repository.RepositoryInitException) File(java.io.File) JsonParseException(com.google.gson.JsonParseException) IOException(java.io.IOException) RepositoryInitException(de.Keyle.MyPet.api.repository.RepositoryInitException)

Example 3 with RepositoryInitException

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);
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) RepositoryInitException(de.Keyle.MyPet.api.repository.RepositoryInitException) JsonParseException(com.google.gson.JsonParseException) RepositoryInitException(de.Keyle.MyPet.api.repository.RepositoryInitException) ZipException(java.util.zip.ZipException) IOException(java.io.IOException)

Example 4 with RepositoryInitException

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);
    }
}
Also used : RepositoryInitException(de.Keyle.MyPet.api.repository.RepositoryInitException) RepositoryInitException(de.Keyle.MyPet.api.repository.RepositoryInitException) ZipException(java.util.zip.ZipException) IOException(java.io.IOException)

Aggregations

RepositoryInitException (de.Keyle.MyPet.api.repository.RepositoryInitException)4 IOException (java.io.IOException)3 JsonParseException (com.google.gson.JsonParseException)2 File (java.io.File)2 ZipException (java.util.zip.ZipException)2 HikariDataSource (com.zaxxer.hikari.HikariDataSource)1 StoredMyPet (de.Keyle.MyPet.api.entity.StoredMyPet)1 MyPetPlayer (de.Keyle.MyPet.api.player.MyPetPlayer)1 Repository (de.Keyle.MyPet.api.repository.Repository)1 MongoDbRepository (de.Keyle.MyPet.repository.types.MongoDbRepository)1 MySqlRepository (de.Keyle.MyPet.repository.types.MySqlRepository)1 NbtRepository (de.Keyle.MyPet.repository.types.NbtRepository)1 SqLiteRepository (de.Keyle.MyPet.repository.types.SqLiteRepository)1 HashSet (java.util.HashSet)1