Search in sources :

Example 26 with EEnumLiteral

use of org.eclipse.emf.ecore.EEnumLiteral in project BIMserver by opensourceBIM.

the class DatabaseSession method convertObjectToByteArray.

private ByteBuffer convertObjectToByteArray(IdEObject object, ByteBuffer buffer, PackageMetaData packageMetaData) throws BimserverDatabaseException {
    if (object.getUuid() == null) {
        throw new BimserverDatabaseException("UUID is required " + object.eClass().getName());
    }
    int bufferSize = getExactSize(object, packageMetaData, true);
    if (bufferSize > buffer.capacity()) {
        LOGGER.debug("Buffer too small (" + bufferSize + ")");
        buffer = ByteBuffer.allocate(bufferSize);
    }
    int unsettedLength = packageMetaData.getUnsettedLength(object.eClass());
    byte[] unsetted = new byte[unsettedLength];
    int fieldCounter = 0;
    for (EStructuralFeature feature : object.eClass().getEAllStructuralFeatures()) {
        if (packageMetaData.useForDatabaseStorage(object.eClass(), feature)) {
            if (useUnsetBit(feature, object)) {
                unsetted[fieldCounter / 8] |= (1 << (fieldCounter % 8));
            }
            fieldCounter++;
        }
    }
    buffer.put(unsetted);
    EClass eClass = getEClassForOid(object.getOid());
    if (!eClass.isSuperTypeOf(object.eClass())) {
        throw new BimserverDatabaseException("Object with oid " + object.getOid() + " is a " + object.eClass().getName() + " but it's cid-part says it's a " + eClass.getName());
    }
    buffer.putLong(object.getUuid().getMostSignificantBits());
    buffer.putLong(object.getUuid().getLeastSignificantBits());
    for (EStructuralFeature feature : object.eClass().getEAllStructuralFeatures()) {
        if (packageMetaData.useForDatabaseStorage(object.eClass(), feature)) {
            if (!useUnsetBit(feature, object)) {
                if (feature.isMany()) {
                    writeList(object, buffer, packageMetaData, feature);
                } else {
                    Object value = object.eGet(feature);
                    if (feature.getEType() instanceof EEnum) {
                        if (value == null) {
                            buffer.putInt(-1);
                        } else {
                            EEnum eEnum = (EEnum) feature.getEType();
                            EEnumLiteral eEnumLiteral = eEnum.getEEnumLiteralByLiteral(((Enum<?>) value).toString());
                            if (eEnumLiteral != null) {
                                buffer.putInt(eEnumLiteral.getValue());
                            } else {
                                LOGGER.error(((Enum<?>) value).toString() + " not found");
                                buffer.putInt(-1);
                            }
                        }
                    } else if (feature.getEType() instanceof EClass) {
                        if (value == null) {
                            buffer.order(ByteOrder.LITTLE_ENDIAN);
                            buffer.putShort((short) -1);
                            buffer.order(ByteOrder.BIG_ENDIAN);
                        } else {
                            IdEObject referencedObject = (IdEObject) value;
                            EClass referencedClass = referencedObject.eClass();
                            if (feature.getEAnnotation("dbembed") != null) {
                                writeEmbeddedValue(object.getPid(), object.getRid(), value, buffer, packageMetaData);
                            } else if (referencedClass.getEAnnotation("wrapped") != null) {
                                writeWrappedValue(object.getPid(), object.getRid(), value, buffer, packageMetaData);
                            } else {
                                writeReference(object, value, buffer, feature);
                            }
                        }
                    } else if (feature.getEType() instanceof EDataType) {
                        writePrimitiveValue(feature, value, buffer);
                    }
                }
            }
        }
    }
    if (buffer.position() != bufferSize) {
        throw new BimserverDatabaseException("Value buffer sizes do not match for " + object.eClass().getName() + " " + buffer.position() + "/" + bufferSize);
    }
    return buffer;
}
Also used : EEnum(org.eclipse.emf.ecore.EEnum) BimserverDatabaseException(org.bimserver.BimserverDatabaseException) EClass(org.eclipse.emf.ecore.EClass) IdEObject(org.bimserver.emf.IdEObject) EDataType(org.eclipse.emf.ecore.EDataType) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) 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) EEnum(org.eclipse.emf.ecore.EEnum)

