Search in sources :

Example 66 with BimserverDatabaseException

use of org.bimserver.BimserverDatabaseException in project BIMserver by opensourceBIM.

the class StreamingCheckinDatabaseAction method execute.

@Override
public ConcreteRevision execute() throws UserException, BimserverDatabaseException {
    try {
        bimServer.getCheckinsInProgress().put(poid, getActingUid());
        if (inputStream instanceof RestartableInputStream) {
            ((RestartableInputStream) inputStream).restartIfAtEnd();
        }
        getDatabaseSession().clearPostCommitActions();
        if (fileSize == -1) {
        // setProgress("Deserializing IFC file...", -1);
        } else {
            setProgress("Deserializing IFC file...", 0);
        }
        authorization.canCheckin(poid);
        project = getProjectByPoid(poid);
        int nrConcreteRevisionsBefore = project.getConcreteRevisions().size();
        User user = getUserByUoid(authorization.getUoid());
        if (project == null) {
            throw new UserException("Project with poid " + poid + " not found");
        }
        if (!authorization.hasRightsOnProjectOrSuperProjects(user, project)) {
            throw new UserException("User has no rights to checkin models to this project");
        }
        if (!MailSystem.isValidEmailAddress(user.getUsername())) {
            throw new UserException("Users must have a valid e-mail address to checkin");
        }
        // if (getModel() != null) {
        // checkCheckSum(project);
        // }
        packageMetaData = bimServer.getMetaDataManager().getPackageMetaData(project.getSchema());
        // TODO checksum
        // TODO modelcheckers
        // TODO test ifc4
        // long size = 0;
        // if (getModel() != null) {
        // for (IdEObject idEObject : getModel().getValues()) {
        // if (idEObject.eClass().getEAnnotation("hidden") == null) {
        // size++;
        // }
        // }
        // getModel().fixInverseMismatches();
        // }
        // for (ModelCheckerInstance modelCheckerInstance : project.getModelCheckers()) {
        // if (modelCheckerInstance.isValid()) {
        // ModelCheckerPlugin modelCheckerPlugin = bimServer.getPluginManager().getModelCheckerPlugin(modelCheckerInstance.getModelCheckerPluginClassName(), true);
        // if (modelCheckerPlugin != null) {
        // ModelChecker modelChecker = modelCheckerPlugin.createModelChecker(null);
        // ModelCheckerResult result = modelChecker.check(getModel(), modelCheckerInstance.getCompiled());
        // if (!result.isValid()) {
        // throw new UserException("Model is not valid according to " + modelCheckerInstance.getName());
        // }
        // }
        // }
        // }
        CreateRevisionResult result = createNewConcreteRevision(getDatabaseSession(), -1, project, user, comment.trim());
        newRevision = result.getRevisions().get(0);
        long newRoid = newRevision.getOid();
        // TODO check
        QueryContext queryContext = new QueryContext(getDatabaseSession(), packageMetaData, result.getConcreteRevision().getProject().getId(), result.getConcreteRevision().getId(), newRoid, -1);
        AtomicLong bytesRead = new AtomicLong();
        deserializer.setProgressReporter(new ByteProgressReporter() {

            @Override
            public void progress(long byteNumber) {
                bytesRead.set(byteNumber);
                if (fileSize != -1) {
                    int perc = (int) (100.0 * byteNumber / fileSize);
                    setProgress("Deserializing...", perc);
                }
            }
        });
        // This will read the full stream of objects and write to the database directly
        long size = deserializer.read(inputStream, fileName, fileSize, queryContext);
        Set<EClass> eClasses = deserializer.getSummaryMap().keySet();
        Map<EClass, Long> startOids = getDatabaseSession().getStartOids();
        if (startOids == null) {
            throw new BimserverDatabaseException("No objects changed");
        }
        Map<EClass, Long> oidCounters = new HashMap<>();
        int s = 0;
        for (EClass eClass : eClasses) {
            if (!DatabaseSession.perRecordVersioning(eClass)) {
                s++;
            }
        }
        ByteBuffer buffer = ByteBuffer.allocate(8 * s);
        buffer.order(ByteOrder.LITTLE_ENDIAN);
        for (EClass eClass : eClasses) {
            if (!DatabaseSession.perRecordVersioning(eClass)) {
                long oid = startOids.get(eClass);
                oidCounters.put(eClass, oid);
                buffer.putLong(oid);
            }
        }
        queryContext.setOidCounters(oidCounters);
        concreteRevision = result.getConcreteRevision();
        concreteRevision.setOidCounters(buffer.array());
        setProgress("Generating inverses/opposites...", -1);
        fixInverses(packageMetaData, newRoid);
        ProgressListener progressListener = new ProgressListener() {

            @Override
            public void updateProgress(String state, int percentage) {
                setProgress("Generating geometry...", percentage);
            }
        };
        GeometryGenerationReport report = new GeometryGenerationReport();
        report.setOriginalIfcFileName(fileName);
        report.setOriginalIfcFileSize(bytesRead.get());
        report.setNumberOfObjects(size);
        report.setOriginalDeserializer(pluginBundleVersion.getGroupId() + "." + pluginBundleVersion.getArtifactId() + ":" + pluginBundleVersion.getVersion());
        StreamingGeometryGenerator geometryGenerator = new StreamingGeometryGenerator(bimServer, progressListener, -1L, report);
        setProgress("Generating geometry...", 0);
        GenerateGeometryResult generateGeometry = geometryGenerator.generateGeometry(getActingUid(), getDatabaseSession(), queryContext);
        for (Revision other : concreteRevision.getRevisions()) {
            other.setHasGeometry(true);
        }
        concreteRevision.setMinBounds(generateGeometry.getMinBoundsAsVector3f());
        concreteRevision.setMaxBounds(generateGeometry.getMaxBoundsAsVector3f());
        setProgress("Doing other stuff...", -1);
        eClasses = deserializer.getSummaryMap().keySet();
        s = (startOids.containsKey(GeometryPackage.eINSTANCE.getGeometryInfo()) && startOids.containsKey(GeometryPackage.eINSTANCE.getGeometryData())) ? 2 : 0;
        for (EClass eClass : eClasses) {
            if (!DatabaseSession.perRecordVersioning(eClass)) {
                s++;
            }
        }
        buffer = ByteBuffer.allocate(8 * s);
        buffer.order(ByteOrder.LITTLE_ENDIAN);
        for (EClass eClass : eClasses) {
            long oid = startOids.get(eClass);
            if (!DatabaseSession.perRecordVersioning(eClass)) {
                buffer.putLong(oid);
            }
        }
        if (startOids.containsKey(GeometryPackage.eINSTANCE.getGeometryInfo()) && startOids.containsKey(GeometryPackage.eINSTANCE.getGeometryData())) {
            buffer.putLong(startOids.get(GeometryPackage.eINSTANCE.getGeometryInfo()));
            buffer.putLong(startOids.get(GeometryPackage.eINSTANCE.getGeometryData()));
        }
        concreteRevision = result.getConcreteRevision();
        concreteRevision.setOidCounters(buffer.array());
        // Clear the cache, we don't want it to cache incomplete oidcounters
        ConcreteRevisionStackFrame.clearCache(concreteRevision.getOid());
        result.getConcreteRevision().setSize(size);
        for (Revision revision : result.getRevisions()) {
            revision.setSize((revision.getSize() == null ? 0 : revision.getSize()) + concreteRevision.getSize());
        }
        IfcHeader ifcHeader = deserializer.getIfcHeader();
        if (ifcHeader != null) {
            getDatabaseSession().store(ifcHeader);
            concreteRevision.setIfcHeader(ifcHeader);
        }
        project.getConcreteRevisions().add(concreteRevision);
        // if (getModel() != null) {
        // concreteRevision.setChecksum(getModel().getModelMetaData().getChecksum());
        // }
        final NewRevisionAdded newRevisionAdded = getDatabaseSession().create(NewRevisionAdded.class);
        newRevisionAdded.setDate(new Date());
        newRevisionAdded.setExecutor(user);
        final Revision revision = concreteRevision.getRevisions().get(0);
        if (newServiceId != -1) {
            NewService newService = getDatabaseSession().get(newServiceId, OldQuery.getDefault());
            revision.getServicesLinked().add(newService);
        }
        concreteRevision.setSummary(new SummaryMap(packageMetaData, deserializer.getSummaryMap()).toRevisionSummary(getDatabaseSession()));
        // If this revision is being created by an external service, store a link to the service in the revision
        if (authorization instanceof ExplicitRightsAuthorization) {
            ExplicitRightsAuthorization explicitRightsAuthorization = (ExplicitRightsAuthorization) authorization;
            if (explicitRightsAuthorization.getSoid() != -1) {
                Service service = getDatabaseSession().get(explicitRightsAuthorization.getSoid(), org.bimserver.database.OldQuery.getDefault());
                revision.setService(service);
            }
        }
        newRevisionAdded.setRevision(revision);
        newRevisionAdded.setProject(project);
        newRevisionAdded.setAccessMethod(getAccessMethod());
        if (nrConcreteRevisionsBefore != 0) {
            // There already was a revision, lets delete it (only when not merging)
            concreteRevision.setClear(true);
        }
        ExtendedData extendedData = getDatabaseSession().create(ExtendedData.class);
        File file = getDatabaseSession().create(File.class);
        byte[] bytes = report.toHtml().getBytes(Charsets.UTF_8);
        file.setData(bytes);
        file.setFilename("geometrygenerationreport.html");
        file.setMime("text/html");
        file.setSize(bytes.length);
        User actingUser = getUserByUoid(authorization.getUoid());
        extendedData.setUser(actingUser);
        extendedData.setTitle("Geometry generation report");
        extendedData.setAdded(new Date());
        extendedData.setSize(file.getData().length);
        extendedData.setFile(file);
        revision.getExtendedData().add(extendedData);
        extendedData.setProject(revision.getProject());
        extendedData.setRevision(revision);
        getDatabaseSession().store(file);
        getDatabaseSession().store(extendedData);
        if (extendedData.getSchema() != null) {
            getDatabaseSession().store(extendedData.getSchema());
        }
        getDatabaseSession().addPostCommitAction(new PostCommitAction() {

            @Override
            public void execute() throws UserException {
                bimServer.getNotificationsManager().notify(new NewRevisionNotification(bimServer, project.getOid(), revision.getOid(), authorization));
            }
        });
        getDatabaseSession().store(concreteRevision);
        getDatabaseSession().store(project);
    } catch (Throwable e) {
        if (e instanceof BimserverDatabaseException) {
            throw (BimserverDatabaseException) e;
        }
        if (e instanceof UserException) {
            throw (UserException) e;
        }
        throw new UserException(e);
    } finally {
        bimServer.getCheckinsInProgress().remove(poid);
    }
    return concreteRevision;
}
Also used : User(org.bimserver.models.store.User) HashMap(java.util.HashMap) RestartableInputStream(org.bimserver.webservices.impl.RestartableInputStream) BimserverDatabaseException(org.bimserver.BimserverDatabaseException) EClass(org.eclipse.emf.ecore.EClass) GeometryGenerationReport(org.bimserver.geometry.GeometryGenerationReport) ExplicitRightsAuthorization(org.bimserver.webservices.authorization.ExplicitRightsAuthorization) UserException(org.bimserver.shared.exceptions.UserException) NewRevisionAdded(org.bimserver.models.log.NewRevisionAdded) NewService(org.bimserver.models.store.NewService) ExtendedData(org.bimserver.models.store.ExtendedData) PostCommitAction(org.bimserver.database.PostCommitAction) StreamingGeometryGenerator(org.bimserver.geometry.StreamingGeometryGenerator) Service(org.bimserver.models.store.Service) NewService(org.bimserver.models.store.NewService) QueryContext(org.bimserver.shared.QueryContext) ByteBuffer(java.nio.ByteBuffer) IfcHeader(org.bimserver.models.store.IfcHeader) Date(java.util.Date) AtomicLong(java.util.concurrent.atomic.AtomicLong) ByteProgressReporter(org.bimserver.plugins.deserializers.ByteProgressReporter) NewRevisionNotification(org.bimserver.notifications.NewRevisionNotification) Revision(org.bimserver.models.store.Revision) ConcreteRevision(org.bimserver.models.store.ConcreteRevision) SummaryMap(org.bimserver.SummaryMap) GenerateGeometryResult(org.bimserver.GenerateGeometryResult) AtomicLong(java.util.concurrent.atomic.AtomicLong) File(org.bimserver.models.store.File)

