Skip to content
Browse files

Merge branch 'master' of github.com:SepiaGroup/Neo4jRestNet

Conflicts:
	Neo4jRestNet/Core/Node.cs
	Neo4jRestNet/Core/Relationship.cs
	Neo4jRestNet/Core/StoreFactory.cs
	Neo4jRestNet/CypherPlugin/Cypher.cs
	Neo4jRestNet/GremlinPlugin/Gremlin.cs
	Neo4jRestNet/Neo4jRestNet.csproj
	Neo4jRestNet/Rest/Neo4jRestApi.cs
  • Loading branch information...
2 parents 63bccc6 + 58b3a9a commit 8352e5be66acf75b30767526c4fb57e72c903662 @SepiaGroup committed
View
6 Neo4jRestNet.Test/App.config
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
- <add name="neo4j" connectionString="http://localhost:7474/db/data/" />
- <add name="neo4jGremlinExtension" connectionString="/ext/GremlinPlugin/graphdb/execute_script/" />
- <add name="neo4jCypherExtension" connectionString="/ext/CypherPlugin/graphdb/execute_query/" />
+ <add name="neo4j" connectionString="http://localhost:7474/db/data/" providerName="Neo4j" />
+ <add name="neo4jGremlinExtension" connectionString="/ext/GremlinPlugin/graphdb/execute_script/" providerName="Neo4j"/>
+ <add name="neo4jCypherExtension" connectionString="/ext/CypherPlugin/graphdb/execute_query/" providerName="Neo4j"/>
</connectionStrings>
<appSettings>
View
9 Neo4jRestNet.Test/EncryptedIDTest.cs
@@ -5,6 +5,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo4jRestNet.Core;
using System.Security.Cryptography;
+using Neo4jRestNet.Core.Implementation;
using Neo4jRestNet.GremlinPlugin;
namespace Neo4jRestNet.Test
@@ -17,6 +18,14 @@ public class EncryptedIDTest
{
[TestMethod]
+ public void GetNode()
+ {
+ var node = Node.GetNode(0);
+
+ Assert.AreEqual(node.Id, 0);
+ }
+
+ [TestMethod]
public void NewGEIDFromLong()
{
EncryptId geid = new EncryptId(123);
View
2 Neo4jRestNet/Core/Implementation/Node.cs
@@ -639,7 +639,7 @@ public IEnumerable<INode> ParseJson(string jsonNodes)
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;
View
2 Neo4jRestNet/Core/Interface/INode.cs
@@ -74,7 +74,7 @@ public interface INode : IGraphObject
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<INode> ParseJson(string jsonNodes);
}
}
View
479 Neo4jRestNet/Core/Relationship.cs.BACKUP.928.cs
@@ -0,0 +1,479 @@
+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 const string DefaultConnectionName = "neo4j";
+ private string _selfDbUrl;
+ 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<Relationship> GetRelationship(string indexName, string key, object value)
+ {
+ return GetRelationship(DefaultConnectionName, indexName, key, value);
+ }
+
+ public static IEnumerable<Relationship> GetRelationship(Enum indexName, string key, object value)
+ {
+ return GetRelationship(DefaultConnectionName, indexName.ToString(), key, value);
+ }
+
+ public static IEnumerable<Relationship> GetRelationship(string indexName, Enum key, object value)
+ {
+ return GetRelationship(DefaultConnectionName, indexName, key.ToString(), value);
+ }
+
+ public static IEnumerable<Relationship> GetRelationship(Enum indexName, Enum key, object value)
+ {
+ return GetRelationship(DefaultConnectionName, indexName.ToString(), key.ToString(), value);
+ }
+
+ public static IEnumerable<Relationship> GetRelationship(string connectionName, Enum indexName, Enum key, object value)
+ {
+ return GetRelationship(connectionName, indexName.ToString(), key.ToString(), value);
+ }
+
+ public static IEnumerable<Relationship> GetRelationship(string connectionName, string indexName, string key, object value)
+ {
+ string 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));
+ }
+
+ return ParseJson(response);
+ }
+
+ public static IEnumerable<Relationship> GetRelationship(string indexName, string searchQuery)
+ {
+ return GetRelationship(DefaultConnectionName, indexName, searchQuery);
+ }
+
+ public static IEnumerable<Relationship> GetRelationship(Enum indexName, string searchQuery)
+ {
+ return GetRelationship(DefaultConnectionName, indexName.ToString(), searchQuery);
+ }
+
+ public static IEnumerable<Relationship> GetRelationship(string connectionName, Enum indexName, string searchQuery)
+ {
+ return GetRelationship(connectionName, indexName.ToString(), searchQuery);
+ }
+
+ public static IEnumerable<Relationship> GetRelationship(string connectionName, string indexName, string searchQuery)
+ {
+ string 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));
+ }
+
+ 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<string>();
+
+ 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<string>());
+ 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<string>());
+ 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<string>();
+
+ 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));
+ }
+
+ _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 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));
+ }
+
+ _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 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));
+ }
+
+ LoadProperties(true);
+ }
+
+ #endregion
+
+ #region Index
+
+ public static Relationship AddRelationshipToIndex(long relationshipId, string indexName, string key, object value)
+ {
+ return AddRelationshipToIndex(DefaultConnectionName, relationshipId, indexName, key, value);
+ }
+
+ public static Relationship AddRelationshipToIndex(long relationshipId, Enum indexName, string key, object value)
+ {
+ return AddRelationshipToIndex(DefaultConnectionName, relationshipId, indexName.ToString(), key, value);
+ }
+
+ public static Relationship AddRelationshipToIndex(long relationshipId, string indexName, Enum key, object value)
+ {
+ return AddRelationshipToIndex(DefaultConnectionName, relationshipId, indexName, key.ToString(), value);
+ }
+
+ public static Relationship AddRelationshipToIndex(long relationshipId, Enum indexName, Enum key, object value)
+ {
+ return AddRelationshipToIndex(DefaultConnectionName, relationshipId, indexName.ToString(), key.ToString(), value);
+ }
+
+ public static Relationship AddRelationshipToIndex(string connectionName, long relationshipId, Enum indexName, Enum key, object value)
+ {
+ return AddRelationshipToIndex(connectionName, relationshipId, indexName.ToString(), key.ToString(), value);
+ }
+
+ public static Relationship AddRelationshipToIndex(string connectionName, long relationshipId, string indexName, string key, object value)
+ {
+ string 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));
+ }
+
+ return InitializeFromRelationshipJson(response);
+ }
+
+ public HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, string indexName)
+ {
+ return RemoveRelationshipFromIndex(DefaultConnectionName, relationshipId, indexName);
+ }
+
+ public HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, Enum indexName)
+ {
+ return RemoveRelationshipFromIndex(DefaultConnectionName, relationshipId, indexName.ToString());
+ }
+
+ public HttpStatusCode RemoveRelationshipFromIndex(string connectionName, long relationshipId, Enum indexName)
+ {
+ return RemoveRelationshipFromIndex(connectionName, relationshipId, indexName.ToString());
+ }
+
+ public HttpStatusCode RemoveRelationshipFromIndex(string connectionName, long relationshipId, string 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));
+ }
+
+ return status;
+ }
+
+ public HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, string indexName, string key)
+ {
+ return RemoveRelationshipFromIndex(DefaultConnectionName, relationshipId, indexName, key);
+ }
+
+ public HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, Enum indexName, string key)
+ {
+ return RemoveRelationshipFromIndex(DefaultConnectionName, relationshipId, indexName.ToString(), key);
+ }
+
+ public HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, string indexName, Enum key)
+ {
+ return RemoveRelationshipFromIndex(DefaultConnectionName, relationshipId, indexName, key.ToString());
+ }
+
+ public HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, Enum indexName, Enum key)
+ {
+ return RemoveRelationshipFromIndex(DefaultConnectionName, relationshipId, indexName.ToString(), key.ToString());
+ }
+
+ public HttpStatusCode RemoveRelationshipFromIndex(string connectionName, long relationshipId, Enum indexName, Enum key)
+ {
+ return RemoveRelationshipFromIndex(connectionName, relationshipId, indexName.ToString(), key.ToString());
+ }
+
+ public HttpStatusCode RemoveRelationshipFromIndex(string connectionName, long relationshipId, string indexName, string 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));
+ }
+
+ return status;
+ }
+
+ public HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, string indexName, string key, object value)
+ {
+ return RemoveRelationshipFromIndex(DefaultConnectionName, relationshipId, indexName, key, value);
+ }
+
+ public HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, Enum indexName, string key, object value)
+ {
+ return RemoveRelationshipFromIndex(DefaultConnectionName, relationshipId, indexName.ToString(), key, value);
+ }
+
+ public HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, string indexName, Enum key, object value)
+ {
+ return RemoveRelationshipFromIndex(DefaultConnectionName, relationshipId, indexName, key.ToString(), value);
+ }
+
+ public HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, Enum indexName, Enum key, object value)
+ {
+ return RemoveRelationshipFromIndex(DefaultConnectionName, relationshipId, indexName.ToString(), key.ToString(), value);
+ }
+
+ public HttpStatusCode RemoveRelationshipFromIndex(string connectionName, long relationshipId, Enum indexName, Enum key, object value)
+ {
+ return RemoveRelationshipFromIndex(connectionName, relationshipId, indexName.ToString(), key.ToString(), value);
+ }
+
+ public HttpStatusCode RemoveRelationshipFromIndex(string connectionName, long relationshipId, string indexName, string key, object 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));
+ }
+
+ return status;
+ }
+ #endregion
+
+ #region Delete
+
+ public HttpStatusCode DeleteRelationship()
+ {
+ 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));
+ }
+
+ return status;
+ }
+
+ #endregion
+
+ #region ParseJson
+
+ public static IEnumerable<Relationship> ParseJson(string jsonRelationships)
+ {
+ if (String.IsNullOrEmpty(jsonRelationships))
+ return null;
+
+ var relationships = new List<Relationship>();
+
+ // 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
+ }
+}
View
480 Neo4jRestNet/Core/Relationship.cs.BASE.928.cs
@@ -0,0 +1,480 @@
+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<Relationship> GetRelationship(string indexName, string key, object value)
+ {
+ return GetRelationship(_defaultDbUrl, indexName, key, value);
+ }
+
+ public static IEnumerable<Relationship> GetRelationship(Enum indexName, string key, object value)
+ {
+ return GetRelationship(_defaultDbUrl, indexName.ToString(), key, value);
+ }
+
+ public static IEnumerable<Relationship> GetRelationship(string indexName, Enum key, object value)
+ {
+ return GetRelationship(_defaultDbUrl, indexName, key.ToString(), value);
+ }
+
+ public static IEnumerable<Relationship> GetRelationship(Enum indexName, Enum key, object value)
+ {
+ return GetRelationship(_defaultDbUrl, indexName.ToString(), key.ToString(), value);
+ }
+
+ public static IEnumerable<Relationship> GetRelationship(string dbUrl, Enum indexName, Enum key, object value)
+ {
+ return GetRelationship(dbUrl, indexName.ToString(), key.ToString(), value);
+ }
+
+ public static IEnumerable<Relationship> 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<Relationship> GetRelationship(string indexName, string searchQuery)
+ {
+ return GetRelationship(_defaultDbUrl, indexName, searchQuery);
+ }
+
+ public static IEnumerable<Relationship> GetRelationship(Enum indexName, string searchQuery)
+ {
+ return GetRelationship(_defaultDbUrl, indexName.ToString(), searchQuery);
+ }
+
+ public static IEnumerable<Relationship> GetRelationship(string dbUrl, Enum indexName, string searchQuery)
+ {
+ return GetRelationship(dbUrl, indexName.ToString(), searchQuery);
+ }
+
+ public static IEnumerable<Relationship> 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<string>();
+
+ 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<string>());
+ 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<string>());
+ 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<string>();
+
+ 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<Relationship> ParseJson(string jsonRelationships)
+ {
+ if (String.IsNullOrEmpty(jsonRelationships))
+ return null;
+
+ var relationships = new List<Relationship>();
+
+ // 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
+ }
+}
View
479 Neo4jRestNet/Core/Relationship.cs.REMOTE.928.cs
@@ -0,0 +1,479 @@
+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 const string DefaultConnectionName = "neo4j";
+ private string _selfDbUrl;
+ 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<Relationship> GetRelationship(string indexName, string key, object value)
+ {
+ return GetRelationship(DefaultConnectionName, indexName, key, value);
+ }
+
+ public static IEnumerable<Relationship> GetRelationship(Enum indexName, string key, object value)
+ {
+ return GetRelationship(DefaultConnectionName, indexName.ToString(), key, value);
+ }
+
+ public static IEnumerable<Relationship> GetRelationship(string indexName, Enum key, object value)
+ {
+ return GetRelationship(DefaultConnectionName, indexName, key.ToString(), value);
+ }
+
+ public static IEnumerable<Relationship> GetRelationship(Enum indexName, Enum key, object value)
+ {
+ return GetRelationship(DefaultConnectionName, indexName.ToString(), key.ToString(), value);
+ }
+
+ public static IEnumerable<Relationship> GetRelationship(string connectionName, Enum indexName, Enum key, object value)
+ {
+ return GetRelationship(connectionName, indexName.ToString(), key.ToString(), value);
+ }
+
+ public static IEnumerable<Relationship> GetRelationship(string connectionName, string indexName, string key, object value)
+ {
+ string 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));
+ }
+
+ return ParseJson(response);
+ }
+
+ public static IEnumerable<Relationship> GetRelationship(string indexName, string searchQuery)
+ {
+ return GetRelationship(DefaultConnectionName, indexName, searchQuery);
+ }
+
+ public static IEnumerable<Relationship> GetRelationship(Enum indexName, string searchQuery)
+ {
+ return GetRelationship(DefaultConnectionName, indexName.ToString(), searchQuery);
+ }
+
+ public static IEnumerable<Relationship> GetRelationship(string connectionName, Enum indexName, string searchQuery)
+ {
+ return GetRelationship(connectionName, indexName.ToString(), searchQuery);
+ }
+
+ public static IEnumerable<Relationship> GetRelationship(string connectionName, string indexName, string searchQuery)
+ {
+ string 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));
+ }
+
+ 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<string>();
+
+ 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<string>());
+ 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<string>());
+ 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<string>();
+
+ 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));
+ }
+
+ _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 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));
+ }
+
+ _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 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));
+ }
+
+ LoadProperties(true);
+ }
+
+ #endregion
+
+ #region Index
+
+ public static Relationship AddRelationshipToIndex(long relationshipId, string indexName, string key, object value)
+ {
+ return AddRelationshipToIndex(DefaultConnectionName, relationshipId, indexName, key, value);
+ }
+
+ public static Relationship AddRelationshipToIndex(long relationshipId, Enum indexName, string key, object value)
+ {
+ return AddRelationshipToIndex(DefaultConnectionName, relationshipId, indexName.ToString(), key, value);
+ }
+
+ public static Relationship AddRelationshipToIndex(long relationshipId, string indexName, Enum key, object value)
+ {
+ return AddRelationshipToIndex(DefaultConnectionName, relationshipId, indexName, key.ToString(), value);
+ }
+
+ public static Relationship AddRelationshipToIndex(long relationshipId, Enum indexName, Enum key, object value)
+ {
+ return AddRelationshipToIndex(DefaultConnectionName, relationshipId, indexName.ToString(), key.ToString(), value);
+ }
+
+ public static Relationship AddRelationshipToIndex(string connectionName, long relationshipId, Enum indexName, Enum key, object value)
+ {
+ return AddRelationshipToIndex(connectionName, relationshipId, indexName.ToString(), key.ToString(), value);
+ }
+
+ public static Relationship AddRelationshipToIndex(string connectionName, long relationshipId, string indexName, string key, object value)
+ {
+ string 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));
+ }
+
+ return InitializeFromRelationshipJson(response);
+ }
+
+ public HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, string indexName)
+ {
+ return RemoveRelationshipFromIndex(DefaultConnectionName, relationshipId, indexName);
+ }
+
+ public HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, Enum indexName)
+ {
+ return RemoveRelationshipFromIndex(DefaultConnectionName, relationshipId, indexName.ToString());
+ }
+
+ public HttpStatusCode RemoveRelationshipFromIndex(string connectionName, long relationshipId, Enum indexName)
+ {
+ return RemoveRelationshipFromIndex(connectionName, relationshipId, indexName.ToString());
+ }
+
+ public HttpStatusCode RemoveRelationshipFromIndex(string connectionName, long relationshipId, string 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));
+ }
+
+ return status;
+ }
+
+ public HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, string indexName, string key)
+ {
+ return RemoveRelationshipFromIndex(DefaultConnectionName, relationshipId, indexName, key);
+ }
+
+ public HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, Enum indexName, string key)
+ {
+ return RemoveRelationshipFromIndex(DefaultConnectionName, relationshipId, indexName.ToString(), key);
+ }
+
+ public HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, string indexName, Enum key)
+ {
+ return RemoveRelationshipFromIndex(DefaultConnectionName, relationshipId, indexName, key.ToString());
+ }
+
+ public HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, Enum indexName, Enum key)
+ {
+ return RemoveRelationshipFromIndex(DefaultConnectionName, relationshipId, indexName.ToString(), key.ToString());
+ }
+
+ public HttpStatusCode RemoveRelationshipFromIndex(string connectionName, long relationshipId, Enum indexName, Enum key)
+ {
+ return RemoveRelationshipFromIndex(connectionName, relationshipId, indexName.ToString(), key.ToString());
+ }
+
+ public HttpStatusCode RemoveRelationshipFromIndex(string connectionName, long relationshipId, string indexName, string 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));
+ }
+
+ return status;
+ }
+
+ public HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, string indexName, string key, object value)
+ {
+ return RemoveRelationshipFromIndex(DefaultConnectionName, relationshipId, indexName, key, value);
+ }
+
+ public HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, Enum indexName, string key, object value)
+ {
+ return RemoveRelationshipFromIndex(DefaultConnectionName, relationshipId, indexName.ToString(), key, value);
+ }
+
+ public HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, string indexName, Enum key, object value)
+ {
+ return RemoveRelationshipFromIndex(DefaultConnectionName, relationshipId, indexName, key.ToString(), value);
+ }
+
+ public HttpStatusCode RemoveRelationshipFromIndex(long relationshipId, Enum indexName, Enum key, object value)
+ {
+ return RemoveRelationshipFromIndex(DefaultConnectionName, relationshipId, indexName.ToString(), key.ToString(), value);
+ }
+
+ public HttpStatusCode RemoveRelationshipFromIndex(string connectionName, long relationshipId, Enum indexName, Enum key, object value)
+ {
+ return RemoveRelationshipFromIndex(connectionName, relationshipId, indexName.ToString(), key.ToString(), value);
+ }
+
+ public HttpStatusCode RemoveRelationshipFromIndex(string connectionName, long relationshipId, string indexName, string key, object 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));
+ }
+
+ return status;
+ }
+ #endregion
+
+ #region Delete
+
+ public HttpStatusCode DeleteRelationship()
+ {
+ 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));
+ }
+
+ return status;
+ }
+
+ #endregion
+
+ #region ParseJson
+
+ public static IEnumerable<Relationship> ParseJson(string jsonRelationships)
+ {
+ if (String.IsNullOrEmpty(jsonRelationships))
+ return null;
+
+ var relationships = new List<Relationship>();
+
+ // 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
+ }
+}
View
236 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<Func<CypherStart, object>> _start = new List<Func<CypherStart, object>>();
+ readonly List<Func<CypherMatch, object>> _match = new List<Func<CypherMatch, object>>();
+ readonly List<Expression<Func<CypherWhere, object>>> _where = new List<Expression<Func<CypherWhere, object>>>();
+ readonly List<Func<CypherReturn, object>> _return = new List<Func<CypherReturn, object>>();
+ readonly List<Func<CypherOrderBy, object>> _orderBy = new List<Func<CypherOrderBy, object>>();
+ 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<object>();
+
+ 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<CypherStart, object> start)
+ {
+ _start.Add(start);
+ }
+
+ public void Match(Func<CypherMatch, object> match)
+ {
+ _match.Add(match);
+ }
+
+ public void Where(Expression<Func<CypherWhere, object>> where)
+ {
+ _where.Add(where);
+ }
+
+ public void Return(Func<CypherReturn, object> cypherReturn)
+ {
+ _return.Add(cypherReturn);
+ }
+
+ public void OrderBy(Func<CypherOrderBy, object> 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<Type> GetReturnTypes
+ {
+ get
+ {
+ var returnTypes = new List<Type>();
+
+ 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();
+ }
+ }
+ }
+}
View
2 Neo4jRestNet/Neo4jRestNet.csproj
@@ -157,4 +157,4 @@
<Target Name="AfterBuild">
</Target>
-->
-</Project>
+</Project>
View
7 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;
}
View
59 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<string> 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<TraverseRelationship> relationships,
+ PruneEvaluator pruneEvaluator, ReturnFilter returnFilter, int? maxDepth,
+ ReturnType returnType, out string response);
+
+ HttpStatusCode PathBetweenNodes(string dbUrl, long fromNodeId, long toNodeId,
+ IEnumerable<TraverseRelationship> relationships, int maxDepth,
+ PathAlgorithm algorithm, bool returnAllPaths, out string response);
+ }
+}
View
2 Neo4jRestNet/Rest/Neo4jRestApi.cs
@@ -316,4 +316,4 @@ public HttpStatusCode GetRelationship(string indexName, string searchQuery, out
return HttpRest.Post(string.Concat(_dbUrl, "/node/", fromNodeId, commandPath), jo.ToString(Formatting.None), out response);
}
}
-}
+}
View
6 Neo4jRestNetExamples/Program.cs
@@ -60,7 +60,7 @@ static void Main(string[] args)
var prop = new Properties();
prop.SetProperty(NodeProperty.FirstName.ToString(), "Joe");
prop.SetProperty(NodeProperty.LastName.ToString(), "Smith");
-
+
var nodeUserWithName = GraphFactory.CreateNode().CreateNode(NodeType.User.ToString(), prop);
// Create a User Node with Properties - using passedin implementation (using default implementation for tesing)
@@ -188,7 +188,7 @@ static void Main(string[] args)
});
tbl = c6.Post();
-
+/*
// Alt Syntax
var c7 = CypherFactory.CreateCypher();
c7.Start(s => s.Node("MyNode", "Index-Name", "QueryString"));
@@ -199,7 +199,7 @@ static void Main(string[] args)
c7.Return(r => r.Node("B"));
tbl = c7.Post();
-
+*/
}

0 comments on commit 8352e5b

Please sign in to comment.
Something went wrong with that request. Please try again.