Search in sources :

Example 41 with EDataType

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

the class DatabaseReadingStackFrame method readEmbeddedValue.

private HashMapWrappedVirtualObject readEmbeddedValue(EStructuralFeature feature, ByteBuffer buffer, EClass eClass) throws BimserverDatabaseException {
    HashMapWrappedVirtualObject eObject = new HashMapWrappedVirtualObject(eClass);
    for (EStructuralFeature eStructuralFeature : eClass.getEAllStructuralFeatures()) {
        if (eStructuralFeature.isMany()) {
        } else {
            if (eStructuralFeature.getEType() instanceof EDataType) {
                Object primitiveValue = readPrimitiveValue(eStructuralFeature.getEType(), buffer);
                eObject.setAttribute((EAttribute) eStructuralFeature, primitiveValue);
            } else {
                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));
                    eObject.setReference((EReference) eStructuralFeature, readEmbeddedValue(eStructuralFeature, buffer, referenceClass));
                }
            }
        }
    }
    return eObject;
}
Also used : EClass(org.eclipse.emf.ecore.EClass) HashMapWrappedVirtualObject(org.bimserver.shared.HashMapWrappedVirtualObject) EDataType(org.eclipse.emf.ecore.EDataType) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) HashMapWrappedVirtualObject(org.bimserver.shared.HashMapWrappedVirtualObject) HashMapVirtualObject(org.bimserver.shared.HashMapVirtualObject) WrappedVirtualObject(org.bimserver.shared.WrappedVirtualObject) EReference(org.eclipse.emf.ecore.EReference)

Example 42 with EDataType

use of org.eclipse.emf.ecore.EDataType 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 43 with EDataType

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

the class GenerateUtils method getType.

public static String getType(EStructuralFeature eStructuralFeature) {
    boolean embedsReference = eStructuralFeature.getEAnnotation("embedsreference") != null;
    EClassifier eType = eStructuralFeature.getEType();
    if (eStructuralFeature.isMany()) {
        if (eType instanceof EDataType) {
            if (eType == EcorePackage.eINSTANCE.getEString()) {
                return "List<String>";
            } else if (eType == EcorePackage.eINSTANCE.getEInt() || eType == EcorePackage.eINSTANCE.getEIntegerObject()) {
                return "List<Integer>";
            } else if (eType == EcorePackage.eINSTANCE.getELong() || eType == EcorePackage.eINSTANCE.getELongObject()) {
                return "List<Long>";
            } else if (eType == EcorePackage.eINSTANCE.getEDouble() || eType == EcorePackage.eINSTANCE.getEDoubleObject()) {
                return "List<Double>";
            } else if (eType == EcorePackage.eINSTANCE.getEBoolean() || eType == EcorePackage.eINSTANCE.getEBooleanObject()) {
                return "List<Boolean>";
            } else if (eType == EcorePackage.eINSTANCE.getEFloat() || eType == EcorePackage.eINSTANCE.getEFloatObject()) {
                return "List<Float>";
            } else if (eType == EcorePackage.eINSTANCE.getEDate()) {
                return "List<Date>";
            }
        } else if (eType instanceof EClass) {
            if (embedsReference) {
                return "List<S" + eType.getName() + ">";
            } else {
                return "List<Long>";
            }
        }
    } else {
        if (eType instanceof EDataType) {
            if (eType == EcorePackage.eINSTANCE.getEString()) {
                return "String";
            } else if (eType == EcorePackage.eINSTANCE.getEInt() || eType == EcorePackage.eINSTANCE.getEIntegerObject()) {
                return "Integer";
            } else if (eType == EcorePackage.eINSTANCE.getELong() || eType == EcorePackage.eINSTANCE.getELongObject()) {
                return "Long";
            } else if (eType == EcorePackage.eINSTANCE.getEShort() || eType == EcorePackage.eINSTANCE.getEShortObject()) {
                return "Short";
            } else if (eType == EcorePackage.eINSTANCE.getEDouble() || eType == EcorePackage.eINSTANCE.getEDoubleObject()) {
                return "Double";
            } else if (eType == EcorePackage.eINSTANCE.getEBoolean() || eType == EcorePackage.eINSTANCE.getEBooleanObject()) {
                return "Boolean";
            } else if (eType == EcorePackage.eINSTANCE.getEFloat() || eType == EcorePackage.eINSTANCE.getEFloatObject()) {
                return "Float";
            } else if (eType == EcorePackage.eINSTANCE.getEDate()) {
                return "Date";
            } else if (eType == EcorePackage.eINSTANCE.getEByteArray()) {
                return "byte[]";
            } else if (eType.getInstanceClass() == DataHandler.class) {
                return "DataHandler";
            }
        } else if (eType instanceof EClass) {
            if (embedsReference) {
                return "S" + eType.getName();
            } else {
                return "Long";
            }
        }
    }
    return "S" + eType.getName();
}
Also used : EClass(org.eclipse.emf.ecore.EClass) EDataType(org.eclipse.emf.ecore.EDataType) EClassifier(org.eclipse.emf.ecore.EClassifier) DataHandler(javax.activation.DataHandler)

Example 44 with EDataType

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

the class HashMapVirtualObject method writeList.

