package nl.fontys.sofa.limo.orientdb.database;

import com.orientechnologies.orient.core.db.ODatabaseRecordThreadLocal;
import com.orientechnologies.orient.object.db.OObjectDatabasePool;
import com.orientechnologies.orient.object.db.OObjectDatabaseTx;
import java.io.File;
import nl.fontys.sofa.limo.api.database.AbstractDBServer;
import nl.fontys.sofa.limo.domain.BaseEntity;
import nl.fontys.sofa.limo.domain.category.Category;
import nl.fontys.sofa.limo.domain.category.CostCategory;
import nl.fontys.sofa.limo.domain.category.TimeCategory;

public class OrientDBAccess extends AbstractDBServer<OObjectDatabaseTx> {

    protected static OrientDBAccess instance;

    public synchronized static OrientDBAccess getInstance() {
        if (instance == null) {
            instance = new OrientDBAccess();
        }
        return instance;
    }

    protected OrientDBAccess() {
        connection = new OObjectDatabaseTx(getDatabaseURL());
        ODatabaseRecordThreadLocal.INSTANCE.set(connection.getUnderlying());

        if (!connection.exists()) {
            connection.create();
            registerClasses();
        }

        if (connection.isClosed()) {
            connection.open("admin", "admin");
            registerClasses();
        }
    }

    @Override
    public void closeConnection() {
        connection.close();
    }

    @Override
    protected void checkConnection() {
        if (connection == null) {
            connection = new OObjectDatabaseTx(getDatabaseURL());
            ODatabaseRecordThreadLocal.INSTANCE.set(connection.getUnderlying());

            if (!connection.exists()) {
                connection.create();
            }

        }

        if (connection.isClosed()) {
            connection.open("admin", "admin");
            registerClasses();
        }
    }

    @Override
    public String getDatabaseURL() {
        return "plocal:" + System.getProperty("user.home") + File.separator + "LIMO";
    }

    protected void registerClasses() {
        connection.getEntityManager().registerEntityClasses("nl.fontys.sofa.limo.domain");
        connection.getEntityManager().registerEntityClass(BaseEntity.class);
        connection.getEntityManager().registerEntityClass(Category.class);
        connection.getEntityManager().registerEntityClass(TimeCategory.class);
        connection.getEntityManager().registerEntityClass(CostCategory.class);
    }

}

@@ -117,75 +117,8 @@
 al;%0A
-import com.orientechnologies.orient.object.db.OObjectDatabasePool;%0A
 impo
@@ -469,16 +469,165 @@
 ategory;
+%0Aimport nl.fontys.sofa.limo.domain.component.Event;%0Aimport nl.fontys.sofa.limo.domain.component.Hub;%0Aimport nl.fontys.sofa.limo.domain.types.HubType;
 %0A%0Apublic
@@ -2474,21 +2474,237 @@
 ory.class);%0A
+        connection.getEntityManager().registerEntityClass(Hub.class);%0A        connection.getEntityManager().registerEntityClass(HubType.class);%0A        connection.getEntityManager().registerEntityClass(Event.class);%0A
     %7D%0A%0A%7D%0A

Access for Hub, HubType and Event