Пример #1
0
        public BasicNodeModel GetNodeAndRelationships(Guid id)
        {
            BasicNodeModel node = null;

            using (ISession session = driver.Session())
            {
                session.ReadTransaction(action =>
                {
                    IStatementResult statementResult = action.Run(kMatchNodeAndRelationshipsQuery, new Dictionary <string, object> {
                        { "id", id.ToString() }
                    });
                    // Get the relationships
                    foreach (IRecord record in statementResult)
                    {
                        if (node == null)
                        {
                            node = BasicNodeModel.FromINode(record[0].As <INode>());
                        }
                        node.AddRelationship(record["r"].As <IRelationship>(), record["e"].As <INode>());
                    }
                });
            }

            return(node);
        }
Пример #2
0
        public static BasicNodeModel FromINode(INode node)
        {
            BasicNodeModel result = null;

            try
            {
                NodeContentType contentType = (NodeContentType)Enum.Parse(typeof(NodeContentType), node.Labels[0]);
                if (contentType == NodeContentType.Company)
                {
                    result = new CompanyNodeModel
                    {
                        Id          = Guid.Parse(node.Properties["id"].As <string>()),
                        ContentType = contentType,
                        ReleaseDate = node.Properties.ContainsKey("releaseDate") ? DateTime.Parse(node.Properties["releaseDate"].As <string>()) : default(DateTime?),
                        DeathDate   = node.Properties.ContainsKey("deathDate") ? DateTime.Parse(node.Properties["deathDate"].As <string>()) : default(DateTime?),
                        CommonName  = node.Properties.ContainsKey("commonName") ? node.Properties["commonName"].As <string>() : null,
                        OtherNames  = node.Properties.ContainsKey("otherNames") ? node.Properties["otherNames"].As <List <string> >() : new List <string>()
                    };
                }
                else if (contentType == NodeContentType.Media)
                {
                    result = new MediaNodeModel
                    {
                        Id          = Guid.Parse(node.Properties["id"].As <string>()),
                        ContentType = contentType,
                        ReleaseDate = node.Properties.ContainsKey("releaseDate") ? DateTime.Parse(node.Properties["releaseDate"].As <string>()) : default(DateTime?),
                        DeathDate   = node.Properties.ContainsKey("deathDate") ? DateTime.Parse(node.Properties["deathDate"].As <string>()) : default(DateTime?),
                        CommonName  = node.Properties.ContainsKey("commonName") ? node.Properties["commonName"].As <string>() : null,
                        OtherNames  = node.Properties.ContainsKey("otherNames") ? node.Properties["otherNames"].As <List <string> >() : new List <string>(),
                        // Media properties
                        MediaType     = (NodeMediaType)Enum.Parse(typeof(NodeMediaType), node.Labels[1]),
                        FranchiseName = node.Properties.ContainsKey("franchise") ? node.Properties["franchise"].As <string>() : null,
                        Genres        = node.Properties.ContainsKey("genres") ? node.Properties["genres"].As <List <string> >() : new List <string>()
                    };
                }
                else if (contentType == NodeContentType.Person)
                {
                    result = new PersonNodeModel
                    {
                        Id          = Guid.Parse(node.Properties["id"].As <string>()),
                        ContentType = contentType,
                        ReleaseDate = node.Properties.ContainsKey("releaseDate") ? DateTime.Parse(node.Properties["releaseDate"].As <string>()) : default(DateTime?),
                        DeathDate   = node.Properties.ContainsKey("deathDate") ? DateTime.Parse(node.Properties["deathDate"].As <string>()) : default(DateTime?),
                        CommonName  = node.Properties.ContainsKey("commonName") ? node.Properties["commonName"].As <string>() : null,
                        OtherNames  = node.Properties.ContainsKey("otherNames") ? node.Properties["otherNames"].As <List <string> >() : new List <string>(),
                        // Person properties
                        FamilyName = node.Properties.ContainsKey("familyName") ? node.Properties["familyName"].As <string>() : null,
                        GivenName  = node.Properties.ContainsKey("givenName") ? node.Properties["givenName"].As <string>() : null,
                        Status     = node.Properties.ContainsKey("status") ? (PersonStatus)Enum.Parse(typeof(PersonStatus), node.Properties["status"].As <string>()) : 0
                    };
                }
            }
            catch (Exception e)
            {
                throw new ArgumentException("Failed to parse the given node: (Type: " + node.Labels[0] + " Id: " + node.Properties["id"].As <string>(), e);
            }

            return(result);
        }
Пример #3
0
        public bool UpdateNode(BasicNodeModel model)
        {
            bool result = false;

            if (DeleteNode(model.Id))
            {
                result = AddNode(model);
            }
            return(result);
        }
