Search in sources :

Example 1 with OidCounters

use of org.bimserver.database.OidCounters in project BIMserver by opensourceBIM.

the class CheckinDatabaseAction method execute.

@Override
public ConcreteRevision execute() throws UserException, BimserverDatabaseException {
    try {
        if (fileSize == -1) {
            setProgress("Deserializing IFC file...", -1);
        } else {
            setProgress("Deserializing IFC file...", 0);
        }
        if (getModel().size() == 0) {
            throw new DeserializeException(DeserializerErrorCode.EMPTY_MODEL_CANNOT_BE_CHECKED_IN, "Cannot checkin empty model");
        }
        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, getModel());
        }
        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 = getBimServer().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(), size, project, user, comment.trim());
        concreteRevision = result.getConcreteRevision();
        IfcHeader ifcHeader = getModel().getModelMetaData().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(getModel()).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(), OldQuery.getDefault());
                revision.setService(service);
            }
        }
        newRevisionAdded.setRevision(revision);
        newRevisionAdded.setProject(project);
        newRevisionAdded.setAccessMethod(getAccessMethod());
        Revision lastRevision = project.getLastRevision();
        IfcModelInterface ifcModel = null;
        if (merge && lastRevision != null) {
            ifcModel = checkinMerge(lastRevision);
        } else {
            ifcModel = getModel();
        }
        ifcModel.fixOidsFlat(getDatabaseSession());
        if (getBimServer().getServerSettingsCache().getServerSettings().isGenerateGeometryOnCheckin()) {
            setProgress("Generating Geometry...", -1);
            UserSettings userSettings = user.getUserSettings();
            RenderEnginePluginConfiguration defaultRenderEngine = userSettings.getDefaultRenderEngine();
            if (defaultRenderEngine == null) {
                throw new UserException("No default render engine has been selected for this user");
            }
            RenderEnginePool pool = getBimServer().getRenderEnginePools().getRenderEnginePool(model.getPackageMetaData().getSchema(), defaultRenderEngine.getPluginDescriptor().getPluginClassName(), getBimServer().getPluginSettingsCache().getPluginSettings(defaultRenderEngine.getOid()));
            GenerateGeometryResult generateGeometry = new GeometryGenerator(getBimServer()).generateGeometry(pool, getBimServer().getPluginManager(), getDatabaseSession(), ifcModel, project.getId(), concreteRevision.getId(), true, geometryCache);
            // TODO OUTDATED!!!
            // TODO deduplicate (this is duplicated 4 times now!)
            concreteRevision.setMultiplierToMm(generateGeometry.getMultiplierToMm());
            concreteRevision.setBounds(generateGeometry.getBounds());
            concreteRevision.setBoundsUntransformed(generateGeometry.getBoundsUntransformed());
            DensityCollection densityCollection = getDatabaseSession().create(DensityCollection.class);
            concreteRevision.eSet(StorePackage.eINSTANCE.getConcreteRevision_DensityCollection(), densityCollection);
            Collections.sort(generateGeometry.getDensities(), new Comparator<Density>() {

                @Override
                public int compare(Density o1, Density o2) {
                    return (int) ((o1.getDensityValue() - o2.getDensityValue()) * 10000000);
                }
            });
            for (Density density : generateGeometry.getDensities()) {
                org.bimserver.models.store.Density dbDensity = getDatabaseSession().create(org.bimserver.models.store.Density.class);
                dbDensity.setDensity(density.getDensityValue());
                dbDensity.setGeometryInfoId(density.getGeometryInfoId());
                dbDensity.setTrianglesBelow(density.getNrPrimitives());
                dbDensity.setVolume(density.getVolume());
                densityCollection.getDensities().add(dbDensity);
            }
            for (Revision other : concreteRevision.getRevisions()) {
                other.setHasGeometry(true);
            }
        }
        if (nrConcreteRevisionsBefore != 0 && !merge) {
            // There already was a revision, lets delete it (only when not merging)
            concreteRevision.setClear(true);
        }
        Set<EClass> eClasses = ifcModel.getUsedClasses();
        Map<String, Long> startOids = getDatabaseSession().getStartOids();
        if (startOids == null) {
            throw new BimserverDatabaseException("No objects changed");
        }
        OidCounters oidCounters = new OidCounters();
        for (EClass eClass : eClasses) {
            long oid = startOids.get(eClass);
            if (!DatabaseSession.perRecordVersioning(eClass)) {
                oidCounters.put(eClass, oid);
            }
        }
        concreteRevision.setOidCounters(oidCounters.getBytes());
        if (ifcModel != null) {
            getDatabaseSession().store(ifcModel.getValues(), project.getId(), concreteRevision.getId());
        }
        getDatabaseSession().addPostCommitAction(new PostCommitAction() {

            @Override
            public void execute() throws UserException {
                try {
                    clearCheckinInProgress();
                } catch (BimserverDatabaseException | ServiceException e) {
                    LOGGER.error("", e);
                }
                getBimServer().getNotificationsManager().notify(new NewRevisionNotification(getBimServer(), project.getOid(), revision.getOid(), authorization));
            }
        });
        getDatabaseSession().store(concreteRevision);
        getDatabaseSession().store(project);
    } catch (Throwable e) {
        try {
            clearCheckinInProgress();
        } catch (ServiceException e1) {
            LOGGER.error("", e1);
        }
        if (e instanceof BimserverDatabaseException) {
            throw (BimserverDatabaseException) e;
        }
        if (e instanceof UserException) {
            throw (UserException) e;
        }
        LOGGER.error("", e);
        throw new UserException(e);
    }
    return concreteRevision;
}
Also used : User(org.bimserver.models.store.User) RenderEnginePool(org.bimserver.renderengine.RenderEnginePool) IfcModelInterface(org.bimserver.emf.IfcModelInterface) Density(org.bimserver.geometry.Density) ModelCheckerResult(org.bimserver.models.store.ModelCheckerResult) BimserverDatabaseException(org.bimserver.BimserverDatabaseException) ModelChecker(org.bimserver.plugins.modelchecker.ModelChecker) EClass(org.eclipse.emf.ecore.EClass) ExplicitRightsAuthorization(org.bimserver.webservices.authorization.ExplicitRightsAuthorization) OidCounters(org.bimserver.database.OidCounters) UserException(org.bimserver.shared.exceptions.UserException) NewRevisionAdded(org.bimserver.models.log.NewRevisionAdded) NewService(org.bimserver.models.store.NewService) ModelCheckerInstance(org.bimserver.models.store.ModelCheckerInstance) ModelCheckerPlugin(org.bimserver.plugins.modelchecker.ModelCheckerPlugin) IdEObject(org.bimserver.emf.IdEObject) UserSettings(org.bimserver.models.store.UserSettings) PostCommitAction(org.bimserver.database.PostCommitAction) GeometryGenerator(org.bimserver.GeometryGenerator) Service(org.bimserver.models.store.Service) NewService(org.bimserver.models.store.NewService) DeserializeException(org.bimserver.plugins.deserializers.DeserializeException) IfcHeader(org.bimserver.models.store.IfcHeader) Date(java.util.Date) RenderEnginePluginConfiguration(org.bimserver.models.store.RenderEnginePluginConfiguration) NewRevisionNotification(org.bimserver.notifications.NewRevisionNotification) Revision(org.bimserver.models.store.Revision) ConcreteRevision(org.bimserver.models.store.ConcreteRevision) SummaryMap(org.bimserver.SummaryMap) ServiceException(org.bimserver.shared.exceptions.ServiceException) GenerateGeometryResult(org.bimserver.GenerateGeometryResult) DensityCollection(org.bimserver.models.store.DensityCollection)

