Search in sources :

Example 1 with EEnum

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

the class SetAttributeChangeAtIndex method execute.

@SuppressWarnings({ "rawtypes", "unchecked" })
@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");
    }
    object = transaction.get(oid);
    if (object == null) {
        throw new UserException("No object of type \"" + eClass.getName() + "\" with oid " + oid + " found in project with pid " + transaction.getProject().getId());
    }
    EAttribute eAttribute = packageMetaData.getEAttribute(eClass.getName(), attributeName);
    if (eAttribute == null) {
        throw new UserException("No attribute with the name \"" + attributeName + "\" found in class \"" + eClass.getName() + "\"");
    }
    if (value instanceof List && eAttribute.isMany()) {
        List sourceList = (List) value;
        if (!eAttribute.isMany()) {
            throw new UserException("Attribute is not of type 'many'");
        }
        List list = (List) object.eGet(eAttribute);
        list.clear();
        List asStringList = null;
        if (eAttribute.getEType() == EcorePackage.eINSTANCE.getEDouble()) {
            asStringList = (List) object.eGet(object.eClass().getEStructuralFeature(attributeName + "AsString"));
            asStringList.clear();
        }
        for (Object o : sourceList) {
            if (eAttribute.getEType() == EcorePackage.eINSTANCE.getEDouble()) {
                asStringList.add(String.valueOf(o));
            }
            list.add(o);
        }
    } else {
        if (eAttribute.isMany()) {
            throw new UserException("Attribute is not of type 'single'");
        }
        if (eAttribute.getEType() instanceof EEnum) {
            EEnum eEnum = (EEnum) eAttribute.getEType();
            object.setListItem(eAttribute, index, eEnum.getEEnumLiteral(((String) value).toUpperCase()).getInstance());
        } else {
            object.setListItem(eAttribute, index, value);
        }
        if (value instanceof Double) {
            EStructuralFeature asStringAttribute = object.eClass().getEStructuralFeature(attributeName + "AsString");
            object.setListItem(asStringAttribute, index, String.valueOf((Double) value));
        }
    }
}
Also used : Query(org.bimserver.database.queries.om.Query) PackageMetaData(org.bimserver.emf.PackageMetaData) QueryPart(org.bimserver.database.queries.om.QueryPart) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) EEnum(org.eclipse.emf.ecore.EEnum) EClass(org.eclipse.emf.ecore.EClass) EAttribute(org.eclipse.emf.ecore.EAttribute) HashMapVirtualObject(org.bimserver.shared.HashMapVirtualObject) QueryObjectProvider(org.bimserver.database.queries.QueryObjectProvider) List(java.util.List) HashMapVirtualObject(org.bimserver.shared.HashMapVirtualObject) UserException(org.bimserver.shared.exceptions.UserException)

Example 2 with EEnum

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

the class DatabaseSession method readPrimitiveValue.

public Object readPrimitiveValue(EClassifier classifier, ByteBuffer buffer, QueryInterface query) {
    if (classifier == EcorePackage.eINSTANCE.getEString()) {
        int length = buffer.getInt();
        if (length != -1) {
            return BinUtils.readString(buffer, length);
        } else {
            return null;
        }
    } else if (classifier == EcorePackage.eINSTANCE.getEInt() || classifier == EcorePackage.eINSTANCE.getEIntegerObject()) {
        return buffer.getInt();
    } else if (classifier == EcorePackage.eINSTANCE.getELong() || classifier == EcorePackage.eINSTANCE.getELongObject()) {
        return buffer.getLong();
    } else if (classifier == EcorePackage.eINSTANCE.getEFloat() || classifier == EcorePackage.eINSTANCE.getEFloatObject()) {
        return buffer.getFloat();
    } else if (classifier == EcorePackage.eINSTANCE.getEDouble() || classifier == EcorePackage.eINSTANCE.getEDoubleObject()) {
        return buffer.getDouble();
    } else if (classifier == EcorePackage.eINSTANCE.getEBoolean() || classifier == EcorePackage.eINSTANCE.getEBooleanObject()) {
        return buffer.get() == 1;
    } else if (classifier == EcorePackage.eINSTANCE.getEDate()) {
        long val = buffer.getLong();
        if (val == -1L) {
            return null;
        }
        return new Date(val);
    } else if (classifier == EcorePackage.eINSTANCE.getEByteArray()) {
        int size = buffer.getInt();
        byte[] result = new byte[size];
        buffer.get(result);
        return result;
    } else if (classifier.getName().equals("Tristate")) {
        int ordinal = buffer.getInt();
        EEnum tristateEnum = query.getPackageMetaData().getEEnum("Tristate");
        return tristateEnum.getEEnumLiteral(ordinal).getInstance();
    } else if (classifier instanceof EEnum) {
        int ordinal = buffer.getInt();
        EEnum eEnum = (EEnum) classifier;
        return eEnum.getEEnumLiteral(ordinal).getInstance();
    } else {
        throw new RuntimeException("Unsupported type " + classifier.getName());
    }
}
Also used : EEnum(org.eclipse.emf.ecore.EEnum) Date(java.util.Date)

