Esempio n. 1
0
        public Task <int> LoadNodes(Query query, MergeOption option, IGraphAdapter adapter, bool lazyLoading)
        {
            if (adapter == null)
            {
                adapter = _loader;
            }

            var tcs = new TaskCompletionSource <int>();
            var cx  = 0;

            if (adapter == null)
            {
                tcs.TrySetResult(cx);
                return(tcs.Task);
            }
            var oldLazyLoader = _lazyLoader;

            try
            {
                // Disable lazy loading
                _lazyLoader = null;

                var q = adapter is ISupportsLazyLoading && lazyLoading ? ((ISupportsLazyLoading)adapter).LazyLoadingNodes(query) : adapter.LoadNodes(query);
                using (var session = this.Store.BeginSession(new SessionConfiguration {
                    Mode = SessionMode.Loading | SessionMode.SkipConstraints
                }))
                {
                    foreach (var result in q)
                    {
                        if (Session.Current != null && Session.Current.TrackingData.GetTrackedElementState(result.Id) == TrackingState.Removed)
                        {
                            continue;
                        }
                        cx++;
                        var newInCache    = false;
                        var nodeMetaclass = result.SchemaInfo;

                        // Si ce noeud n'existe pas dans le cache, on le met
                        GraphNode graphNode;
                        GetGraphNode(result.Id, result.NodeType, out graphNode);
                        var node = graphNode as GraphNode;
                        if (node == null)
                        {
                            if (result.NodeType == NodeType.Edge)
                            {
                                var rSchema = nodeMetaclass as ISchemaRelationship;
                                node = CreateRelationship(result.Id,
                                                          rSchema,
                                                          result.StartId,
                                                          result.EndId
                                                          ) as GraphNode;
                            }
                            else
                            {
                                node = CreateEntity(result.Id, nodeMetaclass as ISchemaEntity) as GraphNode;
                            }
                            newInCache = true;
                        }

                        if (option == MergeOption.AppendOnly && newInCache || option == MergeOption.OverwriteChanges)
                        {
                            foreach (var edge in result.Outgoings)
                            {
                                var edgeSchema = _domainModel.Store.GetSchemaRelationship(edge.SchemaId);
                                node = node.AddEdge(edge.Id, edgeSchema, Direction.Outgoing, edge.EndId);
                            }
                            foreach (var edge in result.Incomings)
                            {
                                var edgeSchema = _domainModel.Store.GetSchemaRelationship(edge.SchemaId);
                                node = node.AddEdge(edge.Id, edgeSchema, Direction.Incoming, edge.EndId);
                            }
                        }

                        var ctx = new SerializationContext(_domainModel, result.SchemaInfo, result);
                        var mel = (IModelElement)result.SchemaInfo.Deserialize(ctx);
                        if (mel != null)
                        {
                            if (result.Properties != null)
                            {
                                foreach (var property in result.Properties)
                                {
                                    // Mise à jour des propriétés lues
                                    if (option == MergeOption.AppendOnly && newInCache || option == MergeOption.OverwriteChanges)
                                    {
                                        SetPropertyValue(mel, property.Key, property.Value.Value, property.Value.CurrentVersion);
                                    }
                                    else if (option == MergeOption.PreserveChanges)
                                    {
                                        if (GetPropertyValue(node.Id, property.Key) == null)
                                        {
                                            SetPropertyValue(mel, property.Key, property.Value.Value, property.Value.CurrentVersion);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    session.AcceptChanges();
                }
                tcs.TrySetResult(cx);
            }
            catch (Exception ex)
            {
                tcs.SetException(ex);
            }
            finally
            {
                _lazyLoader = oldLazyLoader;
            }

            return(tcs.Task);
        }