Search in sources :

Example 51 with IdEObject

use of org.bimserver.emf.IdEObject in project BIMserver by opensourceBIM.

the class DatabaseSession method storeDeep.

private void storeDeep(IdEObject object, Set<IdEObject> done) throws BimserverDatabaseException {
    if (object == null || done.contains(object)) {
        return;
    }
    done.add(object);
    store(object);
    for (EReference eReference : object.eClass().getEAllReferences()) {
        if (eReference.isMany()) {
            List<?> list = (List<?>) object.eGet(eReference);
            for (Object v : list) {
                storeDeep((IdEObject) v, done);
            }
        } else {
            IdEObject reference = (IdEObject) object.eGet(eReference);
            storeDeep(reference, done);
        }
    }
}
Also used : IdEObject(org.bimserver.emf.IdEObject) List(java.util.List) BasicEList(org.eclipse.emf.common.util.BasicEList) ArrayList(java.util.ArrayList) EList(org.eclipse.emf.common.util.EList) AbstractEList(org.eclipse.emf.common.util.AbstractEList) VirtualObject(org.bimserver.shared.VirtualObject) HashMapVirtualObject(org.bimserver.shared.HashMapVirtualObject) EObject(org.eclipse.emf.ecore.EObject) IdEObject(org.bimserver.emf.IdEObject) EReference(org.eclipse.emf.ecore.EReference)

Example 52 with IdEObject

use of org.bimserver.emf.IdEObject in project BIMserver by opensourceBIM.

the class DatabaseSession method writeReference.

private void writeReference(IdEObject object, Object value, ByteBuffer buffer, EStructuralFeature feature) throws BimserverDatabaseException {
    IdEObject idEObject = (IdEObject) value;
    if (idEObject.getOid() < 0) {
        throw new BimserverDatabaseException("Writing a reference with oid " + idEObject.getOid() + ", this is not supposed to happen, referenced: " + idEObject.getOid() + " " + value + " from " + object.getOid() + " " + object);
    }
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    buffer.putLong(idEObject.getOid());
    buffer.order(ByteOrder.BIG_ENDIAN);
}
Also used : BimserverDatabaseException(org.bimserver.BimserverDatabaseException) IdEObject(org.bimserver.emf.IdEObject)

Example 53 with IdEObject

use of org.bimserver.emf.IdEObject in project BIMserver by opensourceBIM.

the class DatabaseSession method writeEmbeddedValue.

private void writeEmbeddedValue(int pid, int rid, Object value, ByteBuffer buffer, PackageMetaData packageMetaData) throws BimserverDatabaseException {
    IdEObject wrappedValue = (IdEObject) value;
    Short cid = database.getCidOfEClass(wrappedValue.eClass());
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    buffer.putShort((short) -cid);
    buffer.order(ByteOrder.BIG_ENDIAN);
    for (EStructuralFeature eStructuralFeature : wrappedValue.eClass().getEAllStructuralFeatures()) {
        if (eStructuralFeature.isMany()) {
            writeList(wrappedValue, buffer, packageMetaData, eStructuralFeature);
        } else {
            writePrimitiveValue(eStructuralFeature, wrappedValue.eGet(eStructuralFeature), buffer);
        }
    }
}
Also used : IdEObject(org.bimserver.emf.IdEObject) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature)

Example 54 with IdEObject

use of org.bimserver.emf.IdEObject in project BIMserver by opensourceBIM.

the class DatabaseSession method query.

public <T extends IdEObject> Map<Long, T> query(IfcModelInterface model, Condition condition, Class<T> clazz, QueryInterface query) throws BimserverDatabaseException {
    Map<Long, T> map = new HashMap<Long, T>();
    Set<EClass> eClasses = new HashSet<EClass>();
    condition.getEClassRequirements(eClasses);
    for (EClass eClass : eClasses) {
        TodoList todoList = new TodoList();
        getMap(eClass, model, query, todoList);
        processTodoList(model, todoList, query);
        List<IdEObject> list = new ArrayList<IdEObject>(model.getValues());
        for (IdEObject object : list) {
            if (clazz.isInstance(object)) {
                if (condition.matches(object)) {
                    map.put(object.getOid(), clazz.cast(object));
                }
            }
        }
    }
    return map;
}
Also used : EClass(org.eclipse.emf.ecore.EClass) HashMap(java.util.HashMap) IdEObject(org.bimserver.emf.IdEObject) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 55 with IdEObject

