Search in sources :

Example 21 with IdEObjectImpl

use of org.bimserver.emf.IdEObjectImpl 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)

Example 22 with IdEObjectImpl

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

the class ClientIfcModel method set.

@Override
public void set(IdEObject idEObject, EStructuralFeature eFeature, Object newValue) {
    if (!recordChanges) {
        return;
    }
    if (!eFeature.isMany()) {
        if (getModelState() != ModelState.LOADING && ((IdEObjectImpl) idEObject).getLoadingState() != State.LOADING) {
            try {
                if (newValue != EStructuralFeature.Internal.DynamicValueHolder.NIL) {
                    LowLevelInterface lowLevelInterface = getBimServerClient().getLowLevelInterface();
                    if (eFeature.getName().equals("wrappedValue")) {
                        // Wrapped objects get the same oid as their
                        // "parent" object, so we know which object the
                        // client wants to update. That's why we can use
                        // idEObject.getOid() here
                        // We are making this crazy hack ever crazier, let's
                        // iterate over our parents features, and see if
                        // there is one matching our wrapped type...
                        // Seriously, when there are multiple fields of the
                        // same type, this fails miserably, a real fix
                        // should probably store the parent-oid + feature
                        // name in the wrapped object (requires two extra,
                        // volatile, fields),
                        // or we just don't support this (just create a new
                        // wrapped object too), we could even throw some
                        // sort of exception. Hack morally okay because it's
                        // client-side...
                        EReference foundReference = null;
                        if (contains(idEObject.getOid())) {
                            IdEObject parentObject = get(idEObject.getOid());
                            int found = 0;
                            foundReference = null;
                            for (EReference testReference : parentObject.eClass().getEAllReferences()) {
                                if (((EClass) testReference.getEType()).isSuperTypeOf(idEObject.eClass())) {
                                    foundReference = testReference;
                                    found++;
                                    if (found > 1) {
                                        throw new RuntimeException("Sorry, crazy hack could not resolve the right field, please let BIMserver developer know (debug info: " + parentObject.eClass().getName() + ", " + idEObject.eClass().getName() + ")");
                                    }
                                }
                            }
                            if (eFeature.getEType() == EcorePackage.eINSTANCE.getEString()) {
                                lowLevelInterface.setWrappedStringAttribute(getTransactionId(), idEObject.getOid(), foundReference.getName(), idEObject.eClass().getName(), (String) newValue);
                            } else if (eFeature.getEType() == EcorePackage.eINSTANCE.getELong() || eFeature.getEType() == EcorePackage.eINSTANCE.getELongObject()) {
                                lowLevelInterface.setWrappedLongAttribute(getTransactionId(), idEObject.getOid(), foundReference.getName(), idEObject.eClass().getName(), (Long) newValue);
                            } else if (eFeature.getEType() == EcorePackage.eINSTANCE.getEDouble() || eFeature.getEType() == EcorePackage.eINSTANCE.getEDoubleObject()) {
                                lowLevelInterface.setWrappedDoubleAttribute(getTransactionId(), idEObject.getOid(), foundReference.getName(), idEObject.eClass().getName(), (Double) newValue);
                            } else if (eFeature.getEType() == EcorePackage.eINSTANCE.getEBoolean() || eFeature.getEType() == EcorePackage.eINSTANCE.getEBooleanObject()) {
                                lowLevelInterface.setWrappedBooleanAttribute(getTransactionId(), idEObject.getOid(), foundReference.getName(), idEObject.eClass().getName(), (Boolean) newValue);
                            } else if (eFeature.getEType() == EcorePackage.eINSTANCE.getEInt() || eFeature.getEType() == EcorePackage.eINSTANCE.getEIntegerObject()) {
                                lowLevelInterface.setWrappedIntegerAttribute(getTransactionId(), idEObject.getOid(), foundReference.getName(), idEObject.eClass().getName(), (Integer) newValue);
                            } else if (eFeature.getEType() == EcorePackage.eINSTANCE.getEByteArray()) {
                                throw new RuntimeException("Unimplemented " + eFeature.getEType().getName() + " " + newValue);
                            }
                        } else {
                            if (eFeature.getEType() == EcorePackage.eINSTANCE.getEString()) {
                                lowLevelInterface.setStringAttribute(getTransactionId(), idEObject.getOid(), eFeature.getName(), (String) newValue);
                            } else if (eFeature.getEType() == EcorePackage.eINSTANCE.getELong() || eFeature.getEType() == EcorePackage.eINSTANCE.getELongObject()) {
                                lowLevelInterface.setLongAttribute(getTransactionId(), idEObject.getOid(), eFeature.getName(), (Long) newValue);
                            } else if (eFeature.getEType() == EcorePackage.eINSTANCE.getEDouble() || eFeature.getEType() == EcorePackage.eINSTANCE.getEDoubleObject()) {
                                lowLevelInterface.setDoubleAttribute(getTransactionId(), idEObject.getOid(), eFeature.getName(), (Double) newValue);
                            } else if (eFeature.getEType() == EcorePackage.eINSTANCE.getEBoolean() || eFeature.getEType() == EcorePackage.eINSTANCE.getEBooleanObject()) {
                                lowLevelInterface.setBooleanAttribute(getTransactionId(), idEObject.getOid(), eFeature.getName(), (Boolean) newValue);
                            } else if (eFeature.getEType() == EcorePackage.eINSTANCE.getEInt() || eFeature.getEType() == EcorePackage.eINSTANCE.getEIntegerObject()) {
                                lowLevelInterface.setIntegerAttribute(getTransactionId(), idEObject.getOid(), eFeature.getName(), (Integer) newValue);
                            } else if (eFeature.getEType() == EcorePackage.eINSTANCE.getEByteArray()) {
                                lowLevelInterface.setByteArrayAttribute(getTransactionId(), idEObject.getOid(), eFeature.getName(), (Byte[]) newValue);
                            } else if (eFeature.getEType() instanceof EEnum) {
                                lowLevelInterface.setEnumAttribute(getTransactionId(), idEObject.getOid(), eFeature.getName(), ((Enum<?>) newValue).toString());
                            } else if (eFeature instanceof EReference) {
                                if (newValue == null) {
                                    lowLevelInterface.setReference(getTransactionId(), idEObject.getOid(), eFeature.getName(), -1L);
                                } else {
                                    lowLevelInterface.setReference(getTransactionId(), idEObject.getOid(), eFeature.getName(), ((IdEObject) newValue).getOid());
                                }
                            } else {
                                throw new RuntimeException("Unimplemented " + eFeature.getEType().getName() + " " + newValue);
                            }
                        }
                    } else {
                        if (eFeature.getEType() == EcorePackage.eINSTANCE.getEString()) {
                            lowLevelInterface.setStringAttribute(getTransactionId(), idEObject.getOid(), eFeature.getName(), (String) newValue);
                        } else if (eFeature.getEType() == EcorePackage.eINSTANCE.getELong() || eFeature.getEType() == EcorePackage.eINSTANCE.getELongObject()) {
                            lowLevelInterface.setLongAttribute(getTransactionId(), idEObject.getOid(), eFeature.getName(), (Long) newValue);
                        } else if (eFeature.getEType() == EcorePackage.eINSTANCE.getEDouble() || eFeature.getEType() == EcorePackage.eINSTANCE.getEDoubleObject()) {
                            lowLevelInterface.setDoubleAttribute(getTransactionId(), idEObject.getOid(), eFeature.getName(), (Double) newValue);
                        } else if (eFeature.getEType() == EcorePackage.eINSTANCE.getEBoolean() || eFeature.getEType() == EcorePackage.eINSTANCE.getEBooleanObject()) {
                            lowLevelInterface.setBooleanAttribute(getTransactionId(), idEObject.getOid(), eFeature.getName(), (Boolean) newValue);
                        } else if (eFeature.getEType() == EcorePackage.eINSTANCE.getEInt() || eFeature.getEType() == EcorePackage.eINSTANCE.getEIntegerObject()) {
                            lowLevelInterface.setIntegerAttribute(getTransactionId(), idEObject.getOid(), eFeature.getName(), (Integer) newValue);
                        } else if (eFeature.getEType() == EcorePackage.eINSTANCE.getEByteArray()) {
                            if (newValue instanceof byte[]) {
                                Byte[] n = new Byte[((byte[]) newValue).length];
                                for (int i = 0; i < n.length; i++) {
                                    n[i] = ((byte[]) newValue)[i];
                                }
                                newValue = n;
                            }
                            lowLevelInterface.setByteArrayAttribute(getTransactionId(), idEObject.getOid(), eFeature.getName(), (Byte[]) newValue);
                        } else if (eFeature.getEType() instanceof EEnum) {
                            lowLevelInterface.setEnumAttribute(getTransactionId(), idEObject.getOid(), eFeature.getName(), ((Enum<?>) newValue).toString());
                        } else if (eFeature instanceof EReference) {
                            if (newValue == null) {
                                lowLevelInterface.setReference(getTransactionId(), idEObject.getOid(), eFeature.getName(), -1L);
                            } else {
                                lowLevelInterface.setReference(getTransactionId(), idEObject.getOid(), eFeature.getName(), ((IdEObject) newValue).getOid());
                            }
                        } else {
                            throw new RuntimeException("Unimplemented " + eFeature.getEType().getName() + " " + newValue);
                        }
                    }
                }
            } catch (ServiceException e) {
                LOGGER.error("", e);
            } catch (PublicInterfaceNotFoundException e) {
                LOGGER.error("", e);
            }
        }
    }
}
Also used : IdEObjectImpl(org.bimserver.emf.IdEObjectImpl) IdEObject(org.bimserver.emf.IdEObject) EEnum(org.eclipse.emf.ecore.EEnum) EClass(org.eclipse.emf.ecore.EClass) ServiceException(org.bimserver.shared.exceptions.ServiceException) PublicInterfaceNotFoundException(org.bimserver.shared.exceptions.PublicInterfaceNotFoundException) LowLevelInterface(org.bimserver.shared.interfaces.LowLevelInterface) EReference(org.eclipse.emf.ecore.EReference)

