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));
}
}
}
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());
}
}
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);
}
}
}
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;
}
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");
}
}
Aggregations