Search in sources :

Example 61 with BimserverDatabaseException

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

the class CommitTransactionDatabaseAction method execute.

@Override
public ConcreteRevision execute() throws UserException, BimserverLockConflictException, BimserverDatabaseException {
    Project project = getProjectByPoid(longTransaction.getPoid());
    User user = getUserByUoid(authorization.getUoid());
    if (project == null) {
        throw new UserException("Project with poid " + longTransaction.getPoid() + " 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");
    }
    long size = 0;
    Revision previousRevision = project.getLastRevision();
    if (project.getLastRevision() != null) {
        size += project.getLastRevision().getSize();
    // for (ConcreteRevision concreteRevision : project.getLastRevision().getConcreteRevisions()) {
    // size += concreteRevision.getSize();
    // }
    }
    for (Change change : longTransaction.getChanges()) {
        if (change instanceof CreateObjectChange) {
            size++;
        } else if (change instanceof RemoveObjectChange) {
            size--;
        }
    }
    Revision oldLastRevision = project.getLastRevision();
    CreateRevisionResult result = createNewConcreteRevision(getDatabaseSession(), size, project, user, comment.trim());
    ConcreteRevision concreteRevision = result.getConcreteRevision();
    revision = concreteRevision.getRevisions().get(0);
    project.setLastRevision(revision);
    final NewRevisionAdded newRevisionAdded = getDatabaseSession().create(NewRevisionAdded.class);
    newRevisionAdded.setDate(new Date());
    newRevisionAdded.setExecutor(user);
    newRevisionAdded.setRevision(concreteRevision.getRevisions().get(0));
    newRevisionAdded.setProject(project);
    newRevisionAdded.setAccessMethod(getAccessMethod());
    PackageMetaData packageMetaData = bimServer.getMetaDataManager().getPackageMetaData(project.getSchema());
    // IfcModelInterface ifcModel = new BasicIfcModel(packageMetaData, null);
    if (oldLastRevision != null) {
        int highestStopId = AbstractDownloadDatabaseAction.findHighestStopRid(project, oldLastRevision.getLastConcreteRevision());
        OldQuery query = new OldQuery(longTransaction.getPackageMetaData(), project.getId(), oldLastRevision.getId(), -1, null, Deep.YES, highestStopId);
        query.updateOidCounters(oldLastRevision.getLastConcreteRevision(), getDatabaseSession());
    // getDatabaseSession().getMap(ifcModel, query);
    }
    getDatabaseSession().addPostCommitAction(new PostCommitAction() {

        @Override
        public void execute() throws UserException {
            bimServer.getNotificationsManager().notify(new SConverter().convertToSObject(newRevisionAdded));
            try {
                bimServer.getLongTransactionManager().remove(longTransaction.getTid());
            } catch (NoTransactionException e) {
                LOGGER.error("", e);
            }
        }
    });
    SummaryMap summaryMap = null;
    if (oldLastRevision != null && oldLastRevision.getConcreteRevisions().size() == 1 && oldLastRevision.getConcreteRevisions().get(0).getSummary() != null) {
        summaryMap = new SummaryMap(packageMetaData, oldLastRevision.getConcreteRevisions().get(0).getSummary());
    } else {
        summaryMap = new SummaryMap(packageMetaData);
    }
    boolean geometryChanged = true;
    // TODO actually change this variable...
    // First create all new objects
    Transaction transaction = new Transaction(bimServer, previousRevision, project, concreteRevision, getDatabaseSession());
    for (Change change : longTransaction.getChanges()) {
        if (change instanceof CreateObjectChange) {
            try {
                change.execute(transaction);
            } catch (IOException | QueryException e) {
                e.printStackTrace();
            }
            summaryMap.add(((CreateObjectChange) change).geteClass(), 1);
        }
    }
    // Then do the rest
    for (Change change : longTransaction.getChanges()) {
        if (!(change instanceof CreateObjectChange)) {
            if (change instanceof RemoveObjectChange) {
                summaryMap.remove(((RemoveObjectChange) change).geteClass(), 1);
            }
            try {
                change.execute(transaction);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (QueryException e) {
                e.printStackTrace();
            }
        }
    }
    for (HashMapVirtualObject object : transaction.getCreated()) {
        getDatabaseSession().save(object);
    }
    for (HashMapVirtualObject object : transaction.getUpdated()) {
        getDatabaseSession().save(object, concreteRevision.getId());
    }
    for (HashMapVirtualObject object : transaction.getDeleted()) {
        getDatabaseSession().delete(object, concreteRevision.getId());
    }
    if (bimServer.getServerSettingsCache().getServerSettings().isGenerateGeometryOnCheckin() && geometryChanged) {
        setProgress("Generating Geometry...", -1);
        try {
            GeometryGenerationReport report = new GeometryGenerationReport();
            report.setOriginalDeserializer("No deserializer, low level call");
            report.setOriginalIfcFileName("No file, low level call");
            report.setOriginalIfcFileSize(-1);
            StreamingGeometryGenerator streamingGeometryGenerator = new StreamingGeometryGenerator(bimServer, null, -1L, report);
            int highestStopId = AbstractDownloadDatabaseAction.findHighestStopRid(concreteRevision.getProject(), concreteRevision);
            QueryContext queryContext = new QueryContext(getDatabaseSession(), packageMetaData, project.getId(), concreteRevision.getId(), concreteRevision.getRevisions().get(0).getOid(), highestStopId);
            Map<EClass, Long> startOids = getDatabaseSession().getStartOids();
            if (startOids == null) {
                throw new BimserverDatabaseException("No objects changed");
            }
            Map<EClass, Long> oidCounters = new HashMap<>();
            // buffer.order(ByteOrder.LITTLE_ENDIAN);
            for (EClass eClass : packageMetaData.getEClasses()) {
                if (startOids.containsKey(eClass)) {
                    long oid = startOids.get(eClass);
                    if (!DatabaseSession.perRecordVersioning(eClass)) {
                        oidCounters.put(eClass, oid);
                    // buffer.putLong(oid);
                    }
                }
            }
            queryContext.setOidCounters(oidCounters);
            GenerateGeometryResult generateGeometry = streamingGeometryGenerator.generateGeometry(authorization.getUoid(), getDatabaseSession(), queryContext);
            concreteRevision.setMinBounds(generateGeometry.getMinBoundsAsVector3f());
            concreteRevision.setMaxBounds(generateGeometry.getMaxBoundsAsVector3f());
        } catch (GeometryGeneratingException e) {
            throw new UserException(e);
        }
        revision.setHasGeometry(true);
    }
    if (oldLastRevision != null) {
        concreteRevision.setOidCounters(oldLastRevision.getConcreteRevisions().get(0).getOidCounters());
    }
    concreteRevision.setSummary(summaryMap.toRevisionSummary(getDatabaseSession()));
    getDatabaseSession().store(concreteRevision);
    getDatabaseSession().store(project);
    return concreteRevision;
}
Also used : User(org.bimserver.models.store.User) HashMap(java.util.HashMap) RemoveObjectChange(org.bimserver.changes.RemoveObjectChange) GeometryGeneratingException(org.bimserver.GeometryGeneratingException) BimserverDatabaseException(org.bimserver.BimserverDatabaseException) GeometryGenerationReport(org.bimserver.geometry.GeometryGenerationReport) EClass(org.eclipse.emf.ecore.EClass) ConcreteRevision(org.bimserver.models.store.ConcreteRevision) CreateObjectChange(org.bimserver.changes.CreateObjectChange) UserException(org.bimserver.shared.exceptions.UserException) NewRevisionAdded(org.bimserver.models.log.NewRevisionAdded) NoTransactionException(org.bimserver.webservices.NoTransactionException) SConverter(org.bimserver.interfaces.SConverter) PackageMetaData(org.bimserver.emf.PackageMetaData) PostCommitAction(org.bimserver.database.PostCommitAction) StreamingGeometryGenerator(org.bimserver.geometry.StreamingGeometryGenerator) RemoveObjectChange(org.bimserver.changes.RemoveObjectChange) CreateObjectChange(org.bimserver.changes.CreateObjectChange) Change(org.bimserver.changes.Change) IOException(java.io.IOException) QueryContext(org.bimserver.shared.QueryContext) Date(java.util.Date) OldQuery(org.bimserver.database.OldQuery) Project(org.bimserver.models.store.Project) QueryException(org.bimserver.database.queries.om.QueryException) Revision(org.bimserver.models.store.Revision) ConcreteRevision(org.bimserver.models.store.ConcreteRevision) SummaryMap(org.bimserver.SummaryMap) LongTransaction(org.bimserver.webservices.LongTransaction) Transaction(org.bimserver.changes.Transaction) HashMapVirtualObject(org.bimserver.shared.HashMapVirtualObject) GenerateGeometryResult(org.bimserver.GenerateGeometryResult)

Example 62 with BimserverDatabaseException

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

the class DownloadQueryDatabaseAction method execute.

@Override
public IfcModelInterface execute() throws UserException, BimserverLockConflictException, BimserverDatabaseException, ServerException {
    DatabaseSession session = getBimServer().getDatabase().createSession();
    try {
        SerializerPluginConfiguration serializerPluginConfiguration = getDatabaseSession().get(StorePackage.eINSTANCE.getSerializerPluginConfiguration(), serializerOid, OldQuery.getDefault());
        BimDatabaseAction<IfcModelInterface> action = new DownloadDatabaseAction(getBimServer(), session, AccessMethod.INTERNAL, roid, -1, serializerPluginConfiguration.getOid(), getAuthorization(), null);
        IfcModelInterface ifcModel = session.executeAndCommitAction(action);
        QueryEnginePluginConfiguration queryEngineObject = session.get(StorePackage.eINSTANCE.getQueryEnginePluginConfiguration(), qeid, OldQuery.getDefault());
        Revision revision = session.get(roid, OldQuery.getDefault());
        PackageMetaData packageMetaData = getBimServer().getMetaDataManager().getPackageMetaData(revision.getProject().getSchema());
        if (objectIDM == null) {
            objectIDM = new HideAllInversesObjectIDM(CollectionUtils.singleSet(Ifc2x3tc1Package.eINSTANCE), packageMetaData);
        }
        if (queryEngineObject != null) {
            QueryEnginePlugin queryEnginePlugin = getBimServer().getPluginManager().getQueryEngine(queryEngineObject.getPluginDescriptor().getPluginClassName(), true);
            if (queryEnginePlugin != null) {
                QueryEngine queryEngine = queryEnginePlugin.getQueryEngine(new PluginConfiguration(queryEngineObject.getSettings()));
                final IfcModelInterface result = new ServerIfcModel(packageMetaData, null, getDatabaseSession());
                ModelHelper modelHelper = new ModelHelper(getBimServer().getMetaDataManager(), objectIDM, result);
                modelHelper.setOidProvider(new OidProvider() {

                    private long oid = 1000000;

                    @Override
                    public long newOid(EClass eClass) {
                        return oid++;
                    }
                });
                IfcModelInterface finalResult = queryEngine.query(ifcModel, code, new Reporter() {

                    @Override
                    public void error(Throwable error) {
                    }

                    @Override
                    public void warning(String warning) {
                    }

                    @Override
                    public void info(String info) {
                    }
                }, modelHelper);
                return finalResult;
            } else {
                throw new UserException("No Query Engine found " + queryEngineObject.getPluginDescriptor().getPluginClassName());
            }
        } else {
            throw new UserException("No configured query engine found with qeid " + qeid);
        }
    } catch (BimserverDatabaseException e) {
        throw new UserException(e);
    } catch (QueryEngineException e) {
        throw new UserException(e);
    } catch (PluginException e) {
        throw new UserException(e);
    } finally {
        session.close();
    }
}
Also used : ServerIfcModel(org.bimserver.ServerIfcModel) DatabaseSession(org.bimserver.database.DatabaseSession) IfcModelInterface(org.bimserver.emf.IfcModelInterface) QueryEngine(org.bimserver.plugins.queryengine.QueryEngine) BimserverDatabaseException(org.bimserver.BimserverDatabaseException) EClass(org.eclipse.emf.ecore.EClass) OidProvider(org.bimserver.emf.OidProvider) QueryEngineException(org.bimserver.plugins.queryengine.QueryEngineException) SerializerPluginConfiguration(org.bimserver.models.store.SerializerPluginConfiguration) QueryEnginePluginConfiguration(org.bimserver.models.store.QueryEnginePluginConfiguration) PluginConfiguration(org.bimserver.plugins.PluginConfiguration) UserException(org.bimserver.shared.exceptions.UserException) QueryEnginePluginConfiguration(org.bimserver.models.store.QueryEnginePluginConfiguration) ModelHelper(org.bimserver.plugins.ModelHelper) PackageMetaData(org.bimserver.emf.PackageMetaData) Reporter(org.bimserver.plugins.Reporter) PluginException(org.bimserver.shared.exceptions.PluginException) QueryEnginePlugin(org.bimserver.plugins.queryengine.QueryEnginePlugin) Revision(org.bimserver.models.store.Revision) HideAllInversesObjectIDM(org.bimserver.plugins.objectidms.HideAllInversesObjectIDM) SerializerPluginConfiguration(org.bimserver.models.store.SerializerPluginConfiguration)

Example 63 with BimserverDatabaseException

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

the class InstallPluginBundle method execute.

@Override
public Void execute() throws UserException, BimserverLockConflictException, BimserverDatabaseException, ServerException {
    LOGGER.info("Installing plugin " + repository + " " + groupId + "." + artifactId + "." + version);
    MavenPluginLocation mavenPluginLocation = bimServer.getMavenPluginRepository().getPluginLocation(repository, groupId, artifactId);
    if (version == null) {
        String latestVersion = mavenPluginLocation.getLatestVersionString();
        LOGGER.info("Using version " + latestVersion + " because no version given");
        version = latestVersion;
    }
    try {
        LOGGER.info(mavenPluginLocation.getRepository(version));
    } catch (Exception e1) {
        e1.printStackTrace();
    }
    MavenPluginBundle mavenPluginBundle = mavenPluginLocation.getMavenPluginBundle(version);
    LOGGER.info(mavenPluginBundle.getVersion());
    try {
        bimServer.getPluginManager().install(mavenPluginBundle, plugins, false);
    } catch (Exception e) {
        LOGGER.error("", e);
        throw new UserException(e);
    }
    return null;
}
Also used : MavenPluginLocation(org.bimserver.plugins.MavenPluginLocation) MavenPluginBundle(org.bimserver.plugins.MavenPluginBundle) 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)

