Search in sources :

Example 1 with QueryPart

use of org.bimserver.database.queries.om.QueryPart in project BIMserver by opensourceBIM.

the class GetNrPrimitivesDatabaseAction method execute.

@Override
public Long execute() throws UserException, BimserverLockConflictException, BimserverDatabaseException, ServerException {
    Revision revision = getDatabaseSession().get(roid, OldQuery.getDefault());
    PackageMetaData packageMetaData = bimServer.getMetaDataManager().getPackageMetaData(revision.getProject().getSchema());
    if (packageMetaData == null) {
        throw new UserException("Schema not fond");
    }
    try {
        Query query = new Query("test", packageMetaData);
        QueryPart queryPart = query.createQueryPart();
        queryPart.addType(packageMetaData.getEClassIncludingDependencies("GeometryInfo"), true);
        QueryObjectProvider queryObjectProvider = new QueryObjectProvider(getDatabaseSession(), bimServer, query, java.util.Collections.singleton(roid), packageMetaData);
        HashMapVirtualObject next = queryObjectProvider.next();
        long totalPrimitives = 0;
        while (next != null) {
            int nrPrimitives = (int) next.get("primitiveCount");
            totalPrimitives += nrPrimitives;
            next = queryObjectProvider.next();
        }
        return totalPrimitives;
    } catch (QueryException e) {
        e.printStackTrace();
    } catch (JsonParseException e) {
        e.printStackTrace();
    } catch (JsonMappingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : OldQuery(org.bimserver.database.OldQuery) Query(org.bimserver.database.queries.om.Query) PackageMetaData(org.bimserver.emf.PackageMetaData) QueryPart(org.bimserver.database.queries.om.QueryPart) IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) QueryException(org.bimserver.database.queries.om.QueryException) Revision(org.bimserver.models.store.Revision) HashMapVirtualObject(org.bimserver.shared.HashMapVirtualObject) QueryObjectProvider(org.bimserver.database.queries.QueryObjectProvider) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) UserException(org.bimserver.shared.exceptions.UserException)

Example 2 with QueryPart

use of org.bimserver.database.queries.om.QueryPart in project BIMserver by opensourceBIM.

the class StreamingCheckinDatabaseAction method getByOid.

public HashMapVirtualObject getByOid(PackageMetaData packageMetaData, DatabaseSession databaseSession, long roid, long oid) throws JsonParseException, JsonMappingException, IOException, QueryException, BimserverDatabaseException {
    Query query = new Query("test", packageMetaData);
    QueryPart queryPart = query.createQueryPart();
    queryPart.addOid(oid);
    QueryObjectProvider queryObjectProvider = new QueryObjectProvider(databaseSession, bimServer, query, Collections.singleton(roid), packageMetaData);
    HashMapVirtualObject first = queryObjectProvider.next();
    return first;
}
Also used : OldQuery(org.bimserver.database.OldQuery) Query(org.bimserver.database.queries.om.Query) HashMapVirtualObject(org.bimserver.shared.HashMapVirtualObject) QueryPart(org.bimserver.database.queries.om.QueryPart) QueryObjectProvider(org.bimserver.database.queries.QueryObjectProvider)

Example 3 with QueryPart

use of org.bimserver.database.queries.om.QueryPart 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)

Example 4 with QueryPart

use of org.bimserver.database.queries.om.QueryPart in project BIMserver by opensourceBIM.