Example 3 with EEnum

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

the class DatabaseSession method writeList.

private void writeList(IdEObject object, 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) {
        EList<?> list = (EList<?>) object.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 {
                IdEObject listObject = (IdEObject) o;
                if (listObject.eClass().getEAnnotation("wrapped") != null || listObject.eClass().getEStructuralFeature("wrappedValue") != null) {
                    writeWrappedValue(object.getPid(), object.getRid(), listObject, buffer, packageMetaData);
                } else if (feature.getEAnnotation("twodimensionalarray") != null) {
                    EStructuralFeature lf = listObject.eClass().getEStructuralFeature("List");
                    writeList(listObject, buffer, packageMetaData, lf);
                } else {
                    writeReference(object, listObject, buffer, feature);
                }
            }
        }
    } else if (feature.getEType() instanceof EDataType) {
        EList<?> list = (EList<?>) object.eGet(feature);
        buffer.putInt(list.size());
        for (Object o : list) {
            writePrimitiveValue(feature, o, buffer);
        }
    }
}
Also used : EClass(org.eclipse.emf.ecore.EClass) BasicEList(org.eclipse.emf.common.util.BasicEList) EList(org.eclipse.emf.common.util.EList) AbstractEList(org.eclipse.emf.common.util.AbstractEList) 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) EEnum(org.eclipse.emf.ecore.EEnum)

Example 4 with EEnum

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

the class DatabaseSession method readList.

@SuppressWarnings("unchecked")
private Object readList(IdEObject idEObject, EClass originalQueryClass, ByteBuffer buffer, IfcModelInterface model, QueryInterface query, TodoList todoList, EStructuralFeature feature) throws BimserverDatabaseException {
    if (feature.getEType() instanceof EEnum) {
    } else if (feature.getEType() instanceof EClass) {
        if (buffer.capacity() == 1 && buffer.get(0) == -1) {
            buffer.position(buffer.position() + 1);
        } else {
            /*
				 * TODO There still is a problem with this,
				 * when readReference (and all calls beyond
				 * that call) alter (by opposites) this
				 * list, this list can potentially grow too
				 * large
				 * 
				 * Only can happen with non-unique
				 * references
				 */
            int listSize = buffer.getInt();
            AbstractEList<Object> list = (AbstractEList<Object>) idEObject.eGet(feature);
            for (int i = 0; i < listSize; i++) {
                if (feature.getEAnnotation("twodimensionalarray") != null) {
                    IdEObjectImpl newObject = createInternal((EClass) feature.getEType(), query);
                    Object result = readList(newObject, originalQueryClass, buffer, model, query, todoList, newObject.eClass().getEStructuralFeature("List"));
                    if (result != null) {
                        newObject.eSet(newObject.eClass().getEStructuralFeature("List"), result);
                    }
                    list.addUnique(newObject);
                } else {
                    IdEObject referencedObject = null;
                    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 = database.getEClassForCid((short) (-cid));
                        if (referenceClass == null) {
                            throw new BimserverDatabaseException("No class found for cid " + (-cid));
                        }
                        referencedObject = readWrappedValue(feature, buffer, referenceClass, query);
                    } else if (cid > 0) {
                        // positive cid means value is a
                        // reference
                        // to another record
                        EClass referenceClass = database.getEClassForCid(cid);
                        if (referenceClass == null) {
                            throw new BimserverDatabaseException("Cannot find class with cid " + cid);
                        }
                        buffer.position(buffer.position() - 2);
                        referencedObject = readReference(originalQueryClass, buffer, model, idEObject, feature, referenceClass, query, todoList);
                    }
                    if (referencedObject != null) {
                        if (!feature.getEType().isInstance(referencedObject)) {
                            throw new BimserverDatabaseException(referencedObject.getClass().getSimpleName() + " cannot be stored in list of type " + feature.getEType().getName() + " for feature " + feature.getName());
                        }
                        if (feature.isUnique()) {
                            list.add(referencedObject);
                        } else {
                            list.addUnique(referencedObject);
                        }
                    }
                }
            }
        }
    } else if (feature.getEType() instanceof EDataType) {
        int listSize = buffer.getInt();
        BasicEList<Object> list = new BasicEList<Object>(listSize);
        for (int i = 0; i < listSize; i++) {
            Object reference = readPrimitiveValue(feature.getEType(), buffer, query);
            if (reference != null) {
                list.addUnique(reference);
            }
        }
        return list;
    }
    return null;
}
Also used : IdEObjectImpl(org.bimserver.emf.IdEObjectImpl) AbstractEList(org.eclipse.emf.common.util.AbstractEList) BimserverDatabaseException(org.bimserver.BimserverDatabaseException) EClass(org.eclipse.emf.ecore.EClass) IdEObject(org.bimserver.emf.IdEObject) EDataType(org.eclipse.emf.ecore.EDataType) BasicEList(org.eclipse.emf.common.util.BasicEList) VirtualObject(org.bimserver.shared.VirtualObject) HashMapVirtualObject(org.bimserver.shared.HashMapVirtualObject) EObject(org.eclipse.emf.ecore.EObject) IdEObject(org.bimserver.emf.IdEObject) EEnum(org.eclipse.emf.ecore.EEnum)