Example 64 with BimserverDatabaseException

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

the class InstallPluginBundleFromBytes method execute.

@Override
public Void execute() throws UserException, BimserverLockConflictException, BimserverDatabaseException, ServerException {
    LocalMavenPluginBundle mavenPluginBundle = new LocalMavenPluginBundle(data);
    try {
        List<SPluginInformation> pluginInformationFromPluginFile = bimServer.getPluginManager().getPluginInformationFromJar(new ByteArrayInputStream(data));
        for (SPluginInformation sPluginInformation : pluginInformationFromPluginFile) {
            sPluginInformation.setEnabled(true);
            if (installAllPluginsForAllUsers) {
                sPluginInformation.setInstallForAllUsers(true);
            }
            if (installAllPluginsForNewUsers) {
                sPluginInformation.setInstallForNewUsers(true);
            }
        }
        bimServer.getPluginManager().install(mavenPluginBundle, pluginInformationFromPluginFile, false);
    } catch (Exception e) {
        LOGGER.error("", e);
        throw new UserException(e);
    }
    return null;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) SPluginInformation(org.bimserver.interfaces.objects.SPluginInformation) UserException(org.bimserver.shared.exceptions.UserException) LocalMavenPluginBundle(org.bimserver.plugins.LocalMavenPluginBundle) BimserverLockConflictException(org.bimserver.database.BimserverLockConflictException) UserException(org.bimserver.shared.exceptions.UserException) ServerException(org.bimserver.shared.exceptions.ServerException) BimserverDatabaseException(org.bimserver.BimserverDatabaseException)