@SuppressWarnings("rawtypes")
private void writeList(VirtualObject virtualObject, ByteBuffer buffer, PackageMetaData packageMetaData, EStructuralFeature feature) throws BimserverDatabaseException {
    if (feature.getEType() instanceof EEnum) {
    // Aggregate relations to enums never occur... at this
    // moment
    } else if (feature.getEType() instanceof EClass) {
        List list = (List) virtualObject.eGet(feature);
        buffer.putInt(list.size());
        for (Object o : list) {
            if (o == null) {
                buffer.order(ByteOrder.LITTLE_ENDIAN);
                buffer.putShort((short) -1);
                buffer.order(ByteOrder.BIG_ENDIAN);
            } else {
                if (o instanceof VirtualObject) {
                    VirtualObject listObject = (VirtualObject) o;
                    if (feature.getEAnnotation("twodimensionalarray") != null) {
                        Short cid = getDatabaseInterface().getCidOfEClass((EClass) feature.getEType());
                        buffer.order(ByteOrder.LITTLE_ENDIAN);
                        buffer.putShort((short) -cid);
                        buffer.order(ByteOrder.BIG_ENDIAN);
                        EStructuralFeature lf = listObject.eClass().getEStructuralFeature("List");
                        writeList(listObject, buffer, packageMetaData, lf);
                    } else {
                        if (listObject.eClass().getEAnnotation("wrapped") != null || listObject.eClass().getEStructuralFeature("wrappedValue") != null) {
                            writeWrappedValue(getPid(), getRid(), listObject, buffer, packageMetaData);
                        }
                    }
                } else if (o instanceof Long) {
                    long listObjectOid = (Long) o;
                    writeReference(listObjectOid, buffer, feature);
                } else if (o instanceof HashMapWrappedVirtualObject) {
                    HashMapWrappedVirtualObject hashMapWrappedVirtualObject = (HashMapWrappedVirtualObject) o;
                    writeWrappedValue(-1, -1, hashMapWrappedVirtualObject, buffer, packageMetaData);
                }
            }
        }
    } else if (feature.getEType() instanceof EDataType) {
        List list = (List) virtualObject.eGet(feature);
        if (list == null) {
            buffer.putInt(-1);
        } else {
            buffer.putInt(list.size());
            for (Object o : list) {
                writePrimitiveValue(feature, o, buffer);
            }
        }
    }
}
Also used : EClass(org.eclipse.emf.ecore.EClass) EDataType(org.eclipse.emf.ecore.EDataType) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) ArrayList(java.util.ArrayList) List(java.util.List) EEnum(org.eclipse.emf.ecore.EEnum)

Example 45 with EDataType

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

the class HashMapVirtualObject method writeWrappedValue.

private void writeWrappedValue(int pid, int rid, WrappedVirtualObject wrappedValue, ByteBuffer buffer, PackageMetaData packageMetaData) throws BimserverDatabaseException {
    Short cid = getDatabaseInterface().getCidOfEClass(wrappedValue.eClass());
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    buffer.putShort((short) -cid);
    buffer.order(ByteOrder.BIG_ENDIAN);
    for (EStructuralFeature eStructuralFeature : wrappedValue.eClass().getEAllStructuralFeatures()) {
        Object val = wrappedValue.eGet(eStructuralFeature);
        if (eStructuralFeature.isMany()) {
            List list = (List) val;
            buffer.putInt(list.size());
            for (Object o : list) {
                if (o instanceof HashMapWrappedVirtualObject) {
                    writeWrappedValue(pid, rid, (VirtualObject) o, buffer, packageMetaData);
                }
            }
        } else {
            if (eStructuralFeature.getEType() instanceof EDataType) {
                writePrimitiveValue(eStructuralFeature, val, buffer);
            } else {
                writeWrappedValue(pid, rid, (HashMapWrappedVirtualObject) val, buffer, packageMetaData);
            }
        }
    }
}
Also used : EDataType(org.eclipse.emf.ecore.EDataType) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

EDataType (org.eclipse.emf.ecore.EDataType)48 EClass (org.eclipse.emf.ecore.EClass)21 EStructuralFeature (org.eclipse.emf.ecore.EStructuralFeature)15 EEnum (org.eclipse.emf.ecore.EEnum)14 EClassifier (org.eclipse.emf.ecore.EClassifier)13 EObject (org.eclipse.emf.ecore.EObject)13 HashMapVirtualObject (org.bimserver.shared.HashMapVirtualObject)10 Test (org.junit.Test)10 List (java.util.List)7 ArrayList (java.util.ArrayList)6 BimserverDatabaseException (org.bimserver.BimserverDatabaseException)6 IdEObject (org.bimserver.emf.IdEObject)6 VirtualObject (org.bimserver.shared.VirtualObject)6 EEnumLiteral (org.eclipse.emf.ecore.EEnumLiteral)6 EPackage (org.eclipse.emf.ecore.EPackage)6 HashMapWrappedVirtualObject (org.bimserver.shared.HashMapWrappedVirtualObject)4 EAttribute (org.eclipse.emf.ecore.EAttribute)4 EReference (org.eclipse.emf.ecore.EReference)4 AbstractRule (org.eclipse.xtext.AbstractRule)4 ByteBuffer (java.nio.ByteBuffer)3