Example 27 with EEnumLiteral

use of org.eclipse.emf.ecore.EEnumLiteral 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;
        ((IdEObjectImpl) idEObject).setUuid(new UUID(buffer.getLong(), buffer.getLong()));
        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 (false) {
                            // 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) UUID(java.util.UUID) EEnumImpl(org.eclipse.emf.ecore.impl.EEnumImpl) BufferUnderflowException(java.nio.BufferUnderflowException)

Example 28 with EEnumLiteral

use of org.eclipse.emf.ecore.EEnumLiteral in project BIMserver by opensourceBIM.

the class DatabaseReadingStackFrame method convertByteArrayToObject.

protected HashMapVirtualObject convertByteArrayToObject(EClass eClass, long oid, ByteBuffer buffer, int rid) throws BimserverDatabaseException {
    try {
        int unsettedLength = getPackageMetaData().getUnsettedLength(eClass);
        byte[] unsetted = new byte[unsettedLength];
        buffer.get(unsetted);
        int fieldCounter = 0;
        HashMapVirtualObject idEObject = new HashMapVirtualObject(reusable, eClass, oid, new UUID(buffer.getLong(), buffer.getLong()));
        for (EStructuralFeature feature : eClass.getEAllStructuralFeatures()) {
            try {
                if (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.setAttribute((EAttribute) feature, feature.getDefaultValue());
                        }
                    } else {
                        Object newValue = null;
                        if (feature.isMany()) {
                            newValue = readList(idEObject, buffer, 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) {
                                    // negative cid means value is embedded in
                                    // record
                                    EClass referenceClass = queryObjectProvider.getDatabaseSession().getEClass((short) (-cid));
                                    if (feature.getEAnnotation("dbembed") != null) {
                                        newValue = readEmbeddedValue(feature, buffer, referenceClass);
                                    } else {
                                        newValue = readWrappedValue(feature, buffer, referenceClass);
                                    }
                                } else if (cid > 0) {
                                    // positive cid means value is reference to
                                    // other record
                                    EClass referenceClass = queryObjectProvider.getDatabaseSession().getEClass(cid);
                                    if (referenceClass == null) {
                                        throw new BimserverDatabaseException("No eClass found for cid " + cid);
                                    }
                                    buffer.position(buffer.position() - 2);
                                    newValue = readReference(buffer, feature, referenceClass);
                                    if ((Long) newValue != -1) {
                                        if (queryObjectProvider.hasReadOrIsGoingToRead(((Long) newValue)) || queryObjectProvider.hasReadOrIsGoingToRead(referenceClass)) {
                                            idEObject.addUseForSerialization(feature);
                                        }
                                    }
                                // if (eReference.getEOpposite() != null &&
                                // ((IdEObjectImpl)
                                // newValue).isLoadedOrLoading()) {
                                // newValue = null;
                                // }
                                }
                            } else if (feature.getEType() instanceof EDataType) {
                                newValue = readPrimitiveValue(feature.getEType(), buffer);
                            }
                            if (newValue != null) {
                                if (feature instanceof EAttribute) {
                                    idEObject.setAttribute((EAttribute) feature, newValue);
                                } else {
                                    if (newValue instanceof Long) {
                                        idEObject.setReference((EReference) feature, (Long) newValue);
                                    } else {
                                        idEObject.setReference((EReference) feature, (WrappedVirtualObject) newValue);
                                    }
                                }
                            }
                        }
                    }
                    fieldCounter++;
                }
            } catch (StringIndexOutOfBoundsException e) {
                throw new BimserverDatabaseException("Reading " + eClass.getName() + "." + feature.getName(), e);
            } catch (BufferUnderflowException e) {
                throw new BimserverDatabaseException("Reading " + eClass.getName() + "." + feature.getName() + " " + buffer.capacity(), e);
            } catch (BufferOverflowException e) {
                throw new BimserverDatabaseException("Reading " + eClass.getName() + "." + feature.getName(), e);
            }
        }
        return idEObject;
    } catch (BufferUnderflowException e) {
        throw new BimserverDatabaseException("Reading " + eClass.getName(), e);
    } catch (BufferOverflowException e) {
        throw new BimserverDatabaseException("Reading " + eClass.getName(), e);
    }
}
Also used : 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) EClass(org.eclipse.emf.ecore.EClass) EAttribute(org.eclipse.emf.ecore.EAttribute) HashMapVirtualObject(org.bimserver.shared.HashMapVirtualObject) HashMapWrappedVirtualObject(org.bimserver.shared.HashMapWrappedVirtualObject) HashMapVirtualObject(org.bimserver.shared.HashMapVirtualObject) WrappedVirtualObject(org.bimserver.shared.WrappedVirtualObject) EEnumLiteral(org.eclipse.emf.ecore.EEnumLiteral) BufferOverflowException(java.nio.BufferOverflowException) UUID(java.util.UUID) EEnumImpl(org.eclipse.emf.ecore.impl.EEnumImpl) BufferUnderflowException(java.nio.BufferUnderflowException)