Example 67 with BimserverDatabaseException

use of org.bimserver.BimserverDatabaseException in project BIMserver by opensourceBIM.

the class UpdatePluginBundle method execute.

@Override
public Void execute() throws UserException, BimserverLockConflictException, BimserverDatabaseException, ServerException {
    MavenPluginLocation mavenPluginLocation = bimServer.getMavenPluginRepository().getPluginLocation(repository, groupId, artifactId);
    try {
        Path jarFile = mavenPluginLocation.getVersionJar(version);
        Path pomFile = mavenPluginLocation.getVersionPom(version);
        try {
            List<SPluginInformation> plugins = null;
            try {
                plugins = bimServer.getPluginManager().getPluginInformationFromPluginFile(new ByteArrayInputStream(mavenPluginLocation.getVersionPluginXml(version)));
            } catch (ArtifactResolutionException e) {
                plugins = bimServer.getPluginManager().getPluginInformationFromJar(jarFile);
            }
            // IfcModelInterface allOfType = getDatabaseSession().getAllOfType(StorePackage.eINSTANCE.getPluginDescriptor(), OldQuery.getDefault());
            // for (SPluginInformation sPluginInformation : plugins) {
            // update plugin information with data from potentially existing plugins
            // }
            bimServer.getPluginManager().update(mavenPluginLocation.getPluginVersionIdentifier(version), mavenPluginLocation.getPluginBundle(version), mavenPluginLocation.getPluginBundleVersion(version), jarFile, pomFile, plugins);
        } catch (Exception e) {
            throw new UserException(e);
        }
    } catch (Exception e) {
        LOGGER.error("", e);
        throw new UserException(e);
    }
    return null;
}
Also used : Path(java.nio.file.Path) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) MavenPluginLocation(org.bimserver.plugins.MavenPluginLocation) ByteArrayInputStream(java.io.ByteArrayInputStream) SPluginInformation(org.bimserver.interfaces.objects.SPluginInformation) UserException(org.bimserver.shared.exceptions.UserException) BimserverLockConflictException(org.bimserver.database.BimserverLockConflictException) UserException(org.bimserver.shared.exceptions.UserException) ServerException(org.bimserver.shared.exceptions.ServerException) BimserverDatabaseException(org.bimserver.BimserverDatabaseException) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException)

