2020-10-27 11:14:34 -07:00
|
|
|
package me.totalfreedom.plex.storage;
|
|
|
|
|
|
|
|
import com.mongodb.MongoClient;
|
|
|
|
import com.mongodb.MongoClientURI;
|
2020-10-27 22:49:56 -05:00
|
|
|
import dev.morphia.Datastore;
|
|
|
|
import dev.morphia.Morphia;
|
2020-10-27 11:14:34 -07:00
|
|
|
import me.totalfreedom.plex.Plex;
|
|
|
|
import me.totalfreedom.plex.player.PlexPlayer;
|
|
|
|
|
|
|
|
public class MongoConnection
|
|
|
|
{
|
|
|
|
// USE MORPHIA API FOR MONGO <3
|
|
|
|
|
2020-10-27 22:49:56 -05:00
|
|
|
private final Plex plugin = Plex.get();
|
2020-10-27 11:14:34 -07:00
|
|
|
|
|
|
|
public Datastore getDatastore()
|
|
|
|
{
|
2020-10-27 21:11:23 -07:00
|
|
|
if (!plugin.config.getString("data.central.storage").equalsIgnoreCase("mongodb"))
|
2020-10-27 11:14:34 -07:00
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2020-10-27 21:11:23 -07:00
|
|
|
String host = plugin.config.getString("data.central.hostname");
|
|
|
|
int port = plugin.config.getInt("data.central.port");
|
|
|
|
String username = plugin.config.getString("data.central.user");
|
|
|
|
String password = plugin.config.getString("data.central.password");
|
|
|
|
String database = plugin.config.getString("data.central.db");
|
2020-10-27 11:14:34 -07:00
|
|
|
|
2020-10-27 22:49:56 -05:00
|
|
|
String connectionString = "mongodb://" + username + ":" + password + "@" + host + ":" + port + "/?authSource=" + database;
|
2020-10-27 11:14:34 -07:00
|
|
|
MongoClient client = new MongoClient(new MongoClientURI(connectionString));
|
|
|
|
Morphia morphia = new Morphia();
|
|
|
|
Datastore datastore = morphia.createDatastore(client, database);
|
2020-10-27 22:49:56 -05:00
|
|
|
datastore.getMapper().addMappedClass(PlexPlayer.class);
|
2020-10-27 11:14:34 -07:00
|
|
|
datastore.ensureIndexes();
|
|
|
|
plugin.setStorageType(StorageType.MONGO);
|
|
|
|
return datastore;
|
|
|
|
}
|
|
|
|
}
|