Example 65 with BimserverDatabaseException

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

the class StreamingCheckinDatabaseAction method rollback.

public void rollback() throws BimserverDatabaseException {
    // TODO do we need to remove indices too?
    LOGGER.info("Rolling back");
    int pid = newRevision.getProject().getId();
    int rid = newRevision.getRid();
    Map<EClass, Long> startOids = getDatabaseSession().getStartOids();
    if (startOids == null) {
        throw new BimserverDatabaseException("No objects changed");
    }
    int deleted = 0;
    for (EClass eClass : startOids.keySet()) {
        Long startOid = startOids.get(eClass);
        ByteBuffer mustStartWith = ByteBuffer.wrap(new byte[4]);
        mustStartWith.putInt(pid);
        ByteBuffer startSearchWith = ByteBuffer.wrap(new byte[12]);
        startSearchWith.putInt(pid);
        startSearchWith.putLong(startOid);
        String tableName = eClass.getEPackage().getName() + "_" + eClass.getName();
        try {
            if (!getDatabaseSession().getKeyValueStore().isTransactional(getDatabaseSession(), tableName)) {
                // System.out.println("Checking " + tableName);
                try (RecordIterator recordIterator = getDatabaseSession().getKeyValueStore().getRecordIterator(tableName, mustStartWith.array(), startSearchWith.array(), getDatabaseSession())) {
                    Record record = recordIterator.next();
                    while (record != null) {
                        // System.out.println("Deleting from " + tableName);
                        ByteBuffer keyBuffer = ByteBuffer.wrap(record.getKey());
                        // pid
                        keyBuffer.getInt();
                        // oid
                        keyBuffer.getLong();
                        int keyRid = -keyBuffer.getInt();
                        if (keyRid == rid) {
                            getDatabaseSession().getKeyValueStore().delete(tableName, record.getKey(), getDatabaseSession());
                            deleted++;
                        }
                        record = recordIterator.next();
                    }
                } catch (BimserverLockConflictException e) {
                    e.printStackTrace();
                } catch (BimserverDatabaseException e) {
                    e.printStackTrace();
                }
            }
        } catch (BimserverDatabaseException e1) {
            e1.printStackTrace();
        }
    }
    LOGGER.info("Deleted " + deleted + " objects in rollback");
// getDatabaseSession().getKeyValueStore().sync();
}
Also used : BimserverDatabaseException(org.bimserver.BimserverDatabaseException) RecordIterator(org.bimserver.database.RecordIterator) EClass(org.eclipse.emf.ecore.EClass) AtomicLong(java.util.concurrent.atomic.AtomicLong) Record(org.bimserver.database.Record) ByteBuffer(java.nio.ByteBuffer) BimserverLockConflictException(org.bimserver.database.BimserverLockConflictException)

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