Example 29 with EEnumLiteral

use of org.eclipse.emf.ecore.EEnumLiteral in project BIMserver by opensourceBIM.

the class HashMapVirtualObject method write.

public ByteBuffer write() throws BimserverDatabaseException {
    int bufferSize = getExactSize(this);
    ByteBuffer buffer = ByteBuffer.allocate(bufferSize);
    byte[] unsetted = new byte[getPackageMetaData().getUnsettedLength(eClass)];
    int fieldCounter = 0;
    for (EStructuralFeature feature : eClass().getEAllStructuralFeatures()) {
        if (getPackageMetaData().useForDatabaseStorage(eClass, feature)) {
            if (useUnsetBit(feature)) {
                unsetted[fieldCounter / 8] |= (1 << (fieldCounter % 8));
            }
            fieldCounter++;
        }
    }
    buffer.put(unsetted);
    EClass eClass = getDatabaseInterface().getEClassForOid(getOid());
    if (!eClass.isSuperTypeOf(eClass())) {
        throw new BimserverDatabaseException("Object with oid " + getOid() + " is a " + eClass().getName() + " but it's cid-part says it's a " + eClass.getName());
    }
    buffer.putLong(getUuid().getMostSignificantBits());
    buffer.putLong(getUuid().getLeastSignificantBits());
    for (EStructuralFeature feature : eClass().getEAllStructuralFeatures()) {
        if (getPackageMetaData().useForDatabaseStorage(eClass, feature)) {
            if (!useUnsetBit(feature)) {
                if (feature.isMany()) {
                    writeList(this, buffer, getPackageMetaData(), feature);
                } else {
                    Object value = eGet(feature);
                    if (feature.getEType() instanceof EEnum) {
                        if (value == null) {
                            buffer.putInt(-1);
                        } else {
                            EEnum eEnum = (EEnum) feature.getEType();
                            EEnumLiteral eEnumLiteral = eEnum.getEEnumLiteralByLiteral(((Enum<?>) value).toString());
                            if (eEnumLiteral != null) {
                                buffer.putInt(eEnumLiteral.getValue());
                            } else {
                                LOGGER.error(((Enum<?>) value).toString() + " not found");
                                buffer.putInt(-1);
                            }
                        }
                    } else if (feature.getEType() instanceof EClass) {
                        if (value == null) {
                            buffer.order(ByteOrder.LITTLE_ENDIAN);
                            buffer.putShort((short) -1);
                            buffer.order(ByteOrder.BIG_ENDIAN);
                        } else if (value instanceof VirtualObject) {
                            writeWrappedValue(getPid(), getRid(), (VirtualObject) value, buffer, getPackageMetaData());
                        } else if (value instanceof WrappedVirtualObject) {
                            writeWrappedValue(getPid(), getRid(), (WrappedVirtualObject) value, buffer, getPackageMetaData());
                        } else {
                            long referencedOid = (Long) value;
                            writeReference(referencedOid, buffer, feature);
                        }
                    } else if (feature.getEType() instanceof EDataType) {
                        writePrimitiveValue(feature, value, buffer);
                    }
                }
            }
        }
    }
    if (buffer.position() != bufferSize) {
        throw new BimserverDatabaseException("Value buffer sizes do not match for " + eClass().getName() + " " + buffer.position() + "/" + bufferSize);
    }
    return buffer;
}
Also used : EEnum(org.eclipse.emf.ecore.EEnum) EDataType(org.eclipse.emf.ecore.EDataType) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) EEnum(org.eclipse.emf.ecore.EEnum) ByteBuffer(java.nio.ByteBuffer) BimserverDatabaseException(org.bimserver.BimserverDatabaseException) EClass(org.eclipse.emf.ecore.EClass) EEnumLiteral(org.eclipse.emf.ecore.EEnumLiteral)

