Search in sources :

Example 1 with IfcGloballyUniqueId

use of org.bimserver.models.ifc2x3tc1.IfcGloballyUniqueId in project BIMserver by opensourceBIM.

the class SharedJsonSerializer method writeObject.

private void writeObject(IdEObject object) throws IOException {
    if (((IdEObjectImpl) object).getLoadingState() != State.LOADED) {
        print("{");
        print("\"_i\":" + object.getOid() + ",");
        print("\"_t\":\"" + object.eClass().getName() + "\",");
        print("\"_s\":0");
        print("}\n");
    } else {
        print("{");
        print("\"_i\":" + object.getOid() + ",");
        print("\"_t\":\"" + object.eClass().getName() + "\",");
        print("\"_s\":1");
        for (EStructuralFeature eStructuralFeature : object.eClass().getEAllStructuralFeatures()) {
            if (eStructuralFeature.getEAnnotation("nolazyload") == null && (eStructuralFeature.getEAnnotation("hidden") == null || includeHidden)) {
                if (eStructuralFeature instanceof EReference) {
                    Object value = object.eGet(eStructuralFeature);
                    if (value != null) {
                        if (eStructuralFeature.isMany()) {
                            List<?> list = (List<?>) value;
                            if (SERIALIZE_EMPTY_LISTS || !list.isEmpty()) {
                                print(",");
                                int wrapped = 0;
                                int referred = 0;
                                for (Object o : list) {
                                    if (((IdEObject) o).eClass().getEAnnotation("wrapped") != null) {
                                        // A little tricky,
                                        // can we assume if
                                        // one object in
                                        // this list is
                                        // embedded, they
                                        // all are?
                                        wrapped++;
                                    } else {
                                        referred++;
                                    }
                                }
                                if (wrapped == 0 && referred != 0) {
                                    print("\"_r" + eStructuralFeature.getName() + "\":[");
                                } else if (wrapped != 0 && referred == 0) {
                                    print("\"_e" + eStructuralFeature.getName() + "\":[");
                                } else if (wrapped == 0 && referred == 0) {
                                // should not happen
                                } else {
                                    // both, this can occur,
                                    // for example
                                    // IfcTrimmedCurve.Trim1
                                    print("\"_e" + eStructuralFeature.getName() + "\":[");
                                }
                                boolean f = true;
                                for (Object o : list) {
                                    if (!f) {
                                        print(",");
                                    } else {
                                        f = false;
                                    }
                                    IdEObject ref = (IdEObject) o;
                                    EClass eClass = ((IdEObject) o).eClass();
                                    if (ref.getOid() < 0 || eClass.getEAnnotation("wrapped") != null || eStructuralFeature.getEAnnotation("dbembed") != null) {
                                        write(ref);
                                    } else {
                                        if (wrapped != 0 && referred != 0) {
                                            // Special
                                            // situation,
                                            // where we have
                                            // to construct
                                            // an object
                                            // around the
                                            // OID to make
                                            // it
                                            // distinguishable
                                            // from embedded
                                            // objects
                                            print("{");
                                            print("\"_i\":");
                                            print("" + ref.getOid());
                                            print(",\"_t\":");
                                            print("\"" + ref.eClass().getName() + "\"");
                                            print("}");
                                        } else {
                                            print("" + ref.getOid());
                                        }
                                    }
                                }
                                print("]");
                            }
                        } else {
                            print(",");
                            IdEObject ref = (IdEObject) value;
                            if (ref instanceof IfcGloballyUniqueId) {
                                print("\"" + eStructuralFeature.getName() + "\":");
                                writePrimitive(eStructuralFeature, ((IfcGloballyUniqueId) ref).getWrappedValue());
                            } else if (((IdEObject) ref).eClass().getEAnnotation("wrapped") != null) {
                                print("\"_e" + eStructuralFeature.getName() + "\":");
                                write(ref);
                            } else if (eStructuralFeature.getEAnnotation("dbembed") != null) {
                                print("\"_e" + eStructuralFeature.getName() + "\":");
                                writeEmbedded(ref);
                            } else {
                                print("\"_r" + eStructuralFeature.getName() + "\":{\"_i\":" + ref.getOid() + ",\"_t\":\"" + ref.eClass().getName() + "\"}");
                            }
                        }
                    }
                } else {
                    Object value = object.eGet(eStructuralFeature);
                    if (value != null) {
                        if (eStructuralFeature.isMany()) {
                            List<?> list = (List<?>) value;
                            if (SERIALIZE_EMPTY_LISTS || !list.isEmpty()) {
                                print(",");
                                print("\"" + eStructuralFeature.getName() + "\":[");
                                boolean f = true;
                                for (Object o : list) {
                                    if (!f) {
                                        print(",");
                                    } else {
                                        f = false;
                                    }
                                    writePrimitive(eStructuralFeature, o);
                                }
                                print("]");
                            }
                        } else {
                            print(",");
                            print("\"" + eStructuralFeature.getName() + "\":");
                            writePrimitive(eStructuralFeature, value);
                        }
                    }
                }
            }
        }
        print("}\n");
    }
}
Also used : EClass(org.eclipse.emf.ecore.EClass) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) IfcGloballyUniqueId(org.bimserver.models.ifc2x3tc1.IfcGloballyUniqueId) List(java.util.List) EReference(org.eclipse.emf.ecore.EReference)

