use of org.bimserver.database.queries.QueryTypeStackFrame in project BIMserver by opensourceBIM.
the class StreamingCheckinDatabaseAction method fixInverses.
@SuppressWarnings("unchecked")
private void fixInverses(PackageMetaData packageMetaData, long newRoid) throws QueryException, JsonParseException, JsonMappingException, IOException, BimserverDatabaseException {
// TODO remove cache, this is essentially a big part of the model in memory again
Map<Long, HashMapVirtualObject> cache = new HashMap<Long, HashMapVirtualObject>();
Query query = new Query("Inverses fixer", packageMetaData);
int nrTypes = 0;
Set<EClass> uniqueTypes = new HashSet<>();
for (EClass eClass : deserializer.getSummaryMap().keySet()) {
if (packageMetaData.hasInverses(eClass)) {
QueryPart queryPart = query.createQueryPart();
queryPart.addType(eClass, true);
uniqueTypes.add(eClass);
nrTypes++;
for (EReference eReference : packageMetaData.getAllHasInverseReferences(eClass)) {
Include include = queryPart.createInclude();
include.addType(eClass, true);
include.addField(eReference.getName());
}
}
}
QueryObjectProvider queryObjectProvider = new QueryObjectProvider(getDatabaseSession(), bimServer, query, Collections.singleton(newRoid), packageMetaData);
HashMapVirtualObject next = queryObjectProvider.next();
EClass lastEClass = null;
int currentType = 0;
while (next != null) {
if (next.eClass() != lastEClass && uniqueTypes.contains(next.eClass()) && queryObjectProvider.getStackFrame() instanceof QueryTypeStackFrame) {
lastEClass = next.eClass();
currentType++;
setProgress("Generating inverses", (100 * currentType / nrTypes));
}
if (packageMetaData.hasInverses(next.eClass())) {
for (EReference eReference : packageMetaData.getAllHasInverseReferences(next.eClass())) {
Object reference = next.eGet(eReference);
if (reference != null) {
if (eReference.isMany()) {
List<Long> references = (List<Long>) reference;
for (Long refOid : references) {
fixInverses(packageMetaData, newRoid, cache, next, eReference, refOid);
}
} else {
fixInverses(packageMetaData, newRoid, cache, next, eReference, (Long) reference);
}
}
}
}
next = queryObjectProvider.next();
}
setProgress("Storing data", -1);
for (HashMapVirtualObject referencedObject : cache.values()) {
referencedObject.saveOverwrite();
}
}
Aggregations