use of org.bimserver.emf.IdEObject in project BIMserver by opensourceBIM.

the class DatabaseSession method convertByteArrayToObject.

@SuppressWarnings({ "unused" })
private IdEObject convertByteArrayToObject(IdEObject idEObject, EClass originalQueryClass, EClass eClass, long oid, ByteBuffer buffer, IfcModelInterface model, int rid, QueryInterface query, TodoList todoList) throws BimserverDatabaseException {
    try {
        if (idEObject == null) {
            idEObject = createInternal(eClass, query);
            ((IdEObjectImpl) idEObject).setOid(oid);
            ((IdEObjectImpl) idEObject).setPid(query.getPid());
            if (rid == Integer.MAX_VALUE) {
                throw new BimserverDatabaseException("Database corrupt, rid cannot be " + Integer.MAX_VALUE);
            }
        }
        if (idEObject.eClass().getEAnnotation("wrapped") == null) {
            try {
                model.addAllowMultiModel(oid, idEObject);
            } catch (IfcModelInterfaceException e) {
                throw new BimserverDatabaseException(e);
            }
        }
        ((IdEObjectImpl) idEObject).setRid(rid);
        ((IdEObjectImpl) idEObject).useInverses(false);
        if (DEVELOPER_DEBUG && StorePackage.eINSTANCE == idEObject.eClass().getEPackage()) {
            LOGGER.info("Read: " + idEObject.eClass().getName() + " pid=" + query.getPid() + " oid=" + oid + " rid=" + rid);
        }
        ((IdEObjectImpl) idEObject).setLoadingState(State.LOADING);
        objectCache.put(oid, idEObject);
        int unsettedLength = model.getPackageMetaData().getUnsettedLength(eClass);
        byte[] unsetted = new byte[unsettedLength];
        buffer.get(unsetted);
        int fieldCounter = 0;
        for (EStructuralFeature feature : eClass.getEAllStructuralFeatures()) {
            try {
                if (model.getPackageMetaData().useForDatabaseStorage(eClass, feature)) {
                    boolean isUnsetted = (unsetted[fieldCounter / 8] & (1 << (fieldCounter % 8))) != 0;
                    if (isUnsetted) {
                        if (feature.isUnsettable()) {
                            idEObject.eUnset(feature);
                        } else if (feature.isMany()) {
                        // do nothing
                        } else if (feature.getDefaultValue() != null) {
                            idEObject.eSet(feature, feature.getDefaultValue());
                        }
                    } else {
                        if (!query.shouldFollowReference(originalQueryClass, eClass, feature)) {
                            // we have to do some reading to maintain a correct
                            // index
                            fakeRead(buffer, feature);
                        } else {
                            Object newValue = null;
                            if (feature.isMany()) {
                                newValue = readList(idEObject, originalQueryClass, buffer, model, query, todoList, feature);
                            } else {
                                if (feature.getEType() instanceof EEnum) {
                                    int enumOrdinal = buffer.getInt();
                                    if (enumOrdinal == -1) {
                                        newValue = null;
                                    } else {
                                        EClassifier eType = feature.getEType();
                                        EEnumLiteral enumLiteral = ((EEnumImpl) eType).getEEnumLiteral(enumOrdinal);
                                        if (enumLiteral != null) {
                                            newValue = enumLiteral.getInstance();
                                        }
                                    }
                                } else if (feature.getEType() instanceof EClass) {
                                    // EReference eReference = (EReference) feature;
                                    buffer.order(ByteOrder.LITTLE_ENDIAN);
                                    short cid = buffer.getShort();
                                    buffer.order(ByteOrder.BIG_ENDIAN);
                                    if (cid == -1) {
                                    // null, do nothing
                                    } else if (cid < 0) {
                                        // non minus one and negative cid means value is embedded in record
                                        EClass referenceClass = database.getEClassForCid((short) (-cid));
                                        if (feature.getEAnnotation("dbembed") != null) {
                                            newValue = readEmbeddedValue(feature, buffer, referenceClass, query);
                                        } else {
                                            newValue = readWrappedValue(feature, buffer, referenceClass, query);
                                        }
                                    } else if (cid > 0) {
                                        // positive cid means value is reference to other record
                                        EClass referenceClass = database.getEClassForCid(cid);
                                        if (referenceClass == null) {
                                            throw new BimserverDatabaseException("No eClass found for cid " + cid);
                                        }
                                        // readReference is going to read a long, which includes the 2 bytes for the cid
                                        buffer.position(buffer.position() - 2);
                                        newValue = readReference(originalQueryClass, buffer, model, idEObject, feature, referenceClass, query, todoList);
                                    // if (eReference.getEOpposite() != null &&
                                    // ((IdEObjectImpl)
                                    // newValue).isLoadedOrLoading()) {
                                    // newValue = null;
                                    // }
                                    }
                                } else if (feature.getEType() instanceof EDataType) {
                                    newValue = readPrimitiveValue(feature.getEType(), buffer, query);
                                }
                            }
                            if (newValue != null) {
                                idEObject.eSet(feature, newValue);
                            }
                        }
                    }
                    fieldCounter++;
                }
            } catch (StringIndexOutOfBoundsException e) {
                throw new BimserverDatabaseException("Reading " + eClass.getName() + "(" + oid + ")." + feature.getName(), e);
            } catch (BufferUnderflowException e) {
                throw new BimserverDatabaseException("Reading " + eClass.getName() + "(" + oid + ")." + feature.getName(), e);
            } catch (BufferOverflowException e) {
                throw new BimserverDatabaseException("Reading " + eClass.getName() + "(" + oid + ")." + feature.getName(), e);
            }
        }
        ((IdEObjectImpl) idEObject).setLoaded();
        ((IdEObjectImpl) idEObject).useInverses(true);
        if (DEVELOPER_DEBUG && idEObject.getRid() > 100000 || idEObject.getRid() < -100000) {
            LOGGER.debug("Improbable rid " + idEObject.getRid() + " - " + idEObject);
        }
        return idEObject;
    } catch (BufferUnderflowException e) {
        throw new BimserverDatabaseException("Reading " + eClass.getName(), e);
    } catch (BufferOverflowException e) {
        throw new BimserverDatabaseException("Reading " + eClass.getName(), e);
    }
}
Also used : IdEObjectImpl(org.bimserver.emf.IdEObjectImpl) EDataType(org.eclipse.emf.ecore.EDataType) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) EClassifier(org.eclipse.emf.ecore.EClassifier) EEnum(org.eclipse.emf.ecore.EEnum) BimserverDatabaseException(org.bimserver.BimserverDatabaseException) IfcModelInterfaceException(org.bimserver.emf.IfcModelInterfaceException) EClass(org.eclipse.emf.ecore.EClass) VirtualObject(org.bimserver.shared.VirtualObject) HashMapVirtualObject(org.bimserver.shared.HashMapVirtualObject) EObject(org.eclipse.emf.ecore.EObject) IdEObject(org.bimserver.emf.IdEObject) EEnumLiteral(org.eclipse.emf.ecore.EEnumLiteral) BufferOverflowException(java.nio.BufferOverflowException) EEnumImpl(org.eclipse.emf.ecore.impl.EEnumImpl) BufferUnderflowException(java.nio.BufferUnderflowException)