Example 2 with IfcGloballyUniqueId

use of org.bimserver.models.ifc2x3tc1.IfcGloballyUniqueId in project BIMserver by opensourceBIM.

the class EmfJsonSerializer method writeObject.

public void writeObject(IdEObject object) throws IOException {
    if (((IdEObjectImpl) object).getLoadingState() != State.LOADED) {
        print("{");
        print("\"_i\":" + object.getOid() + ",");
        print("\"_u\":\"" + object.getUuid() + "\",");
        print("\"_r\":" + object.getRid() + ",");
        print("\"_t\":\"" + object.eClass().getName() + "\",");
        print("\"_s\":0");
        print("}\n");
    } else {
        print("{");
        print("\"_i\":" + object.getOid() + ",");
        print("\"_u\":\"" + object.getUuid() + "\",");
        print("\"_r\":" + object.getRid() + ",");
        print("\"_t\":\"" + object.eClass().getName() + "\",");
        print("\"_s\":1");
        for (EStructuralFeature eStructuralFeature : object.eClass().getEAllStructuralFeatures()) {
            if (eStructuralFeature.getEAnnotation("nolazyload") == null && (eStructuralFeature.getEAnnotation("hidden") == null || includeHidden)) {
                if (eStructuralFeature instanceof EReference) {
                    Object value = object.eGet(eStructuralFeature);
                    if (value != null) {
                        if (eStructuralFeature.isMany()) {
                            List<?> list = (List<?>) value;
                            if (serializeEmptyLists || !list.isEmpty()) {
                                print(",");
                                int wrapped = 0;
                                int referred = 0;
                                for (Object o : list) {
                                    if (((IdEObject) o).eClass().getEAnnotation("wrapped") != null) {
                                        // A little tricky,
                                        // can we assume if
                                        // one object in
                                        // this list is
                                        // embedded, they
                                        // all are?
                                        wrapped++;
                                    } else {
                                        referred++;
                                    }
                                }
                                if (wrapped == 0 && referred != 0) {
                                    print("\"_r" + eStructuralFeature.getName() + "\":[");
                                } else if (wrapped != 0 && referred == 0) {
                                    print("\"_e" + eStructuralFeature.getName() + "\":[");
                                } else if (wrapped == 0 && referred == 0) {
                                // should not happen
                                } else {
                                    // both, this can occur,
                                    // for example
                                    // IfcTrimmedCurve.Trim1
                                    print("\"_e" + eStructuralFeature.getName() + "\":[");
                                }
                                boolean f = true;
                                for (Object o : list) {
                                    if (!f) {
                                        print(",");
                                    } else {
                                        f = false;
                                    }
                                    IdEObject ref = (IdEObject) o;
                                    EClass eClass = ((IdEObject) o).eClass();
                                    if (ref.getOid() < 0 || eClass.getEAnnotation("wrapped") != null || eStructuralFeature.getEAnnotation("dbembed") != null) {
                                        write(ref);
                                    } else {
                                        if (wrapped != 0 && referred != 0) {
                                            // Special
                                            // situation,
                                            // where we have
                                            // to construct
                                            // an object
                                            // around the
                                            // OID to make
                                            // it
                                            // distinguishable
                                            // from embedded
                                            // objects
                                            print("{");
                                            print("\"_i\":");
                                            print("" + ref.getOid());
                                            print(",\"_t\":");
                                            print("\"" + ref.eClass().getName() + "\"");
                                            print("}");
                                        } else {
                                            print("{");
                                            print("\"_i\":");
                                            print("" + ref.getOid() + ",");
                                            print("\"_t\":");
                                            print("\"" + ref.eClass().getName() + "\"");
                                            print("}");
                                        }
                                    }
                                }
                                print("]");
                            }
                        } else {
                            print(",");
                            IdEObject ref = (IdEObject) value;
                            if (ref instanceof IfcGloballyUniqueId) {
                                print("\"" + eStructuralFeature.getName() + "\":");
                                writePrimitive(eStructuralFeature, ((IfcGloballyUniqueId) ref).getWrappedValue());
                            } else if (((IdEObject) ref).eClass().getEAnnotation("wrapped") != null) {
                                print("\"_e" + eStructuralFeature.getName() + "\":");
                                write(ref);
                            } else if (eStructuralFeature.getEAnnotation("dbembed") != null) {
                                print("\"_e" + eStructuralFeature.getName() + "\":");
                                writeEmbedded(ref);
                            } else {
                                print("\"_r" + eStructuralFeature.getName() + "\":{\"_i\":" + ref.getOid() + ",\"_t\":\"" + ref.eClass().getName() + "\"}");
                            }
                        }
                    }
                } else {
                    Object value = object.eGet(eStructuralFeature);
                    if (value != null) {
                        if (eStructuralFeature.isMany()) {
                            List<?> list = (List<?>) value;
                            if (serializeEmptyLists || !list.isEmpty()) {
                                print(",");
                                print("\"" + eStructuralFeature.getName() + "\":[");
                                boolean f = true;
                                for (Object o : list) {
                                    if (!f) {
                                        print(",");
                                    } else {
                                        f = false;
                                    }
                                    writePrimitive(eStructuralFeature, o);
                                }
                                print("]");
                            }
                        } else {
                            print(",");
                            print("\"" + eStructuralFeature.getName() + "\":");
                            writePrimitive(eStructuralFeature, value);
                        }
                    }
                }
            }
        }
        print("}\n");
    }
}
Also used : EClass(org.eclipse.emf.ecore.EClass) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) IfcGloballyUniqueId(org.bimserver.models.ifc2x3tc1.IfcGloballyUniqueId) List(java.util.List) EReference(org.eclipse.emf.ecore.EReference)

