Search in sources :

Example 1 with CreateBaseProjectDatabaseAction

use of org.bimserver.database.actions.CreateBaseProjectDatabaseAction in project BIMserver by opensourceBIM.

the class Database method init.

public void init() throws DatabaseInitException, DatabaseRestartRequiredException, InconsistentModelsException {
    DatabaseSession databaseSession = createSession();
    try {
        if (getKeyValueStore().isNew()) {
            keyValueStore.createTable(CLASS_LOOKUP_TABLE, null, true);
            keyValueStore.createTable(Database.STORE_PROJECT_NAME, null, true);
            keyValueStore.createTable(Registry.REGISTRY_TABLE, null, true);
            setDatabaseVersion(-1, databaseSession);
            created = new Date();
            registry.save(DATE_CREATED, created, databaseSession);
        } else {
            keyValueStore.openTable(databaseSession, CLASS_LOOKUP_TABLE, true);
            keyValueStore.openTable(databaseSession, Database.STORE_PROJECT_NAME, true);
            keyValueStore.openTable(databaseSession, Registry.REGISTRY_TABLE, true);
            created = registry.readDate(DATE_CREATED, databaseSession);
            if (created == null) {
                created = new Date();
                registry.save(DATE_CREATED, created, databaseSession);
            }
        }
        databaseSchemaVersion = registry.readInt(SCHEMA_VERSION, databaseSession, -1);
        migrator = new Migrator(this);
        if (getKeyValueStore().isNew()) {
            try {
                migrator.migrate(databaseSession);
            } catch (MigrationException e) {
                LOGGER.error("", e);
            }
            registry.save("isnew", true, databaseSession);
            databaseSession.commit();
            databaseSession.close();
            throw new DatabaseRestartRequiredException();
        } else if (registry.readBoolean("isnew", true, databaseSession)) {
            initInternalStructure(databaseSession);
            initCounters(databaseSession);
            ServerSettings settings = createDefaultSettings(databaseSession);
            databaseSession.store(settings);
            new CreateBaseProjectDatabaseAction(databaseSession, AccessMethod.INTERNAL).execute();
            AddUserDatabaseAction addUserDatabaseAction = new AddUserDatabaseAction(bimServer, databaseSession, AccessMethod.INTERNAL, "system", "system", "System", UserType.SYSTEM, new SystemAuthorization(1, TimeUnit.HOURS), false, null);
            addUserDatabaseAction.setCreateSystemUser();
            User systemUser = addUserDatabaseAction.execute();
            systemUser.setCreatedBy(systemUser);
            databaseSession.store(systemUser);
            DatabaseCreated databaseCreated = databaseSession.create(DatabaseCreated.class);
            databaseCreated.setAccessMethod(AccessMethod.INTERNAL);
            databaseCreated.setExecutor(systemUser);
            databaseCreated.setDate(new Date());
            databaseCreated.setPath(getKeyValueStore().getLocation());
            databaseCreated.setVersion(databaseSchemaVersion);
            databaseSession.store(databaseCreated);
            registry.save("isnew", false, databaseSession);
        } else {
            initInternalStructure(databaseSession);
            initCounters(databaseSession);
        }
        for (EClass eClass : cidToEclass) {
            if (eClass != null) {
                if (eClass.getEPackage() == Ifc2x3tc1Package.eINSTANCE || eClass.getEPackage() == Ifc4Package.eINSTANCE) {
                    realClasses.add(eClass.getName());
                }
            }
        }
        databaseSession.commit();
    } catch (UserException e) {
        LOGGER.error("", e);
        close();
        throw new DatabaseInitException(e.getMessage());
    } catch (BimserverDatabaseException e) {
        LOGGER.error("", e);
        close();
        throw new DatabaseInitException(e.getMessage());
    } catch (DatabaseRestartRequiredException e) {
        throw e;
    } catch (Exception e) {
        throw new DatabaseInitException(e);
    } finally {
        databaseSession.close();
    }
}
Also used : User(org.bimserver.models.store.User) SystemAuthorization(org.bimserver.webservices.authorization.SystemAuthorization) Date(java.util.Date) InconsistentModelsException(org.bimserver.database.migrations.InconsistentModelsException) DatabaseNotFoundException(com.sleepycat.je.DatabaseNotFoundException) DatabaseInitException(org.bimserver.database.berkeley.DatabaseInitException) MigrationException(org.bimserver.database.migrations.MigrationException) UserException(org.bimserver.shared.exceptions.UserException) BimserverDatabaseException(org.bimserver.BimserverDatabaseException) MigrationException(org.bimserver.database.migrations.MigrationException) DatabaseInitException(org.bimserver.database.berkeley.DatabaseInitException) DatabaseCreated(org.bimserver.models.log.DatabaseCreated) BimserverDatabaseException(org.bimserver.BimserverDatabaseException) EClass(org.eclipse.emf.ecore.EClass) AddUserDatabaseAction(org.bimserver.database.actions.AddUserDatabaseAction) ServerSettings(org.bimserver.models.store.ServerSettings) CreateBaseProjectDatabaseAction(org.bimserver.database.actions.CreateBaseProjectDatabaseAction) UserException(org.bimserver.shared.exceptions.UserException) Migrator(org.bimserver.database.migrations.Migrator)

Aggregations

DatabaseNotFoundException (com.sleepycat.je.DatabaseNotFoundException)1 Date (java.util.Date)1 BimserverDatabaseException (org.bimserver.BimserverDatabaseException)1 AddUserDatabaseAction (org.bimserver.database.actions.AddUserDatabaseAction)1 CreateBaseProjectDatabaseAction (org.bimserver.database.actions.CreateBaseProjectDatabaseAction)1 DatabaseInitException (org.bimserver.database.berkeley.DatabaseInitException)1 InconsistentModelsException (org.bimserver.database.migrations.InconsistentModelsException)1 MigrationException (org.bimserver.database.migrations.MigrationException)1 Migrator (org.bimserver.database.migrations.Migrator)1 DatabaseCreated (org.bimserver.models.log.DatabaseCreated)1 ServerSettings (org.bimserver.models.store.ServerSettings)1 User (org.bimserver.models.store.User)1 UserException (org.bimserver.shared.exceptions.UserException)1 SystemAuthorization (org.bimserver.webservices.authorization.SystemAuthorization)1 EClass (org.eclipse.emf.ecore.EClass)1