Search in sources :

Example 1 with QueryTypeStackFrame

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();
    }
}
Also used : OldQuery(org.bimserver.database.OldQuery) Query(org.bimserver.database.queries.om.Query) HashMap(java.util.HashMap) QueryPart(org.bimserver.database.queries.om.QueryPart) Include(org.bimserver.database.queries.om.Include) EClass(org.eclipse.emf.ecore.EClass) HashMapVirtualObject(org.bimserver.shared.HashMapVirtualObject) QueryObjectProvider(org.bimserver.database.queries.QueryObjectProvider) AtomicLong(java.util.concurrent.atomic.AtomicLong) QueryTypeStackFrame(org.bimserver.database.queries.QueryTypeStackFrame) HashMapVirtualObject(org.bimserver.shared.HashMapVirtualObject) List(java.util.List) EReference(org.eclipse.emf.ecore.EReference) HashSet(java.util.HashSet)

Aggregations

HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 OldQuery (org.bimserver.database.OldQuery)1 QueryObjectProvider (org.bimserver.database.queries.QueryObjectProvider)1 QueryTypeStackFrame (org.bimserver.database.queries.QueryTypeStackFrame)1 Include (org.bimserver.database.queries.om.Include)1 Query (org.bimserver.database.queries.om.Query)1 QueryPart (org.bimserver.database.queries.om.QueryPart)1 HashMapVirtualObject (org.bimserver.shared.HashMapVirtualObject)1 EClass (org.eclipse.emf.ecore.EClass)1 EReference (org.eclipse.emf.ecore.EReference)1