use of org.bimserver.shared.HashMapVirtualObject in project BIMserver by opensourceBIM.
the class RemoveObjectChange method execute.
@Override
public void execute(Transaction transaction) throws UserException, BimserverLockConflictException, BimserverDatabaseException, IOException, QueryException {
PackageMetaData packageMetaData = transaction.getDatabaseSession().getMetaDataManager().getPackageMetaData(transaction.getProject().getSchema());
HashMapVirtualObject object = transaction.get(oid);
if (object == null) {
Query query = new Query(packageMetaData);
QueryPart queryPart = query.createQueryPart();
queryPart.addOid(oid);
QueryObjectProvider queryObjectProvider = new QueryObjectProvider(transaction.getDatabaseSession(), transaction.getBimServer(), query, Collections.singleton(transaction.getPreviousRevision().getOid()), packageMetaData);
object = queryObjectProvider.next();
}
if (object == null) {
throw new UserException("Object with oid " + oid + " not found");
}
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");
}
transaction.deleted(object);
}
use of org.bimserver.shared.HashMapVirtualObject in project BIMserver by opensourceBIM.
the class SetAttributeChangeAtIndex method execute.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void execute(Transaction transaction) throws UserException, BimserverLockConflictException, BimserverDatabaseException, IOException, QueryException {
PackageMetaData packageMetaData = transaction.getDatabaseSession().getMetaDataManager().getPackageMetaData(transaction.getProject().getSchema());
Query query = new Query(packageMetaData);
QueryPart queryPart = query.createQueryPart();
queryPart.addOid(oid);
HashMapVirtualObject object = transaction.get(oid);
if (object == null) {
QueryObjectProvider queryObjectProvider = new QueryObjectProvider(transaction.getDatabaseSession(), transaction.getBimServer(), query, Collections.singleton(transaction.getPreviousRevision().getOid()), packageMetaData);
object = queryObjectProvider.next();
transaction.updated(object);
}
EClass eClass = transaction.getDatabaseSession().getEClassForOid(oid);
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");
}
object = transaction.get(oid);
if (object == null) {
throw new UserException("No object of type \"" + eClass.getName() + "\" with oid " + oid + " found in project with pid " + transaction.getProject().getId());
}
EAttribute eAttribute = packageMetaData.getEAttribute(eClass.getName(), attributeName);
if (eAttribute == null) {
throw new UserException("No attribute with the name \"" + attributeName + "\" found in class \"" + eClass.getName() + "\"");
}
if (!eAttribute.isMany()) {
throw new UserException("Attribute is not of type 'many'");
}
if (value instanceof List) {
throw new UserException("Value is list, only primitive values allowed.");
// If this was a two-dimensional array we would have a structural feature, not an attribute referring to the inner array
} else {
if (eAttribute.getEType() instanceof EEnum) {
EEnum eEnum = (EEnum) eAttribute.getEType();
object.setListItem(eAttribute, index, eEnum.getEEnumLiteral(((String) value).toUpperCase()).getInstance());
} else {
object.setListItem(eAttribute, index, value);
}
if (value instanceof Double) {
EStructuralFeature asStringAttribute = object.eClass().getEStructuralFeature(attributeName + "AsString");
object.setListItem(asStringAttribute, index, String.valueOf((Double) value));
}
}
}
use of org.bimserver.shared.HashMapVirtualObject in project BIMserver by opensourceBIM.
the class DownloadByNewJsonQueryDatabaseAction method execute.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public IfcModelInterface execute() throws UserException, BimserverLockConflictException, BimserverDatabaseException {
List<String> projectNames = new ArrayList<>();
PluginConfiguration serializerPluginConfiguration = getDatabaseSession().get(StorePackage.eINSTANCE.getPluginConfiguration(), serializerOid, OldQuery.getDefault());
SerializerPlugin serializerPlugin = (SerializerPlugin) getBimServer().getPluginManager().getPlugin(serializerPluginConfiguration.getPluginDescriptor().getPluginClassName(), true);
Set<String> geometryFields = serializerPlugin.getRequiredGeometryFields();
setProgress("Querying database...", -1);
for (long roid : roids) {
Revision revision = getDatabaseSession().get(StorePackage.eINSTANCE.getRevision(), roid, OldQuery.getDefault());
projectNames.add(revision.getProject().getName() + "." + revision.getId());
}
String name = Joiner.on("-").join(projectNames);
PackageMetaData lastPackageMetaData = null;
Project lastProject = null;
IfcModelSet ifcModelSet = new IfcModelSet();
Map<Integer, Long> pidRoidMap = new HashMap<>();
Set<CanInclude> updatedIncludes = new HashSet<>();
for (long roid : roids) {
Revision revision = getDatabaseSession().get(StorePackage.eINSTANCE.getRevision(), roid, OldQuery.getDefault());
lastProject = revision.getProject();
PackageMetaData packageMetaData = getBimServer().getMetaDataManager().getPackageMetaData(revision.getProject().getSchema());
lastPackageMetaData = packageMetaData;
JsonQueryObjectModelConverter converter = new JsonQueryObjectModelConverter(packageMetaData);
try {
ObjectNode queryObject = OBJECT_MAPPER.readValue(json, ObjectNode.class);
converter.setCopyExternalDefines(true);
Query query = converter.parseJson("query", (ObjectNode) queryObject);
// We now have the original user query, we'll amend it a little bit to include geometry, but only if the serializer requires certain fields
// TODO only checking the base level of the query now, should check recursive and possibly more
// System.out.println(new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(new JsonQueryObjectModelConverter(packageMetaData).toJson(query)));
pidRoidMap.put(revision.getProject().getId(), roid);
IfcModelInterface ifcModel = getDatabaseSession().createServerModel(packageMetaData, pidRoidMap);
ifcModelSet.add(ifcModel);
Query secondQuery = new Query("second", query.getPackageMetaData());
QueryPart queryPart2 = secondQuery.createQueryPart();
if (geometryFields != null) {
for (String defineName : query.getDefines().keySet()) {
Include define = query.getDefine(defineName);
apply(geometryFields, packageMetaData, define, updatedIncludes);
}
for (QueryPart queryPart : query.getQueryParts()) {
apply(geometryFields, packageMetaData, queryPart, updatedIncludes);
if (queryPart.hasReferences()) {
for (Reference reference : queryPart.getReferences()) {
apply(geometryFields, packageMetaData, reference.getInclude(), updatedIncludes);
}
}
if (queryPart.hasIncludes()) {
apply(geometryFields, packageMetaData, queryPart, updatedIncludes);
}
if (queryPart.hasTypes()) {
for (TypeDef typeDef : queryPart.getTypes()) {
if (packageMetaData.getEClass("IfcProduct").isSuperTypeOf(typeDef.geteClass())) {
Include include = queryPart.createInclude();
applyFields(geometryFields, new TypeDef(packageMetaData.getEClass("IfcProduct"), true), include);
}
}
}
if (queryPart.isIncludeAllFields()) {
Include newInclude = queryPart.createInclude();
applyFields(geometryFields, new TypeDef(packageMetaData.getEClass("IfcProduct"), true), newInclude);
}
}
}
QueryObjectProvider queryObjectProvider = new QueryObjectProvider(getDatabaseSession(), getBimServer(), query, Collections.singleton(roid), packageMetaData);
HashMapVirtualObject next = queryObjectProvider.next();
while (next != null) {
IdEObject newObject = packageMetaData.create(next.eClass());
IdEObjectImpl idEObjectImpl = (IdEObjectImpl) newObject;
idEObjectImpl.setPid(revision.getProject().getId());
idEObjectImpl.setOid(next.getOid());
for (EAttribute eAttribute : newObject.eClass().getEAllAttributes()) {
Object value = next.eGet(eAttribute);
if (eAttribute.isMany()) {
List<?> list = (List<?>) value;
if (list != null) {
AbstractEList targetList = (AbstractEList) newObject.eGet(eAttribute);
for (Object item : list) {
targetList.addUnique(item);
}
}
} else {
if (value != null || eAttribute.isUnsettable()) {
newObject.eSet(eAttribute, value);
}
}
}
ifcModel.add(next.getOid(), newObject);
queryPart2.addOid(next.getOid());
next = queryObjectProvider.next();
}
if (geometryFields != null) {
for (QueryPart queryPart : secondQuery.getQueryParts()) {
apply(geometryFields, packageMetaData, queryPart, updatedIncludes);
// if (queryPart.hasReferences()) {
// for (Reference reference : queryPart.getReferences()) {
// apply(geometryFields, packageMetaData, reference.getInclude(), updatedIncludes);
// }
// }
// if (queryPart.hasIncludes()) {
// apply(geometryFields, packageMetaData, queryPart, updatedIncludes);
// }
// if (queryPart.hasTypes()) {
// for (TypeDef typeDef : queryPart.getTypes()) {
// if (packageMetaData.getEClass("IfcProduct").isSuperTypeOf(typeDef.geteClass())) {
// Include include = queryPart.createInclude();
// applyFields(geometryFields, new TypeDef(packageMetaData.getEClass("IfcProduct"), true), include);
// }
// }
// }
// if (queryPart.isIncludeAllFields()) {
// Include newInclude = queryPart.createInclude();
// applyFields(geometryFields, new TypeDef(packageMetaData.getEClass("IfcProduct"), true), newInclude);
// }
}
}
queryObjectProvider = new QueryObjectProvider(getDatabaseSession(), getBimServer(), secondQuery, Collections.singleton(roid), packageMetaData);
System.out.println(converter.toJson(secondQuery));
next = queryObjectProvider.next();
while (next != null) {
IdEObject idEObject = ifcModel.get(next.getOid());
if (idEObject == null) {
throw new RuntimeException("Object not found");
}
if (idEObject.eClass() != next.eClass()) {
// Something is wrong
throw new RuntimeException("Classes not the same");
}
for (EReference eReference : idEObject.eClass().getEAllReferences()) {
if (eReference.isMany()) {
List refOids = (List) next.eGet(eReference);
AbstractEList<IdEObject> list = (AbstractEList<IdEObject>) idEObject.eGet(eReference);
if (refOids != null) {
for (Object refOid : refOids) {
if (refOid instanceof Long) {
IdEObject ref = ifcModel.get((long) refOid);
if (ref != null) {
if (eReference.isUnique()) {
list.add(ref);
} else {
list.addUnique(ref);
}
// } else {
// throw new BimserverDatabaseException("Could not find reference to " + eReference.getName() + " " + refOid);
}
} else if (refOid instanceof HashMapWrappedVirtualObject) {
// IdEObject ref = ifcModel.get(((HashMapWrappedVirtualObject) refOid).get);
// if (ref != null) {
// list.add(ref);
// }
} else if (refOid instanceof HashMapVirtualObject) {
HashMapVirtualObject hashMapVirtualObject = (HashMapVirtualObject) refOid;
IdEObject listObject = packageMetaData.create(hashMapVirtualObject.eClass());
List subList = (List<?>) hashMapVirtualObject.get("List");
List newList = (List<?>) listObject.eGet(listObject.eClass().getEStructuralFeature("List"));
for (Object o : subList) {
if (o instanceof HashMapWrappedVirtualObject) {
newList.add(convertWrapped(revision, ifcModel, (HashMapWrappedVirtualObject) o));
} else if (o instanceof Long) {
LOGGER.warn("TODO");
// TODO
} else {
newList.add(o);
}
}
list.addUnique(listObject);
} else {
throw new BimserverDatabaseException("Unimplemented");
}
}
}
} else {
Object r = next.eGet(eReference);
if (r instanceof Long) {
long refOid = (Long) r;
IdEObject referred = ifcModel.get(refOid);
if (referred == null) {
System.out.println(eReference.getName() + " " + refOid);
}
idEObject.eSet(eReference, referred);
} else if (r instanceof HashMapWrappedVirtualObject) {
idEObject.eSet(eReference, convertWrapped(revision, ifcModel, (HashMapWrappedVirtualObject) r));
} else if (r instanceof HashMapVirtualObject) {
throw new BimserverDatabaseException("Unimplemented");
} else if (r == null) {
} else {
throw new BimserverDatabaseException("Unimplemented");
}
}
}
next = queryObjectProvider.next();
}
ifcModel.getModelMetaData().setName(name);
ifcModel.getModelMetaData().setRevisionId(1);
if (getAuthorization().getUoid() != -1) {
ifcModel.getModelMetaData().setAuthorizedUser(getUserByUoid(getAuthorization().getUoid()).getName());
}
ifcModel.getModelMetaData().setDate(new Date());
} catch (Throwable e) {
throw new BimserverDatabaseException(e);
}
}
IfcModelInterface ifcModel = getDatabaseSession().createServerModel(lastPackageMetaData, pidRoidMap);
if (ifcModelSet.size() > 1) {
setProgress("Merging IFC data...", -1);
try {
ifcModel = getBimServer().getMergerFactory().createMerger(getDatabaseSession(), getAuthorization().getUoid()).merge(lastProject, ifcModelSet, new ModelHelper(getBimServer().getMetaDataManager(), ifcModel));
} catch (MergeException e) {
throw new UserException(e);
}
} else {
ifcModel = ifcModelSet.iterator().next();
}
ifcModel.getModelMetaData().setName(name);
// ifcModel.getModelMetaData().setRevisionId(project.getRevisions().indexOf(virtualRevision) + 1);
if (getAuthorization().getUoid() != -1) {
ifcModel.getModelMetaData().setAuthorizedUser(getUserByUoid(getAuthorization().getUoid()).getName());
}
ifcModel.getModelMetaData().setDate(new Date());
return ifcModel;
// for (Long roid : roids) {
// Revision virtualRevision = getRevisionByRoid(roid);
// pidRoidMap.put(virtualRevision.getProject().getId(), virtualRevision.getOid());
// project = virtualRevision.getProject();
// name += project.getName() + "-" + virtualRevision.getId() + "-";
// try {
// getAuthorization().canDownload(roid);
// } catch (UserException e) {
// if (!getAuthorization().hasRightsOnProjectOrSuperProjectsOrSubProjects(user, project)) {
// throw new UserException("User has insufficient rights to download revisions from this project");
// }
// if (!getAuthorization().hasRightsOnProjectOrSuperProjectsOrSubProjects(user, project)) {
// throw new UserException("User has insufficient rights to download revisions from this project");
// }
// }
// int size = 0;
//
// for (ConcreteRevision concreteRevision : virtualRevision.getConcreteRevisions()) {
// try {
// int highestStopId = findHighestStopRid(project, concreteRevision);
//
// PackageMetaData packageMetaData = getBimServer().getMetaDataManager().getPackageMetaData(concreteRevision.getProject().getSchema());
// lastPackageMetaData = packageMetaData;
// IfcModelInterface subModel = new ServerIfcModel(packageMetaData, pidRoidMap, getDatabaseSession());
//
// OldQuery databaseQuery = new OldQuery(packageMetaData, concreteRevision.getProject().getId(), concreteRevision.getId(), virtualRevision.getOid(), null, Deep.NO, highestStopId);
// databaseQuery.updateOidCounters(concreteRevision, getDatabaseSession());
// JsonObject queryObject = (JsonObject)query;
// JsonArray queries = queryObject.get("queries").getAsJsonArray();
// for (JsonElement queryElement : queries) {
// processQueryPart(packageMetaData, queryObject, (JsonObject) queryElement, subModel, databaseQuery);
// }
//
// size += subModel.size();
// subModel.getModelMetaData().setDate(concreteRevision.getDate());
// subModel.fixInverseMismatches();
// checkGeometry(serializerPluginConfiguration, getBimServer().getPluginManager(), subModel, project, concreteRevision, virtualRevision);
// ifcModelSet.add(subModel);
// } catch (GeometryGeneratingException | IfcModelInterfaceException e) {
// throw new UserException(e);
// }
// }
//
// }
// TODO check, double merging??
// IfcModelInterface ifcModel = new BasicIfcModel(lastPackageMetaData, pidRoidMap);
// if (ifcModelSet.size() > 1) {
// try {
// ifcModel = getBimServer().getMergerFactory().createMerger(getDatabaseSession(), getAuthorization().getUoid()).merge(project, ifcModelSet, new ModelHelper(getBimServer().getMetaDataManager(), ifcModel));
// } catch (MergeException e) {
// throw new UserException(e);
// }
// } else {
// ifcModel = ifcModelSet.iterator().next();
// }
// if (name.endsWith("-")) {
// name = name.substring(0, name.length()-1);
// }
}
use of org.bimserver.shared.HashMapVirtualObject in project BIMserver by opensourceBIM.
the class SharedJsonStreamingSerializer method write.
private void write(MinimalVirtualObject object) throws IOException {
if (object.eClass().getEAnnotation("wrapped") != null) {
EStructuralFeature wrappedFeature = object.eClass().getEStructuralFeature("wrappedValue");
print("{");
print("\"_t\":\"" + object.eClass().getName() + "\",");
print("\"_v\":");
Object wrappedValue = object.eGet(wrappedFeature);
if (wrappedValue instanceof List) {
print("[");
List<?> list = (List<?>) wrappedValue;
boolean f = true;
for (Object o : list) {
if (!f) {
print(", ");
}
f = false;
writePrimitive(wrappedFeature, o);
}
print("]");
} else {
writePrimitive(wrappedFeature, wrappedValue);
}
print("}");
} else if (object instanceof HashMapVirtualObject) {
EStructuralFeature eStructuralFeature = object.eClass().getEStructuralFeature("List");
if (eStructuralFeature != null) {
print("[");
List<?> l = (List<?>) object.eGet(eStructuralFeature);
boolean f = true;
for (Object o : l) {
if (!f) {
print(", ");
}
f = false;
if (eStructuralFeature instanceof EReference) {
if (o instanceof Long) {
long ref = (Long) o;
print("{\"_r\":" + ref + ",\"_t\":\"" + objectProvider.getEClassForOid(ref).getName() + "\"}");
} else {
writeWrapper((MinimalVirtualObject) o);
}
} else {
print(o.toString());
}
}
print("]");
} else {
print("" + ((HashMapVirtualObject) object).getOid());
}
} else if (object instanceof HashMapWrappedVirtualObject) {
writeWrapper(object);
}
}
use of org.bimserver.shared.HashMapVirtualObject in project BIMserver by opensourceBIM.
the class SharedJsonStreamingSerializer method writeObject.
private void writeObject(HashMapVirtualObject object) throws IOException, BimserverDatabaseException {
// if (((IdEObjectImpl) object).getLoadingState() != State.LOADED) {
// print("{");
// print("\"_i\":" + object.getOid() + ",");
// print("\"_t\":\"" + object.eClass().getName() + "\",");
// print("\"_s\":0");
// print("}\n");
// } else {
print("{");
print("\"_i\":" + object.getOid() + ",");
print("\"_u\":\"" + object.getUuid().toString() + "\",");
print("\"_r\":\"" + object.getRid() + "\",");
print("\"_t\":\"" + object.eClass().getName() + "\",");
print("\"_s\":1");
for (EStructuralFeature eStructuralFeature : object.eClass().getEAllStructuralFeatures()) {
if (eStructuralFeature.getEAnnotation("nolazyload") == null && (eStructuralFeature.getEAnnotation("hidden") == null || includeHidden)) {
if (eStructuralFeature instanceof EReference) {
Object value = object.eGet(eStructuralFeature);
if (value != null) {
if (eStructuralFeature.isMany()) {
List<?> list = (List<?>) value;
if (SERIALIZE_EMPTY_LISTS || !list.isEmpty()) {
print(",");
int wrapped = 0;
int referred = 0;
for (Object o : list) {
if (o instanceof Long) {
referred++;
} else if (o instanceof HashMapWrappedVirtualObject) {
wrapped++;
}
}
if (wrapped == 0 && referred != 0) {
print("\"_r" + eStructuralFeature.getName() + "\":[");
} else if (wrapped != 0 && referred == 0) {
print("\"_e" + eStructuralFeature.getName() + "\":[");
} else if (wrapped == 0 && referred == 0) {
print("\"_e" + eStructuralFeature.getName() + "\":[");
// should not happen
} else {
// both, this can occur,
// for example
// IfcTrimmedCurve.Trim1
print("\"_e" + eStructuralFeature.getName() + "\":[");
}
boolean f = true;
for (Object o : list) {
if (o == null) {
LOGGER.info("Unexpeced null in list " + object.eClass().getName() + ":" + object.getOid() + "." + eStructuralFeature.getName());
} else {
if (!f) {
print(",");
} else {
f = false;
}
if (o instanceof Long) {
long ref = (Long) o;
if (wrapped != 0 && referred != 0) {
// Special
// situation,
// where we have
// to construct
// an object
// around the
// OID to make
// it
// distinguishable
// from embedded
// objects
print("{");
print("\"_i\":");
print("" + ref + ",");
print("\"_t\":");
print("\"" + object.getReusable().getDatabaseInterface().getEClassForOid(ref).getName() + "\"");
print("}");
} else {
print("{");
print("\"_i\":");
print("" + ref + ",");
print("\"_t\":");
print("\"" + object.getReusable().getDatabaseInterface().getEClassForOid(ref).getName() + "\"");
print("}");
}
} else if (o instanceof HashMapWrappedVirtualObject) {
write((HashMapWrappedVirtualObject) o);
} else if (o instanceof HashMapVirtualObject) {
write((HashMapVirtualObject) o);
} else if (eStructuralFeature.getEAnnotation("twodimensionalarray") != null) {
EClass type = (EClass) eStructuralFeature.getEType();
EStructuralFeature listFeature = type.getEStructuralFeature("List");
List<?> listX = (List<?>) (((HashMapVirtualObject) o).eGet(listFeature));
print("[");
boolean fq = true;
for (Object k : listX) {
if (!fq) {
print(",");
}
fq = false;
if (k instanceof HashMapWrappedVirtualObject) {
write((HashMapWrappedVirtualObject) k);
} else {
print("\"" + k + "\"");
}
}
print("]");
} else {
LOGGER.info("Unimplemented " + o.getClass() + " " + o);
}
}
}
print("]");
}
} else {
print(",");
if (value instanceof Long) {
long ref = (Long) value;
print("\"_r" + eStructuralFeature.getName() + "\":{\"_i\":" + ref + ",\"_t\":\"" + object.getReusable().getDatabaseInterface().getEClassForOid(ref).getName() + "\"}");
} else if (value instanceof HashMapWrappedVirtualObject) {
print("\"_e" + eStructuralFeature.getName() + "\":");
HashMapWrappedVirtualObject hashMapWrappedVirtualObject = (HashMapWrappedVirtualObject) value;
write(hashMapWrappedVirtualObject);
} else {
// print("\"" + eStructuralFeature.getName() + "\":");
// writePrimitive(eStructuralFeature, ((IfcGloballyUniqueId) ref).getWrappedValue());
}
}
}
} else {
Object value = object.eGet(eStructuralFeature);
if (value != null) {
if (eStructuralFeature.isMany()) {
List<?> list = (List<?>) value;
if (SERIALIZE_EMPTY_LISTS || !list.isEmpty()) {
print(",");
print("\"" + eStructuralFeature.getName() + "\":[");
boolean f = true;
for (Object o : list) {
if (!f) {
print(",");
} else {
f = false;
}
writePrimitive(eStructuralFeature, o);
}
print("]");
}
} else {
print(",");
print("\"" + eStructuralFeature.getName() + "\":");
writePrimitive(eStructuralFeature, value);
}
}
}
}
}
print("}\n");
// }
}
Aggregations