Search in sources :

Example 1 with AbstractEList

use of org.eclipse.emf.common.util.AbstractEList in project BIMserver by opensourceBIM.

the class ModelHelper method copy.

@SuppressWarnings({ "unchecked", "rawtypes" })
private IdEObject copy(EClass originalEClass, IdEObject original, boolean setOid, ObjectIDM objectIDM) throws IfcModelInterfaceException {
    if (!((IdEObjectImpl) original).isLoadedOrLoading()) {
        return null;
    }
    if (converted.containsKey(original)) {
        return converted.get(original);
    }
    IdEObject newObject = (IdEObject) objectFactory.create(original.eClass());
    ((IdEObjectImpl) newObject).setPid(original.getPid());
    ((IdEObjectImpl) newObject).setLoadingState(State.LOADED);
    long oid = -1;
    if (keepOriginalOids) {
        oid = original.getOid();
        ((IdEObjectImpl) newObject).setOid(oid);
    } else {
        if (newObject.getOid() == -1) {
            if (oidProvider != null) {
                oid = oidProvider.newOid(newObject.eClass());
            } else {
                oid = original.getOid();
            }
        }
    }
    if (setOid && newObject.getOid() == -1) {
        ((IdEObjectImpl) newObject).setOid(oid);
    }
    converted.put(original, newObject);
    if (newObject.eClass().getEAnnotation("wrapped") == null) {
        targetModel.add(newObject.getOid(), newObject);
    }
    if (inverseFixes.containsKey(original.getOid())) {
        InverseFix inverseFix = inverseFixes.get(original.getOid());
        inverseFix.apply(newObject);
    }
    for (EStructuralFeature eStructuralFeature : original.eClass().getEAllStructuralFeatures()) {
        boolean canFollow = objectIDM == null || objectIDM.shouldFollowReference(originalEClass, original.eClass(), eStructuralFeature);
        Object get = original.eGet(eStructuralFeature);
        if (eStructuralFeature instanceof EAttribute) {
            if (get instanceof List) {
                List list = (List) get;
                List targetList = (List) newObject.eGet(eStructuralFeature);
                for (Object o : list) {
                    targetList.add(o);
                }
            } else {
                newObject.eSet(eStructuralFeature, get);
            }
        } else if (eStructuralFeature instanceof EReference) {
            if (!canFollow) {
                continue;
            }
            if (get == null) {
            } else {
                if (eStructuralFeature.isMany()) {
                    EList<EObject> list = (EList<EObject>) get;
                    AbstractEList<EObject> toList = (AbstractEList<EObject>) newObject.eGet(eStructuralFeature);
                    for (Object o : list) {
                        if (converted.containsKey(o)) {
                            toList.addUnique(converted.get(o));
                        } else {
                            if (canFollow) {
                                IdEObject result = copy(originalEClass, (IdEObject) o, setOid, objectIDM);
                                if (result != null) {
                                    toList.addUnique(result);
                                }
                            } else {
                                // Findbugs says canFollow is always true at this point and it's right, TODO
                                if (eStructuralFeature.getName().equals("RelatedElements")) {
                                    inverseFixes.put(((IdEObject) o).getOid(), new InverseFix(Ifc2x3tc1Package.eINSTANCE.getIfcRelContainedInSpatialStructure_RelatedElements(), newObject));
                                }
                            }
                        }
                    }
                } else {
                    if (converted.containsKey(get)) {
                        newObject.eSet(eStructuralFeature, converted.get(get));
                    } else {
                        if (canFollow) {
                            newObject.eSet(eStructuralFeature, copy(originalEClass, (IdEObject) get, setOid, objectIDM));
                        }
                    }
                }
            }
        }
    }
    return newObject;
}
Also used : IdEObjectImpl(org.bimserver.emf.IdEObjectImpl) AbstractEList(org.eclipse.emf.common.util.AbstractEList) IdEObject(org.bimserver.emf.IdEObject) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) EAttribute(org.eclipse.emf.ecore.EAttribute) EList(org.eclipse.emf.common.util.EList) AbstractEList(org.eclipse.emf.common.util.AbstractEList) IdEObject(org.bimserver.emf.IdEObject) EObject(org.eclipse.emf.ecore.EObject) IdEObject(org.bimserver.emf.IdEObject) EObject(org.eclipse.emf.ecore.EObject) EList(org.eclipse.emf.common.util.EList) AbstractEList(org.eclipse.emf.common.util.AbstractEList) List(java.util.List) EReference(org.eclipse.emf.ecore.EReference)

Example 2 with AbstractEList

use of org.eclipse.emf.common.util.AbstractEList in project BIMserver by opensourceBIM.