Example 2 with OidCounters

use of org.bimserver.database.OidCounters in project BIMserver by opensourceBIM.

the class CloneToNewProjectDatabaseAction method execute.

@Override
public ConcreteRevision execute() throws UserException, BimserverLockConflictException, BimserverDatabaseException {
    Revision oldRevision = getDatabaseSession().get(StorePackage.eINSTANCE.getRevision(), roid, OldQuery.getDefault());
    Project oldProject = oldRevision.getProject();
    final User user = getDatabaseSession().get(StorePackage.eINSTANCE.getUser(), authorization.getUoid(), OldQuery.getDefault());
    if (!authorization.hasRightsOnProjectOrSuperProjectsOrSubProjects(user, oldProject)) {
        throw new UserException("User has insufficient rights to download revisions from this project");
    }
    if (oldRevision.getConcreteRevisions().size() != 1) {
        throw new UserException("This method can only be used on Revisions that contain just one ConcreteRevision");
    }
    PackageMetaData packageMetaData = getBimServer().getMetaDataManager().getPackageMetaData(oldProject.getSchema());
    newProject = new AddProjectDatabaseAction(getBimServer(), getDatabaseSession(), getAccessMethod(), projectName, packageMetaData.getSchema().getEPackageName(), authorization).execute();
    ConcreteRevision oldConcreteRevision = oldRevision.getConcreteRevisions().get(0);
    SummaryMap summaryMap = new SummaryMap(packageMetaData, oldConcreteRevision.getSummary());
    Query query = new Query(packageMetaData);
    query.setSpecialQueryType(SpecialQueryType.ALL);
    CreateRevisionResult result = createNewConcreteRevision(getDatabaseSession(), oldRevision.getSize(), newProject, user, comment.trim());
    ConcreteRevision newConcreteRevision = result.getConcreteRevision();
    QueryContext newQueryContext = new QueryContext(getDatabaseSession(), packageMetaData, newProject.getId(), result.getRevisions().get(0).getId(), roid, newConcreteRevision.getOid(), -1);
    newConcreteRevision.setSummary(summaryMap.toRevisionSummary(getDatabaseSession()));
    newConcreteRevision.setOidCounters(new OidCounters(getDatabaseSession(), oldConcreteRevision.getOidCounters()).getBytes());
    newConcreteRevision.setIfcHeader(oldConcreteRevision.getIfcHeader());
    newConcreteRevision.setMultiplierToMm(oldConcreteRevision.getMultiplierToMm());
    newConcreteRevision.setBounds(oldConcreteRevision.getBounds());
    newConcreteRevision.setBoundsUntransformed(oldConcreteRevision.getBoundsUntransformed());
    try {
        QueryObjectProvider queryObjectProvider = new QueryObjectProvider(getDatabaseSession(), getBimServer(), query, Collections.singleton(roid), packageMetaData);
        HashMapVirtualObject next = queryObjectProvider.next();
        // Phase 1, copy all objects
        Map<Long, Long> oldOidToNew = new HashMap<>();
        while (next != null) {
            long oldOid = next.getOid();
            next.prepareForCopy(newQueryContext);
            Long newOid = getOrCreateOid(oldOidToNew, next.getReusable(), oldOid);
            next.setOid(newOid);
            for (EReference eReference : next.eClass().getEAllReferences()) {
                if (eReference.isMany()) {
                    List<Object> list = (List<Object>) next.get(eReference.getName());
                    if (list != null) {
                        for (int i = 0; i < list.size(); i++) {
                            Object object = list.get(i);
                            if (object instanceof Long) {
                                long refOid = (long) object;
                                Long newRefOid = getOrCreateOid(oldOidToNew, next.getReusable(), refOid);
                                list.set(i, newRefOid);
                            }
                        }
                    }
                } else {
                    Object object = next.eGet(eReference);
                    if (object != null) {
                        if (object instanceof Long) {
                            long refOid = (long) object;
                            long newRefOid = getOrCreateOid(oldOidToNew, next.getReusable(), refOid);
                            next.set(eReference.getName(), newRefOid);
                        }
                    }
                }
            }
            next.save();
            next = queryObjectProvider.next();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (QueryException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : User(org.bimserver.models.store.User) Query(org.bimserver.database.queries.om.Query) OldQuery(org.bimserver.database.OldQuery) HashMap(java.util.HashMap) ConcreteRevision(org.bimserver.models.store.ConcreteRevision) QueryObjectProvider(org.bimserver.database.queries.QueryObjectProvider) OidCounters(org.bimserver.database.OidCounters) List(java.util.List) UserException(org.bimserver.shared.exceptions.UserException) EReference(org.eclipse.emf.ecore.EReference) PackageMetaData(org.bimserver.emf.PackageMetaData) QueryContext(org.bimserver.shared.QueryContext) IOException(java.io.IOException) 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) HashMapVirtualObject(org.bimserver.shared.HashMapVirtualObject) HashMapVirtualObject(org.bimserver.shared.HashMapVirtualObject)

Example 3 with OidCounters

use of org.bimserver.database.OidCounters 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();
    ConcreteRevision previousConcreteRevision = null;
    if (previousRevision != null) {
        previousConcreteRevision = previousRevision.getConcreteRevisions().get(0);
    }
    if (project.getLastRevision() != null) {
        size += project.getLastRevision().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();
    if (previousConcreteRevision != null) {
        concreteRevision.setIfcHeader(previousConcreteRevision.getIfcHeader());
    }
    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());
    OidCounters originalOidCounters = null;
    PackageMetaData packageMetaData = getBimServer().getMetaDataManager().getPackageMetaData(project.getSchema());
    if (oldLastRevision != null) {
        int highestStopId = AbstractDownloadDatabaseAction.findHighestStopRid(project, oldLastRevision.getLastConcreteRevision());
        OldQuery query = new OldQuery(longTransaction.getPackageMetaData(), project.getId(), oldLastRevision.getId(), -1, Deep.YES, highestStopId);
        originalOidCounters = query.updateOidCounters(oldLastRevision.getLastConcreteRevision(), getDatabaseSession());
    } else {
        originalOidCounters = new OidCounters();
    }
    getDatabaseSession().addPostCommitAction(new PostCommitAction() {

        @Override
        public void execute() throws UserException {
            getBimServer().getNotificationsManager().notify(new SConverter().convertToSObject(newRevisionAdded));
            try {
                getBimServer().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);
    }
    // First create all new objects
    Transaction transaction = new Transaction(getBimServer(), previousRevision, project, concreteRevision, getDatabaseSession());
    for (Change change : longTransaction.getChanges()) {
        if (change instanceof CreateObjectChange) {
            try {
                CreateObjectChange createObjectChange = (CreateObjectChange) change;
                change.execute(transaction);
                getDatabaseSession().addStartOid(createObjectChange.geteClass(), createObjectChange.getOid());
            } 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());
    }
    setProgress("Generating inverses/opposites...", -1);
    Revision newRevision = result.getRevisions().get(0);
    long newRoid = newRevision.getOid();
    try {
        fixInverses(packageMetaData, newRoid, summaryMap.getSummaryMap());
    } catch (QueryException | IOException e1) {
        e1.printStackTrace();
    }
    int highestStopId = AbstractDownloadDatabaseAction.findHighestStopRid(concreteRevision.getProject(), concreteRevision);
    QueryContext queryContext = new QueryContext(getDatabaseSession(), packageMetaData, project.getId(), concreteRevision.getId(), concreteRevision.getRevisions().get(0).getOid(), concreteRevision.getOid(), highestStopId);
    Map<String, Long> startOids = getDatabaseSession().getStartOids();
    if (startOids == null) {
        throw new BimserverDatabaseException("No objects changed");
    }
    for (EClass eClass : packageMetaData.getEClasses()) {
        if (startOids.containsKey(eClass.getEPackage().getName() + "." + eClass.getName())) {
            long oid = startOids.get(eClass.getEPackage().getName() + "." + eClass.getName());
            if (!DatabaseSession.perRecordVersioning(eClass)) {
                originalOidCounters.putIfAbsent(eClass, oid);
            }
        }
    }
    queryContext.setOidCounters(originalOidCounters);
    concreteRevision.setOidCounters(originalOidCounters == null ? null : originalOidCounters.getBytes());
    if (getBimServer().getServerSettingsCache().getServerSettings().isGenerateGeometryOnCheckin()) {
        if (regenerateAllGeometry) {
            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(getBimServer(), null, -1L, report);
                GenerateGeometryResult generateGeometry = streamingGeometryGenerator.generateGeometry(authorization.getUoid(), getDatabaseSession(), queryContext, summaryMap.count());
                concreteRevision.setMultiplierToMm(generateGeometry.getMultiplierToMm());
                concreteRevision.setBounds(generateGeometry.getBounds());
                concreteRevision.setBoundsUntransformed(generateGeometry.getBoundsUntransformed());
                generateDensityAndBounds(result, generateGeometry, concreteRevision);
                final GeometryGenerationReport finalReport = report;
                getDatabaseSession().addPostCommitAction(new PostCommitAction() {

                    @Override
                    public void execute() throws UserException {
                        if (finalReport != null) {
                            byte[] htmlBytes = finalReport.toHtml().getBytes(Charsets.UTF_8);
                            byte[] jsonBytes = finalReport.toJson().toString().getBytes(Charsets.UTF_8);
                            try (DatabaseSession tmpSession = getBimServer().getDatabase().createSession(OperationType.POSSIBLY_WRITE)) {
                                AddGeometryReports addGeometryReports = new AddGeometryReports(tmpSession, AccessMethod.INTERNAL, htmlBytes, jsonBytes, finalReport.getTimeToGenerateMs(), authorization.getUoid(), revision.getOid());
                                try {
                                    tmpSession.executeAndCommitAction(addGeometryReports);
                                } catch (ServerException e1) {
                                    LOGGER.error("", e1);
                                }
                            } catch (BimserverDatabaseException e1) {
                                LOGGER.error("", e1);
                            }
                        }
                        getBimServer().getNotificationsManager().notify(new NewRevisionNotification(getBimServer(), project.getOid(), revision.getOid(), authorization));
                    }
                });
            } catch (GeometryGeneratingException e) {
                throw new UserException(e);
            }
            revision.setHasGeometry(true);
        } else {
            if (previousRevision != null) {
                byte[] htmlBytes = null;
                byte[] jsonBytes = null;
                long timeToGenerate = -1;
                for (ExtendedData previousExtendedData : previousRevision.getExtendedData()) {
                    ExtendedDataSchema previousSchema = previousExtendedData.getSchema();
                    if (previousSchema.getName().contentEquals("GEOMETRY_GENERATION_REPORT_HTML_1_1")) {
                        htmlBytes = previousExtendedData.getFile().getData();
                    } else if (previousSchema.getName().contentEquals("GEOMETRY_GENERATION_REPORT_JSON_1_1")) {
                        jsonBytes = previousExtendedData.getFile().getData();
                    }
                }
                byte[] finalHtmlBytes = htmlBytes;
                byte[] finalJsonBytes = jsonBytes;
                getDatabaseSession().addPostCommitAction(new PostCommitAction() {

                    public void execute() throws UserException {
                        try (DatabaseSession tmpSession = getBimServer().getDatabase().createSession(OperationType.POSSIBLY_WRITE)) {
                            AddGeometryReports addGeometryReports = new AddGeometryReports(tmpSession, AccessMethod.INTERNAL, finalHtmlBytes, finalJsonBytes, timeToGenerate, authorization.getUoid(), revision.getOid());
                            try {
                                tmpSession.executeAndCommitAction(addGeometryReports);
                            } catch (ServerException e1) {
                                LOGGER.error("", e1);
                            }
                        } catch (BimserverDatabaseException e1) {
                            LOGGER.error("", e1);
                        }
                    }
                });
                concreteRevision.setMultiplierToMm(previousConcreteRevision.getMultiplierToMm());
                concreteRevision.setBounds(previousConcreteRevision.getBounds());
                concreteRevision.setBoundsUntransformed(previousConcreteRevision.getBoundsUntransformed());
                newRevision.setBounds(previousRevision.getBounds());
                newRevision.setBoundsUntransformed(previousRevision.getBoundsUntransformed());
                newRevision.setBoundsMm(previousRevision.getBoundsMm());
                newRevision.setBoundsUntransformedMm(previousRevision.getBoundsUntransformedMm());
                // TODO validate this, contains ids?
                newRevision.setDensityCollection(previousRevision.getDensityCollection());
                revision.setHasGeometry(true);
            }
        }
    }
    concreteRevision.setSummary(summaryMap.toRevisionSummary(getDatabaseSession()));
    getDatabaseSession().store(concreteRevision);
    getDatabaseSession().store(project);
    return concreteRevision;
}
Also used : User(org.bimserver.models.store.User) DatabaseSession(org.bimserver.database.DatabaseSession) RemoveObjectChange(org.bimserver.changes.RemoveObjectChange) GeometryGeneratingException(org.bimserver.GeometryGeneratingException) BimserverDatabaseException(org.bimserver.BimserverDatabaseException) EClass(org.eclipse.emf.ecore.EClass) GeometryGenerationReport(org.bimserver.geometry.GeometryGenerationReport) ConcreteRevision(org.bimserver.models.store.ConcreteRevision) CreateObjectChange(org.bimserver.changes.CreateObjectChange) ExtendedDataSchema(org.bimserver.models.store.ExtendedDataSchema) OidCounters(org.bimserver.database.OidCounters) UserException(org.bimserver.shared.exceptions.UserException) NewRevisionAdded(org.bimserver.models.log.NewRevisionAdded) NoTransactionException(org.bimserver.webservices.NoTransactionException) ServerException(org.bimserver.shared.exceptions.ServerException) SConverter(org.bimserver.interfaces.SConverter) ExtendedData(org.bimserver.models.store.ExtendedData) 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) NewRevisionNotification(org.bimserver.notifications.NewRevisionNotification) 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 4 with OidCounters

use of org.bimserver.database.OidCounters in project BIMserver by opensourceBIM.

the class StreamingCheckinDatabaseAction method execute.

@Override
public ConcreteRevision execute() throws UserException, BimserverDatabaseException {
    try {
        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 = getBimServer().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, result.getConcreteRevision().getOid(), -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<String, Long> startOids = getDatabaseSession().getStartOids();
        if (startOids == null) {
            throw new BimserverDatabaseException("No objects changed");
        }
        OidCounters oidCounters = new OidCounters();
        for (EClass eClass : eClasses) {
            if (!DatabaseSession.perRecordVersioning(eClass)) {
                oidCounters.put(eClass, startOids.get(fullname(eClass)));
            }
        }
        queryContext.setOidCounters(oidCounters);
        concreteRevision = result.getConcreteRevision();
        concreteRevision.setOidCounters(oidCounters.getBytes());
        setProgress("Generating inverses/opposites...", -1);
        fixInverses(packageMetaData, newRoid, deserializer.getSummaryMap());
        ProgressListener progressListener = new ProgressListener() {

            @Override
            public void updateProgress(String state, int percentage) {
                setProgress("Generating geometry...", percentage);
            }
        };
        GeometryGenerationReport report = null;
        if (getBimServer().getServerSettingsCache().getServerSettings().isGenerateGeometryOnCheckin()) {
            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(getBimServer(), progressListener, -1L, report);
            setProgress("Generating geometry...", 0);
            GenerateGeometryResult generateGeometry = geometryGenerator.generateGeometry(getActingUid(), getDatabaseSession(), queryContext, size);
            for (Revision other : concreteRevision.getRevisions()) {
                other.setHasGeometry(true);
            }
            concreteRevision.setMultiplierToMm(generateGeometry.getMultiplierToMm());
            concreteRevision.setBounds(generateGeometry.getBounds());
            concreteRevision.setBoundsUntransformed(generateGeometry.getBoundsUntransformed());
            // TODO terrible code, but had to get it going quickly, will cleanup later
            generateDensityAndBounds(result, generateGeometry, concreteRevision);
        }
        final GeometryGenerationReport finalReport = report;
        // float[] quantizationMatrix = createQuantizationMatrixFromBounds(newRevision.getBoundsMm());
        // generateQuantizedVertices(getDatabaseSession(), newRevision, quantizationMatrix, generateGeometry.getMultiplierToMm());
        setProgress("Doing other stuff...", -1);
        eClasses = deserializer.getSummaryMap().keySet();
        oidCounters = new OidCounters();
        for (EClass eClass : eClasses) {
            String fullname = fullname(eClass);
            Long oid = startOids.get(fullname);
            if (oid == null) {
                // This happens almost never, but it most certainly must be a bug, adding verbose logging to try and identify the problem
                LOGGER.info("");
                LOGGER.info("EClass " + eClass.getName() + " not found in startOids, please report");
                LOGGER.info("eClasses:");
                for (EClass eClass2 : eClasses) {
                    LOGGER.info(fullname(eClass2));
                }
                LOGGER.info("");
                LOGGER.info("startOids");
                for (String fullname2 : startOids.keySet()) {
                    LOGGER.info(fullname2 + ": " + startOids.get(fullname2));
                }
                throw new UserException("EClass " + eClass.getName() + " not found in startOids, please report");
            }
            if (!DatabaseSession.perRecordVersioning(eClass)) {
                oidCounters.put(eClass, oid);
            }
        }
        if (startOids.containsKey(fullname(GeometryPackage.eINSTANCE.getGeometryInfo())) && startOids.containsKey(fullname(GeometryPackage.eINSTANCE.getGeometryData()))) {
            oidCounters.put(GeometryPackage.eINSTANCE.getGeometryInfo(), startOids.get(fullname(GeometryPackage.eINSTANCE.getGeometryInfo())));
            oidCounters.put(GeometryPackage.eINSTANCE.getGeometryData(), startOids.get(fullname(GeometryPackage.eINSTANCE.getGeometryData())));
            oidCounters.put(GeometryPackage.eINSTANCE.getBuffer(), startOids.get(fullname(GeometryPackage.eINSTANCE.getBuffer())));
        }
        concreteRevision = result.getConcreteRevision();
        concreteRevision.setOidCounters(oidCounters.getBytes());
        // 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 || revision.getSize() == -1) ? 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);
        }
        getDatabaseSession().addPostCommitAction(new PostCommitAction() {

            @Override
            public void execute() throws UserException {
                try (DatabaseSession tmpSession = getBimServer().getDatabase().createSession(OperationType.POSSIBLY_WRITE)) {
                    Project project = tmpSession.get(poid, OldQuery.getDefault());
                    project.setCheckinInProgress(0);
                    tmpSession.store(project);
                    tmpSession.commit();
                } catch (BimserverDatabaseException e) {
                    LOGGER.error("", e);
                } catch (ServiceException e) {
                    LOGGER.error("", e);
                }
                if (finalReport != null) {
                    byte[] htmlBytes = finalReport.toHtml().getBytes(Charsets.UTF_8);
                    byte[] jsonBytes = finalReport.toJson().toString().getBytes(Charsets.UTF_8);
                    try (DatabaseSession tmpSession = getBimServer().getDatabase().createSession(OperationType.POSSIBLY_WRITE)) {
                        AddGeometryReports addGeometryReports = new AddGeometryReports(tmpSession, AccessMethod.INTERNAL, htmlBytes, jsonBytes, finalReport.getTimeToGenerateMs(), authorization.getUoid(), revision.getOid());
                        try {
                            tmpSession.executeAndCommitAction(addGeometryReports);
                        } catch (ServerException e1) {
                            LOGGER.error("", e1);
                        }
                    } catch (BimserverDatabaseException e1) {
                        LOGGER.error("", e1);
                    }
                }
                getBimServer().getNotificationsManager().notify(new NewRevisionNotification(getBimServer(), project.getOid(), revision.getOid(), authorization));
            }
        });
        getDatabaseSession().store(concreteRevision);
        getDatabaseSession().store(project);
    } catch (Throwable e) {
        try (DatabaseSession tmpSession = getBimServer().getDatabase().createSession(OperationType.POSSIBLY_WRITE)) {
            Project project = tmpSession.get(poid, OldQuery.getDefault());
            project.setCheckinInProgress(0);
            tmpSession.store(project);
            try {
                tmpSession.commit();
            } catch (ServiceException e2) {
                LOGGER.error("", e2);
            }
        } catch (BimserverDatabaseException e1) {
            LOGGER.error("", e1);
        }
        if (e instanceof BimserverDatabaseException) {
            throw (BimserverDatabaseException) e;
        }
        if (e instanceof UserException) {
            throw (UserException) e;
        }
        throw new UserException("[" + fileName + "] " + e.getMessage(), e);
    }
    return concreteRevision;
}
Also used : User(org.bimserver.models.store.User) DatabaseSession(org.bimserver.database.DatabaseSession) 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) OidCounters(org.bimserver.database.OidCounters) UserException(org.bimserver.shared.exceptions.UserException) NewRevisionAdded(org.bimserver.models.log.NewRevisionAdded) NewService(org.bimserver.models.store.NewService) ServerException(org.bimserver.shared.exceptions.ServerException) 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) IfcHeader(org.bimserver.models.store.IfcHeader) Date(java.util.Date) Project(org.bimserver.models.store.Project) 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) ServiceException(org.bimserver.shared.exceptions.ServiceException) GenerateGeometryResult(org.bimserver.GenerateGeometryResult) AtomicLong(java.util.concurrent.atomic.AtomicLong)

