From c978856407543c97052458687950e77e40554afc Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 11 Jan 2012 01:34:02 -0500 Subject: [PATCH 1/3] Created Interface INeo4jRestApi --- Neo4jRestNet.Test/App.config | 6 +- Neo4jRestNet.Test/EncryptedIDTest.cs | 8 ++ Neo4jRestNet/Core/Node.cs | 151 +++++++++++++------------- Neo4jRestNet/Core/Relationship.cs | 101 +++++++++-------- Neo4jRestNet/Core/StoreFactory.cs | 24 ++-- Neo4jRestNet/CypherPlugin/Cypher.cs | 6 +- Neo4jRestNet/GremlinPlugin/Gremlin.cs | 79 +------------- Neo4jRestNet/Neo4jRestNet.csproj | 1 + Neo4jRestNet/Rest/INeo4jRestApi.cs | 59 ++++++++++ Neo4jRestNet/Rest/Neo4jRestApi.cs | 54 ++++----- 10 files changed, 240 insertions(+), 249 deletions(-) create mode 100644 Neo4jRestNet/Rest/INeo4jRestApi.cs diff --git a/Neo4jRestNet.Test/App.config b/Neo4jRestNet.Test/App.config index 0e21fb1..e44a453 100644 --- a/Neo4jRestNet.Test/App.config +++ b/Neo4jRestNet.Test/App.config @@ -1,9 +1,9 @@  - - - + + + diff --git a/Neo4jRestNet.Test/EncryptedIDTest.cs b/Neo4jRestNet.Test/EncryptedIDTest.cs index 3880b1b..f728900 100644 --- a/Neo4jRestNet.Test/EncryptedIDTest.cs +++ b/Neo4jRestNet.Test/EncryptedIDTest.cs @@ -16,6 +16,14 @@ namespace Neo4jRestNet.Test public class EncryptedIDTest { + [TestMethod] + public void GetNode() + { + var node = Node.GetNode(0); + + Assert.AreEqual(node.Id, 0); + } + [TestMethod] public void NewGEIDFromLong() { diff --git a/Neo4jRestNet/Core/Node.cs b/Neo4jRestNet/Core/Node.cs index 073f000..3edd31c 100644 --- a/Neo4jRestNet/Core/Node.cs +++ b/Neo4jRestNet/Core/Node.cs @@ -16,6 +16,7 @@ private enum NodeProperty NodeType } + private const string DefaultConnectionName = "neo4j"; private string _selfDbUrl; private string _self; private Properties _properties; @@ -30,13 +31,13 @@ private enum NodeProperty public static Node GetRootNode() { - return GetRootNode(DbStore.Default); + return GetRootNode(DefaultConnectionName); } - public static Node GetRootNode(DbStore dbUrl) + public static Node GetRootNode(string connectionName) { string response; - var status = new StoreFactory().Create(dbUrl).GetRoot(out response); + var status = new StoreFactory().CreateNeo4jRestApi(connectionName).GetRoot(out response); if (status != HttpStatusCode.OK) { throw new Exception(string.Format("Error getting root node (http response:{0})", status)); @@ -69,13 +70,13 @@ public static Node GetRootNode(DbStore dbUrl) public static Node GetNode(EncryptId nodeId) { - return GetNode(DbStore.Default, nodeId); + return GetNode(DefaultConnectionName, nodeId); } - public static Node GetNode(DbStore dbUrl, EncryptId nodeId) + public static Node GetNode(string connectionName, EncryptId nodeId) { string response; - var status = new StoreFactory().Create(dbUrl).GetNode((long)nodeId, out response); + var status = new StoreFactory().CreateNeo4jRestApi(connectionName).GetNode((long)nodeId, out response); if (status != HttpStatusCode.OK) { throw new Exception(string.Format("Node not found (node id:{0})", (long)nodeId)); @@ -86,28 +87,28 @@ public static Node GetNode(DbStore dbUrl, EncryptId nodeId) public static IEnumerable GetNode(string indexName, string key, object value) { - return GetNode(DbStore.Default, indexName, key, value); + return GetNode(DefaultConnectionName, indexName, key, value); } public static IEnumerable GetNode(Enum indexName, string key, object value) { - return GetNode(DbStore.Default, indexName.ToString(), key, value); + return GetNode(DefaultConnectionName, indexName.ToString(), key, value); } public static IEnumerable GetNode(string indexName, Enum key, object value) { - return GetNode(DbStore.Default, indexName, key.ToString(), value); + return GetNode(DefaultConnectionName, indexName, key.ToString(), value); } public static IEnumerable GetNode(Enum indexName, Enum key, object value) { - return GetNode(DbStore.Default, indexName.ToString(), key.ToString(), value); + return GetNode(DefaultConnectionName, indexName.ToString(), key.ToString(), value); } - public static IEnumerable GetNode(DbStore dbUrl, string indexName, string key, object value) + public static IEnumerable GetNode(string connectionName, string indexName, string key, object value) { string response; - var status = new StoreFactory().Create(dbUrl).GetNode(indexName, key, value, out response); + var status = new StoreFactory().CreateNeo4jRestApi(connectionName).GetNode(indexName, key, value, out response); if (status != HttpStatusCode.OK) { throw new Exception(string.Format("Index not found in (index:{0})", indexName)); @@ -116,35 +117,35 @@ public static IEnumerable GetNode(DbStore dbUrl, string indexName, string return ParseJson(response); } - public static IEnumerable GetNode(DbStore dbUrl, Enum indexName, string key, object value) + public static IEnumerable GetNode(string connectionName, Enum indexName, string key, object value) { - return GetNode(dbUrl, indexName.ToString(), key, value); + return GetNode(connectionName, indexName.ToString(), key, value); } - public static IEnumerable GetNode(DbStore dbUrl, string indexName, Enum key, object value) + public static IEnumerable GetNode(string connectionName, string indexName, Enum key, object value) { - return GetNode(dbUrl, indexName, key.ToString(), value); + return GetNode(connectionName, indexName, key.ToString(), value); } - public static IEnumerable GetNode(DbStore dbUrl, Enum indexName, Enum key, object value) + public static IEnumerable GetNode(string connectionName, Enum indexName, Enum key, object value) { - return GetNode(dbUrl, indexName.ToString(), key.ToString(), value); + return GetNode(connectionName, indexName.ToString(), key.ToString(), value); } public static IEnumerable GetNode(string indexName, string searchQuery) { - return GetNode(DbStore.Default, indexName, searchQuery); + return GetNode(DefaultConnectionName, indexName, searchQuery); } public static IEnumerable GetNode(Enum indexName, string searchQuery) { - return GetNode(DbStore.Default, indexName.ToString(), searchQuery); + return GetNode(DefaultConnectionName, indexName.ToString(), searchQuery); } - public static IEnumerable GetNode(DbStore dbUrl, string indexName, string searchQuery) + public static IEnumerable GetNode(string connectionName, string indexName, string searchQuery) { string response; - var status = new StoreFactory().Create(dbUrl).GetNode(indexName, searchQuery, out response); + var status = new StoreFactory().CreateNeo4jRestApi(connectionName).GetNode(indexName, searchQuery, out response); if (status != HttpStatusCode.OK) { throw new Exception(string.Format("Index not found in (index:{0})", indexName)); @@ -153,9 +154,9 @@ public static IEnumerable GetNode(DbStore dbUrl, string indexName, string return ParseJson(response); } - public static IEnumerable GetNode(DbStore dbUrl, Enum indexName, string searchQuery) + public static IEnumerable GetNode(string connectionName, Enum indexName, string searchQuery) { - return GetNode(dbUrl, indexName.ToString(), searchQuery); + return GetNode(connectionName, indexName.ToString(), searchQuery); } #endregion @@ -166,7 +167,7 @@ public static Node CreateNode(string nodeType) { var properties = new Properties(); properties.SetProperty(NodeProperty.NodeType.ToString(), nodeType); - return CreateNodeFromJson(DbStore.Default, properties.ToString()); + return CreateNodeFromJson(DefaultConnectionName, properties.ToString()); } public static Node CreateNode(Enum nodeType) @@ -174,21 +175,21 @@ public static Node CreateNode(Enum nodeType) return CreateNode(nodeType.ToString()); } - public static Node CreateNode(DbStore dbUrl, string nodeType) + public static Node CreateNode(string connectionName, string nodeType) { var properties = new Properties(); properties.SetProperty(NodeProperty.NodeType.ToString(), nodeType); - return CreateNodeFromJson(dbUrl, properties.ToString()); + return CreateNodeFromJson(connectionName, properties.ToString()); } - public static Node CreateNode(DbStore dbUrl, Enum nodeType) + public static Node CreateNode(string connectionName, Enum nodeType) { - return CreateNode(dbUrl, nodeType.ToString()); + return CreateNode(connectionName, nodeType.ToString()); } public static Node CreateNode(string nodeType, Properties properties) { properties.SetProperty(NodeProperty.NodeType.ToString(), nodeType); - return CreateNodeFromJson(DbStore.Default, properties.ToString()); + return CreateNodeFromJson(DefaultConnectionName, properties.ToString()); } public static Node CreateNode(Enum nodeType, Properties properties) @@ -196,21 +197,21 @@ public static Node CreateNode(Enum nodeType, Properties properties) return CreateNode(nodeType.ToString(), properties); } - public static Node CreateNode(DbStore dbUrl, string nodeType, Properties properties) + public static Node CreateNode(string connectionName, string nodeType, Properties properties) { properties.SetProperty(NodeProperty.NodeType.ToString(), nodeType); - return CreateNodeFromJson(dbUrl, properties.ToString()); + return CreateNodeFromJson(connectionName, properties.ToString()); } - public static Node CreateNode(DbStore dbUrl, Enum nodeType, Properties properties) + public static Node CreateNode(string connectionName, Enum nodeType, Properties properties) { - return CreateNode(dbUrl, nodeType.ToString(), properties); + return CreateNode(connectionName, nodeType.ToString(), properties); } public static Node CreateNode(string nodeType, IDictionary properties) { properties.Add(NodeProperty.NodeType.ToString(), nodeType); - return CreateNodeFromJson(DbStore.Default, JObject.FromObject(properties).ToString(Formatting.None)); + return CreateNodeFromJson(DefaultConnectionName, JObject.FromObject(properties).ToString(Formatting.None)); } public static Node CreateNode(Enum nodeType, IDictionary properties) @@ -218,21 +219,21 @@ public static Node CreateNode(Enum nodeType, IDictionary propert return CreateNode(nodeType.ToString(), properties); } - public static Node CreateNode(DbStore dbUrl, string nodeType, IDictionary properties) + public static Node CreateNode(string connectionName, string nodeType, IDictionary properties) { properties.Add(NodeProperty.NodeType.ToString(), nodeType); - return CreateNodeFromJson(dbUrl, JObject.FromObject(properties).ToString(Formatting.None)); + return CreateNodeFromJson(connectionName, JObject.FromObject(properties).ToString(Formatting.None)); } - public static Node CreateNode(DbStore dbUrl, Enum nodeType, IDictionary properties) + public static Node CreateNode(string connectionName, Enum nodeType, IDictionary properties) { - return CreateNode(dbUrl, nodeType.ToString(), properties); + return CreateNode(connectionName, nodeType.ToString(), properties); } - private static Node CreateNodeFromJson(DbStore dbUrl, string jsonProperties) + private static Node CreateNodeFromJson(string connectionName, string jsonProperties) { string response; - var status = new StoreFactory().Create(dbUrl).CreateNode(jsonProperties, out response); + var status = new StoreFactory().CreateNeo4jRestApi(connectionName).CreateNode(jsonProperties, out response); if (status != HttpStatusCode.Created) { throw new Exception(string.Format("Error creating node (http response:{0})", status)); @@ -247,7 +248,7 @@ private static Node CreateNodeFromJson(DbStore dbUrl, string jsonProperties) public HttpStatusCode DeleteNode() { - var status = new StoreFactory().Create(DbStore.Default).DeleteNode(Id); + var status = new StoreFactory().CreateNeo4jRestApi(_selfDbUrl).DeleteNode(Id); if (status != HttpStatusCode.NoContent) { throw new Exception(string.Format("Error deleting node (node id:{0} http response:{1})", Id, status)); @@ -367,7 +368,7 @@ private void LoadProperties(bool refresh) } string response; - HttpStatusCode status = new StoreFactory().Create(DbStore.Default).GetPropertiesOnNode((long)EncryptedId, out response); + var status = new StoreFactory().CreateNeo4jRestApi(_selfDbUrl).GetPropertiesOnNode((long)EncryptedId, out response); if (status != HttpStatusCode.OK) { throw new Exception(string.Format("Error retrieving properties on node (node id:{0} http response:{1})", (long)EncryptedId, status)); @@ -398,7 +399,7 @@ public void SaveProperties(Properties properties) properties.SetProperty(NodeProperty.NodeType.ToString(), Properties.GetProperty(NodeProperty.NodeType.ToString())); } - HttpStatusCode status = new StoreFactory().Create(DbStore.Default).SetPropertiesOnNode((long)EncryptedId, properties.ToString()); + var status = new StoreFactory().CreateNeo4jRestApi(_selfDbUrl).SetPropertiesOnNode((long)EncryptedId, properties.ToString()); if (status != HttpStatusCode.NoContent) { throw new Exception(string.Format("Error setting properties on node (node id:{0} http response:{1})", (long)EncryptedId, status)); @@ -459,7 +460,7 @@ public IEnumerable GetRelationships(RelationshipDirection directio public IEnumerable GetRelationships(RelationshipDirection direction, IEnumerable names) { string response; - HttpStatusCode status = new StoreFactory().Create(DbStore.Default).GetRelationshipsOnNode((long)EncryptedId, direction, names, out response); + HttpStatusCode status = new StoreFactory().CreateNeo4jRestApi(DefaultConnectionName).GetRelationshipsOnNode((long)EncryptedId, direction, names, out response); if (status != HttpStatusCode.OK) { throw new Exception(string.Format("Error retrieving relationships on node (node id:{0} http response:{1})", (long)EncryptedId, status)); @@ -486,7 +487,7 @@ public Relationship CreateRelationshipTo(Node toNode, Enum relationshipType, Pro public Relationship CreateRelationshipTo(Node toNode, string relationshipType, Properties relationshipProperties) { string response; - HttpStatusCode status = new StoreFactory().Create(DbStore.Default).CreateRelationship( + HttpStatusCode status = new StoreFactory().CreateNeo4jRestApi(DefaultConnectionName).CreateRelationship( (long)EncryptedId, toNode.Self, relationshipType, @@ -507,7 +508,7 @@ public Relationship CreateRelationshipTo(Node toNode, string relationshipType, P public IEnumerable Traverse(Order order, Uniqueness uniqueness, IEnumerable relationships, PruneEvaluator pruneEvaluator, ReturnFilter returnFilter, int? maxDepth, ReturnType returnType) { string response; - var status = new StoreFactory().Create(DbStore.Default).Traverse((long)EncryptedId, order, uniqueness, relationships, pruneEvaluator, returnFilter, maxDepth, returnType, out response); + var status = new StoreFactory().CreateNeo4jRestApi(DefaultConnectionName).Traverse((long)EncryptedId, order, uniqueness, relationships, pruneEvaluator, returnFilter, maxDepth, returnType, out response); if (status != HttpStatusCode.OK) { throw new Exception(string.Format("Error traversing nodes (node id:{0} status code:{1})", (long)EncryptedId, status)); @@ -537,33 +538,33 @@ public IEnumerable Traverse(Order order, Uniqueness uniqueness, IE public static Node AddNodeToIndex(long nodeId, string indexName, string key, object value) { - return AddNodeToIndex(DbStore.Default, nodeId, indexName, key, value); + return AddNodeToIndex(DefaultConnectionName, nodeId, indexName, key, value); } public static Node AddNodeToIndex(long nodeId, Enum indexName, string key, object value) { - return AddNodeToIndex(DbStore.Default, nodeId, indexName.ToString(), key, value); + return AddNodeToIndex(DefaultConnectionName, nodeId, indexName.ToString(), key, value); } public static Node AddNodeToIndex(long nodeId, string indexName, Enum key, object value) { - return AddNodeToIndex(DbStore.Default, nodeId, indexName, key.ToString(), value); + return AddNodeToIndex(DefaultConnectionName, nodeId, indexName, key.ToString(), value); } public static Node AddNodeToIndex(long nodeId, Enum indexName, Enum key, object value) { - return AddNodeToIndex(DbStore.Default, nodeId, indexName.ToString(), key.ToString(), value); + return AddNodeToIndex(DefaultConnectionName, nodeId, indexName.ToString(), key.ToString(), value); } - public static Node AddNodeToIndex(DbStore dbUrl, long nodeId, Enum indexName, Enum key, object value) + public static Node AddNodeToIndex(string connectionName, long nodeId, Enum indexName, Enum key, object value) { - return AddNodeToIndex(dbUrl, nodeId, indexName.ToString(), key.ToString(), value); + return AddNodeToIndex(connectionName, nodeId, indexName.ToString(), key.ToString(), value); } - public static Node AddNodeToIndex(DbStore dbUrl, long nodeId, string indexName, string key, object value) + public static Node AddNodeToIndex(string connectionName, long nodeId, string indexName, string key, object value) { string response; - var status = new StoreFactory().Create(dbUrl).AddNodeToIndex(nodeId, indexName, key, value, out response); + var status = new StoreFactory().CreateNeo4jRestApi(connectionName).AddNodeToIndex(nodeId, indexName, key, value, out response); if (status != HttpStatusCode.Created) { throw new Exception(string.Format("Error creating index for node (http response:{0})", status)); @@ -574,22 +575,22 @@ public static Node AddNodeToIndex(DbStore dbUrl, long nodeId, string indexName, public static HttpStatusCode RemoveNodeFromIndex(long nodeId, string indexName) { - return RemoveNodeFromIndex(DbStore.Default, nodeId, indexName); + return RemoveNodeFromIndex(DefaultConnectionName, nodeId, indexName); } public static HttpStatusCode RemoveNodeFromIndex(long nodeId, Enum indexName) { - return RemoveNodeFromIndex(DbStore.Default, nodeId, indexName.ToString()); + return RemoveNodeFromIndex(DefaultConnectionName, nodeId, indexName.ToString()); } - public static HttpStatusCode RemoveNodeFromIndex(DbStore dbUrl, long nodeId, Enum indexName) + public static HttpStatusCode RemoveNodeFromIndex(string connectionName, long nodeId, Enum indexName) { - return RemoveNodeFromIndex(dbUrl, nodeId, indexName.ToString()); + return RemoveNodeFromIndex(connectionName, nodeId, indexName.ToString()); } - public static HttpStatusCode RemoveNodeFromIndex(DbStore dbUrl, long nodeId, string indexName) + public static HttpStatusCode RemoveNodeFromIndex(string connectionName, long nodeId, string indexName) { - var status = new StoreFactory().Create(dbUrl).RemoveNodeFromIndex(nodeId, indexName); + var status = new StoreFactory().CreateNeo4jRestApi(connectionName).RemoveNodeFromIndex(nodeId, indexName); if (status != HttpStatusCode.NoContent) { throw new Exception(string.Format("Error remove node from index (node id:{0} index name:{1} http response:{2})", nodeId, indexName, status)); @@ -600,32 +601,32 @@ public static HttpStatusCode RemoveNodeFromIndex(DbStore dbUrl, long nodeId, str public static HttpStatusCode RemoveNodeFromIndex(long nodeId, string indexName, string key) { - return RemoveNodeFromIndex(DbStore.Default, nodeId, indexName, key); + return RemoveNodeFromIndex(DefaultConnectionName, nodeId, indexName, key); } public static HttpStatusCode RemoveNodeFromIndex(long nodeId, Enum indexName, string key) { - return RemoveNodeFromIndex(DbStore.Default, nodeId, indexName.ToString(), key); + return RemoveNodeFromIndex(DefaultConnectionName, nodeId, indexName.ToString(), key); } public static HttpStatusCode RemoveNodeFromIndex(long nodeId, string indexName, Enum key) { - return RemoveNodeFromIndex(DbStore.Default, nodeId, indexName, key.ToString()); + return RemoveNodeFromIndex(DefaultConnectionName, nodeId, indexName, key.ToString()); } public static HttpStatusCode RemoveNodeFromIndex(long nodeId, Enum indexName, Enum key) { - return RemoveNodeFromIndex(DbStore.Default, nodeId, indexName.ToString(), key.ToString()); + return RemoveNodeFromIndex(DefaultConnectionName, nodeId, indexName.ToString(), key.ToString()); } - public static HttpStatusCode RemoveNodeFromIndex(DbStore dbUrl, long nodeId, Enum indexName, Enum key) + public static HttpStatusCode RemoveNodeFromIndex(string connectionName, long nodeId, Enum indexName, Enum key) { - return RemoveNodeFromIndex(dbUrl, nodeId, indexName.ToString(), key.ToString()); + return RemoveNodeFromIndex(connectionName, nodeId, indexName.ToString(), key.ToString()); } - public static HttpStatusCode RemoveNodeFromIndex(DbStore dbUrl, long nodeId, string indexName, string key) + public static HttpStatusCode RemoveNodeFromIndex(string connectionName, long nodeId, string indexName, string key) { - var status = new StoreFactory().Create(dbUrl).RemoveNodeFromIndex(nodeId, indexName, key); + var status = new StoreFactory().CreateNeo4jRestApi(connectionName).RemoveNodeFromIndex(nodeId, indexName, key); if (status != HttpStatusCode.NoContent) { throw new Exception(string.Format("Error remove node from index (node id:{0} index name:{1} http response:{2})", nodeId, indexName, status)); @@ -636,27 +637,27 @@ public static HttpStatusCode RemoveNodeFromIndex(DbStore dbUrl, long nodeId, str public static HttpStatusCode RemoveNodeFromIndex(long nodeId, string indexName, string key, object value) { - return RemoveNodeFromIndex(DbStore.Default, nodeId, indexName, key, value); + return RemoveNodeFromIndex(DefaultConnectionName, nodeId, indexName, key, value); } public static HttpStatusCode RemoveNodeFromIndex(long nodeId, Enum indexName, string key, object value) { - return RemoveNodeFromIndex(DbStore.Default, nodeId, indexName.ToString(), key, value); + return RemoveNodeFromIndex(DefaultConnectionName, nodeId, indexName.ToString(), key, value); } public static HttpStatusCode RemoveNodeFromIndex(long nodeId, string indexName, Enum key, object value) { - return RemoveNodeFromIndex(DbStore.Default, nodeId, indexName, key.ToString(), value); + return RemoveNodeFromIndex(DefaultConnectionName, nodeId, indexName, key.ToString(), value); } public static HttpStatusCode RemoveNodeFromIndex(long nodeId, Enum indexName, Enum key, object value) { - return RemoveNodeFromIndex(DbStore.Default, nodeId, indexName.ToString(), key.ToString(), value); + return RemoveNodeFromIndex(DefaultConnectionName, nodeId, indexName.ToString(), key.ToString(), value); } - public static HttpStatusCode RemoveNodeFromIndex(DbStore dbUrl, long nodeId, string indexName, string key, object value) + public static HttpStatusCode RemoveNodeFromIndex(string connectionName, long nodeId, string indexName, string key, object value) { - var status = new StoreFactory().Create(dbUrl).RemoveNodeFromIndex(nodeId, indexName, key, value); + var status = new StoreFactory().CreateNeo4jRestApi(connectionName).RemoveNodeFromIndex(nodeId, indexName, key, value); if (status != HttpStatusCode.NoContent) { throw new Exception(string.Format("Error remove node from index (node id:{0} index name:{1} http response:{2})", nodeId, indexName, status)); diff --git a/Neo4jRestNet/Core/Relationship.cs b/Neo4jRestNet/Core/Relationship.cs index 0dd8a69..02ff37b 100644 --- a/Neo4jRestNet/Core/Relationship.cs +++ b/Neo4jRestNet/Core/Relationship.cs @@ -12,9 +12,8 @@ namespace Neo4jRestNet.Core { public class Relationship : IGraphObject { - private static readonly string _defaultDbUrl = ConfigurationManager.ConnectionStrings["neo4j"].ConnectionString.TrimEnd('/'); - - private string _dbUrl; + private const string DefaultConnectionName = "neo4j"; + private string _selfDbUrl; private string _self; public Node StartNode { get; private set; } public Node EndNode { get; private set; } @@ -30,33 +29,33 @@ public class Relationship : IGraphObject public static IEnumerable GetRelationship(string indexName, string key, object value) { - return GetRelationship(_defaultDbUrl, indexName, key, value); + return GetRelationship(DefaultConnectionName, indexName, key, value); } public static IEnumerable GetRelationship(Enum indexName, string key, object value) { - return GetRelationship(_defaultDbUrl, indexName.ToString(), key, value); + return GetRelationship(DefaultConnectionName, indexName.ToString(), key, value); } public static IEnumerable GetRelationship(string indexName, Enum key, object value) { - return GetRelationship(_defaultDbUrl, indexName, key.ToString(), value); + return GetRelationship(DefaultConnectionName, indexName, key.ToString(), value); } public static IEnumerable GetRelationship(Enum indexName, Enum key, object value) { - return GetRelationship(_defaultDbUrl, indexName.ToString(), key.ToString(), value); + return GetRelationship(DefaultConnectionName, indexName.ToString(), key.ToString(), value); } - public static IEnumerable GetRelationship(string dbUrl, Enum indexName, Enum key, object value) + public static IEnumerable GetRelationship(string connectionName, Enum indexName, Enum key, object value) { - return GetRelationship(dbUrl, indexName.ToString(), key.ToString(), value); + return GetRelationship(connectionName, indexName.ToString(), key.ToString(), value); } - public static IEnumerable GetRelationship(string dbUrl, string indexName, string key, object value) + public static IEnumerable GetRelationship(string connectionName, string indexName, string key, object value) { string response; - HttpStatusCode status = Neo4jRestApi.GetRelationship(dbUrl, indexName, key, value, out response); + HttpStatusCode status = new StoreFactory().CreateNeo4jRestApi(connectionName).GetRelationship(connectionName, indexName, key, value, out response); if (status != HttpStatusCode.OK) { throw new Exception(string.Format("Index not found in (index:{0})", indexName)); @@ -67,23 +66,23 @@ public static IEnumerable GetRelationship(string dbUrl, string ind public static IEnumerable GetRelationship(string indexName, string searchQuery) { - return GetRelationship(_defaultDbUrl, indexName, searchQuery); + return GetRelationship(DefaultConnectionName, indexName, searchQuery); } public static IEnumerable GetRelationship(Enum indexName, string searchQuery) { - return GetRelationship(_defaultDbUrl, indexName.ToString(), searchQuery); + return GetRelationship(DefaultConnectionName, indexName.ToString(), searchQuery); } - public static IEnumerable GetRelationship(string dbUrl, Enum indexName, string searchQuery) + public static IEnumerable GetRelationship(string connectionName, Enum indexName, string searchQuery) { - return GetRelationship(dbUrl, indexName.ToString(), searchQuery); + return GetRelationship(connectionName, indexName.ToString(), searchQuery); } - public static IEnumerable GetRelationship(string dbUrl, string indexName, string searchQuery) + public static IEnumerable GetRelationship(string connectionName, string indexName, string searchQuery) { string response; - HttpStatusCode status = Neo4jRestApi.GetRelationship(dbUrl, indexName, searchQuery, out response); + HttpStatusCode status = new StoreFactory().CreateNeo4jRestApi(connectionName).GetRelationship(connectionName, indexName, searchQuery, out response); if (status != HttpStatusCode.OK) { throw new Exception(string.Format("Index not found in (index:{0})", indexName)); @@ -234,7 +233,7 @@ private set throw new Exception(string.Format("Invalid Self id ({0})", value)); } - _dbUrl = self.Substring(0, self.LastIndexOf("/relationship")); + _selfDbUrl = self.Substring(0, self.LastIndexOf("/relationship")); _self = self; Id = relationshipId; } @@ -257,7 +256,7 @@ private void LoadProperties(bool refresh) } string response; - HttpStatusCode status = Neo4jRestApi.GetPropertiesOnRelationship(_dbUrl, Id, out response); + var status = new StoreFactory().CreateNeo4jRestApi(_selfDbUrl).GetPropertiesOnRelationship(_selfDbUrl, Id, out response); if (status != HttpStatusCode.OK) { throw new Exception(string.Format("Error retrieving properties on relationship (relationship id:{0} http response:{1})", Id, status)); @@ -282,7 +281,7 @@ public void SaveProperties() public void SaveProperties(Properties properties) { - HttpStatusCode status = Neo4jRestApi.SetPropertiesOnRelationship(_dbUrl, Id, properties.ToString()); + var status = new StoreFactory().CreateNeo4jRestApi(_selfDbUrl).SetPropertiesOnRelationship(_selfDbUrl, Id, properties.ToString()); if (status != HttpStatusCode.NoContent) { throw new Exception(string.Format("Error setting properties on relationship (relationship id:{0} http response:{1})", Id, status)); @@ -297,33 +296,33 @@ public void SaveProperties(Properties properties) public static Relationship AddRelationshipToIndex(long relationshipId, string indexName, string key, object value) { - return AddRelationshipToIndex(_defaultDbUrl, relationshipId, indexName, key, value); + return AddRelationshipToIndex(DefaultConnectionName, relationshipId, indexName, key, value); } public static Relationship AddRelationshipToIndex(long relationshipId, Enum indexName, string key, object value) { - return AddRelationshipToIndex(_defaultDbUrl, relationshipId, indexName.ToString(), key, value); + return AddRelationshipToIndex(DefaultConnectionName, relationshipId, indexName.ToString(), key, value); } public static Relationship AddRelationshipToIndex(long relationshipId, string indexName, Enum key, object value) { - return AddRelationshipToIndex(_defaultDbUrl, relationshipId, indexName, key.ToString(), value); + return AddRelationshipToIndex(DefaultConnectionName, relationshipId, indexName, key.ToString(), value); } public static Relationship AddRelationshipToIndex(long relationshipId, Enum indexName, Enum key, object value) { - return AddRelationshipToIndex(_defaultDbUrl, relationshipId, indexName.ToString(), key.ToString(), value); + return AddRelationshipToIndex(DefaultConnectionName, relationshipId, indexName.ToString(), key.ToString(), value); } - public static Relationship AddRelationshipToIndex(string dbUrl, long relationshipId, Enum indexName, Enum key, object value) + public static Relationship AddRelationshipToIndex(string connectionName, long relationshipId, Enum indexName, Enum key, object value) { - return AddRelationshipToIndex(dbUrl, relationshipId, indexName.ToString(), key.ToString(), value); + return AddRelationshipToIndex(connectionName, relationshipId, indexName.ToString(), key.ToString(), value); } - public static Relationship AddRelationshipToIndex(string dbUrl, long relationshipId, string indexName, string key, object value) + public static Relationship AddRelationshipToIndex(string connectionName, long relationshipId, string indexName, string key, object value) { string response; - var status = Neo4jRestApi.AddRelationshipToIndex(dbUrl, relationshipId, indexName, key, value, out response); + var status = new StoreFactory().CreateNeo4jRestApi(connectionName).AddRelationshipToIndex(connectionName, relationshipId, indexName, key, value, out response); if (status != HttpStatusCode.Created) { throw new Exception(string.Format("Error creating index for relationship (http response:{0})", status)); @@ -334,22 +333,22 @@ public static Relationship AddRelationshipToIndex(string dbUrl, long relationshi public HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, string indexName) { - return RemoveRelationshipFromIndex(_defaultDbUrl, relationshipId, indexName); + return RemoveRelationshipFromIndex(DefaultConnectionName, relationshipId, indexName); } public HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, Enum indexName) { - return RemoveRelationshipFromIndex(_defaultDbUrl, relationshipId, indexName.ToString()); + return RemoveRelationshipFromIndex(DefaultConnectionName, relationshipId, indexName.ToString()); } - public HttpStatusCode RemoveRelationshipFromIndex(string dbUrl, long relationshipId, Enum indexName) + public HttpStatusCode RemoveRelationshipFromIndex(string connectionName, long relationshipId, Enum indexName) { - return RemoveRelationshipFromIndex(dbUrl, relationshipId, indexName.ToString()); + return RemoveRelationshipFromIndex(connectionName, relationshipId, indexName.ToString()); } - public HttpStatusCode RemoveRelationshipFromIndex(string dbUrl, long relationshipId, string indexName) + public HttpStatusCode RemoveRelationshipFromIndex(string connectionName, long relationshipId, string indexName) { - var status = Neo4jRestApi.RemoveRelationshipFromIndex(dbUrl, relationshipId, indexName); + var status = new StoreFactory().CreateNeo4jRestApi(connectionName).RemoveRelationshipFromIndex(connectionName, relationshipId, indexName); if (status != HttpStatusCode.NoContent) { throw new Exception(string.Format("Error remove relationship from index (relationship id:{0} index name:{1} http response:{2})", relationshipId, indexName, status)); @@ -360,32 +359,32 @@ public HttpStatusCode RemoveRelationshipFromIndex(string dbUrl, long relationshi public HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, string indexName, string key) { - return RemoveRelationshipFromIndex(_defaultDbUrl, relationshipId, indexName, key); + return RemoveRelationshipFromIndex(DefaultConnectionName, relationshipId, indexName, key); } public HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, Enum indexName, string key) { - return RemoveRelationshipFromIndex(_defaultDbUrl, relationshipId, indexName.ToString(), key); + return RemoveRelationshipFromIndex(DefaultConnectionName, relationshipId, indexName.ToString(), key); } public HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, string indexName, Enum key) { - return RemoveRelationshipFromIndex(_defaultDbUrl, relationshipId, indexName, key.ToString()); + return RemoveRelationshipFromIndex(DefaultConnectionName, relationshipId, indexName, key.ToString()); } public HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, Enum indexName, Enum key) { - return RemoveRelationshipFromIndex(_defaultDbUrl, relationshipId, indexName.ToString(), key.ToString()); + return RemoveRelationshipFromIndex(DefaultConnectionName, relationshipId, indexName.ToString(), key.ToString()); } - public HttpStatusCode RemoveRelationshipFromIndex(string dbUrl, long relationshipId, Enum indexName, Enum key) + public HttpStatusCode RemoveRelationshipFromIndex(string connectionName, long relationshipId, Enum indexName, Enum key) { - return RemoveRelationshipFromIndex(dbUrl, relationshipId, indexName.ToString(), key.ToString()); + return RemoveRelationshipFromIndex(connectionName, relationshipId, indexName.ToString(), key.ToString()); } - public HttpStatusCode RemoveRelationshipFromIndex(string dbUrl, long relationshipId, string indexName, string key) + public HttpStatusCode RemoveRelationshipFromIndex(string connectionName, long relationshipId, string indexName, string key) { - var status = Neo4jRestApi.RemoveRelationshipFromIndex(dbUrl, relationshipId, indexName, key); + var status = new StoreFactory().CreateNeo4jRestApi(connectionName).RemoveRelationshipFromIndex(connectionName, relationshipId, indexName, key); if (status != HttpStatusCode.NoContent) { throw new Exception(string.Format("Error remove relationship from index (relationship id:{0} index name:{1} http response:{2})", relationshipId, indexName, status)); @@ -396,32 +395,32 @@ public HttpStatusCode RemoveRelationshipFromIndex(string dbUrl, long relationshi public HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, string indexName, string key, object value) { - return RemoveRelationshipFromIndex(_defaultDbUrl, relationshipId, indexName, key, value); + return RemoveRelationshipFromIndex(DefaultConnectionName, relationshipId, indexName, key, value); } public HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, Enum indexName, string key, object value) { - return RemoveRelationshipFromIndex(_defaultDbUrl, relationshipId, indexName.ToString(), key, value); + return RemoveRelationshipFromIndex(DefaultConnectionName, relationshipId, indexName.ToString(), key, value); } public HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, string indexName, Enum key, object value) { - return RemoveRelationshipFromIndex(_defaultDbUrl, relationshipId, indexName, key.ToString(), value); + return RemoveRelationshipFromIndex(DefaultConnectionName, relationshipId, indexName, key.ToString(), value); } public HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, Enum indexName, Enum key, object value) { - return RemoveRelationshipFromIndex(_defaultDbUrl, relationshipId, indexName.ToString(), key.ToString(), value); + return RemoveRelationshipFromIndex(DefaultConnectionName, relationshipId, indexName.ToString(), key.ToString(), value); } - public HttpStatusCode RemoveRelationshipFromIndex(string dbUrl, long relationshipId, Enum indexName, Enum key, object value) + public HttpStatusCode RemoveRelationshipFromIndex(string connectionName, long relationshipId, Enum indexName, Enum key, object value) { - return RemoveRelationshipFromIndex(dbUrl, relationshipId, indexName.ToString(), key.ToString(), value); + return RemoveRelationshipFromIndex(connectionName, relationshipId, indexName.ToString(), key.ToString(), value); } - public HttpStatusCode RemoveRelationshipFromIndex(string dbUrl, long relationshipId, string indexName, string key, object value) + public HttpStatusCode RemoveRelationshipFromIndex(string connectionName, long relationshipId, string indexName, string key, object value) { - var status = Neo4jRestApi.RemoveRelationshipFromIndex(dbUrl, relationshipId, indexName, key, value); + var status = new StoreFactory().CreateNeo4jRestApi(connectionName).RemoveRelationshipFromIndex(connectionName, relationshipId, indexName, key, value); if (status != HttpStatusCode.NoContent) { throw new Exception(string.Format("Error remove relationship from index (relationship id:{0} index name:{1} http response:{2})", relationshipId, indexName, status)); @@ -435,7 +434,7 @@ public HttpStatusCode RemoveRelationshipFromIndex(string dbUrl, long relationshi public HttpStatusCode DeleteRelationship() { - HttpStatusCode status = Neo4jRestApi.DeleteRelationship(_dbUrl, Id); + var status = new StoreFactory().CreateNeo4jRestApi(_selfDbUrl).DeleteRelationship(_selfDbUrl, Id); if (status != HttpStatusCode.NoContent) { throw new Exception(string.Format("Error deleteing relationship (relationship id:{0} http response:{1})", Id, status)); diff --git a/Neo4jRestNet/Core/StoreFactory.cs b/Neo4jRestNet/Core/StoreFactory.cs index e69c447..446c0d2 100644 --- a/Neo4jRestNet/Core/StoreFactory.cs +++ b/Neo4jRestNet/Core/StoreFactory.cs @@ -1,30 +1,24 @@ using System; -using System.Collections.Generic; using System.Configuration; -using System.Linq; -using System.Text; using Neo4jRestNet.Rest; namespace Neo4jRestNet.Core { - public enum DbStore - { - Default, - InMemory - } public class StoreFactory { - private static readonly string DefaultDbUrl = ConfigurationManager.ConnectionStrings["neo4j"].ConnectionString.TrimEnd('/'); - - public Neo4jRestApi Create(DbStore dbStore) + public INeo4jRestApi CreateNeo4jRestApi(string connectionName) { - switch (dbStore) + var dbUrl = ConfigurationManager.ConnectionStrings[connectionName].ConnectionString.TrimEnd('/'); + var provider = ConfigurationManager.ConnectionStrings[connectionName].ProviderName.ToLower(); + + switch (provider) { - case DbStore.Default: - return new Neo4jRestApi(DefaultDbUrl); + case "": + case "neo4j" : + return new Neo4jRestApi(dbUrl); - case DbStore.InMemory: + case "inmemory": throw new NotImplementedException(); default: diff --git a/Neo4jRestNet/CypherPlugin/Cypher.cs b/Neo4jRestNet/CypherPlugin/Cypher.cs index da82453..17e4a30 100644 --- a/Neo4jRestNet/CypherPlugin/Cypher.cs +++ b/Neo4jRestNet/CypherPlugin/Cypher.cs @@ -21,8 +21,8 @@ public class Cypher readonly List>> _where = new List>>(); readonly List> _return = new List>(); readonly List> _orderBy = new List>(); - String _skip = string.Empty; - String _limit = string.Empty; + string _skip = string.Empty; + string _limit = string.Empty; public DataTable Post() { @@ -177,7 +177,7 @@ public string Query { var sbToString = new StringBuilder(); - string label = "START"; + var label = "START"; foreach (var s in _start) { sbToString.AppendFormat("{1}{0}", s.Invoke(new CypherStart()), label); diff --git a/Neo4jRestNet/GremlinPlugin/Gremlin.cs b/Neo4jRestNet/GremlinPlugin/Gremlin.cs index a8da9a3..e8af3d7 100644 --- a/Neo4jRestNet/GremlinPlugin/Gremlin.cs +++ b/Neo4jRestNet/GremlinPlugin/Gremlin.cs @@ -17,8 +17,6 @@ public class Gremlin public static HttpStatusCode Post(GremlinScript script) { - //return Post(string.Concat(DefaultDbUrl, DefaultGremlinExtensionPath), script); - // Remove trailing / var gremlinUrl = string.Concat(DefaultDbUrl, DefaultGremlinExtensionPath).TrimEnd('/'); @@ -29,44 +27,7 @@ public static HttpStatusCode Post(GremlinScript script) return status; } -/* - public static HttpStatusCode Post(string script) - { - return Post(string.Concat(DefaultDbUrl, DefaultGremlinExtensionPath), script); - } -*/ -/* - public static HttpStatusCode Post(string gremlinUrl, GremlinScript script) - { - return Post(gremlinUrl, script.ToString()); - } -*/ -/* - public static HttpStatusCode Post(string gremlinUrl, string script) - { - // Remove trailing / - gremlinUrl = gremlinUrl.TrimEnd('/'); - - var jo = new JObject {{"script", script}}; - - string response; - HttpStatusCode status = Rest.HttpRest.Post(gremlinUrl, jo.ToString(Formatting.None), out response); - return status; - } -*/ -/* - public static IEnumerable Post(EncryptId startNodeId, string script) where T : IGraphObject - { - return Post(string.Concat(DefaultDbUrl, DefaultGremlinExtensionPath), string.Format("g.v({0}).{1}", (long)startNodeId, script)); - } -*/ -/* - public static IEnumerable Post(string script) where T : IGraphObject - { - return Post(string.Concat(DefaultDbUrl, DefaultGremlinExtensionPath), script); - } -*/ public static IEnumerable Post(GremlinScript script) where T : IGraphObject { return Post(string.Concat(DefaultDbUrl, DefaultGremlinExtensionPath), script); @@ -74,8 +35,6 @@ public static HttpStatusCode Post(string gremlinUrl, string script) public static IEnumerable Post(string gremlinUrl, GremlinScript script) where T : IGraphObject { -// return Post(gremlinUrl, script.ToString()); - // Remove trailing / gremlinUrl = gremlinUrl.TrimEnd('/'); @@ -103,37 +62,7 @@ public static HttpStatusCode Post(string gremlinUrl, string script) } -/* - public static IEnumerable Post(string gremlinUrl, string script) where T : IGraphObject - { - // Remove trailing / - gremlinUrl = gremlinUrl.TrimEnd('/'); - var typeParameterType = typeof(T); - - var jo = new JObject {{"script", script}}; - - string response; - HttpStatusCode status = Rest.HttpRest.Post(gremlinUrl, jo.ToString(Formatting.None), out response); - - if (typeParameterType == typeof(Node)) - { - return (IEnumerable)Node.ParseJson(response); - } - - if (typeParameterType == typeof(Relationship)) - { - return (IEnumerable)Relationship.ParseJson(response); - } - - if (typeParameterType == typeof(Path)) - { - return (IEnumerable)Path.ParseJson(response); - } - - throw new Exception("Return type " + typeParameterType.ToString() + " not implemented"); - } -*/ public static DataTable GetTable(string script) { return GetTable(string.Concat(DefaultDbUrl, DefaultGremlinExtensionPath), script); @@ -157,7 +86,7 @@ public static DataTable GetTable(string gremlinUrl, string script) var joScript = new JObject {{"script", script}}; string response; - HttpStatusCode status = Rest.HttpRest.Post(gremlinUrl, joScript.ToString(Formatting.None), out response); + var status = Rest.HttpRest.Post(gremlinUrl, joScript.ToString(Formatting.None), out response); var joResponse = JObject.Parse(response); var jaColumns =(JArray)joResponse["columns"]; @@ -196,8 +125,8 @@ public static DataTable GetTable(string gremlinUrl, string script) } else { - string self = jCol["self"].ToString(); - string[] selfArray = self.Split('/'); + var self = jCol["self"].ToString(); + var selfArray = self.Split('/'); if (selfArray.Length > 2 && selfArray[selfArray.Length - 2] == "node" ) { row.Add(Node.InitializeFromNodeJson((JObject)jCol)); @@ -281,7 +210,7 @@ public static DataTable GetTable(string gremlinUrl, string script) } initColumns = false; - DataRow dtRow = dt.NewRow(); + var dtRow = dt.NewRow(); dtRow.ItemArray = row.ToArray(); dt.Rows.Add(dtRow); } diff --git a/Neo4jRestNet/Neo4jRestNet.csproj b/Neo4jRestNet/Neo4jRestNet.csproj index b51844a..5f03b00 100644 --- a/Neo4jRestNet/Neo4jRestNet.csproj +++ b/Neo4jRestNet/Neo4jRestNet.csproj @@ -114,6 +114,7 @@ + diff --git a/Neo4jRestNet/Rest/INeo4jRestApi.cs b/Neo4jRestNet/Rest/INeo4jRestApi.cs new file mode 100644 index 0000000..8b8cee5 --- /dev/null +++ b/Neo4jRestNet/Rest/INeo4jRestApi.cs @@ -0,0 +1,59 @@ +using System.Collections.Generic; +using System.Net; +using Neo4jRestNet.Core; + +namespace Neo4jRestNet.Rest +{ + public interface INeo4jRestApi + { + HttpStatusCode GetRoot(out string response); + HttpStatusCode CreateNode(string jsonProperties, out string response); + HttpStatusCode GetNode(long nodeId, out string response); + HttpStatusCode SetPropertiesOnNode(long nodeId, string jsonProperties); + HttpStatusCode GetPropertiesOnNode(long nodeId, out string response); + HttpStatusCode RemovePropertiesFromNode(string dbUrl, long nodeId); + HttpStatusCode SetPropertyOnNode(string dbUrl, long nodeId, string propertyName, object value); + HttpStatusCode GetPropertyOnNode(string dbUrl, long nodeId, string propertyName, out string response); + HttpStatusCode RemovePropertyFromNode(string dbUrl, long nodeId, string propertyName); + HttpStatusCode DeleteNode(long nodeId); + HttpStatusCode CreateRelationship(long fromNodeId, string toNodeSelf, string name, string jsonProperties, out string response); + HttpStatusCode SetPropertiesOnRelationship(string dbUrl, long relationshipId, string jsonProperties); + HttpStatusCode GetPropertiesOnRelationship(string dbUrl, long relationshipId, out string response); + HttpStatusCode RemovePropertiesFromRelationship(string dbUrl, long relationshipId); + HttpStatusCode SetPropertyOnRelationship(string dbUrl, long relationshipId, string propertyName, object value); + HttpStatusCode GetPropertyOnRelationship(string dbUrl, long relationshipId, string propertyName, out string response); + HttpStatusCode RemovePropertyFromRelationship(string dbUrl, long relationshipId, string propertyName); + HttpStatusCode DeleteRelationship(string dbUrl, long relationshipId); + HttpStatusCode GetRelationshipsOnNode(long nodeId, RelationshipDirection direction, IEnumerable relationships, out string response); + HttpStatusCode GetRelationshipTypes(string dbUrl, out string response); + HttpStatusCode CreateNodeIndex(string dbUrl, string indexName, string jsonConfig, out string response); + HttpStatusCode CreateRelationshipIndex(string dbUrl, string indexName, string jsonConfig, out string response); + HttpStatusCode DeleteNodeIndex(string dbUrl, string indexName); + HttpStatusCode DeleteRelationshipIndex(string dbUrl, string indexName); + HttpStatusCode ListNodeIndexes(string dbUrl, out string response); + HttpStatusCode ListRelationshipIndexes(string dbUrl, out string response); + HttpStatusCode AddNodeToIndex(long nodeId, string indexName, string key, object value, out string response); + HttpStatusCode AddNodeToIndex(string nodeSelf, string indexName, string key, object value, out string response); + HttpStatusCode AddRelationshipToIndex(string dbUrl, long relationshipId, string indexName, string key, object value, out string response); + HttpStatusCode AddRelationshipToIndex(string dbUrl, string relationshipself, string indexName, string key, object value, out string response); + HttpStatusCode RemoveNodeFromIndex(long nodeId, string indexName, string key, object value); + HttpStatusCode RemoveNodeFromIndex(long nodeId, string indexName, string key); + HttpStatusCode RemoveNodeFromIndex(long nodeId, string indexName); + HttpStatusCode RemoveRelationshipFromIndex(string dbUrl, long relationshipId, string indexName, string key, object value); + HttpStatusCode RemoveRelationshipFromIndex(string dbUrl, long relationshipId, string indexName, string key); + HttpStatusCode RemoveRelationshipFromIndex(string dbUrl, long relationshipId, string indexName); + HttpStatusCode GetNode(string indexName, string key, object value, out string response); + HttpStatusCode GetNode(string indexName, string searchQuery, out string response); + HttpStatusCode GetRelationship(string dbUrl, string indexName, string key, object value, out string response); + HttpStatusCode GetRelationship(string dbUrl, string indexName, string searchQuery, out string response); + + HttpStatusCode Traverse(long nodeId, Order order, Uniqueness uniqueness, + IEnumerable relationships, + PruneEvaluator pruneEvaluator, ReturnFilter returnFilter, int? maxDepth, + ReturnType returnType, out string response); + + HttpStatusCode PathBetweenNodes(string dbUrl, long fromNodeId, long toNodeId, + IEnumerable relationships, int maxDepth, + PathAlgorithm algorithm, bool returnAllPaths, out string response); + } +} \ No newline at end of file diff --git a/Neo4jRestNet/Rest/Neo4jRestApi.cs b/Neo4jRestNet/Rest/Neo4jRestApi.cs index c9d2bee..8bc48b8 100644 --- a/Neo4jRestNet/Rest/Neo4jRestApi.cs +++ b/Neo4jRestNet/Rest/Neo4jRestApi.cs @@ -7,7 +7,7 @@ namespace Neo4jRestNet.Rest { - public class Neo4jRestApi + public class Neo4jRestApi : INeo4jRestApi { private readonly string _dbUrl; public Neo4jRestApi(string dbUrl) @@ -40,23 +40,23 @@ public HttpStatusCode GetPropertiesOnNode(long nodeId, out string response) return HttpRest.Get(string.Concat(_dbUrl, "/node/", nodeId.ToString(), "/properties"), out response); } - public static HttpStatusCode RemovePropertiesFromNode(string dbUrl, long nodeId) + public HttpStatusCode RemovePropertiesFromNode(string dbUrl, long nodeId) { return HttpRest.Delete(string.Concat(dbUrl, "/node/", nodeId.ToString(), "/properties")); } - public static HttpStatusCode SetPropertyOnNode(string dbUrl, long nodeId, string propertyName, object value) + public HttpStatusCode SetPropertyOnNode(string dbUrl, long nodeId, string propertyName, object value) { return HttpRest.Put(string.Concat(dbUrl, "/node/", nodeId.ToString(), "/properties/", propertyName), JToken.FromObject(value).ToString(Formatting.None)); } - public static HttpStatusCode GetPropertyOnNode(string dbUrl, long nodeId, string propertyName, out string response) + public HttpStatusCode GetPropertyOnNode(string dbUrl, long nodeId, string propertyName, out string response) { return HttpRest.Get(string.Concat(dbUrl, "/node/", nodeId.ToString(), "/properties/", propertyName), out response); } - public static HttpStatusCode RemovePropertyFromNode(string dbUrl, long nodeId, string propertyName) + public HttpStatusCode RemovePropertyFromNode(string dbUrl, long nodeId, string propertyName) { return HttpRest.Delete(string.Concat(dbUrl, "/node/", nodeId.ToString(), "/properties/", propertyName)); } @@ -78,22 +78,22 @@ public HttpStatusCode CreateRelationship(long fromNodeId, string toNodeSelf, str return HttpRest.Post(string.Concat(_dbUrl, "/node/", fromNodeId.ToString(), "/relationships"), jo.ToString(Formatting.None), out response); } - public static HttpStatusCode SetPropertiesOnRelationship(string dbUrl, long relationshipId, string jsonProperties) + public HttpStatusCode SetPropertiesOnRelationship(string dbUrl, long relationshipId, string jsonProperties) { return HttpRest.Put(string.Concat(dbUrl, "/relationship/", relationshipId.ToString(), "/properties"), jsonProperties); } - public static HttpStatusCode GetPropertiesOnRelationship(string dbUrl, long relationshipId, out string response) + public HttpStatusCode GetPropertiesOnRelationship(string dbUrl, long relationshipId, out string response) { return HttpRest.Get(string.Concat(dbUrl, "/relationship/", relationshipId.ToString(), "/properties"), out response); } - public static HttpStatusCode RemovePropertiesFromRelationship(string dbUrl, long relationshipId) + public HttpStatusCode RemovePropertiesFromRelationship(string dbUrl, long relationshipId) { return HttpRest.Delete(string.Concat(dbUrl, "/node/", relationshipId.ToString(), "/properties")); } - public static HttpStatusCode SetPropertyOnRelationship(string dbUrl, long relationshipId, string propertyName, object value) + public HttpStatusCode SetPropertyOnRelationship(string dbUrl, long relationshipId, string propertyName, object value) { return HttpRest.Put( @@ -101,7 +101,7 @@ public static HttpStatusCode SetPropertyOnRelationship(string dbUrl, long relati JToken.FromObject(value).ToString()); } - public static HttpStatusCode GetPropertyOnRelationship(string dbUrl, long relationshipId, string propertyName, out string response) + public HttpStatusCode GetPropertyOnRelationship(string dbUrl, long relationshipId, string propertyName, out string response) { return HttpRest.Get( @@ -109,13 +109,13 @@ public static HttpStatusCode GetPropertyOnRelationship(string dbUrl, long relati out response); } - public static HttpStatusCode RemovePropertyFromRelationship(string dbUrl, long relationshipId, string propertyName) + public HttpStatusCode RemovePropertyFromRelationship(string dbUrl, long relationshipId, string propertyName) { return HttpRest.Delete(string.Concat(dbUrl, "/relationship/", relationshipId.ToString(), "/properties/", propertyName)); } - public static HttpStatusCode DeleteRelationship(string dbUrl, long relationshipId) + public HttpStatusCode DeleteRelationship(string dbUrl, long relationshipId) { return HttpRest.Delete(string.Concat(dbUrl, "/relationship/", relationshipId.ToString())); } @@ -141,39 +141,39 @@ public HttpStatusCode GetRelationshipsOnNode(long nodeId, RelationshipDirection string.Join("&", relationships)), out response); } - public static HttpStatusCode GetRelationshipTypes(string dbUrl, out string response) + public HttpStatusCode GetRelationshipTypes(string dbUrl, out string response) { return HttpRest.Get(string.Concat(dbUrl, "/relationship/types"), out response); } - public static HttpStatusCode CreateNodeIndex(string dbUrl, string indexName, string jsonConfig, out string response) + public HttpStatusCode CreateNodeIndex(string dbUrl, string indexName, string jsonConfig, out string response) { var jo = new JObject { { "name", indexName }, { "config", jsonConfig } }; return HttpRest.Post(string.Concat(dbUrl, "/index/node"), jo.ToString(Formatting.None), out response); } - public static HttpStatusCode CreateRelationshipIndex(string dbUrl, string indexName, string jsonConfig, out string response) + public HttpStatusCode CreateRelationshipIndex(string dbUrl, string indexName, string jsonConfig, out string response) { var jo = new JObject { { "name", indexName }, { "config", jsonConfig } }; return HttpRest.Post(string.Concat(dbUrl, "/index/relationship"), jo.ToString(Formatting.None), out response); } - public static HttpStatusCode DeleteNodeIndex(string dbUrl, string indexName) + public HttpStatusCode DeleteNodeIndex(string dbUrl, string indexName) { return HttpRest.Delete(string.Concat(dbUrl, "/index/node/", indexName)); } - public static HttpStatusCode DeleteRelationshipIndex(string dbUrl, string indexName) + public HttpStatusCode DeleteRelationshipIndex(string dbUrl, string indexName) { return HttpRest.Delete(string.Concat(dbUrl, "/index/relationship/", indexName)); } - public static HttpStatusCode ListNodeIndexes(string dbUrl, out string response) + public HttpStatusCode ListNodeIndexes(string dbUrl, out string response) { return HttpRest.Get(string.Concat(dbUrl, "/index/node"), out response); } - public static HttpStatusCode ListRelationshipIndexes(string dbUrl, out string response) + public HttpStatusCode ListRelationshipIndexes(string dbUrl, out string response) { return HttpRest.Get(string.Concat(dbUrl, "/index/relationship"), out response); } @@ -191,13 +191,13 @@ public HttpStatusCode AddNodeToIndex(string nodeSelf, string indexName, string k JToken.FromObject(obj).ToString(Formatting.None), out response); } - public static HttpStatusCode AddRelationshipToIndex(string dbUrl, long relationshipId, string indexName, string key, object value, out string response) + public HttpStatusCode AddRelationshipToIndex(string dbUrl, long relationshipId, string indexName, string key, object value, out string response) { var self = string.Concat(dbUrl, "/", relationshipId.ToString()); return AddRelationshipToIndex(dbUrl, self, indexName, key, value, out response); } - public static HttpStatusCode AddRelationshipToIndex(string dbUrl, string relationshipself, string indexName, string key, object value, out string response) + public HttpStatusCode AddRelationshipToIndex(string dbUrl, string relationshipself, string indexName, string key, object value, out string response) { var obj = new { value, uri = relationshipself, key }; return HttpRest.Post(string.Concat(dbUrl, "/index/relationship/", indexName), @@ -221,19 +221,19 @@ public HttpStatusCode RemoveNodeFromIndex(long nodeId, string indexName) return HttpRest.Delete(string.Concat(_dbUrl, "/index/node/", indexName, "/", nodeId)); } - public static HttpStatusCode RemoveRelationshipFromIndex(string dbUrl, long relationshipId, string indexName, string key, object value) + public HttpStatusCode RemoveRelationshipFromIndex(string dbUrl, long relationshipId, string indexName, string key, object value) { return HttpRest.Delete(string.Concat(dbUrl, "/index/relationship/", indexName, "/", key, "/", JToken.FromObject(value).ToString(Formatting.None), "/", relationshipId)); } - public static HttpStatusCode RemoveRelationshipFromIndex(string dbUrl, long relationshipId, string indexName, string key) + public HttpStatusCode RemoveRelationshipFromIndex(string dbUrl, long relationshipId, string indexName, string key) { return HttpRest.Delete(string.Concat(dbUrl, "/index/relationship/", indexName, "/", key, "/", relationshipId)); } - public static HttpStatusCode RemoveRelationshipFromIndex(string dbUrl, long relationshipId, string indexName) + public HttpStatusCode RemoveRelationshipFromIndex(string dbUrl, long relationshipId, string indexName) { return HttpRest.Delete(string.Concat(dbUrl, "/index/relationship/", indexName, "/", relationshipId)); } @@ -248,12 +248,12 @@ public HttpStatusCode GetNode(string indexName, string searchQuery, out string r return HttpRest.Get(string.Concat(_dbUrl, "/index/node/", indexName, "?query=", searchQuery), out response); } - public static HttpStatusCode GetRelationship(string dbUrl, string indexName, string key, object value, out string response) + public HttpStatusCode GetRelationship(string dbUrl, string indexName, string key, object value, out string response) { return HttpRest.Get(string.Concat(dbUrl, "/index/relationship/", indexName, "/", key, "/", value.ToString()), out response); } - public static HttpStatusCode GetRelationship(string dbUrl, string indexName, string searchQuery, out string response) + public HttpStatusCode GetRelationship(string dbUrl, string indexName, string searchQuery, out string response) { return HttpRest.Get(string.Concat(dbUrl, "/index/relationship/", indexName, "?query=", searchQuery), out response); } @@ -294,7 +294,7 @@ public static HttpStatusCode GetRelationship(string dbUrl, string indexName, str jo.ToString(Formatting.None), out response); } - public static HttpStatusCode PathBetweenNodes(string dbUrl, long fromNodeId, long toNodeId, + public HttpStatusCode PathBetweenNodes(string dbUrl, long fromNodeId, long toNodeId, IEnumerable relationships, int maxDepth, PathAlgorithm algorithm, bool returnAllPaths, out string response) { From 58b3a9a14b496d764604ec466a31ea4f6732255a Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 12 Jan 2012 00:09:31 -0500 Subject: [PATCH 2/3] Update Cypher.cs to support ICypher. --- Neo4jRestNet/CypherPlugin/Cypher.cs | 214 +++----------------- Neo4jRestNet/CypherPlugin/CypherNeo4j.cs | 236 +++++++++++++++++++++++ Neo4jRestNet/CypherPlugin/ICypher.cs | 20 ++ Neo4jRestNet/Neo4jRestNet.csproj | 2 + Neo4jRestNet/Rest/HttpRest.cs | 7 +- 5 files changed, 291 insertions(+), 188 deletions(-) create mode 100644 Neo4jRestNet/CypherPlugin/CypherNeo4j.cs create mode 100644 Neo4jRestNet/CypherPlugin/ICypher.cs diff --git a/Neo4jRestNet/CypherPlugin/Cypher.cs b/Neo4jRestNet/CypherPlugin/Cypher.cs index 17e4a30..fff244f 100644 --- a/Neo4jRestNet/CypherPlugin/Cypher.cs +++ b/Neo4jRestNet/CypherPlugin/Cypher.cs @@ -1,236 +1,82 @@ using System; -using System.Collections.Generic; using System.Configuration; using System.Linq.Expressions; using System.Data; -using Newtonsoft.Json.Linq; -using Newtonsoft.Json; -using Neo4jRestNet.Core; -using System.Collections.ObjectModel; -using System.Text; namespace Neo4jRestNet.CypherPlugin { - public class Cypher + public class Cypher : ICypher { private static readonly string DefaultDbUrl = ConfigurationManager.ConnectionStrings["neo4j"].ConnectionString.TrimEnd('/'); private static readonly string DefaultCypherExtensionPath = ConfigurationManager.ConnectionStrings["neo4jCypherExtension"].ConnectionString.TrimEnd('/'); + private static readonly string CypherProvider = ConfigurationManager.ConnectionStrings["neo4jCypherExtension"].ProviderName; + private readonly ICypher _cypher; + + public Cypher() + { + switch (CypherProvider) + { + case "": + case "neo4j" : + _cypher = new CypherNeo4j(); + break; - readonly List> _start = new List>(); - readonly List> _match = new List>(); - readonly List>> _where = new List>>(); - readonly List> _return = new List>(); - readonly List> _orderBy = new List>(); - string _skip = string.Empty; - string _limit = string.Empty; + case "inmemory": + throw new NotImplementedException(); + + default: + throw new NotImplementedException(); + } + } public DataTable Post() { - return Post(string.Concat(DefaultDbUrl, DefaultCypherExtensionPath)); + return _cypher.Post(string.Concat(DefaultDbUrl, DefaultCypherExtensionPath)); } public DataTable Post(string cypherUrl) { - // Remove trailing / - cypherUrl = cypherUrl.TrimEnd('/'); - - var joScript = new JObject {{"query", Query}}; - - string response; - Rest.HttpRest.Post(cypherUrl, joScript.ToString(Formatting.None), out response); - - var joResponse = JObject.Parse(response); - var jaColumns = (JArray)joResponse["columns"]; - var jaData = (JArray)joResponse["data"]; - var returnTypes = GetReturnTypes; - - var dt = new DataTable(); - - var initColumns = true; - - foreach (JArray jRow in jaData) - { - var colIndex = 0; - var row = new List(); - - foreach (var jCol in jRow) - { - if (initColumns) - { - dt.Columns.Add(jaColumns[colIndex].ToString(), returnTypes[colIndex]); - } - - if (returnTypes[colIndex] == typeof (Node)) - { - row.Add(jCol.Type == JTokenType.Null ? null : Node.InitializeFromNodeJson((JObject)jCol)); - } - else if (returnTypes[colIndex] == typeof (Relationship)) - { - row.Add(jCol.Type == JTokenType.Null ? null : Relationship.InitializeFromRelationshipJson((JObject) jCol)); - } - else if (returnTypes[colIndex] == typeof (Path)) - { - row.Add(jCol.Type == JTokenType.Null ? null : Path.ParseJson((JArray)jCol)); - } - else if (returnTypes[colIndex] == typeof(string)) - { - row.Add(jCol.Type == JTokenType.Null ? null : (string)jCol); - } - else if (returnTypes[colIndex] == typeof (int)) - { - if(jCol.Type == JTokenType.Null) - { - throw new ArgumentNullException(string.Format("Value for column {0} of type {1} can not be null", jaColumns[colIndex], returnTypes[colIndex].Name)); - } - - row.Add((int) jCol); - } - else if (returnTypes[colIndex] == typeof(int?)) - { - row.Add(jCol.Type == JTokenType.Null ? null : (int?)jCol); - } - else if (returnTypes[colIndex] == typeof(long)) - { - if (jCol.Type == JTokenType.Null) - { - throw new ArgumentNullException(string.Format("Value for column {0} of type {1} can not be null", jaColumns[colIndex], returnTypes[colIndex].Name)); - } - - row.Add((long)jCol); - } - else if (returnTypes[colIndex] == typeof(long?)) - { - row.Add(jCol.Type == JTokenType.Null ? null : (long?)jCol); - } - else - { - throw new NotSupportedException(string.Format("Return Type of {0} is not supported", returnTypes[colIndex].Name)); - } - - colIndex++; - } - - initColumns = false; - var dtRow = dt.NewRow(); - dtRow.ItemArray = row.ToArray(); - dt.Rows.Add(dtRow); - } - - return dt; + return _cypher.Post(cypherUrl); } - public void Start(Func start) + public void Start(Func start) { - _start.Add(start); + _cypher.Start(start); } public void Match(Func match) { - _match.Add(match); + _cypher.Match(match); } public void Where(Expression> where) { - _where.Add(where); + _cypher.Where(where); } public void Return(Func cypherReturn) { - _return.Add(cypherReturn); + _cypher.Return(cypherReturn); } public void OrderBy(Func cypherOrderBy) { - _orderBy.Add(cypherOrderBy); + _cypher.OrderBy(cypherOrderBy); } public void Skip(int skip) { - _skip = string.Format(" SKIP {0}", skip); + _cypher.Skip(skip); } public void Limit(int limit) { - _limit = string.Format(" LIMIT {0}", limit); - } - - private ReadOnlyCollection GetReturnTypes - { - get - { - var returnTypes = new List(); - - foreach (var r in _return) - { - // call GetReturnTypes somehow - var obj = (CypherReturn)r.Invoke(new CypherReturn()); - returnTypes.AddRange(obj.GetReturnTypes); - } - - return returnTypes.AsReadOnly(); - } + _cypher.Limit(limit); } public string Query { - get - { - var sbToString = new StringBuilder(); - - var label = "START"; - foreach (var s in _start) - { - sbToString.AppendFormat("{1}{0}", s.Invoke(new CypherStart()), label); - label = ","; - } - - if (_match != null) - { - label = "MATCH"; - foreach (var m in _match) - { - sbToString.AppendFormat(" {1}{0}", m.Invoke(new CypherMatch()), label); - label = ","; - } - } - - if (_where != null) - { - label = "WHERE"; - foreach (var w in _where) - { - sbToString.AppendFormat(" {1} {0}", new ParseWhereLambda().Parse(w), label); - label = string.Empty; - } - } - - if (_return != null) - { - label = "RETURN"; - foreach (var r in _return) - { - sbToString.AppendFormat(" {1}{0}", r.Invoke(new CypherReturn()), label); - label = ","; - } - } - - if (_orderBy != null) - { - label = "ORDER BY"; - foreach (var o in _orderBy) - { - sbToString.AppendFormat(" {1}{0}", o.Invoke(new CypherOrderBy()), label); - label = ","; - } - } - - // Append Skip - sbToString.Append(_skip); - - // Append Limit - sbToString.Append(_limit); - return sbToString.ToString(); - } + get { return _cypher.Query; } } } } diff --git a/Neo4jRestNet/CypherPlugin/CypherNeo4j.cs b/Neo4jRestNet/CypherPlugin/CypherNeo4j.cs new file mode 100644 index 0000000..cb69bc8 --- /dev/null +++ b/Neo4jRestNet/CypherPlugin/CypherNeo4j.cs @@ -0,0 +1,236 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Linq.Expressions; +using System.Data; +using Newtonsoft.Json.Linq; +using Newtonsoft.Json; +using Neo4jRestNet.Core; +using System.Collections.ObjectModel; +using System.Text; + +namespace Neo4jRestNet.CypherPlugin +{ + public class CypherNeo4j : ICypher + { + private static readonly string DefaultDbUrl = ConfigurationManager.ConnectionStrings["neo4j"].ConnectionString.TrimEnd('/'); + private static readonly string DefaultCypherExtensionPath = ConfigurationManager.ConnectionStrings["neo4jCypherExtension"].ConnectionString.TrimEnd('/'); + + readonly List> _start = new List>(); + readonly List> _match = new List>(); + readonly List>> _where = new List>>(); + readonly List> _return = new List>(); + readonly List> _orderBy = new List>(); + string _skip = string.Empty; + string _limit = string.Empty; + + public DataTable Post() + { + return Post(string.Concat(DefaultDbUrl, DefaultCypherExtensionPath)); + } + + public DataTable Post(string cypherUrl) + { + // Remove trailing / + cypherUrl = cypherUrl.TrimEnd('/'); + + var joScript = new JObject {{"query", Query}}; + + string response; + Rest.HttpRest.Post(cypherUrl, joScript.ToString(Formatting.None), out response); + + var joResponse = JObject.Parse(response); + var jaColumns = (JArray)joResponse["columns"]; + var jaData = (JArray)joResponse["data"]; + var returnTypes = GetReturnTypes; + + var dt = new DataTable(); + + var initColumns = true; + + foreach (JArray jRow in jaData) + { + var colIndex = 0; + var row = new List(); + + foreach (var jCol in jRow) + { + if (initColumns) + { + dt.Columns.Add(jaColumns[colIndex].ToString(), returnTypes[colIndex]); + } + + if (returnTypes[colIndex] == typeof (Node)) + { + row.Add(jCol.Type == JTokenType.Null ? null : Node.InitializeFromNodeJson((JObject)jCol)); + } + else if (returnTypes[colIndex] == typeof (Relationship)) + { + row.Add(jCol.Type == JTokenType.Null ? null : Relationship.InitializeFromRelationshipJson((JObject) jCol)); + } + else if (returnTypes[colIndex] == typeof (Path)) + { + row.Add(jCol.Type == JTokenType.Null ? null : Path.ParseJson((JArray)jCol)); + } + else if (returnTypes[colIndex] == typeof(string)) + { + row.Add(jCol.Type == JTokenType.Null ? null : (string)jCol); + } + else if (returnTypes[colIndex] == typeof (int)) + { + if(jCol.Type == JTokenType.Null) + { + throw new ArgumentNullException(string.Format("Value for column {0} of type {1} can not be null", jaColumns[colIndex], returnTypes[colIndex].Name)); + } + + row.Add((int) jCol); + } + else if (returnTypes[colIndex] == typeof(int?)) + { + row.Add(jCol.Type == JTokenType.Null ? null : (int?)jCol); + } + else if (returnTypes[colIndex] == typeof(long)) + { + if (jCol.Type == JTokenType.Null) + { + throw new ArgumentNullException(string.Format("Value for column {0} of type {1} can not be null", jaColumns[colIndex], returnTypes[colIndex].Name)); + } + + row.Add((long)jCol); + } + else if (returnTypes[colIndex] == typeof(long?)) + { + row.Add(jCol.Type == JTokenType.Null ? null : (long?)jCol); + } + else + { + throw new NotSupportedException(string.Format("Return Type of {0} is not supported", returnTypes[colIndex].Name)); + } + + colIndex++; + } + + initColumns = false; + var dtRow = dt.NewRow(); + dtRow.ItemArray = row.ToArray(); + dt.Rows.Add(dtRow); + } + + return dt; + } + + public void Start(Func start) + { + _start.Add(start); + } + + public void Match(Func match) + { + _match.Add(match); + } + + public void Where(Expression> where) + { + _where.Add(where); + } + + public void Return(Func cypherReturn) + { + _return.Add(cypherReturn); + } + + public void OrderBy(Func cypherOrderBy) + { + _orderBy.Add(cypherOrderBy); + } + + public void Skip(int skip) + { + _skip = string.Format(" SKIP {0}", skip); + } + + public void Limit(int limit) + { + _limit = string.Format(" LIMIT {0}", limit); + } + + private ReadOnlyCollection GetReturnTypes + { + get + { + var returnTypes = new List(); + + foreach (var r in _return) + { + // call GetReturnTypes somehow + var obj = (CypherReturn)r.Invoke(new CypherReturn()); + returnTypes.AddRange(obj.GetReturnTypes); + } + + return returnTypes.AsReadOnly(); + } + } + + public string Query + { + get + { + var sbToString = new StringBuilder(); + + var label = "START"; + foreach (var s in _start) + { + sbToString.AppendFormat("{1}{0}", s.Invoke(new CypherStart()), label); + label = ","; + } + + if (_match != null) + { + label = "MATCH"; + foreach (var m in _match) + { + sbToString.AppendFormat(" {1}{0}", m.Invoke(new CypherMatch()), label); + label = ","; + } + } + + if (_where != null) + { + label = "WHERE"; + foreach (var w in _where) + { + sbToString.AppendFormat(" {1} {0}", new ParseWhereLambda().Parse(w), label); + label = string.Empty; + } + } + + if (_return != null) + { + label = "RETURN"; + foreach (var r in _return) + { + sbToString.AppendFormat(" {1}{0}", r.Invoke(new CypherReturn()), label); + label = ","; + } + } + + if (_orderBy != null) + { + label = "ORDER BY"; + foreach (var o in _orderBy) + { + sbToString.AppendFormat(" {1}{0}", o.Invoke(new CypherOrderBy()), label); + label = ","; + } + } + + // Append Skip + sbToString.Append(_skip); + + // Append Limit + sbToString.Append(_limit); + return sbToString.ToString(); + } + } + } +} diff --git a/Neo4jRestNet/CypherPlugin/ICypher.cs b/Neo4jRestNet/CypherPlugin/ICypher.cs new file mode 100644 index 0000000..4f36aa9 --- /dev/null +++ b/Neo4jRestNet/CypherPlugin/ICypher.cs @@ -0,0 +1,20 @@ +using System; +using System.Linq.Expressions; +using System.Data; + +namespace Neo4jRestNet.CypherPlugin +{ + public interface ICypher + { + DataTable Post(); + DataTable Post(string cypherUrl); + void Start(Func start); + void Match(Func match); + void Where(Expression> where); + void Return(Func cypherReturn); + void OrderBy(Func cypherOrderBy); + void Skip(int skip); + void Limit(int limit); + string Query { get; } + } +} diff --git a/Neo4jRestNet/Neo4jRestNet.csproj b/Neo4jRestNet/Neo4jRestNet.csproj index 5f03b00..c3e5f81 100644 --- a/Neo4jRestNet/Neo4jRestNet.csproj +++ b/Neo4jRestNet/Neo4jRestNet.csproj @@ -88,6 +88,8 @@ + + diff --git a/Neo4jRestNet/Rest/HttpRest.cs b/Neo4jRestNet/Rest/HttpRest.cs index e6c0444..9f63fb1 100644 --- a/Neo4jRestNet/Rest/HttpRest.cs +++ b/Neo4jRestNet/Rest/HttpRest.cs @@ -8,7 +8,6 @@ public class HttpRest { private static HttpStatusCode BaseRestRequest(string url, string method, string body) { - var w = (HttpWebRequest)WebRequest.Create(url); w.Proxy = null; w.Method = method; @@ -16,17 +15,17 @@ private static HttpStatusCode BaseRestRequest(string url, string method, string if (!string.IsNullOrEmpty(body)) { - Stream dataStream = w.GetRequestStream(); + var dataStream = w.GetRequestStream(); byte[] b = Encoding.UTF8.GetBytes(body); dataStream.Write(b, 0, b.Length); dataStream.Close(); } - WebResponse resp = w.GetResponse(); + var resp = w.GetResponse(); resp.Close(); - HttpStatusCode statusCode = ((HttpWebResponse)resp).StatusCode; + var statusCode = ((HttpWebResponse)resp).StatusCode; return statusCode; } From 63bccc6745dfb1f1414ed1be4d0a5f1a05977921 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 17 Jan 2012 19:44:32 -0500 Subject: [PATCH 3/3] updated for to support custom implentations of Node, Relationship, Path, Cypher and Gremlin. --- Neo4jRestNet/Core/GraphFactory.cs | 39 + Neo4jRestNet/Core/Implementation/Node.cs | 703 ++++++++++++++++ .../Core/{ => Implementation}/Path.cs | 50 +- .../Core/Implementation/Relationship.cs | 443 ++++++++++ Neo4jRestNet/Core/Interface/INode.cs | 80 ++ Neo4jRestNet/Core/Interface/IPath.cs | 21 + Neo4jRestNet/Core/Interface/IRelationship.cs | 53 ++ Neo4jRestNet/Core/Node.cs | 767 ------------------ Neo4jRestNet/Core/Relationship.cs | 480 ----------- Neo4jRestNet/Core/StoreFactory.cs | 35 - Neo4jRestNet/CypherPlugin/Cypher.cs | 33 +- Neo4jRestNet/CypherPlugin/CypherFactory.cs | 18 + Neo4jRestNet/CypherPlugin/CypherReturn.cs | 7 +- Neo4jRestNet/CypherPlugin/ICypher.cs | 20 + Neo4jRestNet/GremlinPlugin/Gremlin.cs | 165 ++-- Neo4jRestNet/GremlinPlugin/GremlinFactory.cs | 17 + Neo4jRestNet/GremlinPlugin/GremlinScript.cs | 9 +- .../GremlinPlugin/GremlinScriptCommands.cs | 9 +- Neo4jRestNet/GremlinPlugin/IGremlin.cs | 23 + Neo4jRestNet/Neo4jRestNet.csproj | 17 +- Neo4jRestNet/Rest/Neo4jRestApi.cs | 104 +-- Neo4jRestNetExamples/Examples.csproj | 4 +- Neo4jRestNetExamples/Program.cs | 74 +- 23 files changed, 1644 insertions(+), 1527 deletions(-) create mode 100644 Neo4jRestNet/Core/GraphFactory.cs create mode 100644 Neo4jRestNet/Core/Implementation/Node.cs rename Neo4jRestNet/Core/{ => Implementation}/Path.cs (59%) create mode 100644 Neo4jRestNet/Core/Implementation/Relationship.cs create mode 100644 Neo4jRestNet/Core/Interface/INode.cs create mode 100644 Neo4jRestNet/Core/Interface/IPath.cs create mode 100644 Neo4jRestNet/Core/Interface/IRelationship.cs delete mode 100644 Neo4jRestNet/Core/Node.cs delete mode 100644 Neo4jRestNet/Core/Relationship.cs delete mode 100644 Neo4jRestNet/Core/StoreFactory.cs create mode 100644 Neo4jRestNet/CypherPlugin/CypherFactory.cs create mode 100644 Neo4jRestNet/CypherPlugin/ICypher.cs create mode 100644 Neo4jRestNet/GremlinPlugin/GremlinFactory.cs create mode 100644 Neo4jRestNet/GremlinPlugin/IGremlin.cs diff --git a/Neo4jRestNet/Core/GraphFactory.cs b/Neo4jRestNet/Core/GraphFactory.cs new file mode 100644 index 0000000..6f970bd --- /dev/null +++ b/Neo4jRestNet/Core/GraphFactory.cs @@ -0,0 +1,39 @@ +using System; +using Neo4jRestNet.Core.Interface; + +namespace Neo4jRestNet.Core +{ + public class GraphFactory + { + public static INode CreateNode() + { + return new Implementation.Node(); + } + + public static INode CreateNode(params object[] args) where TNode : class, INode, new() + { + return (TNode)Activator.CreateInstance(typeof(TNode), args); + } + + public static IRelationship CreateRelationship() + { + return new Implementation.Relationship(); + } + + public static IRelationship CreateRelationship(params object[] args) where TRelationship : class, IRelationship, new() + { + return (TRelationship)Activator.CreateInstance(typeof(TRelationship), args); + } + + public static IPath CreatePath() + { + return new Implementation.Path(); + } + + public static IPath CreatePath(params object[] args) where TPath : class, IPath, new() + { + return (TPath)Activator.CreateInstance(typeof(TPath), args); + } + + } +} diff --git a/Neo4jRestNet/Core/Implementation/Node.cs b/Neo4jRestNet/Core/Implementation/Node.cs new file mode 100644 index 0000000..8f0ae61 --- /dev/null +++ b/Neo4jRestNet/Core/Implementation/Node.cs @@ -0,0 +1,703 @@ +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.ComponentModel; +using System.Configuration; +using System.Linq; +using System.Net; +using System; +using Neo4jRestNet.Core.Interface; +using Newtonsoft.Json.Linq; +using Newtonsoft.Json; +using Neo4jRestNet.Rest; + +namespace Neo4jRestNet.Core.Implementation +{ + public class Node : INode, IEquatable + { + private enum NodeProperty + { + NodeType + } + + private static readonly string DefaultDbUrl = ConfigurationManager.ConnectionStrings["neo4j"].ConnectionString.TrimEnd('/'); + private readonly Neo4jRestApi _restApi; + private string _selfDbUrl; + private string _self; + private Properties _properties; + + public long Id { get; private set; } + public EncryptId EncryptedId { get; private set; } + public string OriginalJsonNode { get; private set; } + + #region Constructor + + public Node() + { + _restApi = new Neo4jRestApi(DefaultDbUrl); + } + + public Node(string connectionString) + { + _restApi = new Neo4jRestApi(ConfigurationManager.ConnectionStrings[connectionString].ConnectionString.TrimEnd('/')); + } + + public Node(Type relationshipType) + { + if(relationshipType != typeof(IRelationship)) + { + throw new ArgumentException("Create Node with invalid Relationship type"); + } + + _restApi = new Neo4jRestApi(DefaultDbUrl); + } + + #endregion + + #region GetRootNode + + public INode GetRootNode() + { + string response; + var status = _restApi.GetRoot(out response); + if (status != HttpStatusCode.OK) + { + throw new Exception(string.Format("Error getting root node (http response:{0})", status)); + } + + JObject jo; + try + { + jo = JObject.Parse(response); + } + catch (Exception e) + { + throw new Exception("Invalid json", e); + } + + JToken referenceNode; + if (!jo.TryGetValue("reference_node", out referenceNode)) + { + throw new Exception("Invalid json"); + } + + var node = new Node {Self = referenceNode.Value(), _properties = null}; + + return node; + } + + #endregion + + #region GetNode + + public INode GetNode(EncryptId nodeId) + { + string response; + var status = _restApi.GetNode((long)nodeId, out response); + if (status != HttpStatusCode.OK) + { + throw new Exception(string.Format("Node not found (node id:{0})", (long)nodeId)); + } + + return InitializeFromNodeJson(response); + } + + public IEnumerable GetNode(string indexName, string key, object value) + { + string response; + var status = _restApi.GetNode(indexName, key, value, out response); + if (status != HttpStatusCode.OK) + { + throw new Exception(string.Format("Index not found in (index:{0})", indexName)); + } + + return ParseJson(response); + } + + public IEnumerable GetNode(Enum indexName, string key, object value) + { + return GetNode(indexName.ToString(), key, value); + } + + public IEnumerable GetNode(string indexName, Enum key, object value) + { + return GetNode(indexName, key.ToString(), value); + } + + public IEnumerable GetNode(Enum indexName, Enum key, object value) + { + return GetNode(indexName.ToString(), key.ToString(), value); + } + + public IEnumerable GetNode(string connectionName, Enum indexName, string key, object value) + { + return GetNode(indexName.ToString(), key, value); + } + + public IEnumerable GetNode(string connectionName, string indexName, Enum key, object value) + { + return GetNode(indexName, key.ToString(), value); + } + + public IEnumerable GetNode(string connectionName, Enum indexName, Enum key, object value) + { + return GetNode(indexName.ToString(), key.ToString(), value); + } + + public IEnumerable GetNode(string indexName, string searchQuery) + { + string response; + var status = _restApi.GetNode(indexName, searchQuery, out response); + if (status != HttpStatusCode.OK) + { + throw new Exception(string.Format("Index not found in (index:{0})", indexName)); + } + + return ParseJson(response); + } + + public IEnumerable GetNode(Enum indexName, string searchQuery) + { + return GetNode(indexName.ToString(), searchQuery); + } + + public IEnumerable GetNode(string connectionName, Enum indexName, string searchQuery) + { + return GetNode(connectionName, indexName.ToString(), searchQuery); + } + + #endregion + + #region CreateNode + + public INode CreateNode(string nodeType) + { + var properties = new Properties(); + properties.SetProperty(NodeProperty.NodeType.ToString(), nodeType); + return CreateNodeFromJson(properties.ToString()); + } + + public INode CreateNode(Enum nodeType) + { + return CreateNode(nodeType.ToString()); + } + + public INode CreateNode(string nodeType, Properties properties) + { + properties.SetProperty(NodeProperty.NodeType.ToString(), nodeType); + return CreateNodeFromJson(properties.ToString()); + } + + public INode CreateNode(Enum nodeType, Properties properties) + { + return CreateNode(nodeType.ToString(), properties); + } + + public INode CreateNode(string nodeType, IDictionary properties) + { + properties.Add(NodeProperty.NodeType.ToString(), nodeType); + return CreateNodeFromJson(JObject.FromObject(properties).ToString(Formatting.None)); + } + + public INode CreateNode(Enum nodeType, IDictionary properties) + { + return CreateNode(nodeType.ToString(), properties); + } + + private INode CreateNodeFromJson(string jsonProperties) + { + string response; + var status = _restApi.CreateNode(jsonProperties, out response); + if (status != HttpStatusCode.Created) + { + throw new Exception(string.Format("Error creating node (http response:{0})", status)); + } + + return InitializeFromNodeJson(response); + } + + #endregion + + #region Delete + + public HttpStatusCode DeleteNode() + { + + var status = new Neo4jRestApi(_selfDbUrl).DeleteNode(Id); + if (status != HttpStatusCode.NoContent) + { + throw new Exception(string.Format("Error deleting node (node id:{0} http response:{1})", Id, status)); + } + + return status; + } + + #endregion + + #region Initializers + + public INode InitializeFromNodeJson(string nodeJson) + { + JObject jo; + + try + { + jo = JObject.Parse(nodeJson); + } + catch (Exception e) + { + throw new Exception("Invalid json node", e); + } + + return InitializeFromNodeJson(jo); + } + + public INode InitializeFromNodeJson(JObject nodeJson) + { + JToken self; + if (!nodeJson.TryGetValue("self", out self)) + { + throw new Exception("Invalid json node"); + } + + JToken properties; + if (!nodeJson.TryGetValue("data", out properties)) + { + throw new Exception("Invalid json node"); + } + + var node = new Node + { + Self = self.Value(), + _properties = Properties.ParseJson(properties.ToString(Formatting.None)), + OriginalJsonNode = nodeJson.ToString(Formatting.None) + }; + + return node; + } + + public INode InitializeFromSelf(string self) + { + var node = new Node {Self = self, _properties = null}; + return node; + } + + private bool IsSelfANode(string self) + { + var selfArray = self.Split('/'); + return (selfArray.Length > 2 && selfArray[selfArray.Length - 2] == "node"); + } + + #endregion + + #region Self + + public string Self + { + get + { + return _self; + } + + private set + { + if (!IsSelfANode(value)) + { + throw new Exception(string.Format("Self is not a Node ({0})", Self)); + } + + // Make sure there is no trailing / + var self = value.TrimEnd('/'); + + var selfArray = self.Split('/'); + + long nodeId; + if (!long.TryParse(selfArray.Last(), out nodeId)) + { + throw new Exception(string.Format("Invalid Self id ({0})", value)); + } + + _selfDbUrl = self.Substring(0, self.LastIndexOf("/node")); + _self = self; + + // Set Id & NodeId values + Id = nodeId; + EncryptedId = nodeId; + } + } + + #endregion + + #region Properties + + private void LoadProperties(bool refresh) + { + if (refresh) + { + _properties = null; + } + + if (_properties != null) + { + return; + } + + string response; + var status = new Neo4jRestApi(_selfDbUrl).GetPropertiesOnNode((long)EncryptedId, out response); + if (status != HttpStatusCode.OK) + { + throw new Exception(string.Format("Error retrieving properties on node (node id:{0} http response:{1})", (long)EncryptedId, status)); + } + + _properties = Properties.ParseJson(response); + } + + public Properties Properties + { + get + { + LoadProperties(false); + return _properties; + } + } + + public void SaveProperties() + { + SaveProperties(Properties); + } + + public void SaveProperties(Properties properties) + { + LoadProperties(false); + if (Properties.HasProperty(NodeProperty.NodeType.ToString())) + { + properties.SetProperty(NodeProperty.NodeType.ToString(), Properties.GetProperty(NodeProperty.NodeType.ToString())); + } + + var status = new Neo4jRestApi(_selfDbUrl).SetPropertiesOnNode((long)EncryptedId, properties.ToString()); + if (status != HttpStatusCode.NoContent) + { + throw new Exception(string.Format("Error setting properties on node (node id:{0} http response:{1})", (long)EncryptedId, status)); + } + + LoadProperties(true); + } + + #endregion + + #region Relationships + + public IEnumerable GetRelationships() + { + return GetRelationships(RelationshipDirection.All, (IEnumerable)null); + } + + public IEnumerable GetRelationships(RelationshipDirection direction) + { + return GetRelationships(direction, (IEnumerable)null); + } + + public IEnumerable GetRelationships(RelationshipDirection direction, IRelationship relationshipType) + { + return GetRelationships(direction, new List { relationshipType.ToString() }); + } + + public IEnumerable GetRelationships(Enum name) + { + return GetRelationships(RelationshipDirection.All, new List { name.ToString() }); + } + + public IEnumerable GetRelationships(string name) + { + return GetRelationships(RelationshipDirection.All, new List { name }); + } + + public IEnumerable GetRelationships(IEnumerable names) + { + return GetRelationships(RelationshipDirection.All, names.Select(n => n.ToString()).ToList()); + } + + public IEnumerable GetRelationships(IEnumerable names) + { + return GetRelationships(RelationshipDirection.All, names); + } + + public IEnumerable GetRelationships(RelationshipDirection direction, string name) + { + return GetRelationships(direction, new List { name }); + } + + public IEnumerable GetRelationships(RelationshipDirection direction, Enum name) + { + return GetRelationships(direction, new List { name.ToString() }); + } + + public IEnumerable GetRelationships(RelationshipDirection direction, IEnumerable names) + { + string response; + var status = _restApi.GetRelationshipsOnNode((long)EncryptedId, direction, names, out response); + if (status != HttpStatusCode.OK) + { + throw new Exception(string.Format("Error retrieving relationships on node (node id:{0} http response:{1})", (long)EncryptedId, status)); + } + + return new Relationship().ParseJson(response); + } + + public IRelationship CreateRelationshipTo(INode toNode, string relationshipType) + { + return CreateRelationshipTo(toNode, relationshipType, null); + } + + public IRelationship CreateRelationshipTo(INode toNode, Enum relationshipType) + { + return CreateRelationshipTo(toNode, relationshipType.ToString(), null); + } + + public IRelationship CreateRelationshipTo(INode toNode, Enum relationshipType, Properties relationshipProperties) + { + return CreateRelationshipTo(toNode, relationshipType.ToString(), relationshipProperties); + } + + public IRelationship CreateRelationshipTo(INode toNode, string relationshipType, Properties relationshipProperties) + { + string response; + var status = _restApi.CreateRelationship( + (long)EncryptedId, + toNode.Self, + relationshipType, + relationshipProperties == null ? null : relationshipProperties.ToString(), + out response); + + if (status != HttpStatusCode.Created) + { + throw new Exception(string.Format("Error creationg relationship on node (node id:{0} http response:{1})", (long)EncryptedId, status)); + } + + return new Relationship().ParseJson(response).First(); + } + + #endregion + + #region Traverse + + public IEnumerable Traverse(Order order, Uniqueness uniqueness, IEnumerable relationships, PruneEvaluator pruneEvaluator, ReturnFilter returnFilter, int? maxDepth, ReturnType returnType) + { + string response; + var status = _restApi.Traverse((long)EncryptedId, order, uniqueness, relationships, pruneEvaluator, returnFilter, maxDepth, returnType, out response); + if (status != HttpStatusCode.OK) + { + throw new Exception(string.Format("Error traversing nodes (node id:{0} status code:{1})", (long)EncryptedId, status)); + } + + if (returnType.ToString() == ReturnType.Node.ToString()) + { + return ParseJson(response); + } + + if (returnType.ToString() == ReturnType.Relationship.ToString()) + { + return new Relationship().ParseJson(response); + } + + if (returnType.ToString() == ReturnType.Path.ToString() || returnType.ToString() == ReturnType.FullPath.ToString()) + { + return new Path().ParseJson(response); + } + + throw new Exception(string.Format("Return type not implemented (type:{0})", returnType)); + } + + #endregion + + #region Index + + public INode AddNodeToIndex(long nodeId, string indexName, string key, object value) + { + string response; + var status = _restApi.AddNodeToIndex(nodeId, indexName, key, value, out response); + if (status != HttpStatusCode.Created) + { + throw new Exception(string.Format("Error creating index for node (http response:{0})", status)); + } + + return InitializeFromNodeJson(response); + } + + public INode AddNodeToIndex(long nodeId, Enum indexName, string key, object value) + { + return AddNodeToIndex(nodeId, indexName.ToString(), key, value); + } + + public INode AddNodeToIndex(long nodeId, string indexName, Enum key, object value) + { + return AddNodeToIndex(nodeId, indexName, key.ToString(), value); + } + + public INode AddNodeToIndex(long nodeId, Enum indexName, Enum key, object value) + { + return AddNodeToIndex(nodeId, indexName.ToString(), key.ToString(), value); + } + + public HttpStatusCode RemoveNodeFromIndex(long nodeId, string indexName) + { + var status = _restApi.RemoveNodeFromIndex(nodeId, indexName); + if (status != HttpStatusCode.NoContent) + { + throw new Exception(string.Format("Error remove node from index (node id:{0} index name:{1} http response:{2})", nodeId, indexName, status)); + } + + return status; + } + + public HttpStatusCode RemoveNodeFromIndex(long nodeId, Enum indexName) + { + return RemoveNodeFromIndex(nodeId, indexName.ToString()); + } + + public HttpStatusCode RemoveNodeFromIndex(long nodeId, string indexName, string key) + { + var status = _restApi.RemoveNodeFromIndex(nodeId, indexName, key); + if (status != HttpStatusCode.NoContent) + { + throw new Exception(string.Format("Error remove node from index (node id:{0} index name:{1} http response:{2})", nodeId, indexName, status)); + } + + return status; + } + + public HttpStatusCode RemoveNodeFromIndex(long nodeId, Enum indexName, string key) + { + return RemoveNodeFromIndex(nodeId, indexName.ToString(), key); + } + + public HttpStatusCode RemoveNodeFromIndex(long nodeId, string indexName, Enum key) + { + return RemoveNodeFromIndex(nodeId, indexName, key.ToString()); + } + + public HttpStatusCode RemoveNodeFromIndex(long nodeId, Enum indexName, Enum key) + { + return RemoveNodeFromIndex(nodeId, indexName.ToString(), key.ToString()); + } + + public HttpStatusCode RemoveNodeFromIndex(long nodeId, string indexName, string key, object value) + { + var status = _restApi.RemoveNodeFromIndex(nodeId, indexName, key, value); + if (status != HttpStatusCode.NoContent) + { + throw new Exception(string.Format("Error remove node from index (node id:{0} index name:{1} http response:{2})", nodeId, indexName, status)); + } + + return status; + } + + public HttpStatusCode RemoveNodeFromIndex(long nodeId, Enum indexName, string key, object value) + { + return RemoveNodeFromIndex(nodeId, indexName.ToString(), key, value); + } + + public HttpStatusCode RemoveNodeFromIndex(long nodeId, string indexName, Enum key, object value) + { + return RemoveNodeFromIndex(nodeId, indexName, key.ToString(), value); + } + + public HttpStatusCode RemoveNodeFromIndex(long nodeId, Enum indexName, Enum key, object value) + { + return RemoveNodeFromIndex(nodeId, indexName.ToString(), key.ToString(), value); + } + + #endregion + + #region NodeType + + public string NodeType + { + get + { + return _properties == null ? null : _properties.GetProperty(NodeProperty.NodeType.ToString()); + } + } + + #endregion + + #region ParseJson + + public IEnumerable ParseJson(string jsonNodes) + { + if (String.IsNullOrEmpty(jsonNodes)) + { + return null; + } + + var nodes = new List(); + + // The Json passed in can be a JObject or JArray - this is to test for that. + var jo = JObject.Parse(string.Concat("{\"root\":", jsonNodes, "}")); + + switch (jo["root"].Type) + { + case JTokenType.Object: + nodes.Add(InitializeFromNodeJson(jo["root"].ToString(Formatting.None))); + break; + + case JTokenType.Array: + nodes.AddRange(from JObject jsonNode in jo["root"] select InitializeFromNodeJson(jsonNode)); + break; + + default: + throw new Exception("Invalid json node"); + } + + return nodes; + } + + #endregion + + #region IEquatable Members + + public bool Equals(Node other) + { + if (ReferenceEquals(other, null)) + return false; + + return ReferenceEquals(this, other) || Id.Equals(other.Id); + } + + public override bool Equals(Object obj) + { + return Equals(obj as Node); + } + + public override int GetHashCode() + { + return (int)Id; + } + + public static bool operator ==(Node value1, Node value2) + { + if (ReferenceEquals(value1, value2)) + { + return true; + } + + return !ReferenceEquals(value1, null) && value1.Equals(value2); + } + + public static bool operator !=(Node value1, Node value2) + { + if (ReferenceEquals(value1, value2)) + { + return false; + } + + if (ReferenceEquals(value1, null)) // Value2=null is covered by Equals + { + return false; + } + + return !value1.Equals(value2); + } + + #endregion + } +} diff --git a/Neo4jRestNet/Core/Path.cs b/Neo4jRestNet/Core/Implementation/Path.cs similarity index 59% rename from Neo4jRestNet/Core/Path.cs rename to Neo4jRestNet/Core/Implementation/Path.cs index f931d07..63634f7 100644 --- a/Neo4jRestNet/Core/Path.cs +++ b/Neo4jRestNet/Core/Implementation/Path.cs @@ -1,21 +1,27 @@ using System; using System.Collections.Generic; using System.Linq; +using Neo4jRestNet.Core.Interface; using Newtonsoft.Json.Linq; using Newtonsoft.Json; -namespace Neo4jRestNet.Core +namespace Neo4jRestNet.Core.Implementation { - public class Path : IGraphObject + public class Path : IPath { public string Self { get; set; } - public Node StartNode { get; private set; } - public Node EndNode { get; private set; } - public List Nodes { get; private set; } - public List Relationships { get; private set; } + public INode StartNode { get; private set; } + public INode EndNode { get; private set; } + public List Nodes { get; private set; } + public List Relationships { get; private set; } public string OriginalPathJson { get; private set; } + + public Path () + { + + } - private Path(JObject path) + public Path(JObject path) { JToken startNode; if (!path.TryGetValue("start", out startNode)) @@ -26,11 +32,11 @@ private Path(JObject path) switch (startNode.Type) { case JTokenType.String: - StartNode = Node.InitializeFromSelf(startNode.Value()); + StartNode = new Node().InitializeFromSelf(startNode.Value()); break; case JTokenType.Object: - StartNode = Node.InitializeFromNodeJson((JObject)startNode); + StartNode = new Node().InitializeFromNodeJson((JObject)startNode); break; default: @@ -46,18 +52,18 @@ private Path(JObject path) switch (endNode.Type) { case JTokenType.String: - EndNode = Node.InitializeFromSelf(endNode.Value()); + EndNode = new Node().InitializeFromSelf(endNode.Value()); break; case JTokenType.Object: - EndNode = Node.InitializeFromNodeJson((JObject)endNode); + EndNode = new Node().InitializeFromNodeJson((JObject)endNode); break; default: throw new Exception("Invalid path json"); } - - Nodes = new List(); + + Nodes = new List(); JToken nodes; if (!path.TryGetValue("nodes", out nodes) || nodes.Type != JTokenType.Array) { @@ -69,11 +75,11 @@ private Path(JObject path) switch (node.Type) { case JTokenType.String: - Nodes.Add(Node.InitializeFromSelf(node.Value())); + Nodes.Add(new Node().InitializeFromSelf(node.Value())); break; case JTokenType.Object: - Nodes.Add(Node.InitializeFromNodeJson((JObject)node)); + Nodes.Add(new Node().InitializeFromNodeJson((JObject)node)); break; default: @@ -81,23 +87,23 @@ private Path(JObject path) } } - Relationships = new List(); + Relationships = new List(); JToken relationships; if (!path.TryGetValue("relationships", out relationships) || relationships.Type != JTokenType.Array) { throw new Exception("Invalid path json"); } - foreach (JToken relationship in relationships) + foreach (var relationship in relationships) { switch (relationship.Type) { case JTokenType.String: - Relationships.Add(Relationship.InitializeFromSelf(relationship.Value())); + Relationships.Add(new Relationship().InitializeFromSelf(relationship.Value())); break; case JTokenType.Object: - Relationships.Add(Relationship.InitializeFromRelationshipJson((JObject)relationship)); + Relationships.Add(new Relationship().InitializeFromRelationshipJson((JObject)relationship)); break; default: @@ -108,7 +114,7 @@ private Path(JObject path) OriginalPathJson = path.ToString(Formatting.None); } - public static List ParseJson(string jsonPaths) + public List ParseJson(string jsonPaths) { if (String.IsNullOrEmpty(jsonPaths)) { @@ -119,7 +125,7 @@ public static List ParseJson(string jsonPaths) return ParseJson(jaPaths); } - public static List ParseJson(JArray jsonPaths) + public List ParseJson(JArray jsonPaths) { if (jsonPaths == null) { @@ -128,7 +134,7 @@ public static List ParseJson(JArray jsonPaths) var jaPaths = jsonPaths; - return (from JObject joPath in jaPaths select new Path(joPath)).ToList(); + return (from JObject joPath in jaPaths select (IPath) new Path(joPath)).ToList(); } } } diff --git a/Neo4jRestNet/Core/Implementation/Relationship.cs b/Neo4jRestNet/Core/Implementation/Relationship.cs new file mode 100644 index 0000000..17f5188 --- /dev/null +++ b/Neo4jRestNet/Core/Implementation/Relationship.cs @@ -0,0 +1,443 @@ +using System; +using System.Configuration; +using System.Net; +using System.Linq; +using System.Collections.Generic; +using Neo4jRestNet.Core.Interface; +using Neo4jRestNet.Rest; +using Newtonsoft.Json.Linq; +using Newtonsoft.Json; + +namespace Neo4jRestNet.Core.Implementation +{ + public class Relationship : IRelationship + { + private static readonly string DefaultDbUrl = ConfigurationManager.ConnectionStrings["neo4j"].ConnectionString.TrimEnd('/'); + private readonly Neo4jRestApi _restApi; + private string _selfDbUrl; + private string _self; + public INode StartNode { get; private set; } + public INode EndNode { get; private set; } + public string Name { get; private set; } + private Properties _properties; + public long Id { get; private set; } + public EncryptId EncryptedId { get; private set; } + public string OriginalJsonRelationship { get; private set; } + + #region Constructor + + public Relationship() + { + _restApi = new Neo4jRestApi(DefaultDbUrl); + } + + public Relationship(string connectionString) + { + _restApi = new Neo4jRestApi(ConfigurationManager.ConnectionStrings[connectionString].ConnectionString.TrimEnd('/')); + } + + public Relationship(Type nodeType) + { + if (nodeType != typeof(INode)) + { + throw new ArgumentException("Create Relationship with invalid Node type"); + } + + _restApi = new Neo4jRestApi(DefaultDbUrl); + } + + #endregion + + #region GetRelationship + + public IEnumerable GetRelationship(string indexName, string key, object value) + { + string response; + var status = _restApi.GetRelationship(indexName, key, value, out response); + if (status != HttpStatusCode.OK) + { + throw new Exception(string.Format("Index not found in (index:{0})", indexName)); + } + + return ParseJson(response); + } + + public IEnumerable GetRelationship(Enum indexName, string key, object value) + { + return GetRelationship(indexName.ToString(), key, value); + } + + public IEnumerable GetRelationship(string indexName, Enum key, object value) + { + return GetRelationship(indexName, key.ToString(), value); + } + + public IEnumerable GetRelationship(Enum indexName, Enum key, object value) + { + return GetRelationship(indexName.ToString(), key.ToString(), value); + } + + public IEnumerable GetRelationship(string indexName, string searchQuery) + { + string response; + var status = _restApi.GetRelationship(indexName, searchQuery, out response); + if (status != HttpStatusCode.OK) + { + throw new Exception(string.Format("Index not found in (index:{0})", indexName)); + } + + return ParseJson(response); + } + + public IEnumerable GetRelationship(Enum indexName, string searchQuery) + { + return GetRelationship(indexName.ToString(), searchQuery); + } + + #endregion + + #region Initializers + + public IRelationship InitializeFromRelationshipJson(string relationshipJson) + { + JObject jo; + + try + { + jo = JObject.Parse(relationshipJson); + } + catch (Exception e) + { + throw new Exception("Invalid json relationship", e); + } + + return InitializeFromRelationshipJson(jo); + } + + public IRelationship InitializeFromRelationshipJson(JObject relationshipJson) + { + var relationship = new Relationship(); + JToken self; + if (!relationshipJson.TryGetValue("self", out self) || self.Type != JTokenType.String) + { + throw new Exception("Invalid json relationship"); + } + + relationship.Self = self.Value(); + + JToken properties; + if (!relationshipJson.TryGetValue("data", out properties) || properties.Type != JTokenType.Object) + { + throw new Exception("Invalid json relationship"); + } + + JToken startNode; + if (!relationshipJson.TryGetValue("start", out startNode)) + { + throw new Exception("Invalid json relationship"); + } + + switch (startNode.Type) + { + case JTokenType.String: + relationship.StartNode = new Node().InitializeFromSelf(startNode.Value()); + break; + + case JTokenType.Object: + relationship.StartNode = new Node().InitializeFromNodeJson((JObject)startNode); + break; + + default: + throw new Exception("Invalid json relationship"); + } + + JToken endNode; + if (!relationshipJson.TryGetValue("end", out endNode)) + { + throw new Exception("Invalid json relationship"); + } + + switch (endNode.Type) + { + case JTokenType.String: + relationship.EndNode = new Node().InitializeFromSelf(endNode.Value()); + break; + + case JTokenType.Object: + relationship.EndNode = new Node().InitializeFromNodeJson((JObject)endNode); + break; + + default: + throw new Exception("Invalid json relationship"); + } + + JToken name; + if (!relationshipJson.TryGetValue("type", out name) || name.Type != JTokenType.String) + { + throw new Exception("Invalid json relationship"); + } + + relationship.Name = name.Value(); + + relationship._properties = Properties.ParseJson(properties.ToString(Formatting.None)); + + relationship.OriginalJsonRelationship = relationshipJson.ToString(Formatting.None); + + return relationship; + } + + public IRelationship InitializeFromSelf(string self) + { + var relationship = new Relationship + { + Self = self, + StartNode = null, + EndNode = null, + Name = null, + _properties = null, + OriginalJsonRelationship = null + }; + + + return relationship; + } + + public bool IsSelfARelationship(string self) + { + var selfArray = self.Split('/'); + return (selfArray.Length > 2 && selfArray[selfArray.Length - 2] == "relationship"); + } + #endregion + + #region Self + + public string Self + { + get + { + return _self; + } + + private set + { + if (!IsSelfARelationship(value)) + { + throw new Exception(string.Format("Self is not a Relationship ({0})", Self)); + } + + // Make sure there is no trailing / + string self = value.TrimEnd('/'); + + var selfArray = self.Split('/'); + + long relationshipId; + if (!long.TryParse(selfArray.Last(), out relationshipId)) + { + throw new Exception(string.Format("Invalid Self id ({0})", value)); + } + + _selfDbUrl = self.Substring(0, self.LastIndexOf("/relationship")); + _self = self; + Id = relationshipId; + } + } + + #endregion + + #region Properties + + private void LoadProperties(bool refresh) + { + if (refresh) + { + _properties = null; + } + + if (_properties != null) + { + return; + } + + string response; + var status = new Neo4jRestApi(_selfDbUrl).GetPropertiesOnRelationship(_selfDbUrl, Id, out response); + if (status != HttpStatusCode.OK) + { + throw new Exception(string.Format("Error retrieving properties on relationship (relationship id:{0} http response:{1})", Id, status)); + } + + _properties = Properties.ParseJson(response); + } + + public Properties Properties + { + get + { + LoadProperties(false); + return _properties; + } + } + + public void SaveProperties() + { + SaveProperties(Properties); + } + + public void SaveProperties(Properties properties) + { + var status = new Neo4jRestApi(_selfDbUrl).SetPropertiesOnRelationship(_selfDbUrl, Id, properties.ToString()); + if (status != HttpStatusCode.NoContent) + { + throw new Exception(string.Format("Error setting properties on relationship (relationship id:{0} http response:{1})", Id, status)); + } + + LoadProperties(true); + } + + #endregion + + #region Index + + public IRelationship AddRelationshipToIndex(long relationshipId, string indexName, string key, object value) + { + string response; + var status = _restApi.AddRelationshipToIndex(relationshipId, indexName, key, value, out response); + if (status != HttpStatusCode.Created) + { + throw new Exception(string.Format("Error creating index for relationship (http response:{0})", status)); + } + + return InitializeFromRelationshipJson(response); + } + + public IRelationship AddRelationshipToIndex(long relationshipId, Enum indexName, string key, object value) + { + return AddRelationshipToIndex(relationshipId, indexName.ToString(), key, value); + } + + public IRelationship AddRelationshipToIndex(long relationshipId, string indexName, Enum key, object value) + { + return AddRelationshipToIndex(relationshipId, indexName, key.ToString(), value); + } + + public IRelationship AddRelationshipToIndex(long relationshipId, Enum indexName, Enum key, object value) + { + return AddRelationshipToIndex(relationshipId, indexName.ToString(), key.ToString(), value); + } + + public HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, string indexName) + { + var status = _restApi.RemoveRelationshipFromIndex(relationshipId, indexName); + if (status != HttpStatusCode.NoContent) + { + throw new Exception(string.Format("Error remove relationship from index (relationship id:{0} index name:{1} http response:{2})", relationshipId, indexName, status)); + } + + return status; + } + + public HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, Enum indexName) + { + return RemoveRelationshipFromIndex(relationshipId, indexName.ToString()); + } + + public HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, string indexName, string key) + { + var status = _restApi.RemoveRelationshipFromIndex(relationshipId, indexName, key); + if (status != HttpStatusCode.NoContent) + { + throw new Exception(string.Format("Error remove relationship from index (relationship id:{0} index name:{1} http response:{2})", relationshipId, indexName, status)); + } + + return status; + } + + public HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, Enum indexName, string key) + { + return RemoveRelationshipFromIndex(relationshipId, indexName.ToString(), key); + } + + public HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, string indexName, Enum key) + { + return RemoveRelationshipFromIndex(relationshipId, indexName, key.ToString()); + } + + public HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, Enum indexName, Enum key) + { + return RemoveRelationshipFromIndex(relationshipId, indexName.ToString(), key.ToString()); + } + + public HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, string indexName, string key, object value) + { + var status = _restApi.RemoveRelationshipFromIndex(relationshipId, indexName, key, value); + if (status != HttpStatusCode.NoContent) + { + throw new Exception(string.Format("Error remove relationship from index (relationship id:{0} index name:{1} http response:{2})", relationshipId, indexName, status)); + } + + return status; + } + + public HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, Enum indexName, string key, object value) + { + return RemoveRelationshipFromIndex(relationshipId, indexName.ToString(), key, value); + } + + public HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, string indexName, Enum key, object value) + { + return RemoveRelationshipFromIndex(relationshipId, indexName, key.ToString(), value); + } + + public HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, Enum indexName, Enum key, object value) + { + return RemoveRelationshipFromIndex(relationshipId, indexName.ToString(), key.ToString(), value); + } + + #endregion + + #region Delete + + public HttpStatusCode DeleteRelationship() + { + var status = new Neo4jRestApi(_selfDbUrl).DeleteRelationship(Id); + if (status != HttpStatusCode.NoContent) + { + throw new Exception(string.Format("Error deleteing relationship (relationship id:{0} http response:{1})", Id, status)); + } + + return status; + } + + #endregion + + #region ParseJson + + public IEnumerable ParseJson(string jsonRelationships) + { + if (String.IsNullOrEmpty(jsonRelationships)) + return null; + + var relationships = new List(); + + // The Json passed in can be a JObject or JArray - this is to test for that. + var jo = JObject.Parse(string.Concat("{\"root\":", jsonRelationships, "}")); + + switch (jo["root"].Type) + { + case JTokenType.Object: + relationships.Add(InitializeFromRelationshipJson(jo["root"].ToString(Formatting.None))); + break; + + case JTokenType.Array: + relationships.AddRange(from JObject jsonRelationship in jo["root"] select InitializeFromRelationshipJson(jsonRelationship)); + break; + + default: + throw new Exception("Invalid json relationship"); + } + + return relationships; + } + + #endregion + } +} diff --git a/Neo4jRestNet/Core/Interface/INode.cs b/Neo4jRestNet/Core/Interface/INode.cs new file mode 100644 index 0000000..52c8a43 --- /dev/null +++ b/Neo4jRestNet/Core/Interface/INode.cs @@ -0,0 +1,80 @@ +using System.Collections.Generic; +using System.Net; +using System; +using Newtonsoft.Json.Linq; +using Neo4jRestNet.Rest; + +namespace Neo4jRestNet.Core.Interface +{ + public interface INode : IGraphObject + { + long Id { get; } + EncryptId EncryptedId { get; } + string OriginalJsonNode { get; } + Properties Properties { get; } + string NodeType { get; } + + INode GetRootNode(); + + INode GetNode(EncryptId nodeId); + IEnumerable GetNode(string indexName, string key, object value); + IEnumerable GetNode(Enum indexName, string key, object value); + IEnumerable GetNode(string indexName, Enum key, object value); + IEnumerable GetNode(Enum indexName, Enum key, object value); + IEnumerable GetNode(string indexName, string searchQuery); + IEnumerable GetNode(Enum indexName, string searchQuery); + + INode CreateNode(string nodeType); + INode CreateNode(Enum nodeType); + INode CreateNode(string nodeType, Properties properties); + INode CreateNode(Enum nodeType, Properties properties); + INode CreateNode(string nodeType, IDictionary properties); + INode CreateNode(Enum nodeType, IDictionary properties); + + HttpStatusCode DeleteNode(); + + INode InitializeFromNodeJson(string nodeJson); + INode InitializeFromNodeJson(JObject nodeJson); + + INode InitializeFromSelf(string self); + + void SaveProperties(); + void SaveProperties(Properties properties); + + IEnumerable GetRelationships(); + IEnumerable GetRelationships(RelationshipDirection direction); + IEnumerable GetRelationships(RelationshipDirection direction, IRelationship relationshipType); + IEnumerable GetRelationships(Enum name); + IEnumerable GetRelationships(string name); + IEnumerable GetRelationships(IEnumerable names); + IEnumerable GetRelationships(IEnumerable names); + IEnumerable GetRelationships(RelationshipDirection direction, string name); + IEnumerable GetRelationships(RelationshipDirection direction, Enum name); + IEnumerable GetRelationships(RelationshipDirection direction, IEnumerable names); + + IRelationship CreateRelationshipTo(INode toNode, string relationshipType); + IRelationship CreateRelationshipTo(INode toNode, Enum relationshipType); + IRelationship CreateRelationshipTo(INode toNode, Enum relationshipType, Properties relationshipProperties); + IRelationship CreateRelationshipTo(INode toNode, string relationshipType, Properties relationshipProperties); + + IEnumerable Traverse(Order order, Uniqueness uniqueness, IEnumerable relationships, PruneEvaluator pruneEvaluator, ReturnFilter returnFilter, int? maxDepth, ReturnType returnType); + + INode AddNodeToIndex(long nodeId, string indexName, string key, object value); + INode AddNodeToIndex(long nodeId, Enum indexName, string key, object value); + INode AddNodeToIndex(long nodeId, string indexName, Enum key, object value); + INode AddNodeToIndex(long nodeId, Enum indexName, Enum key, object value); + + HttpStatusCode RemoveNodeFromIndex(long nodeId, string indexName); + HttpStatusCode RemoveNodeFromIndex(long nodeId, Enum indexName); + HttpStatusCode RemoveNodeFromIndex(long nodeId, string indexName, string key); + HttpStatusCode RemoveNodeFromIndex(long nodeId, Enum indexName, string key); + HttpStatusCode RemoveNodeFromIndex(long nodeId, string indexName, Enum key); + HttpStatusCode RemoveNodeFromIndex(long nodeId, Enum indexName, Enum key); + HttpStatusCode RemoveNodeFromIndex(long nodeId, string indexName, string key, object value); + HttpStatusCode RemoveNodeFromIndex(long nodeId, Enum indexName, string key, object value); + HttpStatusCode RemoveNodeFromIndex(long nodeId, string indexName, Enum key, object value); + HttpStatusCode RemoveNodeFromIndex(long nodeId, Enum indexName, Enum key, object value); + + IEnumerable ParseJson(string jsonNodes); + } +} diff --git a/Neo4jRestNet/Core/Interface/IPath.cs b/Neo4jRestNet/Core/Interface/IPath.cs new file mode 100644 index 0000000..ab490a6 --- /dev/null +++ b/Neo4jRestNet/Core/Interface/IPath.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Neo4jRestNet.Core.Interface; +using Newtonsoft.Json.Linq; +using Newtonsoft.Json; + +namespace Neo4jRestNet.Core.Interface +{ + public interface IPath : IGraphObject + { + INode StartNode { get; } + INode EndNode { get; } + List Nodes { get; } + List Relationships { get; } + string OriginalPathJson { get; } + + List ParseJson(string jsonPaths); + List ParseJson(JArray jsonPaths); + } +} diff --git a/Neo4jRestNet/Core/Interface/IRelationship.cs b/Neo4jRestNet/Core/Interface/IRelationship.cs new file mode 100644 index 0000000..b040d06 --- /dev/null +++ b/Neo4jRestNet/Core/Interface/IRelationship.cs @@ -0,0 +1,53 @@ +using System; +using System.Net; +using System.Collections.Generic; +using Newtonsoft.Json.Linq; + +namespace Neo4jRestNet.Core.Interface +{ + public interface IRelationship : IGraphObject + { + INode StartNode { get; } + INode EndNode { get; } + string Name { get; } + long Id { get; } + EncryptId EncryptedId { get; } + string OriginalJsonRelationship { get; } + Properties Properties { get; } + + IEnumerable GetRelationship(string indexName, string key, object value); + IEnumerable GetRelationship(Enum indexName, string key, object value); + IEnumerable GetRelationship(string indexName, Enum key, object value); + IEnumerable GetRelationship(Enum indexName, Enum key, object value); + IEnumerable GetRelationship(string indexName, string searchQuery); + IEnumerable GetRelationship(Enum indexName, string searchQuery); + + IRelationship InitializeFromRelationshipJson(string relationshipJson); + IRelationship InitializeFromRelationshipJson(JObject relationshipJson); + IRelationship InitializeFromSelf(string self); + bool IsSelfARelationship(string self); + + void SaveProperties(); + void SaveProperties(Properties properties); + + IRelationship AddRelationshipToIndex(long relationshipId, string indexName, string key, object value); + IRelationship AddRelationshipToIndex(long relationshipId, Enum indexName, string key, object value); + IRelationship AddRelationshipToIndex(long relationshipId, string indexName, Enum key, object value); + IRelationship AddRelationshipToIndex(long relationshipId, Enum indexName, Enum key, object value); + + HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, string indexName); + HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, Enum indexName); + HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, string indexName, string key); + HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, Enum indexName, string key); + HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, string indexName, Enum key); + HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, Enum indexName, Enum key); + HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, string indexName, string key, object value); + HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, Enum indexName, string key, object value); + HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, string indexName, Enum key, object value); + HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, Enum indexName, Enum key, object value); + + HttpStatusCode DeleteRelationship(); + + IEnumerable ParseJson(string jsonRelationships); + } +} diff --git a/Neo4jRestNet/Core/Node.cs b/Neo4jRestNet/Core/Node.cs deleted file mode 100644 index 073f000..0000000 --- a/Neo4jRestNet/Core/Node.cs +++ /dev/null @@ -1,767 +0,0 @@ -using System.Collections.Generic; -using System.Linq; -using System.Net; -using System; -using Newtonsoft.Json.Linq; -using Newtonsoft.Json; -using Neo4jRestNet.Rest; -using System.Configuration; - -namespace Neo4jRestNet.Core -{ - public class Node : IGraphObject, IEquatable - { - private enum NodeProperty - { - NodeType - } - - private string _selfDbUrl; - private string _self; - private Properties _properties; - - public long Id { get; private set; } - public EncryptId EncryptedId { get; private set; } - public string OriginalNodeJson { get; private set; } - - protected Node() { } - - #region GetRootNode - - public static Node GetRootNode() - { - return GetRootNode(DbStore.Default); - } - - public static Node GetRootNode(DbStore dbUrl) - { - string response; - var status = new StoreFactory().Create(dbUrl).GetRoot(out response); - if (status != HttpStatusCode.OK) - { - throw new Exception(string.Format("Error getting root node (http response:{0})", status)); - } - - JObject jo; - try - { - jo = JObject.Parse(response); - } - catch (Exception e) - { - throw new Exception("Invalid json", e); - } - - JToken referenceNode; - if (!jo.TryGetValue("reference_node", out referenceNode)) - { - throw new Exception("Invalid json"); - } - - var node = new Node {Self = referenceNode.Value(), _properties = null}; - - return node; - } - - #endregion - - #region GetNode - - public static Node GetNode(EncryptId nodeId) - { - return GetNode(DbStore.Default, nodeId); - } - - public static Node GetNode(DbStore dbUrl, EncryptId nodeId) - { - string response; - var status = new StoreFactory().Create(dbUrl).GetNode((long)nodeId, out response); - if (status != HttpStatusCode.OK) - { - throw new Exception(string.Format("Node not found (node id:{0})", (long)nodeId)); - } - - return InitializeFromNodeJson(response); - } - - public static IEnumerable GetNode(string indexName, string key, object value) - { - return GetNode(DbStore.Default, indexName, key, value); - } - - public static IEnumerable GetNode(Enum indexName, string key, object value) - { - return GetNode(DbStore.Default, indexName.ToString(), key, value); - } - - public static IEnumerable GetNode(string indexName, Enum key, object value) - { - return GetNode(DbStore.Default, indexName, key.ToString(), value); - } - - public static IEnumerable GetNode(Enum indexName, Enum key, object value) - { - return GetNode(DbStore.Default, indexName.ToString(), key.ToString(), value); - } - - public static IEnumerable GetNode(DbStore dbUrl, string indexName, string key, object value) - { - string response; - var status = new StoreFactory().Create(dbUrl).GetNode(indexName, key, value, out response); - if (status != HttpStatusCode.OK) - { - throw new Exception(string.Format("Index not found in (index:{0})", indexName)); - } - - return ParseJson(response); - } - - public static IEnumerable GetNode(DbStore dbUrl, Enum indexName, string key, object value) - { - return GetNode(dbUrl, indexName.ToString(), key, value); - } - - public static IEnumerable GetNode(DbStore dbUrl, string indexName, Enum key, object value) - { - return GetNode(dbUrl, indexName, key.ToString(), value); - } - - public static IEnumerable GetNode(DbStore dbUrl, Enum indexName, Enum key, object value) - { - return GetNode(dbUrl, indexName.ToString(), key.ToString(), value); - } - - public static IEnumerable GetNode(string indexName, string searchQuery) - { - return GetNode(DbStore.Default, indexName, searchQuery); - } - - public static IEnumerable GetNode(Enum indexName, string searchQuery) - { - return GetNode(DbStore.Default, indexName.ToString(), searchQuery); - } - - public static IEnumerable GetNode(DbStore dbUrl, string indexName, string searchQuery) - { - string response; - var status = new StoreFactory().Create(dbUrl).GetNode(indexName, searchQuery, out response); - if (status != HttpStatusCode.OK) - { - throw new Exception(string.Format("Index not found in (index:{0})", indexName)); - } - - return ParseJson(response); - } - - public static IEnumerable GetNode(DbStore dbUrl, Enum indexName, string searchQuery) - { - return GetNode(dbUrl, indexName.ToString(), searchQuery); - } - - #endregion - - #region CreateNode - - public static Node CreateNode(string nodeType) - { - var properties = new Properties(); - properties.SetProperty(NodeProperty.NodeType.ToString(), nodeType); - return CreateNodeFromJson(DbStore.Default, properties.ToString()); - } - - public static Node CreateNode(Enum nodeType) - { - return CreateNode(nodeType.ToString()); - } - - public static Node CreateNode(DbStore dbUrl, string nodeType) - { - var properties = new Properties(); - properties.SetProperty(NodeProperty.NodeType.ToString(), nodeType); - return CreateNodeFromJson(dbUrl, properties.ToString()); - } - public static Node CreateNode(DbStore dbUrl, Enum nodeType) - { - return CreateNode(dbUrl, nodeType.ToString()); - } - - public static Node CreateNode(string nodeType, Properties properties) - { - properties.SetProperty(NodeProperty.NodeType.ToString(), nodeType); - return CreateNodeFromJson(DbStore.Default, properties.ToString()); - } - - public static Node CreateNode(Enum nodeType, Properties properties) - { - return CreateNode(nodeType.ToString(), properties); - } - - public static Node CreateNode(DbStore dbUrl, string nodeType, Properties properties) - { - properties.SetProperty(NodeProperty.NodeType.ToString(), nodeType); - return CreateNodeFromJson(dbUrl, properties.ToString()); - } - - public static Node CreateNode(DbStore dbUrl, Enum nodeType, Properties properties) - { - return CreateNode(dbUrl, nodeType.ToString(), properties); - } - - public static Node CreateNode(string nodeType, IDictionary properties) - { - properties.Add(NodeProperty.NodeType.ToString(), nodeType); - return CreateNodeFromJson(DbStore.Default, JObject.FromObject(properties).ToString(Formatting.None)); - } - - public static Node CreateNode(Enum nodeType, IDictionary properties) - { - return CreateNode(nodeType.ToString(), properties); - } - - public static Node CreateNode(DbStore dbUrl, string nodeType, IDictionary properties) - { - properties.Add(NodeProperty.NodeType.ToString(), nodeType); - return CreateNodeFromJson(dbUrl, JObject.FromObject(properties).ToString(Formatting.None)); - } - - public static Node CreateNode(DbStore dbUrl, Enum nodeType, IDictionary properties) - { - return CreateNode(dbUrl, nodeType.ToString(), properties); - } - - private static Node CreateNodeFromJson(DbStore dbUrl, string jsonProperties) - { - string response; - var status = new StoreFactory().Create(dbUrl).CreateNode(jsonProperties, out response); - if (status != HttpStatusCode.Created) - { - throw new Exception(string.Format("Error creating node (http response:{0})", status)); - } - - return InitializeFromNodeJson(response); - } - - #endregion - - #region Delete - - public HttpStatusCode DeleteNode() - { - var status = new StoreFactory().Create(DbStore.Default).DeleteNode(Id); - if (status != HttpStatusCode.NoContent) - { - throw new Exception(string.Format("Error deleting node (node id:{0} http response:{1})", Id, status)); - } - - return status; - } - - #endregion - - #region Initializers - - public static Node InitializeFromNodeJson(string nodeJson) - { - JObject jo; - - try - { - jo = JObject.Parse(nodeJson); - } - catch (Exception e) - { - throw new Exception("Invalid node json", e); - } - - return InitializeFromNodeJson(jo); - } - - public static Node InitializeFromNodeJson(JObject nodeJson) - { - JToken self; - if (!nodeJson.TryGetValue("self", out self)) - { - throw new Exception("Invalid node json"); - } - - JToken properties; - if (!nodeJson.TryGetValue("data", out properties)) - { - throw new Exception("Invalid node json"); - } - - var node = new Node - { - Self = self.Value(), - _properties = Properties.ParseJson(properties.ToString(Formatting.None)), - OriginalNodeJson = nodeJson.ToString(Formatting.None) - }; - - return node; - } - - public static Node InitializeFromSelf(string self) - { - var node = new Node {Self = self, _properties = null}; - return node; - } - - public static bool IsSelfANode(string self) - { - var selfArray = self.Split('/'); - return (selfArray.Length > 2 && selfArray[selfArray.Length - 2] == "node"); - } - - #endregion - - #region Self - - public string Self - { - get - { - return _self; - } - - private set - { - if (!IsSelfANode(value)) - { - throw new Exception(string.Format("Self is not a Node ({0})", Self)); - } - - // Make sure there is no trailing / - var self = value.TrimEnd('/'); - - var selfArray = self.Split('/'); - - long nodeId; - if (!long.TryParse(selfArray.Last(), out nodeId)) - { - throw new Exception(string.Format("Invalid Self id ({0})", value)); - } - - _selfDbUrl = self.Substring(0, self.LastIndexOf("/node")); - _self = self; - - // Set Id & NodeId values - Id = nodeId; - EncryptedId = nodeId; - } - } - - #endregion - - #region Properties - - private void LoadProperties(bool refresh) - { - if (refresh) - { - _properties = null; - } - - if (_properties != null) - { - return; - } - - string response; - HttpStatusCode status = new StoreFactory().Create(DbStore.Default).GetPropertiesOnNode((long)EncryptedId, out response); - if (status != HttpStatusCode.OK) - { - throw new Exception(string.Format("Error retrieving properties on node (node id:{0} http response:{1})", (long)EncryptedId, status)); - } - - _properties = Properties.ParseJson(response); - } - - public Properties Properties - { - get - { - LoadProperties(false); - return _properties; - } - } - - public void SaveProperties() - { - SaveProperties(Properties); - } - - public void SaveProperties(Properties properties) - { - LoadProperties(false); - if (Properties.HasProperty(NodeProperty.NodeType.ToString())) - { - properties.SetProperty(NodeProperty.NodeType.ToString(), Properties.GetProperty(NodeProperty.NodeType.ToString())); - } - - HttpStatusCode status = new StoreFactory().Create(DbStore.Default).SetPropertiesOnNode((long)EncryptedId, properties.ToString()); - if (status != HttpStatusCode.NoContent) - { - throw new Exception(string.Format("Error setting properties on node (node id:{0} http response:{1})", (long)EncryptedId, status)); - } - - LoadProperties(true); - } - - #endregion - - #region Relationships - - public IEnumerable GetRelationships() - { - return GetRelationships(RelationshipDirection.All, (IEnumerable)null); - } - - public IEnumerable GetRelationships(RelationshipDirection direction) - { - return GetRelationships(direction, (IEnumerable)null); - } - - public IEnumerable GetRelationships(RelationshipDirection direction, Relationship relationshipType) - { - return GetRelationships(direction, new List { relationshipType.ToString() }); - } - - public IEnumerable GetRelationships(Enum name) - { - return GetRelationships(RelationshipDirection.All, new List { name.ToString() }); - } - - public IEnumerable GetRelationships(string name) - { - return GetRelationships(RelationshipDirection.All, new List { name }); - } - - public IEnumerable GetRelationships(IEnumerable names) - { - return GetRelationships(RelationshipDirection.All, names.Select(n => n.ToString()).ToList()); - } - - public IEnumerable GetRelationships(IEnumerable names) - { - return GetRelationships(RelationshipDirection.All, names); - } - - public IEnumerable GetRelationships(RelationshipDirection direction, string name) - { - return GetRelationships(direction, new List { name }); - } - - public IEnumerable GetRelationships(RelationshipDirection direction, Enum name) - { - return GetRelationships(direction, new List { name.ToString() }); - } - - public IEnumerable GetRelationships(RelationshipDirection direction, IEnumerable names) - { - string response; - HttpStatusCode status = new StoreFactory().Create(DbStore.Default).GetRelationshipsOnNode((long)EncryptedId, direction, names, out response); - if (status != HttpStatusCode.OK) - { - throw new Exception(string.Format("Error retrieving relationships on node (node id:{0} http response:{1})", (long)EncryptedId, status)); - } - - return Relationship.ParseJson(response); - } - - public Relationship CreateRelationshipTo(Node toNode, string relationshipType) - { - return CreateRelationshipTo(toNode, relationshipType, null); - } - - public Relationship CreateRelationshipTo(Node toNode, Enum relationshipType) - { - return CreateRelationshipTo(toNode, relationshipType.ToString(), null); - } - - public Relationship CreateRelationshipTo(Node toNode, Enum relationshipType, Properties relationshipProperties) - { - return CreateRelationshipTo(toNode, relationshipType.ToString(), relationshipProperties); - } - - public Relationship CreateRelationshipTo(Node toNode, string relationshipType, Properties relationshipProperties) - { - string response; - HttpStatusCode status = new StoreFactory().Create(DbStore.Default).CreateRelationship( - (long)EncryptedId, - toNode.Self, - relationshipType, - relationshipProperties == null ? null : relationshipProperties.ToString(), - out response); - if (status != HttpStatusCode.Created) - { - throw new Exception(string.Format("Error creationg relationship on node (node id:{0} http response:{1})", (long)EncryptedId, status)); - } - - return Relationship.ParseJson(response).First(); - } - - #endregion - - #region Traverse - - public IEnumerable Traverse(Order order, Uniqueness uniqueness, IEnumerable relationships, PruneEvaluator pruneEvaluator, ReturnFilter returnFilter, int? maxDepth, ReturnType returnType) - { - string response; - var status = new StoreFactory().Create(DbStore.Default).Traverse((long)EncryptedId, order, uniqueness, relationships, pruneEvaluator, returnFilter, maxDepth, returnType, out response); - if (status != HttpStatusCode.OK) - { - throw new Exception(string.Format("Error traversing nodes (node id:{0} status code:{1})", (long)EncryptedId, status)); - } - - if (returnType.ToString() == ReturnType.Node.ToString()) - { - return ParseJson(response); - } - - if (returnType.ToString() == ReturnType.Relationship.ToString()) - { - return Relationship.ParseJson(response); - } - - if (returnType.ToString() == ReturnType.Path.ToString() || returnType.ToString() == ReturnType.FullPath.ToString()) - { - return Path.ParseJson(response); - } - - throw new Exception(string.Format("Return type not implemented (type:{0})", returnType)); - } - - #endregion - - #region Index - - public static Node AddNodeToIndex(long nodeId, string indexName, string key, object value) - { - return AddNodeToIndex(DbStore.Default, nodeId, indexName, key, value); - } - - public static Node AddNodeToIndex(long nodeId, Enum indexName, string key, object value) - { - return AddNodeToIndex(DbStore.Default, nodeId, indexName.ToString(), key, value); - } - - public static Node AddNodeToIndex(long nodeId, string indexName, Enum key, object value) - { - return AddNodeToIndex(DbStore.Default, nodeId, indexName, key.ToString(), value); - } - - public static Node AddNodeToIndex(long nodeId, Enum indexName, Enum key, object value) - { - return AddNodeToIndex(DbStore.Default, nodeId, indexName.ToString(), key.ToString(), value); - } - - public static Node AddNodeToIndex(DbStore dbUrl, long nodeId, Enum indexName, Enum key, object value) - { - return AddNodeToIndex(dbUrl, nodeId, indexName.ToString(), key.ToString(), value); - } - - public static Node AddNodeToIndex(DbStore dbUrl, long nodeId, string indexName, string key, object value) - { - string response; - var status = new StoreFactory().Create(dbUrl).AddNodeToIndex(nodeId, indexName, key, value, out response); - if (status != HttpStatusCode.Created) - { - throw new Exception(string.Format("Error creating index for node (http response:{0})", status)); - } - - return InitializeFromNodeJson(response); - } - - public static HttpStatusCode RemoveNodeFromIndex(long nodeId, string indexName) - { - return RemoveNodeFromIndex(DbStore.Default, nodeId, indexName); - } - - public static HttpStatusCode RemoveNodeFromIndex(long nodeId, Enum indexName) - { - return RemoveNodeFromIndex(DbStore.Default, nodeId, indexName.ToString()); - } - - public static HttpStatusCode RemoveNodeFromIndex(DbStore dbUrl, long nodeId, Enum indexName) - { - return RemoveNodeFromIndex(dbUrl, nodeId, indexName.ToString()); - } - - public static HttpStatusCode RemoveNodeFromIndex(DbStore dbUrl, long nodeId, string indexName) - { - var status = new StoreFactory().Create(dbUrl).RemoveNodeFromIndex(nodeId, indexName); - if (status != HttpStatusCode.NoContent) - { - throw new Exception(string.Format("Error remove node from index (node id:{0} index name:{1} http response:{2})", nodeId, indexName, status)); - } - - return status; - } - - public static HttpStatusCode RemoveNodeFromIndex(long nodeId, string indexName, string key) - { - return RemoveNodeFromIndex(DbStore.Default, nodeId, indexName, key); - } - - public static HttpStatusCode RemoveNodeFromIndex(long nodeId, Enum indexName, string key) - { - return RemoveNodeFromIndex(DbStore.Default, nodeId, indexName.ToString(), key); - } - - public static HttpStatusCode RemoveNodeFromIndex(long nodeId, string indexName, Enum key) - { - return RemoveNodeFromIndex(DbStore.Default, nodeId, indexName, key.ToString()); - } - - public static HttpStatusCode RemoveNodeFromIndex(long nodeId, Enum indexName, Enum key) - { - return RemoveNodeFromIndex(DbStore.Default, nodeId, indexName.ToString(), key.ToString()); - } - - public static HttpStatusCode RemoveNodeFromIndex(DbStore dbUrl, long nodeId, Enum indexName, Enum key) - { - return RemoveNodeFromIndex(dbUrl, nodeId, indexName.ToString(), key.ToString()); - } - - public static HttpStatusCode RemoveNodeFromIndex(DbStore dbUrl, long nodeId, string indexName, string key) - { - var status = new StoreFactory().Create(dbUrl).RemoveNodeFromIndex(nodeId, indexName, key); - if (status != HttpStatusCode.NoContent) - { - throw new Exception(string.Format("Error remove node from index (node id:{0} index name:{1} http response:{2})", nodeId, indexName, status)); - } - - return status; - } - - public static HttpStatusCode RemoveNodeFromIndex(long nodeId, string indexName, string key, object value) - { - return RemoveNodeFromIndex(DbStore.Default, nodeId, indexName, key, value); - } - - public static HttpStatusCode RemoveNodeFromIndex(long nodeId, Enum indexName, string key, object value) - { - return RemoveNodeFromIndex(DbStore.Default, nodeId, indexName.ToString(), key, value); - } - - public static HttpStatusCode RemoveNodeFromIndex(long nodeId, string indexName, Enum key, object value) - { - return RemoveNodeFromIndex(DbStore.Default, nodeId, indexName, key.ToString(), value); - } - - public static HttpStatusCode RemoveNodeFromIndex(long nodeId, Enum indexName, Enum key, object value) - { - return RemoveNodeFromIndex(DbStore.Default, nodeId, indexName.ToString(), key.ToString(), value); - } - - public static HttpStatusCode RemoveNodeFromIndex(DbStore dbUrl, long nodeId, string indexName, string key, object value) - { - var status = new StoreFactory().Create(dbUrl).RemoveNodeFromIndex(nodeId, indexName, key, value); - if (status != HttpStatusCode.NoContent) - { - throw new Exception(string.Format("Error remove node from index (node id:{0} index name:{1} http response:{2})", nodeId, indexName, status)); - } - - return status; - } - #endregion - - #region NodeType - - public string NodeType - { - get - { - return _properties == null ? null : _properties.GetProperty(NodeProperty.NodeType.ToString()); - } - } - - #endregion - - #region ParseJson - - public static IEnumerable ParseJson(string jsonNodes) - { - if (String.IsNullOrEmpty(jsonNodes)) - { - return null; - } - - var nodes = new List(); - - // The Json passed in can be a JObject or JArray - this is to test for that. - var jo = JObject.Parse(string.Concat("{\"root\":", jsonNodes, "}")); - - switch (jo["root"].Type) - { - case JTokenType.Object: - nodes.Add(InitializeFromNodeJson(jo["root"].ToString(Formatting.None))); - break; - - case JTokenType.Array: - nodes.AddRange(from JObject jsonNode in jo["root"] select InitializeFromNodeJson(jsonNode)); - break; - - default: - throw new Exception("Invalid node json"); - } - - return nodes; - } - - #endregion - - #region IEquatable Members - - public bool Equals(Node other) - { - if (ReferenceEquals(other, null)) - return false; - - if (ReferenceEquals(this, other)) - return true; - - if (EncryptedId == null || other.EncryptedId == null) - return false; - - return EncryptedId.Equals(other.EncryptedId); - } - - public override bool Equals(Object obj) - { - return Equals(obj as Node); - } - - public override int GetHashCode() - { - return (int)EncryptedId; - } - - public static bool operator ==(Node value1, Node value2) - { - if (ReferenceEquals(value1, value2)) - { - return true; - } - - return !ReferenceEquals(value1, null) && value1.Equals(value2); - } - - public static bool operator !=(Node value1, Node value2) - { - if (ReferenceEquals(value1, value2)) - { - return false; - } - - if (ReferenceEquals(value1, null)) // Value2=null is covered by Equals - { - return false; - } - - return !value1.Equals(value2); - } - - #endregion - } -} diff --git a/Neo4jRestNet/Core/Relationship.cs b/Neo4jRestNet/Core/Relationship.cs deleted file mode 100644 index 0dd8a69..0000000 --- a/Neo4jRestNet/Core/Relationship.cs +++ /dev/null @@ -1,480 +0,0 @@ -using System; -using System.Net; -using System.Linq; -using System.Configuration; -using System.Collections.Generic; -using Neo4jRestNet.Rest; -using Newtonsoft.Json.Linq; -using Newtonsoft.Json; - - -namespace Neo4jRestNet.Core -{ - public class Relationship : IGraphObject - { - private static readonly string _defaultDbUrl = ConfigurationManager.ConnectionStrings["neo4j"].ConnectionString.TrimEnd('/'); - - private string _dbUrl; - private string _self; - public Node StartNode { get; private set; } - public Node EndNode { get; private set; } - public string Name { get; private set; } - private Properties _properties; - public long Id { get; private set; } - public EncryptId EncryptedId { get; private set; } - public string OriginalRelationshipJson { get; private set; } - - private Relationship() { } - - #region GetRelationship - - public static IEnumerable GetRelationship(string indexName, string key, object value) - { - return GetRelationship(_defaultDbUrl, indexName, key, value); - } - - public static IEnumerable GetRelationship(Enum indexName, string key, object value) - { - return GetRelationship(_defaultDbUrl, indexName.ToString(), key, value); - } - - public static IEnumerable GetRelationship(string indexName, Enum key, object value) - { - return GetRelationship(_defaultDbUrl, indexName, key.ToString(), value); - } - - public static IEnumerable GetRelationship(Enum indexName, Enum key, object value) - { - return GetRelationship(_defaultDbUrl, indexName.ToString(), key.ToString(), value); - } - - public static IEnumerable GetRelationship(string dbUrl, Enum indexName, Enum key, object value) - { - return GetRelationship(dbUrl, indexName.ToString(), key.ToString(), value); - } - - public static IEnumerable GetRelationship(string dbUrl, string indexName, string key, object value) - { - string response; - HttpStatusCode status = Neo4jRestApi.GetRelationship(dbUrl, indexName, key, value, out response); - if (status != HttpStatusCode.OK) - { - throw new Exception(string.Format("Index not found in (index:{0})", indexName)); - } - - return ParseJson(response); - } - - public static IEnumerable GetRelationship(string indexName, string searchQuery) - { - return GetRelationship(_defaultDbUrl, indexName, searchQuery); - } - - public static IEnumerable GetRelationship(Enum indexName, string searchQuery) - { - return GetRelationship(_defaultDbUrl, indexName.ToString(), searchQuery); - } - - public static IEnumerable GetRelationship(string dbUrl, Enum indexName, string searchQuery) - { - return GetRelationship(dbUrl, indexName.ToString(), searchQuery); - } - - public static IEnumerable GetRelationship(string dbUrl, string indexName, string searchQuery) - { - string response; - HttpStatusCode status = Neo4jRestApi.GetRelationship(dbUrl, indexName, searchQuery, out response); - if (status != HttpStatusCode.OK) - { - throw new Exception(string.Format("Index not found in (index:{0})", indexName)); - } - - return ParseJson(response); - } - - #endregion - - #region Initializers - - public static Relationship InitializeFromRelationshipJson(string relationshipJson) - { - JObject jo; - - try - { - jo = JObject.Parse(relationshipJson); - } - catch (Exception e) - { - throw new Exception("Invalid relationship json", e); - } - - return InitializeFromRelationshipJson(jo); - } - - public static Relationship InitializeFromRelationshipJson(JObject relationshipJson) - { - var relationship = new Relationship(); - JToken self; - if (!relationshipJson.TryGetValue("self", out self) || self.Type != JTokenType.String) - { - throw new Exception("Invalid relationship json"); - } - - relationship.Self = self.Value(); - - JToken properties; - if (!relationshipJson.TryGetValue("data", out properties) || properties.Type != JTokenType.Object) - { - throw new Exception("Invalid relationship json"); - } - - JToken startNode; - if (!relationshipJson.TryGetValue("start", out startNode)) - { - throw new Exception("Invalid relationship json"); - } - - switch (startNode.Type) - { - case JTokenType.String: - relationship.StartNode = Node.InitializeFromSelf(startNode.Value()); - break; - - case JTokenType.Object: - relationship.StartNode = Node.InitializeFromNodeJson((JObject)startNode); - break; - - default: - throw new Exception("Invalid relationship json"); - } - - JToken endNode; - if (!relationshipJson.TryGetValue("end", out endNode)) - { - throw new Exception("Invalid relationship json"); - } - - switch (endNode.Type) - { - case JTokenType.String: - relationship.EndNode = Node.InitializeFromSelf(endNode.Value()); - break; - - case JTokenType.Object: - relationship.EndNode = Node.InitializeFromNodeJson((JObject)endNode); - break; - - default: - throw new Exception("Invalid relationship json"); - } - - JToken name; - if (!relationshipJson.TryGetValue("type", out name) || name.Type != JTokenType.String) - { - throw new Exception("Invalid relationship json"); - } - - relationship.Name = name.Value(); - - relationship._properties = Properties.ParseJson(properties.ToString(Formatting.None)); - - relationship.OriginalRelationshipJson = relationshipJson.ToString(Formatting.None); - - return relationship; - } - - public static Relationship InitializeFromSelf(string self) - { - var relationship = new Relationship - { - Self = self, - StartNode = null, - EndNode = null, - Name = null, - _properties = null, - OriginalRelationshipJson = null - }; - - - return relationship; - } - - public static bool IsSelfARelationship(string self) - { - var selfArray = self.Split('/'); - return (selfArray.Length > 2 && selfArray[selfArray.Length - 2] == "relationship"); - } - #endregion - - #region Self - - public string Self - { - get - { - return _self; - } - - private set - { - if (!IsSelfARelationship(value)) - { - throw new Exception(string.Format("Self is not a Relationship ({0})", Self)); - } - - // Make sure there is no trailing / - string self = value.TrimEnd('/'); - - var selfArray = self.Split('/'); - - long relationshipId; - if (!long.TryParse(selfArray.Last(), out relationshipId)) - { - throw new Exception(string.Format("Invalid Self id ({0})", value)); - } - - _dbUrl = self.Substring(0, self.LastIndexOf("/relationship")); - _self = self; - Id = relationshipId; - } - } - - #endregion - - #region Properties - - private void LoadProperties(bool refresh) - { - if (refresh) - { - _properties = null; - } - - if (_properties != null) - { - return; - } - - string response; - HttpStatusCode status = Neo4jRestApi.GetPropertiesOnRelationship(_dbUrl, Id, out response); - if (status != HttpStatusCode.OK) - { - throw new Exception(string.Format("Error retrieving properties on relationship (relationship id:{0} http response:{1})", Id, status)); - } - - _properties = Properties.ParseJson(response); - } - - public Properties Properties - { - get - { - LoadProperties(false); - return _properties; - } - } - - public void SaveProperties() - { - SaveProperties(Properties); - } - - public void SaveProperties(Properties properties) - { - HttpStatusCode status = Neo4jRestApi.SetPropertiesOnRelationship(_dbUrl, Id, properties.ToString()); - if (status != HttpStatusCode.NoContent) - { - throw new Exception(string.Format("Error setting properties on relationship (relationship id:{0} http response:{1})", Id, status)); - } - - LoadProperties(true); - } - - #endregion - - #region Index - - public static Relationship AddRelationshipToIndex(long relationshipId, string indexName, string key, object value) - { - return AddRelationshipToIndex(_defaultDbUrl, relationshipId, indexName, key, value); - } - - public static Relationship AddRelationshipToIndex(long relationshipId, Enum indexName, string key, object value) - { - return AddRelationshipToIndex(_defaultDbUrl, relationshipId, indexName.ToString(), key, value); - } - - public static Relationship AddRelationshipToIndex(long relationshipId, string indexName, Enum key, object value) - { - return AddRelationshipToIndex(_defaultDbUrl, relationshipId, indexName, key.ToString(), value); - } - - public static Relationship AddRelationshipToIndex(long relationshipId, Enum indexName, Enum key, object value) - { - return AddRelationshipToIndex(_defaultDbUrl, relationshipId, indexName.ToString(), key.ToString(), value); - } - - public static Relationship AddRelationshipToIndex(string dbUrl, long relationshipId, Enum indexName, Enum key, object value) - { - return AddRelationshipToIndex(dbUrl, relationshipId, indexName.ToString(), key.ToString(), value); - } - - public static Relationship AddRelationshipToIndex(string dbUrl, long relationshipId, string indexName, string key, object value) - { - string response; - var status = Neo4jRestApi.AddRelationshipToIndex(dbUrl, relationshipId, indexName, key, value, out response); - if (status != HttpStatusCode.Created) - { - throw new Exception(string.Format("Error creating index for relationship (http response:{0})", status)); - } - - return InitializeFromRelationshipJson(response); - } - - public HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, string indexName) - { - return RemoveRelationshipFromIndex(_defaultDbUrl, relationshipId, indexName); - } - - public HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, Enum indexName) - { - return RemoveRelationshipFromIndex(_defaultDbUrl, relationshipId, indexName.ToString()); - } - - public HttpStatusCode RemoveRelationshipFromIndex(string dbUrl, long relationshipId, Enum indexName) - { - return RemoveRelationshipFromIndex(dbUrl, relationshipId, indexName.ToString()); - } - - public HttpStatusCode RemoveRelationshipFromIndex(string dbUrl, long relationshipId, string indexName) - { - var status = Neo4jRestApi.RemoveRelationshipFromIndex(dbUrl, relationshipId, indexName); - if (status != HttpStatusCode.NoContent) - { - throw new Exception(string.Format("Error remove relationship from index (relationship id:{0} index name:{1} http response:{2})", relationshipId, indexName, status)); - } - - return status; - } - - public HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, string indexName, string key) - { - return RemoveRelationshipFromIndex(_defaultDbUrl, relationshipId, indexName, key); - } - - public HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, Enum indexName, string key) - { - return RemoveRelationshipFromIndex(_defaultDbUrl, relationshipId, indexName.ToString(), key); - } - - public HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, string indexName, Enum key) - { - return RemoveRelationshipFromIndex(_defaultDbUrl, relationshipId, indexName, key.ToString()); - } - - public HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, Enum indexName, Enum key) - { - return RemoveRelationshipFromIndex(_defaultDbUrl, relationshipId, indexName.ToString(), key.ToString()); - } - - public HttpStatusCode RemoveRelationshipFromIndex(string dbUrl, long relationshipId, Enum indexName, Enum key) - { - return RemoveRelationshipFromIndex(dbUrl, relationshipId, indexName.ToString(), key.ToString()); - } - - public HttpStatusCode RemoveRelationshipFromIndex(string dbUrl, long relationshipId, string indexName, string key) - { - var status = Neo4jRestApi.RemoveRelationshipFromIndex(dbUrl, relationshipId, indexName, key); - if (status != HttpStatusCode.NoContent) - { - throw new Exception(string.Format("Error remove relationship from index (relationship id:{0} index name:{1} http response:{2})", relationshipId, indexName, status)); - } - - return status; - } - - public HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, string indexName, string key, object value) - { - return RemoveRelationshipFromIndex(_defaultDbUrl, relationshipId, indexName, key, value); - } - - public HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, Enum indexName, string key, object value) - { - return RemoveRelationshipFromIndex(_defaultDbUrl, relationshipId, indexName.ToString(), key, value); - } - - public HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, string indexName, Enum key, object value) - { - return RemoveRelationshipFromIndex(_defaultDbUrl, relationshipId, indexName, key.ToString(), value); - } - - public HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, Enum indexName, Enum key, object value) - { - return RemoveRelationshipFromIndex(_defaultDbUrl, relationshipId, indexName.ToString(), key.ToString(), value); - } - - public HttpStatusCode RemoveRelationshipFromIndex(string dbUrl, long relationshipId, Enum indexName, Enum key, object value) - { - return RemoveRelationshipFromIndex(dbUrl, relationshipId, indexName.ToString(), key.ToString(), value); - } - - public HttpStatusCode RemoveRelationshipFromIndex(string dbUrl, long relationshipId, string indexName, string key, object value) - { - var status = Neo4jRestApi.RemoveRelationshipFromIndex(dbUrl, relationshipId, indexName, key, value); - if (status != HttpStatusCode.NoContent) - { - throw new Exception(string.Format("Error remove relationship from index (relationship id:{0} index name:{1} http response:{2})", relationshipId, indexName, status)); - } - - return status; - } - #endregion - - #region Delete - - public HttpStatusCode DeleteRelationship() - { - HttpStatusCode status = Neo4jRestApi.DeleteRelationship(_dbUrl, Id); - if (status != HttpStatusCode.NoContent) - { - throw new Exception(string.Format("Error deleteing relationship (relationship id:{0} http response:{1})", Id, status)); - } - - return status; - } - - #endregion - - #region ParseJson - - public static IEnumerable ParseJson(string jsonRelationships) - { - if (String.IsNullOrEmpty(jsonRelationships)) - return null; - - var relationships = new List(); - - // The Json passed in can be a JObject or JArray - this is to test for that. - JObject jo = JObject.Parse(string.Concat("{\"root\":", jsonRelationships, "}")); - - switch (jo["root"].Type) - { - case JTokenType.Object: - relationships.Add(InitializeFromRelationshipJson(jo["root"].ToString(Formatting.None))); - break; - - case JTokenType.Array: - relationships.AddRange(from JObject jsonRelationship in jo["root"] select InitializeFromRelationshipJson(jsonRelationship)); - break; - - default: - throw new Exception("Invalid relationship json"); - } - - return relationships; - } - - #endregion - } -} diff --git a/Neo4jRestNet/Core/StoreFactory.cs b/Neo4jRestNet/Core/StoreFactory.cs deleted file mode 100644 index e69c447..0000000 --- a/Neo4jRestNet/Core/StoreFactory.cs +++ /dev/null @@ -1,35 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Configuration; -using System.Linq; -using System.Text; -using Neo4jRestNet.Rest; - -namespace Neo4jRestNet.Core -{ - - public enum DbStore - { - Default, - InMemory - } - public class StoreFactory - { - private static readonly string DefaultDbUrl = ConfigurationManager.ConnectionStrings["neo4j"].ConnectionString.TrimEnd('/'); - - public Neo4jRestApi Create(DbStore dbStore) - { - switch (dbStore) - { - case DbStore.Default: - return new Neo4jRestApi(DefaultDbUrl); - - case DbStore.InMemory: - throw new NotImplementedException(); - - default: - throw new NotImplementedException(); - } - } - } -} diff --git a/Neo4jRestNet/CypherPlugin/Cypher.cs b/Neo4jRestNet/CypherPlugin/Cypher.cs index da82453..630ed72 100644 --- a/Neo4jRestNet/CypherPlugin/Cypher.cs +++ b/Neo4jRestNet/CypherPlugin/Cypher.cs @@ -3,26 +3,27 @@ using System.Configuration; using System.Linq.Expressions; using System.Data; +using Neo4jRestNet.Core.Implementation; +using Neo4jRestNet.Core.Interface; using Newtonsoft.Json.Linq; using Newtonsoft.Json; -using Neo4jRestNet.Core; using System.Collections.ObjectModel; using System.Text; namespace Neo4jRestNet.CypherPlugin { - public class Cypher + public class Cypher : ICypher { private static readonly string DefaultDbUrl = ConfigurationManager.ConnectionStrings["neo4j"].ConnectionString.TrimEnd('/'); private static readonly string DefaultCypherExtensionPath = ConfigurationManager.ConnectionStrings["neo4jCypherExtension"].ConnectionString.TrimEnd('/'); - readonly List> _start = new List>(); - readonly List> _match = new List>(); - readonly List>> _where = new List>>(); - readonly List> _return = new List>(); - readonly List> _orderBy = new List>(); - String _skip = string.Empty; - String _limit = string.Empty; + private readonly List> _start = new List>(); + private readonly List> _match = new List>(); + private readonly List>> _where = new List>>(); + private readonly List> _return = new List>(); + private readonly List> _orderBy = new List>(); + private String _skip = string.Empty; + private String _limit = string.Empty; public DataTable Post() { @@ -60,17 +61,17 @@ public DataTable Post(string cypherUrl) dt.Columns.Add(jaColumns[colIndex].ToString(), returnTypes[colIndex]); } - if (returnTypes[colIndex] == typeof (Node)) + if (returnTypes[colIndex] == typeof (INode)) { - row.Add(jCol.Type == JTokenType.Null ? null : Node.InitializeFromNodeJson((JObject)jCol)); + row.Add(jCol.Type == JTokenType.Null ? null : new Node().InitializeFromNodeJson((JObject)jCol)); } - else if (returnTypes[colIndex] == typeof (Relationship)) + else if (returnTypes[colIndex] == typeof (IRelationship)) { - row.Add(jCol.Type == JTokenType.Null ? null : Relationship.InitializeFromRelationshipJson((JObject) jCol)); + row.Add(jCol.Type == JTokenType.Null ? null : new Relationship().InitializeFromRelationshipJson((JObject) jCol)); } - else if (returnTypes[colIndex] == typeof (Path)) + else if (returnTypes[colIndex] == typeof (IPath)) { - row.Add(jCol.Type == JTokenType.Null ? null : Path.ParseJson((JArray)jCol)); + row.Add(jCol.Type == JTokenType.Null ? null : new Path().ParseJson((JArray)jCol)); } else if (returnTypes[colIndex] == typeof(string)) { @@ -177,7 +178,7 @@ public string Query { var sbToString = new StringBuilder(); - string label = "START"; + var label = "START"; foreach (var s in _start) { sbToString.AppendFormat("{1}{0}", s.Invoke(new CypherStart()), label); diff --git a/Neo4jRestNet/CypherPlugin/CypherFactory.cs b/Neo4jRestNet/CypherPlugin/CypherFactory.cs new file mode 100644 index 0000000..a5ebe5f --- /dev/null +++ b/Neo4jRestNet/CypherPlugin/CypherFactory.cs @@ -0,0 +1,18 @@ +using System; +using Neo4jRestNet.GremlinPlugin; + +namespace Neo4jRestNet.CypherPlugin +{ + public class CypherFactory + { + public static ICypher CreateCypher() + { + return new Cypher(); + } + + public static ICypher CreateCypher(params object[] args) where TCypher : class, ICypher, new() + { + return (TCypher)Activator.CreateInstance(typeof(TCypher), args); + } + } +} diff --git a/Neo4jRestNet/CypherPlugin/CypherReturn.cs b/Neo4jRestNet/CypherPlugin/CypherReturn.cs index b1e0fe6..e4355e6 100644 --- a/Neo4jRestNet/CypherPlugin/CypherReturn.cs +++ b/Neo4jRestNet/CypherPlugin/CypherReturn.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System; using Neo4jRestNet.Core; +using Neo4jRestNet.Core.Interface; namespace Neo4jRestNet.CypherPlugin { @@ -20,7 +21,7 @@ public CypherReturn Node(string name) _isStringEmpty = false; - _returnTypes.Add(typeof(Node)); + _returnTypes.Add(typeof(INode)); return this; } @@ -51,7 +52,7 @@ public CypherReturn Relationship(string name) _isStringEmpty = false; - _returnTypes.Add(typeof(Relationship)); + _returnTypes.Add(typeof(IRelationship)); return this; } @@ -82,7 +83,7 @@ public CypherReturn Path(string name) _isStringEmpty = false; - _returnTypes.Add(typeof(Path)); + _returnTypes.Add(typeof(IPath)); return this; } diff --git a/Neo4jRestNet/CypherPlugin/ICypher.cs b/Neo4jRestNet/CypherPlugin/ICypher.cs new file mode 100644 index 0000000..4f36aa9 --- /dev/null +++ b/Neo4jRestNet/CypherPlugin/ICypher.cs @@ -0,0 +1,20 @@ +using System; +using System.Linq.Expressions; +using System.Data; + +namespace Neo4jRestNet.CypherPlugin +{ + public interface ICypher + { + DataTable Post(); + DataTable Post(string cypherUrl); + void Start(Func start); + void Match(Func match); + void Where(Expression> where); + void Return(Func cypherReturn); + void OrderBy(Func cypherOrderBy); + void Skip(int skip); + void Limit(int limit); + string Query { get; } + } +} diff --git a/Neo4jRestNet/GremlinPlugin/Gremlin.cs b/Neo4jRestNet/GremlinPlugin/Gremlin.cs index a8da9a3..8311f17 100644 --- a/Neo4jRestNet/GremlinPlugin/Gremlin.cs +++ b/Neo4jRestNet/GremlinPlugin/Gremlin.cs @@ -3,161 +3,90 @@ using System.Data; using System.Configuration; using System.Collections.Generic; +using Neo4jRestNet.Core.Interface; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Neo4jRestNet.Core; +using Neo4jRestNet.Core.Implementation; namespace Neo4jRestNet.GremlinPlugin { - public class Gremlin + public class Gremlin : IGremlin { private static readonly string DefaultDbUrl = ConfigurationManager.ConnectionStrings["neo4j"].ConnectionString.TrimEnd('/'); private static readonly string DefaultGremlinExtensionPath = ConfigurationManager.ConnectionStrings["neo4jGremlinExtension"].ConnectionString.TrimEnd('/'); - public static HttpStatusCode Post(GremlinScript script) - { - //return Post(string.Concat(DefaultDbUrl, DefaultGremlinExtensionPath), script); - - // Remove trailing / - var gremlinUrl = string.Concat(DefaultDbUrl, DefaultGremlinExtensionPath).TrimEnd('/'); - - var jo = new JObject { { "script", script.GetScript() } }; + private readonly string _gremlinUrl; - string response; - var status = Rest.HttpRest.Post(gremlinUrl, jo.ToString(Formatting.None), out response); - - return status; - } -/* - public static HttpStatusCode Post(string script) + public Gremlin() { - return Post(string.Concat(DefaultDbUrl, DefaultGremlinExtensionPath), script); + _gremlinUrl = string.Concat(DefaultDbUrl, DefaultGremlinExtensionPath).TrimEnd('/'); } -*/ -/* - public static HttpStatusCode Post(string gremlinUrl, GremlinScript script) + + public Gremlin(string connectionString) { - return Post(gremlinUrl, script.ToString()); + _gremlinUrl = string.Concat(ConfigurationManager.ConnectionStrings[connectionString].ConnectionString.TrimEnd('/'), DefaultGremlinExtensionPath).TrimEnd('/'); } -*/ -/* - public static HttpStatusCode Post(string gremlinUrl, string script) - { - // Remove trailing / - gremlinUrl = gremlinUrl.TrimEnd('/'); - var jo = new JObject {{"script", script}}; + public HttpStatusCode Post(GremlinScript script) + { + var jo = new JObject { { "script", script.GetScript() } }; string response; - HttpStatusCode status = Rest.HttpRest.Post(gremlinUrl, jo.ToString(Formatting.None), out response); + var status = Rest.HttpRest.Post(_gremlinUrl, jo.ToString(Formatting.None), out response); return status; } -*/ -/* - public static IEnumerable Post(EncryptId startNodeId, string script) where T : IGraphObject - { - return Post(string.Concat(DefaultDbUrl, DefaultGremlinExtensionPath), string.Format("g.v({0}).{1}", (long)startNodeId, script)); - } -*/ -/* - public static IEnumerable Post(string script) where T : IGraphObject + + public IEnumerableGetNodes(GremlinScript script) { - return Post(string.Concat(DefaultDbUrl, DefaultGremlinExtensionPath), script); + string response; + var status = Rest.HttpRest.Post(_gremlinUrl, script.GetScript(), out response); + return GraphFactory.CreateNode().ParseJson(response); } -*/ - public static IEnumerable Post(GremlinScript script) where T : IGraphObject + + public IEnumerable GetNodes(GremlinScript script) where T : class, INode, new() { - return Post(string.Concat(DefaultDbUrl, DefaultGremlinExtensionPath), script); + string response; + var status = Rest.HttpRest.Post(_gremlinUrl, script.GetScript(), out response); + return GraphFactory.CreateNode().ParseJson(response); } - public static IEnumerable Post(string gremlinUrl, GremlinScript script) where T : IGraphObject + public IEnumerable GetRelationships(GremlinScript script) { -// return Post(gremlinUrl, script.ToString()); - - // Remove trailing / - gremlinUrl = gremlinUrl.TrimEnd('/'); - - var typeParameterType = typeof(T); - string response; - var status = Rest.HttpRest.Post(gremlinUrl, script.GetScript(), out response); - - if (typeParameterType == typeof(Node)) - { - return (IEnumerable)Node.ParseJson(response); - } - - if (typeParameterType == typeof(Relationship)) - { - return (IEnumerable)Relationship.ParseJson(response); - } - - if (typeParameterType == typeof(Path)) - { - return (IEnumerable)Path.ParseJson(response); - } - - throw new Exception("Return type " + typeParameterType + " not implemented"); - - + var status = Rest.HttpRest.Post(_gremlinUrl, script.GetScript(), out response); + return GraphFactory.CreateRelationship().ParseJson(response); } -/* - public static IEnumerable Post(string gremlinUrl, string script) where T : IGraphObject - { - // Remove trailing / - gremlinUrl = gremlinUrl.TrimEnd('/'); - - var typeParameterType = typeof(T); - - var jo = new JObject {{"script", script}}; - - string response; - HttpStatusCode status = Rest.HttpRest.Post(gremlinUrl, jo.ToString(Formatting.None), out response); - - if (typeParameterType == typeof(Node)) - { - return (IEnumerable)Node.ParseJson(response); - } - - if (typeParameterType == typeof(Relationship)) - { - return (IEnumerable)Relationship.ParseJson(response); - } - - if (typeParameterType == typeof(Path)) - { - return (IEnumerable)Path.ParseJson(response); - } - throw new Exception("Return type " + typeParameterType.ToString() + " not implemented"); - } -*/ - public static DataTable GetTable(string script) + public IEnumerable GetRelationships(GremlinScript script) where T : class, IRelationship, new() { - return GetTable(string.Concat(DefaultDbUrl, DefaultGremlinExtensionPath), script); + string response; + var status = Rest.HttpRest.Post(_gremlinUrl, script.GetScript(), out response); + return GraphFactory.CreateRelationship().ParseJson(response); } - public static DataTable GetTable(GremlinScript script) + public IEnumerable GetPaths(GremlinScript script) { - return GetTable(string.Concat(DefaultDbUrl, DefaultGremlinExtensionPath), script.ToString()); + string response; + var status = Rest.HttpRest.Post(_gremlinUrl, script.GetScript(), out response); + return GraphFactory.CreatePath().ParseJson(response); } - public static DataTable GetTable(string gremlinUrl, GremlinScript script) + public IEnumerable GetPaths(GremlinScript script) where T : class, IPath, new() { - return GetTable(gremlinUrl, script.ToString()); + string response; + var status = Rest.HttpRest.Post(_gremlinUrl, script.GetScript(), out response); + return GraphFactory.CreatePath().ParseJson(response); } - public static DataTable GetTable(string gremlinUrl, string script) + public DataTable GetTable(GremlinScript script) { - // Remove trailing / - gremlinUrl = gremlinUrl.TrimEnd('/'); - - var joScript = new JObject {{"script", script}}; + string response; - HttpStatusCode status = Rest.HttpRest.Post(gremlinUrl, joScript.ToString(Formatting.None), out response); + var status = Rest.HttpRest.Post(_gremlinUrl, script.GetScript(), out response); var joResponse = JObject.Parse(response); var jaColumns =(JArray)joResponse["columns"]; @@ -196,21 +125,21 @@ public static DataTable GetTable(string gremlinUrl, string script) } else { - string self = jCol["self"].ToString(); - string[] selfArray = self.Split('/'); + var self = jCol["self"].ToString(); + var selfArray = self.Split('/'); if (selfArray.Length > 2 && selfArray[selfArray.Length - 2] == "node" ) { - row.Add(Node.InitializeFromNodeJson((JObject)jCol)); + row.Add(new Node().InitializeFromNodeJson((JObject)jCol)); if (initColumns) { - dt.Columns.Add(jaColumns[colIndex].ToString(), typeof(Node)); + dt.Columns.Add(jaColumns[colIndex].ToString(), typeof(INode)); colIndex++; } } else if (selfArray.Length > 2 && selfArray[selfArray.Length - 2] == "relationship") { - row.Add(Relationship.InitializeFromRelationshipJson((JObject)jCol)); + row.Add(new Relationship().InitializeFromRelationshipJson((JObject)jCol)); if (initColumns) { @@ -281,7 +210,7 @@ public static DataTable GetTable(string gremlinUrl, string script) } initColumns = false; - DataRow dtRow = dt.NewRow(); + var dtRow = dt.NewRow(); dtRow.ItemArray = row.ToArray(); dt.Rows.Add(dtRow); } diff --git a/Neo4jRestNet/GremlinPlugin/GremlinFactory.cs b/Neo4jRestNet/GremlinPlugin/GremlinFactory.cs new file mode 100644 index 0000000..273bea2 --- /dev/null +++ b/Neo4jRestNet/GremlinPlugin/GremlinFactory.cs @@ -0,0 +1,17 @@ +using System; + +namespace Neo4jRestNet.GremlinPlugin +{ + public class GremlinFactory + { + public static IGremlin CreateGremlin() + { + return new Gremlin(); + } + + public static IGremlin CreateGremlin(params object[] args) where TGremlin : class, IGremlin, new() + { + return (TGremlin)Activator.CreateInstance(typeof(TGremlin), args); + } + } +} diff --git a/Neo4jRestNet/GremlinPlugin/GremlinScript.cs b/Neo4jRestNet/GremlinPlugin/GremlinScript.cs index dc9d5a8..98a47df 100644 --- a/Neo4jRestNet/GremlinPlugin/GremlinScript.cs +++ b/Neo4jRestNet/GremlinPlugin/GremlinScript.cs @@ -2,6 +2,7 @@ using System.Text; using Neo4jRestNet.Core; using System.Collections.Generic; +using Neo4jRestNet.Core.Interface; using Newtonsoft.Json; using Newtonsoft.Json.Linq; @@ -16,13 +17,13 @@ public GremlinScript() { } - public GremlinScript(Node node) + public GremlinScript(INode node) { Append("g.v({0})", node.Id); // _sb.AppendFormat("g.v({0})", node.Id); } - public GremlinScript(Relationship relationship) + public GremlinScript(IRelationship relationship) { Append("g.e({0})", relationship.Id); //_sb.AppendFormat("g.e({0})", relationship.Id); @@ -69,12 +70,12 @@ public GremlinScript Append(string format, params object[] parameters) return this; } -/* + public override string ToString() { return _sb.ToString(); } -*/ + public string GetScript() { var joScript = new JObject{{"script", _sb.ToString()}}; diff --git a/Neo4jRestNet/GremlinPlugin/GremlinScriptCommands.cs b/Neo4jRestNet/GremlinPlugin/GremlinScriptCommands.cs index 78ce1b7..4d462d6 100644 --- a/Neo4jRestNet/GremlinPlugin/GremlinScriptCommands.cs +++ b/Neo4jRestNet/GremlinPlugin/GremlinScriptCommands.cs @@ -4,6 +4,7 @@ using System.Text; using Neo4jRestNet.Core; using System.Linq.Expressions; +using Neo4jRestNet.Core.Interface; namespace Neo4jRestNet.GremlinPlugin { @@ -11,12 +12,12 @@ public static class GremlinScriptCommands { #region g() - public static GremlinScript g(this GremlinScript query, Node node) + public static GremlinScript g(this GremlinScript query, INode node) { return query.Append("g.v({0})", node.Id); } - public static GremlinScript g(this GremlinScript query, Relationship relationship) + public static GremlinScript g(this GremlinScript query, IRelationship relationship) { return query.Append("g.e({0})", relationship.Id); } @@ -26,7 +27,7 @@ public static GremlinScript gV(this GremlinScript query, long Id) return query.Append("g.v({0})", Id); } - public static GremlinScript gV(this GremlinScript query, Node node) + public static GremlinScript gV(this GremlinScript query, INode node) { return gV(query, node.Id); } @@ -36,7 +37,7 @@ public static GremlinScript gE(this GremlinScript query, long Id) return query.Append("g.e({0})", Id); } - public static GremlinScript gE(this GremlinScript query, Relationship relationship) + public static GremlinScript gE(this GremlinScript query, IRelationship relationship) { return gE(query, relationship.Id); } diff --git a/Neo4jRestNet/GremlinPlugin/IGremlin.cs b/Neo4jRestNet/GremlinPlugin/IGremlin.cs new file mode 100644 index 0000000..da32edf --- /dev/null +++ b/Neo4jRestNet/GremlinPlugin/IGremlin.cs @@ -0,0 +1,23 @@ +using System.Net; +using System.Data; +using System.Collections.Generic; +using Neo4jRestNet.Core; +using Neo4jRestNet.Core.Interface; + + +namespace Neo4jRestNet.GremlinPlugin +{ + public interface IGremlin + { + HttpStatusCode Post(GremlinScript script); + + IEnumerable GetNodes(GremlinScript script); + IEnumerable GetNodes(GremlinScript script) where T : class, INode, new(); + IEnumerable GetRelationships(GremlinScript script); + IEnumerable GetRelationships(GremlinScript script) where T : class, IRelationship, new(); + IEnumerable GetPaths(GremlinScript script); + IEnumerable GetPaths(GremlinScript script) where T : class, IPath, new(); + + DataTable GetTable(GremlinScript script); + } +} diff --git a/Neo4jRestNet/Neo4jRestNet.csproj b/Neo4jRestNet/Neo4jRestNet.csproj index b51844a..0128ab6 100644 --- a/Neo4jRestNet/Neo4jRestNet.csproj +++ b/Neo4jRestNet/Neo4jRestNet.csproj @@ -9,7 +9,7 @@ Library Properties Neo4jRestNet - Neo4Net + Neo4jRestNet v4.0 512 @@ -82,12 +82,18 @@ + + + + - + + - + + @@ -98,11 +104,13 @@ + + - + @@ -113,7 +121,6 @@ - diff --git a/Neo4jRestNet/Rest/Neo4jRestApi.cs b/Neo4jRestNet/Rest/Neo4jRestApi.cs index c9d2bee..c6ad78d 100644 --- a/Neo4jRestNet/Rest/Neo4jRestApi.cs +++ b/Neo4jRestNet/Rest/Neo4jRestApi.cs @@ -40,25 +40,25 @@ public HttpStatusCode GetPropertiesOnNode(long nodeId, out string response) return HttpRest.Get(string.Concat(_dbUrl, "/node/", nodeId.ToString(), "/properties"), out response); } - public static HttpStatusCode RemovePropertiesFromNode(string dbUrl, long nodeId) + public HttpStatusCode RemovePropertiesFromNode(long nodeId) { - return HttpRest.Delete(string.Concat(dbUrl, "/node/", nodeId.ToString(), "/properties")); + return HttpRest.Delete(string.Concat(_dbUrl, "/node/", nodeId.ToString(), "/properties")); } - public static HttpStatusCode SetPropertyOnNode(string dbUrl, long nodeId, string propertyName, object value) + public HttpStatusCode SetPropertyOnNode(long nodeId, string propertyName, object value) { - return HttpRest.Put(string.Concat(dbUrl, "/node/", nodeId.ToString(), "/properties/", propertyName), + return HttpRest.Put(string.Concat(_dbUrl, "/node/", nodeId.ToString(), "/properties/", propertyName), JToken.FromObject(value).ToString(Formatting.None)); } - public static HttpStatusCode GetPropertyOnNode(string dbUrl, long nodeId, string propertyName, out string response) + public HttpStatusCode GetPropertyOnNode(long nodeId, string propertyName, out string response) { - return HttpRest.Get(string.Concat(dbUrl, "/node/", nodeId.ToString(), "/properties/", propertyName), out response); + return HttpRest.Get(string.Concat(_dbUrl, "/node/", nodeId.ToString(), "/properties/", propertyName), out response); } - public static HttpStatusCode RemovePropertyFromNode(string dbUrl, long nodeId, string propertyName) + public HttpStatusCode RemovePropertyFromNode(long nodeId, string propertyName) { - return HttpRest.Delete(string.Concat(dbUrl, "/node/", nodeId.ToString(), "/properties/", propertyName)); + return HttpRest.Delete(string.Concat(_dbUrl, "/node/", nodeId.ToString(), "/properties/", propertyName)); } public HttpStatusCode DeleteNode(long nodeId) @@ -78,46 +78,46 @@ public HttpStatusCode CreateRelationship(long fromNodeId, string toNodeSelf, str return HttpRest.Post(string.Concat(_dbUrl, "/node/", fromNodeId.ToString(), "/relationships"), jo.ToString(Formatting.None), out response); } - public static HttpStatusCode SetPropertiesOnRelationship(string dbUrl, long relationshipId, string jsonProperties) + public HttpStatusCode SetPropertiesOnRelationship(string dbUrl, long relationshipId, string jsonProperties) { return HttpRest.Put(string.Concat(dbUrl, "/relationship/", relationshipId.ToString(), "/properties"), jsonProperties); } - public static HttpStatusCode GetPropertiesOnRelationship(string dbUrl, long relationshipId, out string response) + public HttpStatusCode GetPropertiesOnRelationship(string dbUrl, long relationshipId, out string response) { return HttpRest.Get(string.Concat(dbUrl, "/relationship/", relationshipId.ToString(), "/properties"), out response); } - public static HttpStatusCode RemovePropertiesFromRelationship(string dbUrl, long relationshipId) + public HttpStatusCode RemovePropertiesFromRelationship(long relationshipId) { - return HttpRest.Delete(string.Concat(dbUrl, "/node/", relationshipId.ToString(), "/properties")); + return HttpRest.Delete(string.Concat(_dbUrl, "/node/", relationshipId.ToString(), "/properties")); } - public static HttpStatusCode SetPropertyOnRelationship(string dbUrl, long relationshipId, string propertyName, object value) + public HttpStatusCode SetPropertyOnRelationship(long relationshipId, string propertyName, object value) { return HttpRest.Put( - string.Concat(dbUrl, "/relationship/", relationshipId.ToString(), "/properties/", propertyName), + string.Concat(_dbUrl, "/relationship/", relationshipId.ToString(), "/properties/", propertyName), JToken.FromObject(value).ToString()); } - public static HttpStatusCode GetPropertyOnRelationship(string dbUrl, long relationshipId, string propertyName, out string response) + public HttpStatusCode GetPropertyOnRelationship(long relationshipId, string propertyName, out string response) { return HttpRest.Get( - string.Concat(dbUrl, "/relationship/", relationshipId.ToString(), "/properties/", propertyName), + string.Concat(_dbUrl, "/relationship/", relationshipId.ToString(), "/properties/", propertyName), out response); } - public static HttpStatusCode RemovePropertyFromRelationship(string dbUrl, long relationshipId, string propertyName) + public HttpStatusCode RemovePropertyFromRelationship(long relationshipId, string propertyName) { - return HttpRest.Delete(string.Concat(dbUrl, "/relationship/", relationshipId.ToString(), "/properties/", + return HttpRest.Delete(string.Concat(_dbUrl, "/relationship/", relationshipId.ToString(), "/properties/", propertyName)); } - public static HttpStatusCode DeleteRelationship(string dbUrl, long relationshipId) + public HttpStatusCode DeleteRelationship(long relationshipId) { - return HttpRest.Delete(string.Concat(dbUrl, "/relationship/", relationshipId.ToString())); + return HttpRest.Delete(string.Concat(_dbUrl, "/relationship/", relationshipId.ToString())); } public HttpStatusCode GetRelationshipsOnNode(long nodeId, RelationshipDirection direction, IEnumerable relationships, out string response) @@ -127,7 +127,7 @@ public HttpStatusCode GetRelationshipsOnNode(long nodeId, RelationshipDirection direction = RelationshipDirection.All; } - if (relationships == null || relationships.Count() == 0) + if (relationships == null || !relationships.Any()) { return HttpRest.Get( @@ -141,39 +141,39 @@ public HttpStatusCode GetRelationshipsOnNode(long nodeId, RelationshipDirection string.Join("&", relationships)), out response); } - public static HttpStatusCode GetRelationshipTypes(string dbUrl, out string response) + public HttpStatusCode GetRelationshipTypes(out string response) { - return HttpRest.Get(string.Concat(dbUrl, "/relationship/types"), out response); + return HttpRest.Get(string.Concat(_dbUrl, "/relationship/types"), out response); } - public static HttpStatusCode CreateNodeIndex(string dbUrl, string indexName, string jsonConfig, out string response) + public HttpStatusCode CreateNodeIndex(string indexName, string jsonConfig, out string response) { var jo = new JObject { { "name", indexName }, { "config", jsonConfig } }; - return HttpRest.Post(string.Concat(dbUrl, "/index/node"), jo.ToString(Formatting.None), out response); + return HttpRest.Post(string.Concat(_dbUrl, "/index/node"), jo.ToString(Formatting.None), out response); } - public static HttpStatusCode CreateRelationshipIndex(string dbUrl, string indexName, string jsonConfig, out string response) + public HttpStatusCode CreateRelationshipIndex(string indexName, string jsonConfig, out string response) { var jo = new JObject { { "name", indexName }, { "config", jsonConfig } }; - return HttpRest.Post(string.Concat(dbUrl, "/index/relationship"), jo.ToString(Formatting.None), out response); + return HttpRest.Post(string.Concat(_dbUrl, "/index/relationship"), jo.ToString(Formatting.None), out response); } - public static HttpStatusCode DeleteNodeIndex(string dbUrl, string indexName) + public HttpStatusCode DeleteNodeIndex(string indexName) { - return HttpRest.Delete(string.Concat(dbUrl, "/index/node/", indexName)); + return HttpRest.Delete(string.Concat(_dbUrl, "/index/node/", indexName)); } - public static HttpStatusCode DeleteRelationshipIndex(string dbUrl, string indexName) + public HttpStatusCode DeleteRelationshipIndex(string indexName) { - return HttpRest.Delete(string.Concat(dbUrl, "/index/relationship/", indexName)); + return HttpRest.Delete(string.Concat(_dbUrl, "/index/relationship/", indexName)); } - public static HttpStatusCode ListNodeIndexes(string dbUrl, out string response) + public HttpStatusCode ListNodeIndexes(out string response) { - return HttpRest.Get(string.Concat(dbUrl, "/index/node"), out response); + return HttpRest.Get(string.Concat(_dbUrl, "/index/node"), out response); } - public static HttpStatusCode ListRelationshipIndexes(string dbUrl, out string response) + public HttpStatusCode ListRelationshipIndexes(string dbUrl, out string response) { return HttpRest.Get(string.Concat(dbUrl, "/index/relationship"), out response); } @@ -191,16 +191,16 @@ public HttpStatusCode AddNodeToIndex(string nodeSelf, string indexName, string k JToken.FromObject(obj).ToString(Formatting.None), out response); } - public static HttpStatusCode AddRelationshipToIndex(string dbUrl, long relationshipId, string indexName, string key, object value, out string response) + public HttpStatusCode AddRelationshipToIndex(long relationshipId, string indexName, string key, object value, out string response) { - var self = string.Concat(dbUrl, "/", relationshipId.ToString()); - return AddRelationshipToIndex(dbUrl, self, indexName, key, value, out response); + var self = string.Concat(_dbUrl, "/", relationshipId.ToString()); + return AddRelationshipToIndex(self, indexName, key, value, out response); } - public static HttpStatusCode AddRelationshipToIndex(string dbUrl, string relationshipself, string indexName, string key, object value, out string response) + public HttpStatusCode AddRelationshipToIndex(string relationshipself, string indexName, string key, object value, out string response) { var obj = new { value, uri = relationshipself, key }; - return HttpRest.Post(string.Concat(dbUrl, "/index/relationship/", indexName), + return HttpRest.Post(string.Concat(_dbUrl, "/index/relationship/", indexName), JToken.FromObject(obj).ToString(Formatting.None), out response); } @@ -221,21 +221,21 @@ public HttpStatusCode RemoveNodeFromIndex(long nodeId, string indexName) return HttpRest.Delete(string.Concat(_dbUrl, "/index/node/", indexName, "/", nodeId)); } - public static HttpStatusCode RemoveRelationshipFromIndex(string dbUrl, long relationshipId, string indexName, string key, object value) + public HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, string indexName, string key, object value) { return - HttpRest.Delete(string.Concat(dbUrl, "/index/relationship/", indexName, "/", key, "/", + HttpRest.Delete(string.Concat(_dbUrl, "/index/relationship/", indexName, "/", key, "/", JToken.FromObject(value).ToString(Formatting.None), "/", relationshipId)); } - public static HttpStatusCode RemoveRelationshipFromIndex(string dbUrl, long relationshipId, string indexName, string key) + public HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, string indexName, string key) { - return HttpRest.Delete(string.Concat(dbUrl, "/index/relationship/", indexName, "/", key, "/", relationshipId)); + return HttpRest.Delete(string.Concat(_dbUrl, "/index/relationship/", indexName, "/", key, "/", relationshipId)); } - public static HttpStatusCode RemoveRelationshipFromIndex(string dbUrl, long relationshipId, string indexName) + public HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, string indexName) { - return HttpRest.Delete(string.Concat(dbUrl, "/index/relationship/", indexName, "/", relationshipId)); + return HttpRest.Delete(string.Concat(_dbUrl, "/index/relationship/", indexName, "/", relationshipId)); } public HttpStatusCode GetNode(string indexName, string key, object value, out string response) @@ -248,14 +248,14 @@ public HttpStatusCode GetNode(string indexName, string searchQuery, out string r return HttpRest.Get(string.Concat(_dbUrl, "/index/node/", indexName, "?query=", searchQuery), out response); } - public static HttpStatusCode GetRelationship(string dbUrl, string indexName, string key, object value, out string response) + public HttpStatusCode GetRelationship(string indexName, string key, object value, out string response) { - return HttpRest.Get(string.Concat(dbUrl, "/index/relationship/", indexName, "/", key, "/", value.ToString()), out response); + return HttpRest.Get(string.Concat(_dbUrl, "/index/relationship/", indexName, "/", key, "/", value.ToString()), out response); } - public static HttpStatusCode GetRelationship(string dbUrl, string indexName, string searchQuery, out string response) + public HttpStatusCode GetRelationship(string indexName, string searchQuery, out string response) { - return HttpRest.Get(string.Concat(dbUrl, "/index/relationship/", indexName, "?query=", searchQuery), out response); + return HttpRest.Get(string.Concat(_dbUrl, "/index/relationship/", indexName, "?query=", searchQuery), out response); } public HttpStatusCode Traverse(long nodeId, Order order, Uniqueness uniqueness, @@ -294,11 +294,11 @@ public static HttpStatusCode GetRelationship(string dbUrl, string indexName, str jo.ToString(Formatting.None), out response); } - public static HttpStatusCode PathBetweenNodes(string dbUrl, long fromNodeId, long toNodeId, + public HttpStatusCode PathBetweenNodes(long fromNodeId, long toNodeId, IEnumerable relationships, int maxDepth, PathAlgorithm algorithm, bool returnAllPaths, out string response) { - var jo = new JObject { { "to", string.Concat(dbUrl, "/node/", toNodeId.ToString()) } }; + var jo = new JObject { { "to", string.Concat(_dbUrl, "/node/", toNodeId.ToString()) } }; var ja = new JArray(); foreach (var r in relationships) @@ -313,7 +313,7 @@ public static HttpStatusCode GetRelationship(string dbUrl, string indexName, str var commandPath = returnAllPaths ? "/paths" : "/path"; - return HttpRest.Post(string.Concat(dbUrl, "/node/", fromNodeId, commandPath), jo.ToString(Formatting.None), out response); + return HttpRest.Post(string.Concat(_dbUrl, "/node/", fromNodeId, commandPath), jo.ToString(Formatting.None), out response); } } } \ No newline at end of file diff --git a/Neo4jRestNetExamples/Examples.csproj b/Neo4jRestNetExamples/Examples.csproj index 612c2eb..0ad85d7 100644 --- a/Neo4jRestNetExamples/Examples.csproj +++ b/Neo4jRestNetExamples/Examples.csproj @@ -46,7 +46,9 @@ - + + Code + diff --git a/Neo4jRestNetExamples/Program.cs b/Neo4jRestNetExamples/Program.cs index 01b0941..98f3f9d 100644 --- a/Neo4jRestNetExamples/Program.cs +++ b/Neo4jRestNetExamples/Program.cs @@ -1,8 +1,10 @@ using System.Collections.Generic; +using System.Data; using Neo4jRestNet.Core; -using Neo4jRestNet.GremlinPlugin; +using Neo4jRestNet.Core.Implementation; +using Neo4jRestNet.Core.Interface; using Neo4jRestNet.CypherPlugin; -using System.Data; +using Neo4jRestNet.GremlinPlugin; namespace Example { @@ -39,18 +41,35 @@ public enum RelationshipType static void Main(string[] args) { - // Get Root Node from graphDB - var rootNode = Node.GetRootNode(); + // Get Root Node from graphDB - default implementation + var rootNode = GraphFactory.CreateNode().GetRootNode(); + + // Get Root Node from graphDB - using passedin implementation (using default implementation for tesing) + rootNode = GraphFactory.CreateNode().GetRootNode(); + - // Create a User Node with no Properties - var nodeUser = Node.CreateNode(NodeType.User.ToString()); - // Create a User Node with Properties + // Create a User Node with no Properties - default implementation + var nodeUser = GraphFactory.CreateNode().CreateNode(NodeType.User.ToString()); + + // Create a User Node with no Properties - using passedin implementation (using default implementation for tesing) + nodeUser = GraphFactory.CreateNode().CreateNode(NodeType.User.ToString()); + + + // Create a User Node with Properties - default implementation var prop = new Properties(); prop.SetProperty(NodeProperty.FirstName.ToString(), "Joe"); prop.SetProperty(NodeProperty.LastName.ToString(), "Smith"); - var nodeUserWithName = Node.CreateNode(NodeType.User.ToString(), prop); + var nodeUserWithName = GraphFactory.CreateNode().CreateNode(NodeType.User.ToString(), prop); + + // Create a User Node with Properties - using passedin implementation (using default implementation for tesing) + prop = new Properties(); + prop.SetProperty(NodeProperty.FirstName.ToString(), "Joe"); + prop.SetProperty(NodeProperty.LastName.ToString(), "Smith"); + + nodeUserWithName = GraphFactory.CreateNode().CreateNode(NodeType.User.ToString(), prop); + // Create Relationships to Nodes rootNode.CreateRelationshipTo(nodeUser, RelationshipType.Likes.ToString()); @@ -72,7 +91,9 @@ static void Main(string[] args) // Same as above - var sameLikeNodes = Gremlin.Post(new GremlinScript(rootNode).Out(RelationshipType.Likes.ToString())); + var sameLikeNodes = GremlinFactory.CreateGremlin().GetNodes(new GremlinScript(rootNode).Out(RelationshipType.Likes.ToString())); + + sameLikeNodes = GremlinFactory.CreateGremlin().GetNodes(new GremlinScript(rootNode).Out(RelationshipType.Likes.ToString())); // More Gremlin example var script = new GremlinScript(rootNode); @@ -81,7 +102,9 @@ static void Main(string[] args) .OutE() .Filter("it.getProperty('{0}') == '{1}'", RelationshipProperty.Name, "MyRelationship"); - var myRelationship = Gremlin.Post(script); + var myRelationship = GremlinFactory.CreateGremlin().GetRelationships(script); + + myRelationship = GremlinFactory.CreateGremlin().GetRelationships(script); // More Gremlin example var script1 = new GremlinScript(rootNode); @@ -90,7 +113,9 @@ static void Main(string[] args) .OutE() .Filter(it => it.GetProperty(RelationshipProperty.Name.ToString()) == "MyRelationship"); - IEnumerable myRelationship1 = Gremlin.Post(script1); + IEnumerable myRelationship1 = GremlinFactory.CreateGremlin().GetRelationships(script1); + + myRelationship1 = GremlinFactory.CreateGremlin().GetRelationships(script1); // Gremlin returning a datatable var tblScript = new GremlinScript(); @@ -101,17 +126,24 @@ static void Main(string[] args) .Table("t", "Like") .Append(" >> -1; t;"); - DataTable dt = Gremlin.GetTable(tblScript.ToString()); + DataTable dt = GremlinFactory.CreateGremlin().GetTable(tblScript); // Basic Cypher query - var c1 = new Cypher(); + var c1 = CypherFactory.CreateCypher(); c1.Start(s => s.Node("A", 0)); c1.Return( r => r.Node("A")); DataTable tbl = c1.Post(); + var c1i = CypherFactory.CreateCypher(); + c1i.Start(s => s.Node("A", 0)); + c1i.Return(r => r.Node("A")); + + tbl = c1i.Post(); + + // Cypher with Match clause - var c2 = new Cypher(); + var c2 = CypherFactory.CreateCypher(); c2.Start(s => s.Node("A", 0)); c2.Match(m => m.Node("A").To("r", "Likes").Node("B")); c2.Return(r => r.Node("A").Relationship("r").Node("B")); @@ -119,7 +151,7 @@ static void Main(string[] args) tbl = c2.Post(); // Cypher with multi start and return optional property - var c3 = new Cypher(); + var c3 = CypherFactory.CreateCypher(); c3.Start(s => s.Node("A", 0, 1)); c3.Match(m => m.Node("A").Any("r", "Likes").Node("C")); c3.Return(r => r.Node("C").Node("C").Property("Name?")); @@ -127,14 +159,14 @@ static void Main(string[] args) tbl = c3.Post(); // Multi Start - var c4 = new Cypher(); + var c4 = CypherFactory.CreateCypher(); c4.Start(s => s.Node("A", 0).Node("B",1)); c4.Return(r => r.Node("A").Node("B")); tbl = c4.Post(); // Cypher with Where clause - var c5 = new Cypher(); + var c5 = CypherFactory.CreateCypher(); c5.Start(s => s.Node("A", 0, 1)); c5.Where(w => w.Node("A").Property("Age?") < 30 && w.Node("A").Property("Name?") == "Tobias" || !(w.Node("A").Property("Name?") == "Tobias")); c5.Return(r => r.Node("A")); @@ -142,7 +174,7 @@ static void Main(string[] args) tbl = c5.Post(); // Alt Syntax - var c6 = new Cypher(); + var c6 = CypherFactory.CreateCypher(); c6.Start(s => { s.Node("A", 0); s.Node("B", 1); @@ -158,7 +190,7 @@ static void Main(string[] args) tbl = c6.Post(); // Alt Syntax - var c7 = new Cypher(); + var c7 = CypherFactory.CreateCypher(); c7.Start(s => s.Node("MyNode", "Index-Name", "QueryString")); c7.Start(s => s.Node("A", 0)); c7.Start(s => s.Node("B", 1)); @@ -168,6 +200,8 @@ static void Main(string[] args) tbl = c7.Post(); - } + + } + } }