Beispiel #1
0
        public string GetAllFrames()
        {
            string   jsonFrameStack = null;
            ISession session        = GetSession();

            if (session == null)
            {
                return(null);
            }
            using (session) {
                string           cypher      = @"
MATCH (f:Frame) 
WITH f ORDER BY f.name 
WITH f MATCH (mf:MainFrame) 
WITH {nodetype:'mainframe', title:mf.name} + collect({nodetype:'frame', title:f.name}) as frames 
RETURN frames
";
                IStatementResult result      = session.Run(cypher);
                IRecord          frameRecord = result.FirstOrDefault();
                if (frameRecord != null)
                {
                    jsonFrameStack = JsonConvert.SerializeObject(frameRecord.Values["frames"]);
                }
                return(jsonFrameStack);
            }
        }
        public static T Map <T>(this IStatementResult statementResult) where T : new()
        {
            var recordValue = statementResult?.FirstOrDefault()?[0];

            if (recordValue == null)
            {
                return(default);
Beispiel #3
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);
        }
Beispiel #4
0
        public string GetFrameTree(string frameName)
        {
            string jsonFrameTree = null;

            ISession session = GetSession();

            if (session == null)
            {
                return(null);
            }

            //using (var session = MvcApplication.NeoDTDriver.Session()) {
            using (session) {
                string           cypher = @"
MATCH (f:Frame{name: {frameName} })-[hs:HAS_SLOT]->(s:Slot)
OPTIONAL MATCH (s)-[ht:HAS_TERM]->(t:Term)
WITH f,s,hs,ht, t
ORDER BY ht.index
WITH f,s,hs,
CASE WHEN t IS NOT NULL THEN collect({nodetype: 'term', origTitle: t.name, title: t.name, index: ht.index, parent: s.name}) ELSE [] END as c_terms
ORDER BY hs.index
WITH collect({nodetype: 'slot', origTitle: s.name, title: s.name, index:hs.index,  nodes: c_terms, parent: f.name}) as c_slotrows
MATCH (f:Frame{name: 'Complaints' })-[ltf:LINK_TO_FRAME]->(sf:Frame)
WITH ltf, f, c_slotrows + collect({nodetype: 'framelink', origTitle: sf.name, title: sf.name, index: ltf.index, parent: 'frame'}) as c_allrows
WITH f, c_allrows
UNWIND c_allrows AS u_allrows
WITH f, u_allrows
ORDER BY u_allrows.index
WITH {title:f.name, nodes:collect(u_allrows)} AS frame
RETURN frame.nodes
";
                IStatementResult result = session.Run(cypher, new Dictionary <string, object> {
                    { "frameName", frameName }
                });
                IRecord frameRecord = result.FirstOrDefault();
                if (frameRecord != null)
                {
                    jsonFrameTree = JsonConvert.SerializeObject(frameRecord.Values["frame.nodes"]);
                }
            }

            return(jsonFrameTree);
        }
Beispiel #5
0
        public IRecord GetRelationship(Guid source, Guid target)
        {
            IRecord relationship = null;

            using (ISession session = driver.Session())
            {
                // Create a transaction
                relationship = session.ReadTransaction(action =>
                {
                    IStatementResult result = action.Run("MATCH (s {id: $sourceId})-[r]-(e {id: $targetId}) RETURN s.commonName, r.roles, e.commonName",
                                                         new Dictionary <string, object>
                    {
                        { "sourceId", source.ToString() },
                        { "targetId", target.ToString() }
                    });
                    return(result.FirstOrDefault());
                });
            }

            return(relationship);
        }
Beispiel #6
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);
        }