Example 5 with OidCounters

use of org.bimserver.database.OidCounters in project BIMserver by opensourceBIM.

the class GetAvailableClassesInRevisionDatabaseAction method execute.

@Override
public List<String> execute() throws UserException, BimserverLockConflictException, BimserverDatabaseException {
    Revision revision = getRevisionByRoid(roid);
    EPackage ePackage = bimServer.getMetaDataManager().getPackageMetaData(revision.getProject().getSchema()).getEPackage();
    // TODO there are still some situations where there are no oid counters, in those cases this will fail
    Set<String> set = new HashSet<>();
    for (ConcreteRevision concreteRevision : revision.getConcreteRevisions()) {
        OidCounters oidCounters = new OidCounters(getDatabaseSession(), concreteRevision.getOidCounters());
        set.addAll(oidCounters.keySet().stream().filter(eClass -> eClass.getEPackage() == ePackage).map(EClass::getName).collect(Collectors.toSet()));
    }
    ArrayList<String> list = new ArrayList<>(set);
    Collections.sort(list);
    return list;
}
Also used : BimServer(org.bimserver.BimServer) Set(java.util.Set) AccessMethod(org.bimserver.models.log.AccessMethod) BimserverLockConflictException(org.bimserver.database.BimserverLockConflictException) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) EPackage(org.eclipse.emf.ecore.EPackage) HashSet(java.util.HashSet) Revision(org.bimserver.models.store.Revision) List(java.util.List) EClass(org.eclipse.emf.ecore.EClass) ConcreteRevision(org.bimserver.models.store.ConcreteRevision) UserException(org.bimserver.shared.exceptions.UserException) OidCounters(org.bimserver.database.OidCounters) Collections(java.util.Collections) BimserverDatabaseException(org.bimserver.BimserverDatabaseException) DatabaseSession(org.bimserver.database.DatabaseSession) EClass(org.eclipse.emf.ecore.EClass) Revision(org.bimserver.models.store.Revision) ConcreteRevision(org.bimserver.models.store.ConcreteRevision) ConcreteRevision(org.bimserver.models.store.ConcreteRevision) ArrayList(java.util.ArrayList) OidCounters(org.bimserver.database.OidCounters) EPackage(org.eclipse.emf.ecore.EPackage) HashSet(java.util.HashSet)

Aggregations

OidCounters (org.bimserver.database.OidCounters)5 ConcreteRevision (org.bimserver.models.store.ConcreteRevision)5 Revision (org.bimserver.models.store.Revision)5 UserException (org.bimserver.shared.exceptions.UserException)5 BimserverDatabaseException (org.bimserver.BimserverDatabaseException)4 SummaryMap (org.bimserver.SummaryMap)4 User (org.bimserver.models.store.User)4 Date (java.util.Date)3 GenerateGeometryResult (org.bimserver.GenerateGeometryResult)3 DatabaseSession (org.bimserver.database.DatabaseSession)3 PostCommitAction (org.bimserver.database.PostCommitAction)3 NewRevisionAdded (org.bimserver.models.log.NewRevisionAdded)3 Project (org.bimserver.models.store.Project)3 NewRevisionNotification (org.bimserver.notifications.NewRevisionNotification)3 QueryContext (org.bimserver.shared.QueryContext)3 EClass (org.eclipse.emf.ecore.EClass)3 IOException (java.io.IOException)2 List (java.util.List)2 OldQuery (org.bimserver.database.OldQuery)2 QueryException (org.bimserver.database.queries.om.QueryException)2