use of org.bimserver.shared.QueryContext in project BIMserver by opensourceBIM.
the class CreateObjectChange method execute.
@Override
public void execute(Transaction transaction) throws UserException, BimserverLockConflictException, BimserverDatabaseException {
PackageMetaData packageMetaData = transaction.getDatabaseSession().getMetaDataManager().getPackageMetaData(transaction.getProject().getSchema());
// TODO
QueryContext queryContext = new QueryContext(transaction.getDatabaseSession(), packageMetaData, transaction.getConcreteRevision().getProject().getId(), transaction.getPreviousRevision() == null ? 1 : transaction.getPreviousRevision().getRid() + 1, -1, 0);
if (!ChangeHelper.canBeChanged(eClass)) {
throw new UserException("Only objects from the following schemas are allowed to be changed: Ifc2x3tc1 and IFC4, this object (" + eClass.getName() + ") is from the \"" + eClass.getEPackage().getName() + "\" package");
}
HashMapVirtualObject object = new HashMapVirtualObject(queryContext, eClass, oid);
if (generateGuid) {
EStructuralFeature globalIdFeature = object.eClass().getEStructuralFeature("GlobalId");
if (globalIdFeature != null) {
object.setAttribute(globalIdFeature, GuidCompressor.getNewIfcGloballyUniqueId());
} else {
throw new UserException("Cannot generate GUID for " + object.eClass().getName() + ", no GlobalId property");
}
}
transaction.created(object);
}
use of org.bimserver.shared.QueryContext 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;
}
use of org.bimserver.shared.QueryContext in project BIMserver by opensourceBIM.
the class RegenerateGeometryDatabaseAction method execute.
@Override
public Void execute() throws UserException, BimserverLockConflictException, BimserverDatabaseException, ServerException {
ProgressListener progressListener = new ProgressListener() {
@Override
public void updateProgress(String state, int percentage) {
setProgress("Generating geometry...", percentage);
}
};
GeometryGenerationReport report = new GeometryGenerationReport();
report.setOriginalIfcFileName("rerun");
report.setOriginalIfcFileSize(-1);
report.setOriginalDeserializer("rerun");
StreamingGeometryGenerator streamingGeometryGenerator = new StreamingGeometryGenerator(bimServer, progressListener, eoid, report);
Revision revision = getDatabaseSession().get(roid, OldQuery.getDefault());
ConcreteRevision concreteRevision = revision.getConcreteRevisions().get(0);
PackageMetaData packageMetaData = bimServer.getMetaDataManager().getPackageMetaData(revision.getProject().getSchema());
int highestStopId = AbstractDownloadDatabaseAction.findHighestStopRid(concreteRevision.getProject(), concreteRevision);
QueryContext queryContext = new QueryContext(getDatabaseSession(), packageMetaData, revision.getProject().getId(), revision.getId(), roid, highestStopId);
try {
GenerateGeometryResult generateGeometry = streamingGeometryGenerator.generateGeometry(uoid, getDatabaseSession(), queryContext);
for (Revision other : concreteRevision.getRevisions()) {
other.setHasGeometry(true);
}
concreteRevision.setMinBounds(generateGeometry.getMinBoundsAsVector3f());
concreteRevision.setMaxBounds(generateGeometry.getMaxBoundsAsVector3f());
ExtendedData extendedData = getDatabaseSession().create(ExtendedData.class);
File file = getDatabaseSession().create(File.class);
byte[] bytes = report.toHtml().getBytes(Charsets.UTF_8);
file.setData(bytes);
file.setFilename("geometrygenerationreport.html");
file.setMime("text/html");
file.setSize(bytes.length);
User actingUser = getUserByUoid(uoid);
extendedData.setUser(actingUser);
extendedData.setTitle("Geometry generation report (rerun)");
extendedData.setAdded(new Date());
extendedData.setSize(file.getData().length);
extendedData.setFile(file);
revision.getExtendedData().add(extendedData);
extendedData.setProject(revision.getProject());
extendedData.setRevision(revision);
getDatabaseSession().store(file);
getDatabaseSession().store(extendedData);
getDatabaseSession().store(revision);
if (extendedData.getSchema() != null) {
getDatabaseSession().store(extendedData.getSchema());
}
getDatabaseSession().store(concreteRevision);
renderEngineName = streamingGeometryGenerator.getRenderEngineName();
} catch (GeometryGeneratingException e) {
e.printStackTrace();
}
return null;
}
use of org.bimserver.shared.QueryContext in project BIMserver by opensourceBIM.
the class StreamingCheckinDatabaseAction method execute.
@Override
public ConcreteRevision execute() throws UserException, BimserverDatabaseException {
try {
bimServer.getCheckinsInProgress().put(poid, getActingUid());
if (inputStream instanceof RestartableInputStream) {
((RestartableInputStream) inputStream).restartIfAtEnd();
}
getDatabaseSession().clearPostCommitActions();
if (fileSize == -1) {
// setProgress("Deserializing IFC file...", -1);
} else {
setProgress("Deserializing IFC file...", 0);
}
authorization.canCheckin(poid);
project = getProjectByPoid(poid);
int nrConcreteRevisionsBefore = project.getConcreteRevisions().size();
User user = getUserByUoid(authorization.getUoid());
if (project == null) {
throw new UserException("Project with poid " + poid + " not found");
}
if (!authorization.hasRightsOnProjectOrSuperProjects(user, project)) {
throw new UserException("User has no rights to checkin models to this project");
}
if (!MailSystem.isValidEmailAddress(user.getUsername())) {
throw new UserException("Users must have a valid e-mail address to checkin");
}
// if (getModel() != null) {
// checkCheckSum(project);
// }
packageMetaData = bimServer.getMetaDataManager().getPackageMetaData(project.getSchema());
// TODO checksum
// TODO modelcheckers
// TODO test ifc4
// long size = 0;
// if (getModel() != null) {
// for (IdEObject idEObject : getModel().getValues()) {
// if (idEObject.eClass().getEAnnotation("hidden") == null) {
// size++;
// }
// }
// getModel().fixInverseMismatches();
// }
// for (ModelCheckerInstance modelCheckerInstance : project.getModelCheckers()) {
// if (modelCheckerInstance.isValid()) {
// ModelCheckerPlugin modelCheckerPlugin = bimServer.getPluginManager().getModelCheckerPlugin(modelCheckerInstance.getModelCheckerPluginClassName(), true);
// if (modelCheckerPlugin != null) {
// ModelChecker modelChecker = modelCheckerPlugin.createModelChecker(null);
// ModelCheckerResult result = modelChecker.check(getModel(), modelCheckerInstance.getCompiled());
// if (!result.isValid()) {
// throw new UserException("Model is not valid according to " + modelCheckerInstance.getName());
// }
// }
// }
// }
CreateRevisionResult result = createNewConcreteRevision(getDatabaseSession(), -1, project, user, comment.trim());
newRevision = result.getRevisions().get(0);
long newRoid = newRevision.getOid();
// TODO check
QueryContext queryContext = new QueryContext(getDatabaseSession(), packageMetaData, result.getConcreteRevision().getProject().getId(), result.getConcreteRevision().getId(), newRoid, -1);
AtomicLong bytesRead = new AtomicLong();
deserializer.setProgressReporter(new ByteProgressReporter() {
@Override
public void progress(long byteNumber) {
bytesRead.set(byteNumber);
if (fileSize != -1) {
int perc = (int) (100.0 * byteNumber / fileSize);
setProgress("Deserializing...", perc);
}
}
});
// This will read the full stream of objects and write to the database directly
long size = deserializer.read(inputStream, fileName, fileSize, queryContext);
Set<EClass> eClasses = deserializer.getSummaryMap().keySet();
Map<EClass, Long> startOids = getDatabaseSession().getStartOids();
if (startOids == null) {
throw new BimserverDatabaseException("No objects changed");
}
Map<EClass, Long> oidCounters = new HashMap<>();
int s = 0;
for (EClass eClass : eClasses) {
if (!DatabaseSession.perRecordVersioning(eClass)) {
s++;
}
}
ByteBuffer buffer = ByteBuffer.allocate(8 * s);
buffer.order(ByteOrder.LITTLE_ENDIAN);
for (EClass eClass : eClasses) {
if (!DatabaseSession.perRecordVersioning(eClass)) {
long oid = startOids.get(eClass);
oidCounters.put(eClass, oid);
buffer.putLong(oid);
}
}
queryContext.setOidCounters(oidCounters);
concreteRevision = result.getConcreteRevision();
concreteRevision.setOidCounters(buffer.array());
setProgress("Generating inverses/opposites...", -1);
fixInverses(packageMetaData, newRoid);
ProgressListener progressListener = new ProgressListener() {
@Override
public void updateProgress(String state, int percentage) {
setProgress("Generating geometry...", percentage);
}
};
GeometryGenerationReport report = new GeometryGenerationReport();
report.setOriginalIfcFileName(fileName);
report.setOriginalIfcFileSize(bytesRead.get());
report.setNumberOfObjects(size);
report.setOriginalDeserializer(pluginBundleVersion.getGroupId() + "." + pluginBundleVersion.getArtifactId() + ":" + pluginBundleVersion.getVersion());
StreamingGeometryGenerator geometryGenerator = new StreamingGeometryGenerator(bimServer, progressListener, -1L, report);
setProgress("Generating geometry...", 0);
GenerateGeometryResult generateGeometry = geometryGenerator.generateGeometry(getActingUid(), getDatabaseSession(), queryContext);
for (Revision other : concreteRevision.getRevisions()) {
other.setHasGeometry(true);
}
concreteRevision.setMinBounds(generateGeometry.getMinBoundsAsVector3f());
concreteRevision.setMaxBounds(generateGeometry.getMaxBoundsAsVector3f());
setProgress("Doing other stuff...", -1);
eClasses = deserializer.getSummaryMap().keySet();
s = (startOids.containsKey(GeometryPackage.eINSTANCE.getGeometryInfo()) && startOids.containsKey(GeometryPackage.eINSTANCE.getGeometryData())) ? 2 : 0;
for (EClass eClass : eClasses) {
if (!DatabaseSession.perRecordVersioning(eClass)) {
s++;
}
}
buffer = ByteBuffer.allocate(8 * s);
buffer.order(ByteOrder.LITTLE_ENDIAN);
for (EClass eClass : eClasses) {
long oid = startOids.get(eClass);
if (!DatabaseSession.perRecordVersioning(eClass)) {
buffer.putLong(oid);
}
}
if (startOids.containsKey(GeometryPackage.eINSTANCE.getGeometryInfo()) && startOids.containsKey(GeometryPackage.eINSTANCE.getGeometryData())) {
buffer.putLong(startOids.get(GeometryPackage.eINSTANCE.getGeometryInfo()));
buffer.putLong(startOids.get(GeometryPackage.eINSTANCE.getGeometryData()));
}
concreteRevision = result.getConcreteRevision();
concreteRevision.setOidCounters(buffer.array());
// Clear the cache, we don't want it to cache incomplete oidcounters
ConcreteRevisionStackFrame.clearCache(concreteRevision.getOid());
result.getConcreteRevision().setSize(size);
for (Revision revision : result.getRevisions()) {
revision.setSize((revision.getSize() == null ? 0 : revision.getSize()) + concreteRevision.getSize());
}
IfcHeader ifcHeader = deserializer.getIfcHeader();
if (ifcHeader != null) {
getDatabaseSession().store(ifcHeader);
concreteRevision.setIfcHeader(ifcHeader);
}
project.getConcreteRevisions().add(concreteRevision);
// if (getModel() != null) {
// concreteRevision.setChecksum(getModel().getModelMetaData().getChecksum());
// }
final NewRevisionAdded newRevisionAdded = getDatabaseSession().create(NewRevisionAdded.class);
newRevisionAdded.setDate(new Date());
newRevisionAdded.setExecutor(user);
final Revision revision = concreteRevision.getRevisions().get(0);
if (newServiceId != -1) {
NewService newService = getDatabaseSession().get(newServiceId, OldQuery.getDefault());
revision.getServicesLinked().add(newService);
}
concreteRevision.setSummary(new SummaryMap(packageMetaData, deserializer.getSummaryMap()).toRevisionSummary(getDatabaseSession()));
// If this revision is being created by an external service, store a link to the service in the revision
if (authorization instanceof ExplicitRightsAuthorization) {
ExplicitRightsAuthorization explicitRightsAuthorization = (ExplicitRightsAuthorization) authorization;
if (explicitRightsAuthorization.getSoid() != -1) {
Service service = getDatabaseSession().get(explicitRightsAuthorization.getSoid(), org.bimserver.database.OldQuery.getDefault());
revision.setService(service);
}
}
newRevisionAdded.setRevision(revision);
newRevisionAdded.setProject(project);
newRevisionAdded.setAccessMethod(getAccessMethod());
if (nrConcreteRevisionsBefore != 0) {
// There already was a revision, lets delete it (only when not merging)
concreteRevision.setClear(true);
}
ExtendedData extendedData = getDatabaseSession().create(ExtendedData.class);
File file = getDatabaseSession().create(File.class);
byte[] bytes = report.toHtml().getBytes(Charsets.UTF_8);
file.setData(bytes);
file.setFilename("geometrygenerationreport.html");
file.setMime("text/html");
file.setSize(bytes.length);
User actingUser = getUserByUoid(authorization.getUoid());
extendedData.setUser(actingUser);
extendedData.setTitle("Geometry generation report");
extendedData.setAdded(new Date());
extendedData.setSize(file.getData().length);
extendedData.setFile(file);
revision.getExtendedData().add(extendedData);
extendedData.setProject(revision.getProject());
extendedData.setRevision(revision);
getDatabaseSession().store(file);
getDatabaseSession().store(extendedData);
if (extendedData.getSchema() != null) {
getDatabaseSession().store(extendedData.getSchema());
}
getDatabaseSession().addPostCommitAction(new PostCommitAction() {
@Override
public void execute() throws UserException {
bimServer.getNotificationsManager().notify(new NewRevisionNotification(bimServer, project.getOid(), revision.getOid(), authorization));
}
});
getDatabaseSession().store(concreteRevision);
getDatabaseSession().store(project);
} catch (Throwable e) {
if (e instanceof BimserverDatabaseException) {
throw (BimserverDatabaseException) e;
}
if (e instanceof UserException) {
throw (UserException) e;
}
throw new UserException(e);
} finally {
bimServer.getCheckinsInProgress().remove(poid);
}
return concreteRevision;
}
Aggregations