use of org.bimserver.changes.CreateObjectChange in project BIMserver by opensourceBIM.
the class LowLevelServiceImpl method createObject.
@Override
public Long createObject(Long tid, String className, Boolean generateGuid) throws UserException, ServerException {
requireAuthenticationAndRunningServer();
try {
LongTransaction longTransaction = getBimServer().getLongTransactionManager().get(tid);
if (longTransaction == null) {
throw new UserException("No transaction with tid " + tid + " was found");
}
try {
EClass eClass = ((Database) getBimServer().getDatabase()).getEClass(longTransaction.getPackageMetaData().getEPackage().getName(), className);
Long oid = getBimServer().getDatabase().newOid(eClass);
CreateObjectChange createObject = new CreateObjectChange(className, oid, eClass, generateGuid);
longTransaction.add(createObject);
return oid;
} catch (BimserverDatabaseException e) {
throw new UserException("Unknown type: \"" + className + "\"");
}
} catch (Exception e) {
return handleException(e);
}
}
use of org.bimserver.changes.CreateObjectChange 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;
}
Aggregations