Example 30 with EEnumLiteral

use of org.eclipse.emf.ecore.EEnumLiteral in project BIMserver by opensourceBIM.

the class SchemaChecker method compareEEnum.

private void compareEEnum(EEnum eEnum1, EEnum eEnum2) throws SchemaCompareException {
    if (eEnum1.getELiterals().size() != eEnum2.getELiterals().size()) {
        throw new SchemaCompareException("Not the same amount of literals");
    }
    Iterator<EEnumLiteral> iterator1 = eEnum1.getELiterals().iterator();
    Iterator<EEnumLiteral> iterator2 = eEnum2.getELiterals().iterator();
    while (iterator1.hasNext()) {
        EEnumLiteral enumLiteral1 = iterator1.next();
        EEnumLiteral enumLiteral2 = iterator2.next();
        if (!enumLiteral1.getName().equals(enumLiteral2.getName())) {
            throw new SchemaCompareException("Literal not the same");
        }
    }
}
Also used : EEnumLiteral(org.eclipse.emf.ecore.EEnumLiteral)

Aggregations

EEnumLiteral (org.eclipse.emf.ecore.EEnumLiteral)35 EEnum (org.eclipse.emf.ecore.EEnum)22 EPackage (org.eclipse.emf.ecore.EPackage)10 Grammar (org.eclipse.xtext.Grammar)10 Test (org.junit.Test)9 EClass (org.eclipse.emf.ecore.EClass)8 EDataType (org.eclipse.emf.ecore.EDataType)6 EStructuralFeature (org.eclipse.emf.ecore.EStructuralFeature)6 ENotificationImpl (org.eclipse.emf.ecore.impl.ENotificationImpl)5 BimserverDatabaseException (org.bimserver.BimserverDatabaseException)4 EClassifier (org.eclipse.emf.ecore.EClassifier)4 EnumLiteralDeclaration (org.eclipse.xtext.EnumLiteralDeclaration)4 IdEObject (org.bimserver.emf.IdEObject)3 HashMapVirtualObject (org.bimserver.shared.HashMapVirtualObject)3 EAttribute (org.eclipse.emf.ecore.EAttribute)3 EObject (org.eclipse.emf.ecore.EObject)3 DTDEnumGroupKind (org.eclipse.wst.dtd.core.internal.emf.DTDEnumGroupKind)3 DTDEnumerationType (org.eclipse.wst.dtd.core.internal.emf.DTDEnumerationType)3 EnumRule (org.eclipse.xtext.EnumRule)3 BufferOverflowException (java.nio.BufferOverflowException)2