the class DownloadByNewJsonQueryDatabaseAction method execute.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public IfcModelInterface execute() throws UserException, BimserverLockConflictException, BimserverDatabaseException {
    List<String> projectNames = new ArrayList<>();
    PluginConfiguration serializerPluginConfiguration = getDatabaseSession().get(StorePackage.eINSTANCE.getPluginConfiguration(), serializerOid, OldQuery.getDefault());
    SerializerPlugin serializerPlugin = (SerializerPlugin) getBimServer().getPluginManager().getPlugin(serializerPluginConfiguration.getPluginDescriptor().getPluginClassName(), true);
    Set<String> geometryFields = serializerPlugin.getRequiredGeometryFields();
    setProgress("Querying database...", -1);
    for (long roid : roids) {
        Revision revision = getDatabaseSession().get(StorePackage.eINSTANCE.getRevision(), roid, OldQuery.getDefault());
        projectNames.add(revision.getProject().getName() + "." + revision.getId());
    }
    String name = Joiner.on("-").join(projectNames);
    PackageMetaData lastPackageMetaData = null;
    Project lastProject = null;
    IfcModelSet ifcModelSet = new IfcModelSet();
    Map<Integer, Long> pidRoidMap = new HashMap<>();
    Set<CanInclude> updatedIncludes = new HashSet<>();
    for (long roid : roids) {
        Revision revision = getDatabaseSession().get(StorePackage.eINSTANCE.getRevision(), roid, OldQuery.getDefault());
        lastProject = revision.getProject();
        PackageMetaData packageMetaData = getBimServer().getMetaDataManager().getPackageMetaData(revision.getProject().getSchema());
        lastPackageMetaData = packageMetaData;
        JsonQueryObjectModelConverter converter = new JsonQueryObjectModelConverter(packageMetaData);
        try {
            ObjectNode queryObject = OBJECT_MAPPER.readValue(json, ObjectNode.class);
            converter.setCopyExternalDefines(true);
            Query query = converter.parseJson("query", (ObjectNode) queryObject);
            // We now have the original user query, we'll amend it a little bit to include geometry, but only if the serializer requires certain fields
            // TODO only checking the base level of the query now, should check recursive and possibly more
            // System.out.println(new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(new JsonQueryObjectModelConverter(packageMetaData).toJson(query)));
            pidRoidMap.put(revision.getProject().getId(), roid);
            IfcModelInterface ifcModel = getDatabaseSession().createServerModel(packageMetaData, pidRoidMap);
            ifcModelSet.add(ifcModel);
            Query secondQuery = new Query("second", query.getPackageMetaData());
            QueryPart queryPart2 = secondQuery.createQueryPart();
            if (geometryFields != null) {
                for (String defineName : query.getDefines().keySet()) {
                    Include define = query.getDefine(defineName);
                    apply(geometryFields, packageMetaData, define, updatedIncludes);
                }
                for (QueryPart queryPart : query.getQueryParts()) {
                    apply(geometryFields, packageMetaData, queryPart, updatedIncludes);
                    if (queryPart.hasReferences()) {
                        for (Reference reference : queryPart.getReferences()) {
                            apply(geometryFields, packageMetaData, reference.getInclude(), updatedIncludes);
                        }
                    }
                    if (queryPart.hasIncludes()) {
                        apply(geometryFields, packageMetaData, queryPart, updatedIncludes);
                    }
                    if (queryPart.hasTypes()) {
                        for (TypeDef typeDef : queryPart.getTypes()) {
                            if (packageMetaData.getEClass("IfcProduct").isSuperTypeOf(typeDef.geteClass())) {
                                Include include = queryPart.createInclude();
                                applyFields(geometryFields, new TypeDef(packageMetaData.getEClass("IfcProduct"), true), include);
                            }
                        }
                    }
                    if (queryPart.isIncludeAllFields()) {
                        Include newInclude = queryPart.createInclude();
                        applyFields(geometryFields, new TypeDef(packageMetaData.getEClass("IfcProduct"), true), newInclude);
                    }
                }
            }
            QueryObjectProvider queryObjectProvider = new QueryObjectProvider(getDatabaseSession(), getBimServer(), query, Collections.singleton(roid), packageMetaData);
            HashMapVirtualObject next = queryObjectProvider.next();
            while (next != null) {
                IdEObject newObject = packageMetaData.create(next.eClass());
                IdEObjectImpl idEObjectImpl = (IdEObjectImpl) newObject;
                idEObjectImpl.setPid(revision.getProject().getId());
                idEObjectImpl.setOid(next.getOid());
                for (EAttribute eAttribute : newObject.eClass().getEAllAttributes()) {
                    Object value = next.eGet(eAttribute);
                    if (eAttribute.isMany()) {
                        List<?> list = (List<?>) value;
                        if (list != null) {
                            AbstractEList targetList = (AbstractEList) newObject.eGet(eAttribute);
                            for (Object item : list) {
                                targetList.addUnique(item);
                            }
                        }
                    } else {
                        if (value != null || eAttribute.isUnsettable()) {
                            newObject.eSet(eAttribute, value);
                        }
                    }
                }
                ifcModel.add(next.getOid(), newObject);
                queryPart2.addOid(next.getOid());
                next = queryObjectProvider.next();
            }
            if (geometryFields != null) {
                for (QueryPart queryPart : secondQuery.getQueryParts()) {
                    apply(geometryFields, packageMetaData, queryPart, updatedIncludes);
                // if (queryPart.hasReferences()) {
                // for (Reference reference : queryPart.getReferences()) {
                // apply(geometryFields, packageMetaData, reference.getInclude(), updatedIncludes);
                // }
                // }
                // if (queryPart.hasIncludes()) {
                // apply(geometryFields, packageMetaData, queryPart, updatedIncludes);
                // }
                // if (queryPart.hasTypes()) {
                // for (TypeDef typeDef : queryPart.getTypes()) {
                // if (packageMetaData.getEClass("IfcProduct").isSuperTypeOf(typeDef.geteClass())) {
                // Include include = queryPart.createInclude();
                // applyFields(geometryFields, new TypeDef(packageMetaData.getEClass("IfcProduct"), true), include);
                // }
                // }
                // }
                // if (queryPart.isIncludeAllFields()) {
                // Include newInclude = queryPart.createInclude();
                // applyFields(geometryFields, new TypeDef(packageMetaData.getEClass("IfcProduct"), true), newInclude);
                // }
                }
            }
            queryObjectProvider = new QueryObjectProvider(getDatabaseSession(), getBimServer(), secondQuery, Collections.singleton(roid), packageMetaData);
            System.out.println(converter.toJson(secondQuery));
            next = queryObjectProvider.next();
            while (next != null) {
                IdEObject idEObject = ifcModel.get(next.getOid());
                if (idEObject == null) {
                    throw new RuntimeException("Object not found");
                }
                if (idEObject.eClass() != next.eClass()) {
                    // Something is wrong
                    throw new RuntimeException("Classes not the same");
                }
                for (EReference eReference : idEObject.eClass().getEAllReferences()) {
                    if (eReference.isMany()) {
                        List refOids = (List) next.eGet(eReference);
                        AbstractEList<IdEObject> list = (AbstractEList<IdEObject>) idEObject.eGet(eReference);
                        if (refOids != null) {
                            for (Object refOid : refOids) {
                                if (refOid instanceof Long) {
                                    IdEObject ref = ifcModel.get((long) refOid);
                                    if (ref != null) {
                                        if (eReference.isUnique()) {
                                            list.add(ref);
                                        } else {
                                            list.addUnique(ref);
                                        }
                                    // } else {
                                    // throw new BimserverDatabaseException("Could not find reference to " + eReference.getName() + " " + refOid);
                                    }
                                } else if (refOid instanceof HashMapWrappedVirtualObject) {
                                // IdEObject ref = ifcModel.get(((HashMapWrappedVirtualObject) refOid).get);
                                // if (ref != null) {
                                // list.add(ref);
                                // }
                                } else if (refOid instanceof HashMapVirtualObject) {
                                    HashMapVirtualObject hashMapVirtualObject = (HashMapVirtualObject) refOid;
                                    IdEObject listObject = packageMetaData.create(hashMapVirtualObject.eClass());
                                    List subList = (List<?>) hashMapVirtualObject.get("List");
                                    List newList = (List<?>) listObject.eGet(listObject.eClass().getEStructuralFeature("List"));
                                    for (Object o : subList) {
                                        if (o instanceof HashMapWrappedVirtualObject) {
                                            newList.add(convertWrapped(revision, ifcModel, (HashMapWrappedVirtualObject) o));
                                        } else if (o instanceof Long) {
                                            LOGGER.warn("TODO");
                                        // TODO
                                        } else {
                                            newList.add(o);
                                        }
                                    }
                                    list.addUnique(listObject);
                                } else {
                                    throw new BimserverDatabaseException("Unimplemented");
                                }
                            }
                        }
                    } else {
                        Object r = next.eGet(eReference);
                        if (r instanceof Long) {
                            long refOid = (Long) r;
                            IdEObject referred = ifcModel.get(refOid);
                            if (referred == null) {
                                System.out.println(eReference.getName() + " " + refOid);
                            }
                            idEObject.eSet(eReference, referred);
                        } else if (r instanceof HashMapWrappedVirtualObject) {
                            idEObject.eSet(eReference, convertWrapped(revision, ifcModel, (HashMapWrappedVirtualObject) r));
                        } else if (r instanceof HashMapVirtualObject) {
                            throw new BimserverDatabaseException("Unimplemented");
                        } else if (r == null) {
                        } else {
                            throw new BimserverDatabaseException("Unimplemented");
                        }
                    }
                }
                next = queryObjectProvider.next();
            }
            ifcModel.getModelMetaData().setName(name);
            ifcModel.getModelMetaData().setRevisionId(1);
            if (getAuthorization().getUoid() != -1) {
                ifcModel.getModelMetaData().setAuthorizedUser(getUserByUoid(getAuthorization().getUoid()).getName());
            }
            ifcModel.getModelMetaData().setDate(new Date());
        } catch (Throwable e) {
            throw new BimserverDatabaseException(e);
        }
    }
    IfcModelInterface ifcModel = getDatabaseSession().createServerModel(lastPackageMetaData, pidRoidMap);
    if (ifcModelSet.size() > 1) {
        setProgress("Merging IFC data...", -1);
        try {
            ifcModel = getBimServer().getMergerFactory().createMerger(getDatabaseSession(), getAuthorization().getUoid()).merge(lastProject, ifcModelSet, new ModelHelper(getBimServer().getMetaDataManager(), ifcModel));
        } catch (MergeException e) {
            throw new UserException(e);
        }
    } else {
        ifcModel = ifcModelSet.iterator().next();
    }
    ifcModel.getModelMetaData().setName(name);
    // ifcModel.getModelMetaData().setRevisionId(project.getRevisions().indexOf(virtualRevision) + 1);
    if (getAuthorization().getUoid() != -1) {
        ifcModel.getModelMetaData().setAuthorizedUser(getUserByUoid(getAuthorization().getUoid()).getName());
    }
    ifcModel.getModelMetaData().setDate(new Date());
    return ifcModel;
// for (Long roid : roids) {
// Revision virtualRevision = getRevisionByRoid(roid);
// pidRoidMap.put(virtualRevision.getProject().getId(), virtualRevision.getOid());
// project = virtualRevision.getProject();
// name += project.getName() + "-" + virtualRevision.getId() + "-";
// try {
// getAuthorization().canDownload(roid);
// } catch (UserException e) {
// if (!getAuthorization().hasRightsOnProjectOrSuperProjectsOrSubProjects(user, project)) {
// throw new UserException("User has insufficient rights to download revisions from this project");
// }
// if (!getAuthorization().hasRightsOnProjectOrSuperProjectsOrSubProjects(user, project)) {
// throw new UserException("User has insufficient rights to download revisions from this project");
// }
// }
// int size = 0;
// 
// for (ConcreteRevision concreteRevision : virtualRevision.getConcreteRevisions()) {
// try {
// int highestStopId = findHighestStopRid(project, concreteRevision);
// 
// PackageMetaData packageMetaData = getBimServer().getMetaDataManager().getPackageMetaData(concreteRevision.getProject().getSchema());
// lastPackageMetaData = packageMetaData;
// IfcModelInterface subModel = new ServerIfcModel(packageMetaData, pidRoidMap, getDatabaseSession());
// 
// OldQuery databaseQuery = new OldQuery(packageMetaData, concreteRevision.getProject().getId(), concreteRevision.getId(), virtualRevision.getOid(), null, Deep.NO, highestStopId);
// databaseQuery.updateOidCounters(concreteRevision, getDatabaseSession());
// JsonObject queryObject = (JsonObject)query;
// JsonArray queries = queryObject.get("queries").getAsJsonArray();
// for (JsonElement queryElement : queries) {
// processQueryPart(packageMetaData, queryObject, (JsonObject) queryElement, subModel, databaseQuery);
// }
// 
// size += subModel.size();
// subModel.getModelMetaData().setDate(concreteRevision.getDate());
// subModel.fixInverseMismatches();
// checkGeometry(serializerPluginConfiguration, getBimServer().getPluginManager(), subModel, project, concreteRevision, virtualRevision);
// ifcModelSet.add(subModel);
// } catch (GeometryGeneratingException | IfcModelInterfaceException e) {
// throw new UserException(e);
// }
// }
// 
// }
// TODO check, double merging??
// IfcModelInterface ifcModel = new BasicIfcModel(lastPackageMetaData, pidRoidMap);
// if (ifcModelSet.size() > 1) {
// try {
// ifcModel = getBimServer().getMergerFactory().createMerger(getDatabaseSession(), getAuthorization().getUoid()).merge(project, ifcModelSet, new ModelHelper(getBimServer().getMetaDataManager(), ifcModel));
// } catch (MergeException e) {
// throw new UserException(e);
// }
// } else {
// ifcModel = ifcModelSet.iterator().next();
// }
// if (name.endsWith("-")) {
// name = name.substring(0, name.length()-1);
// }
}
Also used : IdEObjectImpl(org.bimserver.emf.IdEObjectImpl) AbstractEList(org.eclipse.emf.common.util.AbstractEList) JsonQueryObjectModelConverter(org.bimserver.database.queries.om.JsonQueryObjectModelConverter) OldQuery(org.bimserver.database.OldQuery) Query(org.bimserver.database.queries.om.Query) HashMapWrappedVirtualObject(org.bimserver.shared.HashMapWrappedVirtualObject) HashMap(java.util.HashMap) IfcModelInterface(org.bimserver.emf.IfcModelInterface) QueryPart(org.bimserver.database.queries.om.QueryPart) ArrayList(java.util.ArrayList) Include(org.bimserver.database.queries.om.Include) CanInclude(org.bimserver.database.queries.om.CanInclude) BimserverDatabaseException(org.bimserver.BimserverDatabaseException) EAttribute(org.eclipse.emf.ecore.EAttribute) TypeDef(org.bimserver.database.queries.om.Include.TypeDef) MergeException(org.bimserver.plugins.modelmerger.MergeException) QueryObjectProvider(org.bimserver.database.queries.QueryObjectProvider) PluginConfiguration(org.bimserver.models.store.PluginConfiguration) List(java.util.List) ArrayList(java.util.ArrayList) AbstractEList(org.eclipse.emf.common.util.AbstractEList) UserException(org.bimserver.shared.exceptions.UserException) EReference(org.eclipse.emf.ecore.EReference) HashSet(java.util.HashSet) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ModelHelper(org.bimserver.plugins.ModelHelper) IdEObject(org.bimserver.emf.IdEObject) PackageMetaData(org.bimserver.emf.PackageMetaData) Reference(org.bimserver.database.queries.om.Reference) EReference(org.eclipse.emf.ecore.EReference) SerializerPlugin(org.bimserver.plugins.serializers.SerializerPlugin) Date(java.util.Date) Project(org.bimserver.models.store.Project) CanInclude(org.bimserver.database.queries.om.CanInclude) Revision(org.bimserver.models.store.Revision) HashMapVirtualObject(org.bimserver.shared.HashMapVirtualObject) IfcModelSet(org.bimserver.plugins.IfcModelSet) HashMapWrappedVirtualObject(org.bimserver.shared.HashMapWrappedVirtualObject) HashMapVirtualObject(org.bimserver.shared.HashMapVirtualObject) IdEObject(org.bimserver.emf.IdEObject)