Aggregations

IdEObject (org.bimserver.emf.IdEObject)68 List (java.util.List)25 EStructuralFeature (org.eclipse.emf.ecore.EStructuralFeature)25 EClass (org.eclipse.emf.ecore.EClass)20 EReference (org.eclipse.emf.ecore.EReference)18 IdEObjectImpl (org.bimserver.emf.IdEObjectImpl)15 UserException (org.bimserver.shared.exceptions.UserException)15 BimserverDatabaseException (org.bimserver.BimserverDatabaseException)14 EList (org.eclipse.emf.common.util.EList)14 EObject (org.eclipse.emf.ecore.EObject)14 ArrayList (java.util.ArrayList)12 AbstractEList (org.eclipse.emf.common.util.AbstractEList)12 HashSet (java.util.HashSet)10 IfcModelInterface (org.bimserver.emf.IfcModelInterface)10 HashMapVirtualObject (org.bimserver.shared.HashMapVirtualObject)10 OldQuery (org.bimserver.database.OldQuery)9 IfcModelInterfaceException (org.bimserver.emf.IfcModelInterfaceException)9 VirtualObject (org.bimserver.shared.VirtualObject)9 EAttribute (org.eclipse.emf.ecore.EAttribute)9 Project (org.bimserver.models.store.Project)8