Example 3 with IfcGloballyUniqueId

use of org.bimserver.models.ifc2x3tc1.IfcGloballyUniqueId in project BIMserver by opensourceBIM.

the class RevisionMerger method copy.

@SuppressWarnings({ "unchecked", "rawtypes" })
private IdEObject copy(IfcModel target, IdEObject idEObject, boolean limitToNonGuids) throws IfcModelInterfaceException {
    if (target.contains(idEObject.getOid())) {
        return target.get(idEObject.getOid());
    }
    IdEObject newObject = (IdEObject) idEObject.eClass().getEPackage().getEFactoryInstance().create(idEObject.eClass());
    ((IdEObjectImpl) newObject).setOid(idEObject.getOid());
    if (newObject.getOid() != -1) {
        target.add(newObject.getOid(), newObject);
    }
    for (EAttribute eAttribute : newObject.eClass().getEAllAttributes()) {
        newObject.eSet(eAttribute, idEObject.eGet(eAttribute));
    }
    for (EReference eReference : newObject.eClass().getEAllReferences()) {
        Object referencedObject = idEObject.eGet(eReference);
        if (referencedObject instanceof IdEObject) {
            IdEObject refEObject = (IdEObject) referencedObject;
            if (!limitToNonGuids || !(referencedObject instanceof IfcRoot) && !(referencedObject instanceof IfcGloballyUniqueId)) {
                newObject.eSet(eReference, copy(target, refEObject, limitToNonGuids));
            }
        } else if (referencedObject instanceof List) {
            List list = (List) referencedObject;
            List newList = (List) newObject.eGet(eReference);
            for (Object o : list) {
                if (!limitToNonGuids || !(o instanceof IfcRoot) && !(o instanceof IfcGloballyUniqueId)) {
                    IdEObject listObject = (IdEObject) o;
                    IdEObject smartCopy = copy(target, listObject, limitToNonGuids);
                    if (!newList.contains(smartCopy)) {
                        newList.add(smartCopy);
                    }
                }
            }
        }
    }
    return 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) IfcGloballyUniqueId(org.bimserver.models.ifc2x3tc1.IfcGloballyUniqueId) IdEObject(org.bimserver.emf.IdEObject) List(java.util.List) EReference(org.eclipse.emf.ecore.EReference)