Example 3 with AbstractEList

use of org.eclipse.emf.common.util.AbstractEList in project BIMserver by opensourceBIM.

the class AbstractSchemaConverter method copy.

@SuppressWarnings("unchecked")
protected IdEObject copy(IdEObject original) throws IfcModelInterfaceException, ObjectAlreadyExistsException {
    if (!((IdEObjectImpl) original).isLoadedOrLoading()) {
        return null;
    }
    if (converted.containsKey(original)) {
        return converted.get(original);
    }
    if (original.eClass().getName().equals("GeometryInfo") || original.eClass().getName().equals("GeometryData") || original.eClass().getName().equals("Vector3f")) {
        return null;
    }
    EClass eClass = (EClass) target.getPackageMetaData().getEPackage().getEClassifier(original.eClass().getName());
    if (eClass == null) {
        LOGGER.info("No class " + original.eClass().getName() + " in " + target.getPackageMetaData().getEPackage().getName());
        return null;
    }
    IdEObject newObject = (IdEObject) eClass.getEPackage().getEFactoryInstance().create(eClass);
    ((IdEObjectImpl) newObject).setOid(original.getOid());
    converted.put(original, newObject);
    if (newObject.eClass().getEAnnotation("wrapped") == null) {
        target.add(newObject.getOid(), newObject);
    }
    for (EStructuralFeature eStructuralFeature : original.eClass().getEAllStructuralFeatures()) {
        EStructuralFeature targetFeature = newObject.eClass().getEStructuralFeature(eStructuralFeature.getName());
        if (targetFeature == null) {
            continue;
        }
        Object get = original.eGet(eStructuralFeature);
        if (eStructuralFeature instanceof EAttribute) {
            if (get instanceof Double) {
                EStructuralFeature doubleStringFeature = original.eClass().getEStructuralFeature("wrappedValueAsString");
                EStructuralFeature targetDoubleStringFeature = newObject.eClass().getEStructuralFeature("wrappedValueAsString");
                if (doubleStringFeature != null && targetDoubleStringFeature != null) {
                    Object doubleString = original.eGet(doubleStringFeature);
                    newObject.eSet(targetDoubleStringFeature, doubleString);
                } else {
                    newObject.eSet(targetFeature, get);
                }
            } else {
                if (eStructuralFeature.getEType() instanceof EEnum) {
                    EEnum targetEnum = (EEnum) targetFeature.getEType();
                    EEnumLiteral newLiteral = targetEnum.getEEnumLiteral(get.toString());
                    if (newLiteral != null) {
                        newObject.eSet(targetFeature, newLiteral.getInstance());
                    }
                } else {
                    if (targetFeature instanceof EAttribute) {
                        newObject.eSet(targetFeature, get);
                    }
                }
            }
        } else if (eStructuralFeature instanceof EReference) {
            if (get == null) {
            } else {
                if (eStructuralFeature.isMany()) {
                    EList<EObject> list = (EList<EObject>) get;
                    AbstractEList<EObject> toList = (AbstractEList<EObject>) newObject.eGet(targetFeature);
                    if (toList != null) {
                        for (Object o : list) {
                            IdEObject ref = (IdEObject) o;
                            if (targetFeature.getEType().isInstance(ref)) {
                                if (converted.containsKey(o)) {
                                    toList.addUnique(converted.get(o));
                                } else {
                                    IdEObject result = copy((IdEObject) o);
                                    if (result != null) {
                                        toList.addUnique(result);
                                    }
                                }
                            }
                        }
                    }
                } else {
                    if (targetFeature.isMany()) {
                        LOGGER.info("Different multiplicity");
                    } else {
                        if (converted.containsKey(get)) {
                            newObject.eSet(targetFeature, converted.get(get));
                        } else {
                            newObject.eSet(targetFeature, copy((IdEObject) get));
                        }
                    }
                }
            }
        }
    }
    return newObject;
}
Also used : IdEObjectImpl(org.bimserver.emf.IdEObjectImpl) AbstractEList(org.eclipse.emf.common.util.AbstractEList) IdEObject(org.bimserver.emf.IdEObject) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) EEnum(org.eclipse.emf.ecore.EEnum) EClass(org.eclipse.emf.ecore.EClass) EAttribute(org.eclipse.emf.ecore.EAttribute) EList(org.eclipse.emf.common.util.EList) AbstractEList(org.eclipse.emf.common.util.AbstractEList) EObject(org.eclipse.emf.ecore.EObject) IdEObject(org.bimserver.emf.IdEObject) EObject(org.eclipse.emf.ecore.EObject) IdEObject(org.bimserver.emf.IdEObject) EEnumLiteral(org.eclipse.emf.ecore.EEnumLiteral) EReference(org.eclipse.emf.ecore.EReference)