Example 5 with EEnum

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

the class PackageMetaData method getEEnumLiteral.

public EEnumLiteral getEEnumLiteral(String enumName, String literalName) {
    EClassifier eClassifier = ePackage.getEClassifier(enumName);
    if (eClassifier == null) {
        throw new RuntimeException("Classifier " + enumName + " not found in package " + ePackage.getName());
    }
    if (eClassifier instanceof EEnum) {
        EEnum eEnum = (EEnum) eClassifier;
        EEnumLiteral literal = eEnum.getEEnumLiteral(literalName);
        if (literal == null) {
            throw new RuntimeException("No enum literal " + literalName + " found on " + ePackage.getName() + "." + enumName);
        }
        return literal;
    } else {
        throw new RuntimeException("Classifier " + enumName + " is not of type enum");
    }
}
Also used : EClassifier(org.eclipse.emf.ecore.EClassifier) EEnumLiteral(org.eclipse.emf.ecore.EEnumLiteral) EEnum(org.eclipse.emf.ecore.EEnum)

Aggregations

EEnum (org.eclipse.emf.ecore.EEnum)50 EClass (org.eclipse.emf.ecore.EClass)28 EEnumLiteral (org.eclipse.emf.ecore.EEnumLiteral)22 EDataType (org.eclipse.emf.ecore.EDataType)13 EPackage (org.eclipse.emf.ecore.EPackage)13 EStructuralFeature (org.eclipse.emf.ecore.EStructuralFeature)11 Grammar (org.eclipse.xtext.Grammar)10 HashMapVirtualObject (org.bimserver.shared.HashMapVirtualObject)9 Test (org.junit.Test)9 EClassifier (org.eclipse.emf.ecore.EClassifier)8 EReference (org.eclipse.emf.ecore.EReference)7 BimserverDatabaseException (org.bimserver.BimserverDatabaseException)6 IdEObject (org.bimserver.emf.IdEObject)6 EObject (org.eclipse.emf.ecore.EObject)6 EnumRule (org.eclipse.xtext.EnumRule)6 List (java.util.List)5 EnumLiteralDeclaration (org.eclipse.xtext.EnumLiteralDeclaration)5 IdEObjectImpl (org.bimserver.emf.IdEObjectImpl)4 VirtualObject (org.bimserver.shared.VirtualObject)4 EAttribute (org.eclipse.emf.ecore.EAttribute)4