Beispiel #7
0
        public T GetByUuidWithRelatedNodes <T>(string uuid) where T : EntityBase, new()
        {
            if (string.IsNullOrWhiteSpace(uuid))
            {
                throw new ArgumentNullException("uuid");
            }

            var query = new StringFormatter(QueryTemplates.TEMPLATE_GET_BY_PROPERTY);

            query.Add("@label", new T().Label);
            query.Add("@property", "Uuid");
            query.Add("@result", TAG);
            query.Add("@relatedNode", string.Empty);
            query.Add("@relationship", string.Empty);

            IStatementResult result = ExecuteQuery(query.ToString(), new { value = uuid });

            IReadOnlyDictionary <string, object> node = result.FirstOrDefault()?[0].As <INode>().Properties;

            if (node == null)
            {
                return(default);
Beispiel #8
0
        public bool UpdateNode(BasicNodeModel model)
        {
            bool success = false;

            using (ISession session = driver.Session())
            {
                success = session.ReadTransaction(action =>
                {
                    IStatementResult result = action.Run(kMatchNodeQuery, new Dictionary <string, object> {
                        { "id", model.Id.ToString() }
                    });
                    // We found a node to delete
                    if (result.FirstOrDefault() != null)
                    {
                        // Delete the node
                        Delete(action, model.Id);
                    }
                    // Update the node
                    return(Update(action, model));
                });
            }

            return(success);
        }
Beispiel #9
0
        public string GetFrameStack(string frameName)
        {
            string   jsonFrameStack = null;
            ISession session        = GetSession();

            if (session == null)
            {
                return(null);
            }
            using (session) {
                string           cypher = @"
match f=(head:MainFrame) -[:LINK_TO_FRAME*]-> (lastFrame:Frame{name:{frameName}}) return collect(nodes(f)) as frames
";
                IStatementResult result = session.Run(cypher, new Dictionary <string, object> {
                    { "frameName", frameName }
                });
                IRecord frameRecord = result.FirstOrDefault();
                if (frameRecord != null)
                {
                    jsonFrameStack = JsonConvert.SerializeObject(frameRecord.Values["frames"]);
                }
                return(jsonFrameStack);
            }
        }
Beispiel #10
0
        private IDictionary <string, object> FetchRelatedNode <T>(string uuid)
            where T : EntityBase, new()
        {
            if (string.IsNullOrWhiteSpace(uuid))
            {
                throw new ArgumentNullException("uuid");
            }

            var query = new StringFormatter(QueryTemplates.TEMPLATE_GET_BY_PROPERTIES);

            query.Add("@label", new T().Label);
            query.Add("@clause", "Uuid:$Uuid");
            query.Add("@result", TAG);
            query.Add("@relatedNode", string.Empty);
            query.Add("@relationship", string.Empty);

            var nodes = new Lazy <Dictionary <string, object> >();

            foreach (PropertyInfo prop in typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                if (prop.GetCustomAttribute(typeof(NotMappedAttribute), true) != null)
                {
                    continue;
                }

                RelationshipAttribute relationshipAttribute = (RelationshipAttribute)prop.GetCustomAttributes(typeof(RelationshipAttribute), true).FirstOrDefault();

                if (relationshipAttribute != null)
                {
                    string labelName;
                    if (prop.PropertyType.GetInterfaces().Contains(typeof(IEnumerable)))
                    {
                        labelName = (Activator.CreateInstance(prop.PropertyType.GetGenericArguments()[0], null) as EntityBase).Label;
                    }
                    else
                    {
                        labelName = (Activator.CreateInstance(prop.PropertyType, null) as EntityBase).Label;
                    }

                    string parameterRelatedNode = string.Format(@"(rNode:{0}{{IsDeleted:false}})", labelName);

                    string parameterRelationship = string.Format(
                        @"{0}[r:{1}]{2}",
                        relationshipAttribute.Direction == DIRECTION.INCOMING ? "<-" : "-",
                        relationshipAttribute.Name,
                        relationshipAttribute.Direction == DIRECTION.INCOMING ? "-" : "->");

                    query.Remove("@result");
                    query.Remove("@relatedNode");
                    query.Remove("@relationship");

                    query.Add("@relatedNode", parameterRelatedNode);
                    query.Add("@relationship", parameterRelationship);
                    query.Add("@result", "rNode");

                    IStatementResult resultRelatedNode = ExecuteQuery(query.ToString(), new { Uuid = uuid });

                    if (prop.PropertyType.GetInterfaces().Contains(typeof(IEnumerable)))
                    {
                        var relatedNodes = new Lazy <List <IReadOnlyDictionary <string, object> > >();

                        foreach (IRecord record in resultRelatedNode)
                        {
                            IReadOnlyDictionary <string, object> node = record[0].As <INode>().Properties;

                            relatedNodes.Value.Add(node);
                        }

                        if (relatedNodes.IsValueCreated)
                        {
                            nodes.Value.Add(prop.Name, relatedNodes.Value);
                        }
                    }
                    else
                    {
                        IReadOnlyDictionary <string, object> relatedNode = resultRelatedNode.FirstOrDefault()?[0].As <INode>().Properties;

                        if (relatedNode != null)
                        {
                            nodes.Value.Add(prop.Name, relatedNode);
                        }
                    }
                }
            }

            return(nodes.Value);
        }
Beispiel #11
0
        /// <summary>
        /// Ping method.
        /// </summary>
        /// <returns>Return answer from the database.</returns>
        public bool Ping()
        {
            IStatementResult result = ExecuteQuery("RETURN 1");

            return(result.FirstOrDefault()?[0].As <int>() == 1);
        }