Example 4 with AbstractEList

use of org.eclipse.emf.common.util.AbstractEList in project BIMserver by opensourceBIM.

the class WaitingList method updateNode.

@SuppressWarnings("unchecked")
public void updateNode(T expressId, EClass ec, IdEObject eObject) throws DeserializeException {
    for (WaitingObject waitingObject : waitingObjects.get(expressId)) {
        if (waitingObject.getEReference().isMany()) {
            AbstractEList<EObject> list = (AbstractEList<EObject>) waitingObject.getObject().eGet(waitingObject.getEReference());
            if (waitingObject instanceof SingleWaitingObject) {
                list.addUnique(eObject);
            } else {
                ListWaitingObject listWaitingObject = (ListWaitingObject) waitingObject;
                if (((EClass) waitingObject.getEReference().getEType()).isSuperTypeOf(eObject.eClass())) {
                    while (list.size() <= listWaitingObject.getIndex()) {
                        EObject create = ec.getEPackage().getEFactoryInstance().create(eObject.eClass());
                        ((IdEObjectImpl) create).setOid(-2);
                        list.addUnique(create);
                    }
                    list.setUnique(listWaitingObject.getIndex(), eObject);
                } else {
                    throw new DeserializeException(DeserializerErrorCode.REFERENCED_OBJECT_CANNOT_BE_STORED_IN_THIS_FIELD, waitingObject.getLineNumber(), "Field " + waitingObject.getEReference().getName() + " of " + waitingObject.getEReference().getEContainingClass().getName() + " cannot contain a " + eObject.eClass().getName());
                }
            }
        } else {
            if (((EClass) waitingObject.getEReference().getEType()).isSuperTypeOf(eObject.eClass())) {
                waitingObject.getObject().eSet(waitingObject.getEReference(), eObject);
            } else {
                throw new DeserializeException(DeserializerErrorCode.NON_EXISTING_ENTITY_REFERENCED, waitingObject.getLineNumber(), "Field " + waitingObject.getEReference().getName() + " of " + waitingObject.getEReference().getEContainingClass().getName() + " cannot contain a " + eObject.eClass().getName() + "/" + eObject.getOid());
            }
        }
    }
    waitingObjects.remove(expressId);
}
Also used : IdEObjectImpl(org.bimserver.emf.IdEObjectImpl) AbstractEList(org.eclipse.emf.common.util.AbstractEList) EClass(org.eclipse.emf.ecore.EClass) EObject(org.eclipse.emf.ecore.EObject) IdEObject(org.bimserver.emf.IdEObject) DeserializeException(org.bimserver.plugins.deserializers.DeserializeException)