Пример #4
0
        /// <summary>
        /// Creates a single large query for the creation of a node and it's direct relationships.
        /// </summary>
        /// <param name="model">The for which to create the creation query</param>
        /// <returns>The query string for the creation of the node and its direct relationships</returns>
        private string BuildNodeCreationQuery(BasicNodeModel model)
        {
            StringBuilder builder = new StringBuilder();

            // Append the node creation statement
            builder.AppendFormat(kNodeCreationStatement, model.GetNodeLabels());
            builder.AppendLine();
            // Append the relationship statements
            BuildRelationshipMergeStatement(builder, model.RelatedCompanies, NodeContentType.Company, model.ContentType);
            BuildRelationshipMergeStatement(builder, model.RelatedMedia, NodeContentType.Media, model.ContentType);
            BuildRelationshipMergeStatement(builder, model.RelatedPeople, NodeContentType.Person, model.ContentType);
            // Append the return statement
            builder.Append("RETURN n");

            return(builder.ToString());
        }
Пример #5
0
        public BasicNodeModel GetNode(Guid id)
        {
            INode result;

            using (ISession session = driver.Session())
            {
                result = session.ReadTransaction(action =>
                {
                    IStatementResult statementResult = action.Run(kMatchNodeQuery, new Dictionary <string, object> {
                        { "id", id.ToString() }
                    });
                    return(statementResult.FirstOrDefault()?[0].As <INode>());
                });
            }

            return(result != null?BasicNodeModel.FromINode(result) : null);
        }
Пример #6
0
        private static bool MapNode(BasicNodeModel node, ref Dictionary <string, Guid> nodeMap)
        {
            // Guard clauses
            // If the node exists in the database already,
            if (ExistingRecords.Find(x => x.CommonName.ToLowerInvariant() == node.CommonName.ToLowerInvariant()) != null)
            {
                LogInColor($"Skipping [{node.CommonName}] - It already exists in the database", ConsoleColor.Red);
                return(false);
            }
            // If the node exists in the node map,
            if (nodeMap.ContainsKey(node.CommonName.ToLowerInvariant()))
            {
                LogInColor($"Skipping [{node.CommonName}] - It is duplicated within the provided file", ConsoleColor.Red);
                return(false);
            }

            // If we reach this point then the node is a node to be added
            node.Id = Guid.NewGuid();
            nodeMap.Add(node.CommonName.ToLowerInvariant(), node.Id);
            LogInColor($"Mapped [{node.CommonName}] with id {node.Id.ToString()}", ConsoleColor.Yellow);
            // Check each of the referenced nodes
            foreach (RelationshipModel relModel in node.Relationships)
            {
                // If the node map contains the node's name
                if (nodeMap.ContainsKey(relModel.TargetName.ToLowerInvariant()))
                {
                    // Give the relationship the existing id
                    relModel.TargetId = nodeMap[relModel.TargetName.ToLowerInvariant()];
                    LogInColor($"\tMaped related node [{relModel.TargetName}] to existing id {relModel.TargetId}", ConsoleColor.DarkCyan);
                }
                else
                {
                    // Give the relationship a new id
                    relModel.TargetId      = Guid.NewGuid();
                    relModel.IsNewAddition = true;
                    nodeMap.Add(relModel.TargetName.ToLowerInvariant(), relModel.TargetId.Value);
                    LogInColor($"\tMapped related node [{relModel.TargetName}] with id {relModel.TargetId}", ConsoleColor.DarkGreen);
                }
            }

            Console.WriteLine();

            return(true);
        }
Пример #7
0
        /// <summary>
        /// Adds the given node to the graph.
        /// PRECONDITION: The node must be valid
        /// </summary>
        /// <param name="node">The BasicNodeModel of the node to add to the graph</param>
        /// <returns>Returns true if the node was added to the graph successfully</returns>
        public bool AddNode(BasicNodeModel node)
        {
            INode sessionResult;

            // Create the session
            using (ISession session = driver.Session())
            {
                // Run the statement in a transaction
                sessionResult = session.WriteTransaction(action =>
                {
                    // Create the node
                    IStatementResult result = action.Run(BuildNodeCreationQuery(node), new { props = node.GetPropertyMap() });

                    return(result.Single()[0].As <INode>());
                });
            }

            return(sessionResult != null);
        }
Пример #8
0
        public BasicNodeModel GetNode(string commonName)
        {
            if (commonName == null)
            {
                return(null);
            }

            INode result = null;

            using (ISession session = driver.Session())
            {
                result = session.ReadTransaction(action =>
                {
                    IStatementResult statementResult = action.Run(kMatchNodeByNameQuery, new Dictionary <string, object> {
                        { "name", commonName }
                    });
                    return(statementResult.FirstOrDefault()?[0].As <INode>());
                });
            }

            return(result != null?BasicNodeModel.FromINode(result) : null);
        }