package com.ibtp.kontor;

import com.mongodb.MongoClient;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.Morphia;

import java.util.HashMap;

/**
 * Created by thomas on 01.03.16.
 */
public class Database {

    private static Database instance = null;

    private HashMap<String, Datastore> dbMap;
    private Morphia morphia;

    private Database() {
        dbMap = new HashMap<String, Datastore>();
        initMorphia();
    }

    private final void initMorphia() {
        morphia = new Morphia();
        morphia.mapPackage("com.ibtp.kontor.comics.entity");
    }

    public void registerDatastore(String databaseName) {
        if (!dbMap.containsKey(databaseName)) {
            Datastore store = morphia.createDatastore(new MongoClient("127.0.0.1"), databaseName);
            dbMap.put(databaseName, store);
        }
    }

    public Datastore getDatastore(String databaseName) {
        return dbMap.get(databaseName);
    }

    public static Database init() {
        if (instance == null) {
            instance = new Database();
        }
        return instance;
    }
}