Example 5 with AbstractEList

use of org.eclipse.emf.common.util.AbstractEList in project BIMserver by opensourceBIM.

the class SharedJsonDeserializer method processObject.

@SuppressWarnings({ "rawtypes", "unchecked" })
private IdEObject processObject(IfcModelInterface model, WaitingList<Long> waitingList, JsonReader jsonReader, EClass eClass) throws IOException, DeserializeException, IfcModelInterfaceException {
    IdEObjectImpl object = null;
    jsonReader.beginObject();
    if (jsonReader.nextName().equals("_i")) {
        long oid = jsonReader.nextLong();
        if (jsonReader.nextName().equals("_u")) {
            UUID uuid = UUID.fromString(jsonReader.nextString());
            if (jsonReader.nextName().equals("_r")) {
                int rid = jsonReader.nextInt();
                if (jsonReader.nextName().equals("_t")) {
                    String type = jsonReader.nextString();
                    if (eClass == null) {
                        eClass = model.getPackageMetaData().getEClassIncludingDependencies(type);
                    }
                    if (eClass == null) {
                        throw new DeserializeException(DeserializerErrorCode.UNKNOWN_ENTITY, "No class found with name " + type);
                    }
                    if (model.containsNoFetch(oid)) {
                        object = (IdEObjectImpl) model.getNoFetch(oid);
                    } else {
                        if (eClass == StorePackage.eINSTANCE.getIfcHeader()) {
                            object = (IdEObjectImpl) StoreFactory.eINSTANCE.createIfcHeader();
                        } else {
                            object = (IdEObjectImpl) model.create(eClass, oid);
                        }
                    }
                    object.useInverses(false);
                    ((IdEObjectImpl) object).setUuid(uuid);
                    ((IdEObjectImpl) object).setRid(rid);
                    if (jsonReader.nextName().equals("_s")) {
                        int state = jsonReader.nextInt();
                        if (state == 1) {
                            object.setLoadingState(State.LOADING);
                            while (jsonReader.hasNext()) {
                                String featureName = jsonReader.nextName();
                                boolean embedded = false;
                                if (featureName.startsWith("_r")) {
                                    featureName = featureName.substring(2);
                                } else if (featureName.startsWith("_e")) {
                                    embedded = true;
                                    featureName = featureName.substring(2);
                                }
                                EStructuralFeature eStructuralFeature = eClass.getEStructuralFeature(featureName);
                                if (eStructuralFeature == null) {
                                    throw new DeserializeException(DeserializerErrorCode.UNKNOWN_FIELD, "Unknown field (" + featureName + ") on class " + eClass.getName());
                                }
                                if (eStructuralFeature.isMany()) {
                                    jsonReader.beginArray();
                                    if (eStructuralFeature instanceof EAttribute) {
                                        List list = (List) object.eGet(eStructuralFeature);
                                        List<String> stringList = null;
                                        if (eStructuralFeature.getEType() == EcorePackage.eINSTANCE.getEDoubleObject() || eStructuralFeature.getEType() == EcorePackage.eINSTANCE.getEDouble()) {
                                            EStructuralFeature asStringFeature = eClass.getEStructuralFeature(eStructuralFeature.getName() + "AsString");
                                            stringList = (List<String>) object.eGet(asStringFeature);
                                        }
                                        while (jsonReader.hasNext()) {
                                            Object e = readPrimitive(jsonReader, eStructuralFeature);
                                            list.add(e);
                                            if (eStructuralFeature.getEType() == EcorePackage.eINSTANCE.getEDouble()) {
                                                double val = (Double) e;
                                                // TODO
                                                stringList.add("" + val);
                                            // this
                                            // is
                                            // losing
                                            // precision,
                                            // maybe
                                            // also
                                            // send
                                            // the
                                            // string
                                            // value?
                                            }
                                        }
                                    } else if (eStructuralFeature instanceof EReference) {
                                        int index = 0;
                                        AbstractEList list = (AbstractEList) object.eGet(eStructuralFeature);
                                        while (jsonReader.hasNext()) {
                                            if (embedded) {
                                                JsonToken peek = jsonReader.peek();
                                                if (peek == JsonToken.NUMBER) {
                                                    long refOid = jsonReader.nextLong();
                                                    processRef(model, waitingList, object, eStructuralFeature, index, list, refOid);
                                                } else if (eStructuralFeature.getEAnnotation("twodimensionalarray") != null) {
                                                    IdEObjectImpl listObject = model.create(((EReference) eStructuralFeature).getEReferenceType());
                                                    addToList(eStructuralFeature, index, list, listObject);
                                                    EStructuralFeature listFeature = listObject.eClass().getEStructuralFeature("List");
                                                    jsonReader.beginArray();
                                                    AbstractEList innerList = (AbstractEList) listObject.eGet(listFeature);
                                                    if (listFeature instanceof EAttribute) {
                                                        while (jsonReader.hasNext()) {
                                                            innerList.add(readPrimitive(jsonReader, listFeature));
                                                        }
                                                    } else {
                                                        while (jsonReader.hasNext()) {
                                                            jsonReader.beginObject();
                                                            String nextName = jsonReader.nextName();
                                                            if (nextName.equals("_t")) {
                                                                String t = jsonReader.nextString();
                                                                IdEObject wrappedObject = model.create(model.getPackageMetaData().getEClass(t), -1);
                                                                EList<EStructuralFeature> eAllStructuralFeatures = wrappedObject.eClass().getEAllStructuralFeatures();
                                                                for (EStructuralFeature eStructuralFeature2 : eAllStructuralFeatures) {
                                                                    if (jsonReader.nextName().contentEquals(eStructuralFeature2.getName())) {
                                                                        wrappedObject.eSet(eStructuralFeature2, readPrimitive(jsonReader, eStructuralFeature2));
                                                                    } else {
                                                                    // trouble
                                                                    }
                                                                }
                                                                innerList.add(wrappedObject);
                                                            } else if (nextName.equals("_r")) {
                                                                // Reference
                                                                long referenceOid = jsonReader.nextLong();
                                                                if (jsonReader.nextName().equals("_t")) {
                                                                    String refType = jsonReader.nextString();
                                                                    IdEObject refObject = (IdEObject) model.create(model.getPackageMetaData().getEClassIncludingDependencies(refType), referenceOid);
                                                                    ((IdEObjectImpl) refObject).setLoadingState(State.OPPOSITE_SETTING);
                                                                    model.add(refObject.getOid(), refObject);
                                                                    addToList(listFeature, index, innerList, refObject);
                                                                    ((IdEObjectImpl) refObject).setLoadingState(State.TO_BE_LOADED);
                                                                }
                                                            }
                                                            jsonReader.endObject();
                                                        }
                                                    }
                                                    jsonReader.endArray();
                                                } else {
                                                    jsonReader.beginObject();
                                                    String nextName = jsonReader.nextName();
                                                    if (nextName.equals("_t")) {
                                                        String t = jsonReader.nextString();
                                                        if (jsonReader.nextName().equals("_v")) {
                                                            if (jsonReader.peek() == JsonToken.BEGIN_ARRAY) {
                                                                // Embedded array
                                                                jsonReader.beginArray();
                                                                // Here we are, IfcLineIndex should implement IfcSegmentIndexSelect, just as IfcArcIndex should
                                                                IdEObjectImpl listObject = model.create(model.getPackageMetaData().getEClassIncludingDependencies(t));
                                                                // addToList(eStructuralFeature, index, list, listObject);
                                                                EStructuralFeature listFeature = listObject.eClass().getEStructuralFeature("wrappedValue");
                                                                AbstractEList innerList = (AbstractEList) listObject.eGet(listFeature);
                                                                while (jsonReader.hasNext()) {
                                                                    innerList.add(readPrimitive(jsonReader, listFeature));
                                                                }
                                                                jsonReader.endArray();
                                                                list.add(listObject);
                                                            } else {
                                                                IdEObject wrappedObject = (IdEObject) model.create(model.getPackageMetaData().getEClass(t), -1);
                                                                EStructuralFeature wv = wrappedObject.eClass().getEStructuralFeature("wrappedValue");
                                                                wrappedObject.eSet(wv, readPrimitive(jsonReader, wv));
                                                                list.add(wrappedObject);
                                                            }
                                                        } else {
                                                            throw new DeserializeException(DeserializerErrorCode.UNEXPECTED_FIELD, "Expected _v");
                                                        }
                                                    } else if (nextName.equals("_i")) {
                                                        // Not all are embedded...
                                                        long refOid = jsonReader.nextLong();
                                                        if (jsonReader.nextName().equals("_t")) {
                                                            String refType = jsonReader.nextString();
                                                            IdEObject refObject = (IdEObject) model.create(model.getPackageMetaData().getEClassIncludingDependencies(refType), refOid);
                                                            ((IdEObjectImpl) refObject).setLoadingState(State.OPPOSITE_SETTING);
                                                            model.add(refObject.getOid(), refObject);
                                                            addToList(eStructuralFeature, index, list, refObject);
                                                            ((IdEObjectImpl) refObject).setLoadingState(State.TO_BE_LOADED);
                                                        } else {
                                                            processRef(model, waitingList, object, eStructuralFeature, index, list, refOid);
                                                        }
                                                    }
                                                    jsonReader.endObject();
                                                }
                                            } else {
                                                jsonReader.beginObject();
                                                if (jsonReader.nextName().equals("_i")) {
                                                    long refOid = jsonReader.nextLong();
                                                    if (jsonReader.nextName().equals("_t")) {
                                                        String refType = jsonReader.nextString();
                                                        EClass referenceEClass = model.getPackageMetaData().getEClassIncludingDependencies(refType);
                                                        if (model.getNoFetch(refOid) != null) {
                                                            processRef(model, waitingList, object, eStructuralFeature, index, list, refOid);
                                                        } else {
                                                            IdEObject refObject = (IdEObject) model.create(referenceEClass, refOid);
                                                            ((IdEObjectImpl) refObject).setLoadingState(State.OPPOSITE_SETTING);
                                                            model.add(refObject.getOid(), refObject);
                                                            addToList(eStructuralFeature, index, list, refObject);
                                                            ((IdEObjectImpl) refObject).setLoadingState(State.TO_BE_LOADED);
                                                        }
                                                    }
                                                }
                                                jsonReader.endObject();
                                            }
                                            index++;
                                        }
                                    }
                                    jsonReader.endArray();
                                } else {
                                    if (eStructuralFeature instanceof EAttribute) {
                                        Object x = readPrimitive(jsonReader, eStructuralFeature);
                                        if (eStructuralFeature.getEType() == EcorePackage.eINSTANCE.getEDouble()) {
                                            EStructuralFeature asStringFeature = object.eClass().getEStructuralFeature(eStructuralFeature.getName() + "AsString");
                                            if (asStringFeature != null) {
                                                // TODO
                                                object.eSet(asStringFeature, "" + x);
                                            }
                                        // this
                                        // is
                                        // losing
                                        // precision,
                                        // maybe
                                        // also
                                        // send
                                        // the
                                        // string
                                        // value?
                                        }
                                        object.eSet(eStructuralFeature, x);
                                    } else if (eStructuralFeature instanceof EReference) {
                                        if (eStructuralFeature.getName().equals("GlobalId")) {
                                            IfcGloballyUniqueId globallyUniqueId = Ifc2x3tc1Factory.eINSTANCE.createIfcGloballyUniqueId();
                                            globallyUniqueId.setWrappedValue(jsonReader.nextString());
                                            object.eSet(eStructuralFeature, globallyUniqueId);
                                        } else if (embedded) {
                                            jsonReader.beginObject();
                                            if (jsonReader.nextName().equals("_t")) {
                                                String t = jsonReader.nextString();
                                                IdEObject wrappedObject = (IdEObject) model.create(model.getPackageMetaData().getEClassIncludingDependencies(t), -1);
                                                ((IdEObjectImpl) wrappedObject).setLoadingState(State.LOADING);
                                                if (eStructuralFeature.getEAnnotation("dbembed") != null) {
                                                    for (EStructuralFeature eStructuralFeature2 : wrappedObject.eClass().getEAllStructuralFeatures()) {
                                                        String fn = jsonReader.nextName();
                                                        if (fn.equals(eStructuralFeature2.getName())) {
                                                            wrappedObject.eSet(eStructuralFeature2, readPrimitive(jsonReader, eStructuralFeature2));
                                                        } else {
                                                            throw new DeserializeException(DeserializerErrorCode.UNEXPECTED_FIELD, fn + " / " + eStructuralFeature2.getName());
                                                        }
                                                    }
                                                    object.eSet(eStructuralFeature, wrappedObject);
                                                } else {
                                                    if (jsonReader.nextName().equals("_v")) {
                                                        EStructuralFeature wv = wrappedObject.eClass().getEStructuralFeature("wrappedValue");
                                                        wrappedObject.eSet(wv, readPrimitive(jsonReader, wv));
                                                        object.eSet(eStructuralFeature, wrappedObject);
                                                    }
                                                }
                                                ((IdEObjectImpl) wrappedObject).setLoadingState(State.LOADED);
                                            }
                                            jsonReader.endObject();
                                        } else {
                                            jsonReader.beginObject();
                                            if (jsonReader.nextName().equals("_i")) {
                                                long refOid = jsonReader.nextLong();
                                                if (jsonReader.nextName().equals("_t")) {
                                                    String refType = jsonReader.nextString();
                                                    boolean isInverse = false;
                                                    EntityDefinition entityBN = model.getPackageMetaData().getSchemaDefinition().getEntityBN(object.eClass().getName());
                                                    if (entityBN != null) {
                                                        // Some entities like GeometryInfo/Data are not in IFC schema, but valid
                                                        Attribute attributeBN = entityBN.getAttributeBNWithSuper(eStructuralFeature.getName());
                                                        if (attributeBN != null) {
                                                            if (attributeBN instanceof InverseAttribute) {
                                                                isInverse = true;
                                                            }
                                                        }
                                                    }
                                                    // if (!isInverse) {
                                                    if (model.getNoFetch(refOid) != null) {
                                                        object.eSet(eStructuralFeature, model.getNoFetch(refOid));
                                                    } else {
                                                        IdEObject refObject = (IdEObject) model.create(model.getPackageMetaData().getEClassIncludingDependencies(refType), refOid);
                                                        ((IdEObjectImpl) refObject).setLoadingState(State.OPPOSITE_SETTING);
                                                        model.add(refObject.getOid(), refObject);
                                                        object.eSet(eStructuralFeature, refObject);
                                                        ((IdEObjectImpl) refObject).setLoadingState(State.TO_BE_LOADED);
                                                    }
                                                // }
                                                }
                                            }
                                            jsonReader.endObject();
                                        }
                                    }
                                }
                            }
                            object.useInverses(true);
                            object.setLoadingState(State.LOADED);
                        } else {
                            // state not_loaded
                            object.setLoadingState(State.TO_BE_LOADED);
                        }
                        if (waitingList.containsKey(oid)) {
                            waitingList.updateNode(oid, eClass, object);
                        }
                        model.add(object.getOid(), object);
                    } else {
                        LOGGER.info("_s expected");
                    }
                } else {
                    LOGGER.info("_t expected");
                }
            } else {
                LOGGER.info("_r expected");
            }
        } else {
            LOGGER.info("_u expected");
        }
    } else {
        LOGGER.info("_i expected");
    }
    jsonReader.endObject();
    return object;
}
Also used : AbstractEList(org.eclipse.emf.common.util.AbstractEList) EAttribute(org.eclipse.emf.ecore.EAttribute) Attribute(nl.tue.buildingsmart.schema.Attribute) InverseAttribute(nl.tue.buildingsmart.schema.InverseAttribute) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) IfcGloballyUniqueId(org.bimserver.models.ifc2x3tc1.IfcGloballyUniqueId) DeserializeException(org.bimserver.plugins.deserializers.DeserializeException) EntityDefinition(nl.tue.buildingsmart.schema.EntityDefinition) EAttribute(org.eclipse.emf.ecore.EAttribute) EClass(org.eclipse.emf.ecore.EClass) EList(org.eclipse.emf.common.util.EList) AbstractEList(org.eclipse.emf.common.util.AbstractEList) WaitingList(org.bimserver.shared.WaitingList) List(java.util.List) ListWaitingObject(org.bimserver.shared.ListWaitingObject) EObject(org.eclipse.emf.ecore.EObject) JsonToken(com.google.gson.stream.JsonToken) InverseAttribute(nl.tue.buildingsmart.schema.InverseAttribute) UUID(java.util.UUID) EReference(org.eclipse.emf.ecore.EReference)

Aggregations

AbstractEList (org.eclipse.emf.common.util.AbstractEList)6 IdEObject (org.bimserver.emf.IdEObject)5 IdEObjectImpl (org.bimserver.emf.IdEObjectImpl)5 EAttribute (org.eclipse.emf.ecore.EAttribute)5 EObject (org.eclipse.emf.ecore.EObject)5 EReference (org.eclipse.emf.ecore.EReference)5 List (java.util.List)4 EList (org.eclipse.emf.common.util.EList)4 EStructuralFeature (org.eclipse.emf.ecore.EStructuralFeature)4 EClass (org.eclipse.emf.ecore.EClass)3 DeserializeException (org.bimserver.plugins.deserializers.DeserializeException)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 JsonToken (com.google.gson.stream.JsonToken)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashMap (java.util.HashMap)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