Search in sources :

Example 66 with Project

use of org.bimserver.models.store.Project 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 67 with Project

use of org.bimserver.models.store.Project in project BIMserver by opensourceBIM.

the class UpdateRevisionDatabaseAction method execute.

@Override
public Void execute() throws UserException, BimserverLockConflictException, BimserverDatabaseException {
    User actingUser = getUserByUoid(authorization.getUoid());
    final Revision revision = getRevisionByRoid(sRevision.getOid());
    if (revision == null) {
        throw new UserException("Revision with pid " + sRevision.getOid() + " not found");
    }
    Project project = revision.getProject();
    if (!authorization.hasRightsOnProjectOrSuperProjects(actingUser, project)) {
        throw new UserException("User has no rights to update project properties");
    }
    final RevisionUpdated revisionUpdated = getDatabaseSession().create(RevisionUpdated.class);
    revisionUpdated.setRevision(revision);
    revisionUpdated.setDate(new Date());
    revisionUpdated.setExecutor(actingUser);
    revisionUpdated.setAccessMethod(getAccessMethod());
    getDatabaseSession().addPostCommitAction(new PostCommitAction() {

        @Override
        public void execute() throws UserException {
            bimServer.getNotificationsManager().notify(new SConverter().convertToSObject(revisionUpdated));
        }
    });
    revision.setTag(sRevision.getTag());
    getDatabaseSession().store(revision);
    return null;
}
Also used : Project(org.bimserver.models.store.Project) RevisionUpdated(org.bimserver.models.log.RevisionUpdated) User(org.bimserver.models.store.User) SRevision(org.bimserver.interfaces.objects.SRevision) Revision(org.bimserver.models.store.Revision) SConverter(org.bimserver.interfaces.SConverter) PostCommitAction(org.bimserver.database.PostCommitAction) UserException(org.bimserver.shared.exceptions.UserException) Date(java.util.Date)

Example 68 with Project

use of org.bimserver.models.store.Project in project BIMserver by opensourceBIM.

the class UserHasRightsDatabaseAction method execute.

@Override
public Boolean execute() throws UserException, BimserverLockConflictException, BimserverDatabaseException {
    Project project = getProjectByPoid(poid);
    User user = getUserByUoid(uoid);
    return authorization.hasRightsOnProjectOrSuperProjectsOrSubProjects(user, project);
}
Also used : Project(org.bimserver.models.store.Project) User(org.bimserver.models.store.User)

Example 69 with Project

use of org.bimserver.models.store.Project in project BIMserver by opensourceBIM.

the class RemoveNewServiceFromProjectDatabaseAction method execute.

@Override
public Boolean execute() throws UserException, BimserverDatabaseException, BimserverLockConflictException {
    NewService service = getDatabaseSession().get(soid, OldQuery.getDefault());
    Project project = getDatabaseSession().get(poid, OldQuery.getDefault());
    project.getNewServices().remove(service);
    getDatabaseSession().store(project);
    return true;
}
Also used : Project(org.bimserver.models.store.Project) NewService(org.bimserver.models.store.NewService)

Example 70 with Project

use of org.bimserver.models.store.Project in project BIMserver by opensourceBIM.

the class SetRevisionTagDatabaseAction method execute.

@Override
public String execute() throws UserException, BimserverLockConflictException, BimserverDatabaseException {
    Revision revision = getRevisionByRoid(roid);
    String trimmedTag = tag.trim();
    Project project = revision.getProject();
    if (project.getParent() != null) {
        throw new UserException("Revision is not contained by a top project.");
    }
    EList<Revision> projRevs = project.getRevisions();
    revision.setTag(trimmedTag);
    getDatabaseSession().store(revision);
    EList<ConcreteRevision> concreteRevisions = revision.getConcreteRevisions();
    for (ConcreteRevision cRev : concreteRevisions) {
        EList<Revision> revisions = cRev.getRevisions();
        for (Revision vRev : revisions) {
            if (projRevs.contains(vRev)) {
                continue;
            }
            vRev.setTag(trimmedTag);
            getDatabaseSession().store(vRev);
        }
    }
    return null;
}
Also used : Project(org.bimserver.models.store.Project) Revision(org.bimserver.models.store.Revision) ConcreteRevision(org.bimserver.models.store.ConcreteRevision) ConcreteRevision(org.bimserver.models.store.ConcreteRevision) UserException(org.bimserver.shared.exceptions.UserException)

Aggregations

Project (org.bimserver.models.store.Project)87 UserException (org.bimserver.shared.exceptions.UserException)53 User (org.bimserver.models.store.User)51 DatabaseSession (org.bimserver.database.DatabaseSession)33 BimserverDatabaseException (org.bimserver.BimserverDatabaseException)25 Revision (org.bimserver.models.store.Revision)25 ServerException (org.bimserver.shared.exceptions.ServerException)23 Date (java.util.Date)19 ServiceException (org.bimserver.shared.exceptions.ServiceException)19 SProject (org.bimserver.interfaces.objects.SProject)18 ConcreteRevision (org.bimserver.models.store.ConcreteRevision)15 PackageMetaData (org.bimserver.emf.PackageMetaData)14 IOException (java.io.IOException)12 ArrayList (java.util.ArrayList)12 HashSet (java.util.HashSet)12 IfcModelInterface (org.bimserver.emf.IfcModelInterface)12 OldQuery (org.bimserver.database.OldQuery)11 PostCommitAction (org.bimserver.database.PostCommitAction)11 SConverter (org.bimserver.interfaces.SConverter)9 SUser (org.bimserver.interfaces.objects.SUser)9