Example 23 with IdEObjectImpl

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

the class RevisionMerger method copyAttributesGuidObjectsAndAddNewObjects.

private void copyAttributesGuidObjectsAndAddNewObjects() throws IfcModelInterfaceException, ObjectAlreadyExistsException {
    for (IdEObject idEObject : newModel.getValues()) {
        if (idEObject instanceof IfcRoot) {
            IfcRoot ifcRoot = (IfcRoot) idEObject;
            String guid = ifcRoot.getGlobalId();
            if (resultModel.containsGuid(guid)) {
                // LOGGER.info("Updating attributes for object " + idEObject.eClass().getName() + " " + guid);
                IfcRoot oldObject = (IfcRoot) resultModel.getByGuid(guid);
                for (EAttribute eAttribute : idEObject.eClass().getEAllAttributes()) {
                    Object newValue = idEObject.eGet(eAttribute);
                    oldObject.eSet(eAttribute, newValue);
                }
            } else {
                // LOGGER.info("Adding new GUID object " + idEObject.eClass().getName() + " " + guid);
                IdEObject newObject = (IdEObject) idEObject.eClass().getEPackage().getEFactoryInstance().create(idEObject.eClass());
                ((IdEObjectImpl) newObject).setOid(idEObject.getOid());
                ((IfcRoot) newObject).setGlobalId(newGuid(guid));
                for (EAttribute eAttribute : newObject.eClass().getEAllAttributes()) {
                    newObject.eSet(eAttribute, idEObject.eGet(eAttribute));
                }
                resultModel.add(newObject.getOid(), newObject);
            }
        }
    }
}
Also used : IdEObjectImpl(org.bimserver.emf.IdEObjectImpl) EAttribute(org.eclipse.emf.ecore.EAttribute) IdEObject(org.bimserver.emf.IdEObject) IfcRoot(org.bimserver.models.ifc2x3tc1.IfcRoot) IdEObject(org.bimserver.emf.IdEObject)

Aggregations

IdEObjectImpl (org.bimserver.emf.IdEObjectImpl)23 IdEObject (org.bimserver.emf.IdEObject)16 EObject (org.eclipse.emf.ecore.EObject)10 EClass (org.eclipse.emf.ecore.EClass)8 UserException (org.bimserver.shared.exceptions.UserException)7 EStructuralFeature (org.eclipse.emf.ecore.EStructuralFeature)7 HashMap (java.util.HashMap)6 IfcModelInterfaceException (org.bimserver.emf.IfcModelInterfaceException)6 EAttribute (org.eclipse.emf.ecore.EAttribute)6 EReference (org.eclipse.emf.ecore.EReference)6 IfcModelInterface (org.bimserver.emf.IfcModelInterface)5 IfcRoot (org.bimserver.models.ifc2x3tc1.IfcRoot)5 HashMapVirtualObject (org.bimserver.shared.HashMapVirtualObject)5 AbstractEList (org.eclipse.emf.common.util.AbstractEList)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 BimserverDatabaseException (org.bimserver.BimserverDatabaseException)4 OldQuery (org.bimserver.database.OldQuery)4 QueryException (org.bimserver.database.queries.om.QueryException)4 PackageMetaData (org.bimserver.emf.PackageMetaData)4