the class AddReferenceChange 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();
        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");
    }
    if (object == null) {
        throw new UserException("No object of type \"" + eClass.getName() + "\" with oid " + oid + " found in project with pid " + transaction.getProject().getId());
    }
    EReference eReference = packageMetaData.getEReference(eClass.getName(), referenceName);
    if (eReference == null) {
        throw new UserException("No reference with the name \"" + referenceName + "\" found in class \"" + eClass.getName() + "\"");
    }
    if (!eReference.isMany()) {
        throw new UserException("Reference is not of type 'many'");
    }
    EClass eClassForOid = transaction.getDatabaseSession().getEClassForOid(referenceOid);
    object.addReference(eReference, eClassForOid, referenceOid);
    EReference inverseOrOpposite = packageMetaData.getInverseOrOpposite(eClassForOid, eReference);
    if (inverseOrOpposite != null) {
        HashMapVirtualObject referencedObject = transaction.get(referenceOid);
        if (referencedObject == null) {
            Query query = new Query(packageMetaData);
            QueryPart queryPart = query.createQueryPart();
            queryPart.addOid(referenceOid);
            QueryObjectProvider queryObjectProvider = new QueryObjectProvider(transaction.getDatabaseSession(), transaction.getBimServer(), query, Collections.singleton(transaction.getPreviousRevision().getOid()), packageMetaData);
            referencedObject = queryObjectProvider.next();
        }
        if (inverseOrOpposite.isMany()) {
            referencedObject.addReference(inverseOrOpposite, eClassForOid, oid);
        } else {
            referencedObject.setReference(inverseOrOpposite, oid);
        }
        transaction.updated(referencedObject);
    }
}
Also used : EClass(org.eclipse.emf.ecore.EClass) HashMapVirtualObject(org.bimserver.shared.HashMapVirtualObject) Query(org.bimserver.database.queries.om.Query) PackageMetaData(org.bimserver.emf.PackageMetaData) QueryPart(org.bimserver.database.queries.om.QueryPart) QueryObjectProvider(org.bimserver.database.queries.QueryObjectProvider) UserException(org.bimserver.shared.exceptions.UserException) EReference(org.eclipse.emf.ecore.EReference)

Example 5 with QueryPart

use of org.bimserver.database.queries.om.QueryPart in project BIMserver by opensourceBIM.

the class RemoveAllReferencesChange method execute.

@SuppressWarnings("rawtypes")
@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");
    }
    if (object == null) {
        throw new UserException("No object of type \"" + eClass.getName() + "\" with oid " + oid + " found in project with pid " + transaction.getProject().getId());
    }
    EReference eReference = packageMetaData.getEReference(eClass.getName(), referenceName);
    if (eReference == null) {
        throw new UserException("No reference with the name \"" + referenceName + "\" found in class \"" + eClass.getName() + "\"");
    }
    if (!eReference.isMany()) {
        throw new UserException("Reference is not of type 'many'");
    }
    List list = (List) object.get(eReference.getName());
    while (!list.isEmpty()) {
        list.remove(0);
    }
}
Also used : EClass(org.eclipse.emf.ecore.EClass) Query(org.bimserver.database.queries.om.Query) HashMapVirtualObject(org.bimserver.shared.HashMapVirtualObject) PackageMetaData(org.bimserver.emf.PackageMetaData) QueryPart(org.bimserver.database.queries.om.QueryPart) QueryObjectProvider(org.bimserver.database.queries.QueryObjectProvider) List(java.util.List) UserException(org.bimserver.shared.exceptions.UserException) EReference(org.eclipse.emf.ecore.EReference)

Aggregations

QueryPart (org.bimserver.database.queries.om.QueryPart)36 Query (org.bimserver.database.queries.om.Query)33 QueryObjectProvider (org.bimserver.database.queries.QueryObjectProvider)23 HashMapVirtualObject (org.bimserver.shared.HashMapVirtualObject)22 UserException (org.bimserver.shared.exceptions.UserException)18 EClass (org.eclipse.emf.ecore.EClass)17 Include (org.bimserver.database.queries.om.Include)16 PackageMetaData (org.bimserver.emf.PackageMetaData)16 IOException (java.io.IOException)12 List (java.util.List)10 JsonQueryObjectModelConverter (org.bimserver.database.queries.om.JsonQueryObjectModelConverter)10 OldQuery (org.bimserver.database.OldQuery)9 QueryException (org.bimserver.database.queries.om.QueryException)9 EReference (org.eclipse.emf.ecore.EReference)8 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)7 HashMap (java.util.HashMap)7 HashSet (java.util.HashSet)7 BimserverDatabaseException (org.bimserver.BimserverDatabaseException)6 ServiceException (org.bimserver.shared.exceptions.ServiceException)6 IfcModelInterfaceException (org.bimserver.emf.IfcModelInterfaceException)5