Search in sources :

Example 1 with Change

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

Aggregations

IOException (java.io.IOException)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 BimserverDatabaseException (org.bimserver.BimserverDatabaseException)1 GenerateGeometryResult (org.bimserver.GenerateGeometryResult)1 GeometryGeneratingException (org.bimserver.GeometryGeneratingException)1 SummaryMap (org.bimserver.SummaryMap)1 Change (org.bimserver.changes.Change)1 CreateObjectChange (org.bimserver.changes.CreateObjectChange)1 RemoveObjectChange (org.bimserver.changes.RemoveObjectChange)1 Transaction (org.bimserver.changes.Transaction)1 OldQuery (org.bimserver.database.OldQuery)1 PostCommitAction (org.bimserver.database.PostCommitAction)1 QueryException (org.bimserver.database.queries.om.QueryException)1 PackageMetaData (org.bimserver.emf.PackageMetaData)1 GeometryGenerationReport (org.bimserver.geometry.GeometryGenerationReport)1 StreamingGeometryGenerator (org.bimserver.geometry.StreamingGeometryGenerator)1 SConverter (org.bimserver.interfaces.SConverter)1 NewRevisionAdded (org.bimserver.models.log.NewRevisionAdded)1 ConcreteRevision (org.bimserver.models.store.ConcreteRevision)1