Example 68 with BimserverDatabaseException

use of org.bimserver.BimserverDatabaseException 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)

Example 69 with BimserverDatabaseException

use of org.bimserver.BimserverDatabaseException in project BIMserver by opensourceBIM.

the class Database method getEClassForOid.

public EClass getEClassForOid(long oid) throws BimserverDatabaseException {
    short cid = (short) oid;
    EClass eClass = getEClassForCid(cid);
    if (eClass == null) {
        throw new BimserverDatabaseException("No class for cid " + cid + " (cid came from oid: " + oid + ")");
    }
    return eClass;
}
Also used : BimserverDatabaseException(org.bimserver.BimserverDatabaseException) EClass(org.eclipse.emf.ecore.EClass)

Example 70 with BimserverDatabaseException

use of org.bimserver.BimserverDatabaseException in project BIMserver by opensourceBIM.

the class DatabaseSession method querySingle.

@SuppressWarnings("unchecked")
public <T extends IdEObject> T querySingle(EAttribute attribute, Object value, int pid, int rid) throws BimserverLockConflictException, BimserverDatabaseException {
    if (attribute.getEAnnotation("singleindex") != null) {
        String indexTableName = attribute.getEContainingClass().getEPackage().getName() + "_" + attribute.getEContainingClass().getName() + "_" + attribute.getName();
        byte[] queryBytes = null;
        if (value instanceof String) {
            queryBytes = ((String) value).getBytes(Charsets.UTF_8);
        } else if (value instanceof Integer) {
            queryBytes = BinUtils.intToByteArray((Integer) value);
        } else {
            throw new BimserverDatabaseException("Unsupported type " + value);
        }
        boolean perRecordVersioning = perRecordVersioning(attribute.getEContainingClass());
        if (!perRecordVersioning) {
            ByteBuffer valueBuffer = ByteBuffer.allocate(queryBytes.length + 8);
            valueBuffer.putInt(pid);
            valueBuffer.putInt(-rid);
            valueBuffer.put(queryBytes);
            queryBytes = valueBuffer.array();
        }
        byte[] firstDuplicate = database.getKeyValueStore().get(indexTableName, queryBytes, this);
        if (firstDuplicate != null) {
            ByteBuffer buffer = ByteBuffer.wrap(firstDuplicate);
            // pid
            buffer.getInt();
            long oid = buffer.getLong();
            return (T) get(oid, OldQuery.getDefault());
        }
    } else {
        throw new UnsupportedOperationException(attribute.getContainerClass().getName() + "." + attribute.getName() + " does not have a \"singleindex\"");
    }
    return null;
}
Also used : BimserverDatabaseException(org.bimserver.BimserverDatabaseException) ByteBuffer(java.nio.ByteBuffer)