Example 4 with IfcGloballyUniqueId

use of org.bimserver.models.ifc2x3tc1.IfcGloballyUniqueId in project BIMserver by opensourceBIM.

the class RevisionMerger method fixNonGuidObjects.

@SuppressWarnings({ "rawtypes", "unchecked" })
private void fixNonGuidObjects() throws IfcModelInterfaceException {
    Set<List> clearedLists = new HashSet<List>();
    for (IdEObject idEObject : newModel.getValues()) {
        if (idEObject instanceof IfcRoot) {
            String guid = ((IfcRoot) idEObject).getGlobalId();
            for (EReference eReference : idEObject.eClass().getEAllReferences()) {
                Object referencedObject = idEObject.eGet(eReference);
                if (eReference.isMany()) {
                    List list = (List) referencedObject;
                    List newList = (List) resultModel.getByGuid(guid).eGet(eReference);
                    boolean listIsCleared = false;
                    for (Object o : list) {
                        if (!(o instanceof IfcRoot) && !(o instanceof IfcGloballyUniqueId)) {
                            if (!listIsCleared) {
                                newList.clear();
                                listIsCleared = true;
                            }
                            IdEObject referencedIDEObject = (IdEObject) o;
                            if (resultModel.contains(referencedIDEObject.getOid())) {
                                newList.add(resultModel.get(referencedIDEObject.getOid()));
                            } else {
                                IdEObject smartCopy = copy(resultModel, referencedIDEObject, true);
                                newList.add(smartCopy);
                            }
                        }
                    }
                } else {
                    if (referencedObject == null) {
                        if (resultModel.getByGuid(guid).eGet(eReference) != null && eReference.getEOpposite() != null) {
                            IdEObject x = (IdEObject) resultModel.getByGuid(guid).eGet(eReference);
                            if (eReference.getEOpposite().isMany()) {
                                List l = (List) x.eGet(eReference.getEOpposite());
                                if (!clearedLists.contains(l)) {
                                    clearedLists.add(l);
                                    l.clear();
                                }
                            } else {
                                x.eSet(eReference.getEOpposite(), null);
                            }
                        }
                        resultModel.getByGuid(guid).eSet(eReference, null);
                    } else {
                        if (!(referencedObject instanceof IfcRoot) && !(referencedObject instanceof IfcGloballyUniqueId)) {
                            IdEObject referencedIDEObject = (IdEObject) referencedObject;
                            if (resultModel.contains(referencedIDEObject.getOid())) {
                                if (resultModel.getByGuid(guid).eGet(eReference) != null && eReference.getEOpposite() != null) {
                                    IdEObject x = (IdEObject) resultModel.getByGuid(guid).eGet(eReference);
                                    if (eReference.getEOpposite().isMany()) {
                                        List l = (List) x.eGet(eReference.getEOpposite());
                                        if (!clearedLists.contains(l)) {
                                            clearedLists.add(l);
                                            l.clear();
                                        }
                                    } else {
                                        x.eSet(eReference.getEOpposite(), null);
                                    }
                                }
                                resultModel.getByGuid(guid).eSet(eReference, resultModel.get(referencedIDEObject.getOid()));
                            } else {
                                IdEObject smartCopy = copy(resultModel, referencedIDEObject, true);
                                if (resultModel.getByGuid(guid).eGet(eReference) != null && eReference.getEOpposite() != null) {
                                    IdEObject re = (IdEObject) resultModel.getByGuid(guid).eGet(eReference);
                                    if (eReference.getEOpposite().isMany()) {
                                        List l = (List) re.eGet(eReference.getEOpposite());
                                        if (!clearedLists.contains(l)) {
                                            clearedLists.add(l);
                                            l.clear();
                                        }
                                    } else {
                                        re.eSet(eReference.getEOpposite(), null);
                                    }
                                }
                                resultModel.getByGuid(guid).eSet(eReference, smartCopy);
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : IdEObject(org.bimserver.emf.IdEObject) IfcRoot(org.bimserver.models.ifc2x3tc1.IfcRoot) IfcGloballyUniqueId(org.bimserver.models.ifc2x3tc1.IfcGloballyUniqueId) List(java.util.List) IdEObject(org.bimserver.emf.IdEObject) EReference(org.eclipse.emf.ecore.EReference) HashSet(java.util.HashSet)

Example 5 with IfcGloballyUniqueId

use of org.bimserver.models.ifc2x3tc1.IfcGloballyUniqueId in project BIMserver by opensourceBIM.

the class SharedJsonDeserializer method readPrimitive.

private Object readPrimitive(JsonReader jsonReader, EStructuralFeature eStructuralFeature) throws IOException, DeserializeException {
    EClassifier eClassifier = eStructuralFeature.getEType();
    if (eClassifier == EcorePackage.eINSTANCE.getEString()) {
        return jsonReader.nextString();
    } else if (eClassifier == EcorePackage.eINSTANCE.getEDouble()) {
        return jsonReader.nextDouble();
    } else if (eClassifier == EcorePackage.eINSTANCE.getEBoolean()) {
        return jsonReader.nextBoolean();
    } else if (eClassifier == EcorePackage.eINSTANCE.getEInt()) {
        return jsonReader.nextInt();
    } else if (eClassifier == EcorePackage.eINSTANCE.getEShort()) {
        return (short) jsonReader.nextInt();
    } else if (eClassifier == EcorePackage.eINSTANCE.getELong()) {
        return jsonReader.nextLong();
    } else if (eClassifier == EcorePackage.eINSTANCE.getEIntegerObject()) {
        return jsonReader.nextInt();
    } else if (eClassifier == EcorePackage.eINSTANCE.getEByteArray()) {
        return Base64.decodeBase64(jsonReader.nextString());
    } else if (eClassifier == EcorePackage.eINSTANCE.getEDate()) {
        long timestamp = jsonReader.nextLong();
        return new Date(timestamp);
    } else if (eClassifier == EcorePackage.eINSTANCE.getEFloat()) {
        return (float) jsonReader.nextDouble();
    } else if (eClassifier == EcorePackage.eINSTANCE.getEEnum()) {
        EEnum eEnum = (EEnum) eStructuralFeature.getEType();
        return eEnum.getEEnumLiteral(jsonReader.nextString()).getInstance();
    } else if (eClassifier instanceof EClass) {
        if (eStructuralFeature.getEType().getName().equals("IfcGloballyUniqueId")) {
            IfcGloballyUniqueId ifcGloballyUniqueId = Ifc2x3tc1Factory.eINSTANCE.createIfcGloballyUniqueId();
            ifcGloballyUniqueId.setWrappedValue(jsonReader.nextString());
            return ifcGloballyUniqueId;
        } else if (eStructuralFeature.getEType().getName().equals("Vector3f")) {
            jsonReader.beginObject();
            Vector3f vector3f = GeometryFactory.eINSTANCE.createVector3f();
            jsonReader.nextName();
            jsonReader.nextString();
            jsonReader.nextName();
            vector3f.setX(jsonReader.nextDouble());
            jsonReader.nextName();
            vector3f.setY(jsonReader.nextDouble());
            jsonReader.nextName();
            vector3f.setZ(jsonReader.nextDouble());
            jsonReader.endObject();
            return vector3f;
        } else {
            throw new DeserializeException(DeserializerErrorCode.UNIMPLEMENTED_BIMSERVER_FEATURE, eStructuralFeature.getEType().getName() + " not implemented");
        }
    } else if (eClassifier instanceof EEnum) {
        EEnum eEnum = (EEnum) eStructuralFeature.getEType();
        if (jsonReader.peek() == JsonToken.BOOLEAN) {
            return eEnum.getEEnumLiteral(jsonReader.nextBoolean() ? "TRUE" : "FALSE").getInstance();
        } else {
            return eEnum.getEEnumLiteral(jsonReader.nextString()).getInstance();
        }
    } else {
        throw new DeserializeException(DeserializerErrorCode.UNIMPLEMENTED_BIMSERVER_FEATURE, "Unimplemented type " + eStructuralFeature.getEType().getName());
    }
}
Also used : EClass(org.eclipse.emf.ecore.EClass) Vector3f(org.bimserver.models.geometry.Vector3f) EClassifier(org.eclipse.emf.ecore.EClassifier) IfcGloballyUniqueId(org.bimserver.models.ifc2x3tc1.IfcGloballyUniqueId) EEnum(org.eclipse.emf.ecore.EEnum) DeserializeException(org.bimserver.plugins.deserializers.DeserializeException) Date(java.util.Date)

Aggregations

IfcGloballyUniqueId (org.bimserver.models.ifc2x3tc1.IfcGloballyUniqueId)6 List (java.util.List)5 EReference (org.eclipse.emf.ecore.EReference)5 EClass (org.eclipse.emf.ecore.EClass)4 EStructuralFeature (org.eclipse.emf.ecore.EStructuralFeature)3 IdEObject (org.bimserver.emf.IdEObject)2 IfcRoot (org.bimserver.models.ifc2x3tc1.IfcRoot)2 DeserializeException (org.bimserver.plugins.deserializers.DeserializeException)2 EAttribute (org.eclipse.emf.ecore.EAttribute)2 JsonToken (com.google.gson.stream.JsonToken)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 UUID (java.util.UUID)1 Attribute (nl.tue.buildingsmart.schema.Attribute)1 EntityDefinition (nl.tue.buildingsmart.schema.EntityDefinition)1 InverseAttribute (nl.tue.buildingsmart.schema.InverseAttribute)1 IdEObjectImpl (org.bimserver.emf.IdEObjectImpl)1 Vector3f (org.bimserver.models.geometry.Vector3f)1 ListWaitingObject (org.bimserver.shared.ListWaitingObject)1 WaitingList (org.bimserver.shared.WaitingList)1