Aggregations

BimserverDatabaseException (org.bimserver.BimserverDatabaseException)123 DatabaseSession (org.bimserver.database.DatabaseSession)56 UserException (org.bimserver.shared.exceptions.UserException)30 EClass (org.eclipse.emf.ecore.EClass)24 User (org.bimserver.models.store.User)20 ByteBuffer (java.nio.ByteBuffer)19 BimserverLockConflictException (org.bimserver.database.BimserverLockConflictException)18 ServerSettings (org.bimserver.models.store.ServerSettings)18 IOException (java.io.IOException)16 ServerSettingsSetter (org.bimserver.database.actions.ServerSettingsSetter)16 SetServerSettingDatabaseAction (org.bimserver.database.actions.SetServerSettingDatabaseAction)16 SServerSettings (org.bimserver.interfaces.objects.SServerSettings)16 ServerException (org.bimserver.shared.exceptions.ServerException)15 UserSettings (org.bimserver.models.store.UserSettings)12 ServiceException (org.bimserver.shared.exceptions.ServiceException)11 HashMapVirtualObject (org.bimserver.shared.HashMapVirtualObject)10 ArrayList (java.util.ArrayList)9 IdEObject (org.bimserver.emf.IdEObject)9 Project (org.bimserver.models.store.Project)9 SerializerPluginConfiguration (org.bimserver.models.store.SerializerPluginConfiguration)9