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..60b8b9b 100644 --- a/Neo4jRestNet.Test/EncryptedIDTest.cs +++ b/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 @@ -16,6 +17,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/Implementation/Node.cs b/Neo4jRestNet/Core/Implementation/Node.cs deleted file mode 100644 index 55f5e43..0000000 --- a/Neo4jRestNet/Core/Implementation/Node.cs +++ /dev/null @@ -1,703 +0,0 @@ -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/Implementation/Path.cs b/Neo4jRestNet/Core/Implementation/Path.cs deleted file mode 100644 index 63634f7..0000000 --- a/Neo4jRestNet/Core/Implementation/Path.cs +++ /dev/null @@ -1,140 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using Neo4jRestNet.Core.Interface; -using Newtonsoft.Json.Linq; -using Newtonsoft.Json; - -namespace Neo4jRestNet.Core.Implementation -{ - public class Path : IPath - { - public string Self { get; 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 () - { - - } - - public Path(JObject path) - { - JToken startNode; - if (!path.TryGetValue("start", out startNode)) - { - throw new Exception("Invalid path json"); - } - - switch (startNode.Type) - { - case JTokenType.String: - StartNode = new Node().InitializeFromSelf(startNode.Value()); - break; - - case JTokenType.Object: - StartNode = new Node().InitializeFromNodeJson((JObject)startNode); - break; - - default: - throw new Exception("Invalid path json"); - } - - JToken endNode; - if (!path.TryGetValue("end", out endNode)) - { - throw new Exception("Invalid path json"); - } - - switch (endNode.Type) - { - case JTokenType.String: - EndNode = new Node().InitializeFromSelf(endNode.Value()); - break; - - case JTokenType.Object: - EndNode = new Node().InitializeFromNodeJson((JObject)endNode); - break; - - default: - throw new Exception("Invalid path json"); - } - - Nodes = new List(); - JToken nodes; - if (!path.TryGetValue("nodes", out nodes) || nodes.Type != JTokenType.Array) - { - throw new Exception("Invalid path json"); - } - - foreach (JToken node in nodes) - { - switch (node.Type) - { - case JTokenType.String: - Nodes.Add(new Node().InitializeFromSelf(node.Value())); - break; - - case JTokenType.Object: - Nodes.Add(new Node().InitializeFromNodeJson((JObject)node)); - break; - - default: - throw new Exception("Invalid path json"); - } - } - - Relationships = new List(); - JToken relationships; - if (!path.TryGetValue("relationships", out relationships) || relationships.Type != JTokenType.Array) - { - throw new Exception("Invalid path json"); - } - - foreach (var relationship in relationships) - { - switch (relationship.Type) - { - case JTokenType.String: - Relationships.Add(new Relationship().InitializeFromSelf(relationship.Value())); - break; - - case JTokenType.Object: - Relationships.Add(new Relationship().InitializeFromRelationshipJson((JObject)relationship)); - break; - - default: - throw new Exception("Invalid path json"); - } - } - - OriginalPathJson = path.ToString(Formatting.None); - } - - public List ParseJson(string jsonPaths) - { - if (String.IsNullOrEmpty(jsonPaths)) - { - return null; - } - - var jaPaths = JArray.Parse(jsonPaths); - return ParseJson(jaPaths); - } - - public List ParseJson(JArray jsonPaths) - { - if (jsonPaths == null) - { - return null; - } - - var jaPaths = jsonPaths; - - 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 deleted file mode 100644 index 17f5188..0000000 --- a/Neo4jRestNet/Core/Implementation/Relationship.cs +++ /dev/null @@ -1,443 +0,0 @@ -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 deleted file mode 100644 index bc74fbe..0000000 --- a/Neo4jRestNet/Core/Interface/INode.cs +++ /dev/null @@ -1,80 +0,0 @@ -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 deleted file mode 100644 index ab490a6..0000000 --- a/Neo4jRestNet/Core/Interface/IPath.cs +++ /dev/null @@ -1,21 +0,0 @@ -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 deleted file mode 100644 index b040d06..0000000 --- a/Neo4jRestNet/Core/Interface/IRelationship.cs +++ /dev/null @@ -1,53 +0,0 @@ -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 eeb419e..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 static readonly string DefaultDbUrl = ConfigurationManager.ConnectionStrings["neo4j"].ConnectionString.TrimEnd('/'); - - 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; } - - #region GetRootNode - - public static Node GetRootNode() - { - return GetRootNode(DefaultDbUrl); - } - - public static Node GetRootNode(string dbUrl) - { - string response; - var status = Neo4jRestApi.GetRoot(dbUrl, 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(DefaultDbUrl, nodeId); - } - - public static Node GetNode(string dbUrl, EncryptId nodeId) - { - string response; - var status = Neo4jRestApi.GetNode(dbUrl, (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(DefaultDbUrl, indexName, key, value); - } - - public static IEnumerable GetNode(Enum indexName, string key, object value) - { - return GetNode(DefaultDbUrl, indexName.ToString(), key, value); - } - - public static IEnumerable GetNode(string indexName, Enum key, object value) - { - return GetNode(DefaultDbUrl, indexName, key.ToString(), value); - } - - public static IEnumerable GetNode(Enum indexName, Enum key, object value) - { - return GetNode(DefaultDbUrl, indexName.ToString(), key.ToString(), value); - } - - public static IEnumerable GetNode(string dbUrl, string indexName, string key, object value) - { - string response; - var status = Neo4jRestApi.GetNode(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 GetNode(string dbUrl, Enum indexName, string key, object value) - { - return GetNode(dbUrl, indexName.ToString(), key, value); - } - - public static IEnumerable GetNode(string dbUrl, string indexName, Enum key, object value) - { - return GetNode(dbUrl, indexName, key.ToString(), value); - } - - public static IEnumerable GetNode(string 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(DefaultDbUrl, indexName, searchQuery); - } - - public static IEnumerable GetNode(Enum indexName, string searchQuery) - { - return GetNode(DefaultDbUrl, indexName.ToString(), searchQuery); - } - - public static IEnumerable GetNode(string dbUrl, string indexName, string searchQuery) - { - string response; - var status = Neo4jRestApi.GetNode(dbUrl, 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(string 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(DefaultDbUrl, properties.ToString()); - } - - public static Node CreateNode(Enum nodeType) - { - return CreateNode(nodeType.ToString()); - } - - public static Node CreateNode(string dbUrl, string nodeType) - { - var properties = new Properties(); - properties.SetProperty(NodeProperty.NodeType.ToString(), nodeType); - return CreateNodeFromJson(dbUrl, properties.ToString()); - } - public static Node CreateNode(string dbUrl, Enum nodeType) - { - return CreateNode(dbUrl, nodeType.ToString()); - } - - public static Node CreateNode(string nodeType, Properties properties) - { - properties.SetProperty(NodeProperty.NodeType.ToString(), nodeType); - return CreateNodeFromJson(DefaultDbUrl, properties.ToString()); - } - - public static Node CreateNode(Enum nodeType, Properties properties) - { - return CreateNode(nodeType.ToString(), properties); - } - - public static Node CreateNode(string dbUrl, string nodeType, Properties properties) - { - properties.SetProperty(NodeProperty.NodeType.ToString(), nodeType); - return CreateNodeFromJson(dbUrl, properties.ToString()); - } - - public static Node CreateNode(string 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(DefaultDbUrl, JObject.FromObject(properties).ToString(Formatting.None)); - } - - public static Node CreateNode(Enum nodeType, IDictionary properties) - { - return CreateNode(nodeType.ToString(), properties); - } - - public static Node CreateNode(string dbUrl, string nodeType, IDictionary properties) - { - properties.Add(NodeProperty.NodeType.ToString(), nodeType); - return CreateNodeFromJson(dbUrl, JObject.FromObject(properties).ToString(Formatting.None)); - } - - public static Node CreateNode(string dbUrl, Enum nodeType, IDictionary properties) - { - return CreateNode(dbUrl, nodeType.ToString(), properties); - } - - private static Node CreateNodeFromJson(string dbUrl, string jsonProperties) - { - string response; - var status = Neo4jRestApi.CreateNode(dbUrl, 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 = Neo4jRestApi.DeleteNode(DefaultDbUrl, 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; - var status = Neo4jRestApi.GetPropertiesOnNode(DefaultDbUrl, (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 = Neo4jRestApi.SetPropertiesOnNode(DefaultDbUrl, (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; - var status = Neo4jRestApi.GetRelationshipsOnNode(DefaultDbUrl, (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; - var status = Neo4jRestApi.CreateRelationship( DefaultDbUrl, - (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 = Neo4jRestApi.Traverse(DefaultDbUrl, (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(DefaultDbUrl, nodeId, indexName, key, value); - } - - public static Node AddNodeToIndex(long nodeId, Enum indexName, string key, object value) - { - return AddNodeToIndex(DefaultDbUrl, nodeId, indexName.ToString(), key, value); - } - - public static Node AddNodeToIndex(long nodeId, string indexName, Enum key, object value) - { - return AddNodeToIndex(DefaultDbUrl, nodeId, indexName, key.ToString(), value); - } - - public static Node AddNodeToIndex(long nodeId, Enum indexName, Enum key, object value) - { - return AddNodeToIndex(DefaultDbUrl, nodeId, indexName.ToString(), key.ToString(), value); - } - - public static Node AddNodeToIndex(string dbUrl, long nodeId, Enum indexName, Enum key, object value) - { - return AddNodeToIndex(dbUrl, nodeId, indexName.ToString(), key.ToString(), value); - } - - public static Node AddNodeToIndex(string dbUrl, long nodeId, string indexName, string key, object value) - { - string response; - var status = Neo4jRestApi.AddNodeToIndex(DefaultDbUrl, 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(DefaultDbUrl, nodeId, indexName); - } - - public static HttpStatusCode RemoveNodeFromIndex(long nodeId, Enum indexName) - { - return RemoveNodeFromIndex(DefaultDbUrl, nodeId, indexName.ToString()); - } - - public static HttpStatusCode RemoveNodeFromIndex(string dbUrl, long nodeId, Enum indexName) - { - return RemoveNodeFromIndex(dbUrl, nodeId, indexName.ToString()); - } - - public static HttpStatusCode RemoveNodeFromIndex(string dbUrl, long nodeId, string indexName) - { - var status = Neo4jRestApi.RemoveNodeFromIndex(DefaultDbUrl, 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(DefaultDbUrl, nodeId, indexName, key); - } - - public static HttpStatusCode RemoveNodeFromIndex(long nodeId, Enum indexName, string key) - { - return RemoveNodeFromIndex(DefaultDbUrl, nodeId, indexName.ToString(), key); - } - - public static HttpStatusCode RemoveNodeFromIndex(long nodeId, string indexName, Enum key) - { - return RemoveNodeFromIndex(DefaultDbUrl, nodeId, indexName, key.ToString()); - } - - public static HttpStatusCode RemoveNodeFromIndex(long nodeId, Enum indexName, Enum key) - { - return RemoveNodeFromIndex(DefaultDbUrl, nodeId, indexName.ToString(), key.ToString()); - } - - public static HttpStatusCode RemoveNodeFromIndex(string dbUrl, long nodeId, Enum indexName, Enum key) - { - return RemoveNodeFromIndex(dbUrl, nodeId, indexName.ToString(), key.ToString()); - } - - public static HttpStatusCode RemoveNodeFromIndex(string dbUrl, long nodeId, string indexName, string key) - { - var status = Neo4jRestApi.RemoveNodeFromIndex(DefaultDbUrl, 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(DefaultDbUrl, nodeId, indexName, key, value); - } - - public static HttpStatusCode RemoveNodeFromIndex(long nodeId, Enum indexName, string key, object value) - { - return RemoveNodeFromIndex(DefaultDbUrl, nodeId, indexName.ToString(), key, value); - } - - public static HttpStatusCode RemoveNodeFromIndex(long nodeId, string indexName, Enum key, object value) - { - return RemoveNodeFromIndex(DefaultDbUrl, nodeId, indexName, key.ToString(), value); - } - - public static HttpStatusCode RemoveNodeFromIndex(long nodeId, Enum indexName, Enum key, object value) - { - return RemoveNodeFromIndex(DefaultDbUrl, nodeId, indexName.ToString(), key.ToString(), value); - } - - public static HttpStatusCode RemoveNodeFromIndex(string dbUrl, long nodeId, string indexName, string key, object value) - { - var status = Neo4jRestApi.RemoveNodeFromIndex(DefaultDbUrl, 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/Path.cs b/Neo4jRestNet/Core/Path.cs deleted file mode 100644 index f931d07..0000000 --- a/Neo4jRestNet/Core/Path.cs +++ /dev/null @@ -1,134 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using Newtonsoft.Json.Linq; -using Newtonsoft.Json; - -namespace Neo4jRestNet.Core -{ - public class Path : IGraphObject - { - 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 string OriginalPathJson { get; private set; } - - private Path(JObject path) - { - JToken startNode; - if (!path.TryGetValue("start", out startNode)) - { - throw new Exception("Invalid path json"); - } - - switch (startNode.Type) - { - case JTokenType.String: - StartNode = Node.InitializeFromSelf(startNode.Value()); - break; - - case JTokenType.Object: - StartNode = Node.InitializeFromNodeJson((JObject)startNode); - break; - - default: - throw new Exception("Invalid path json"); - } - - JToken endNode; - if (!path.TryGetValue("end", out endNode)) - { - throw new Exception("Invalid path json"); - } - - switch (endNode.Type) - { - case JTokenType.String: - EndNode = Node.InitializeFromSelf(endNode.Value()); - break; - - case JTokenType.Object: - EndNode = Node.InitializeFromNodeJson((JObject)endNode); - break; - - default: - throw new Exception("Invalid path json"); - } - - Nodes = new List(); - JToken nodes; - if (!path.TryGetValue("nodes", out nodes) || nodes.Type != JTokenType.Array) - { - throw new Exception("Invalid path json"); - } - - foreach (JToken node in nodes) - { - switch (node.Type) - { - case JTokenType.String: - Nodes.Add(Node.InitializeFromSelf(node.Value())); - break; - - case JTokenType.Object: - Nodes.Add(Node.InitializeFromNodeJson((JObject)node)); - break; - - default: - throw new Exception("Invalid path json"); - } - } - - 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) - { - switch (relationship.Type) - { - case JTokenType.String: - Relationships.Add(Relationship.InitializeFromSelf(relationship.Value())); - break; - - case JTokenType.Object: - Relationships.Add(Relationship.InitializeFromRelationshipJson((JObject)relationship)); - break; - - default: - throw new Exception("Invalid path json"); - } - } - - OriginalPathJson = path.ToString(Formatting.None); - } - - public static List ParseJson(string jsonPaths) - { - if (String.IsNullOrEmpty(jsonPaths)) - { - return null; - } - - var jaPaths = JArray.Parse(jsonPaths); - return ParseJson(jaPaths); - } - - public static List ParseJson(JArray jsonPaths) - { - if (jsonPaths == null) - { - return null; - } - - var jaPaths = jsonPaths; - - return (from JObject joPath in jaPaths select new Path(joPath)).ToList(); - } - } -} diff --git a/Neo4jRestNet/Core/Relationship.cs.BACKUP.928.cs b/Neo4jRestNet/Core/Relationship.cs.BACKUP.928.cs new file mode 100644 index 0000000..02ff37b --- /dev/null +++ b/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 GetRelationship(string indexName, string key, object value) + { + return GetRelationship(DefaultConnectionName, indexName, key, value); + } + + public static IEnumerable GetRelationship(Enum indexName, string key, object value) + { + return GetRelationship(DefaultConnectionName, indexName.ToString(), key, value); + } + + public static IEnumerable GetRelationship(string indexName, Enum key, object value) + { + return GetRelationship(DefaultConnectionName, indexName, key.ToString(), value); + } + + public static IEnumerable GetRelationship(Enum indexName, Enum key, object value) + { + return GetRelationship(DefaultConnectionName, indexName.ToString(), key.ToString(), value); + } + + public static IEnumerable GetRelationship(string connectionName, Enum indexName, Enum key, object value) + { + return GetRelationship(connectionName, indexName.ToString(), key.ToString(), value); + } + + public static IEnumerable 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 GetRelationship(string indexName, string searchQuery) + { + return GetRelationship(DefaultConnectionName, indexName, searchQuery); + } + + public static IEnumerable GetRelationship(Enum indexName, string searchQuery) + { + return GetRelationship(DefaultConnectionName, indexName.ToString(), searchQuery); + } + + public static IEnumerable GetRelationship(string connectionName, Enum indexName, string searchQuery) + { + return GetRelationship(connectionName, indexName.ToString(), searchQuery); + } + + public static IEnumerable 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(); + + 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)); + } + + _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 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/Relationship.cs.BASE.928.cs b/Neo4jRestNet/Core/Relationship.cs.BASE.928.cs new file mode 100644 index 0000000..0dd8a69 --- /dev/null +++ b/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 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/Relationship.cs b/Neo4jRestNet/Core/Relationship.cs.REMOTE.928.cs similarity index 100% rename from Neo4jRestNet/Core/Relationship.cs rename to Neo4jRestNet/Core/Relationship.cs.REMOTE.928.cs diff --git a/Neo4jRestNet/Neo4jRestNet.csproj b/Neo4jRestNet/Neo4jRestNet.csproj index aef4027..847b5c3 100644 --- a/Neo4jRestNet/Neo4jRestNet.csproj +++ b/Neo4jRestNet/Neo4jRestNet.csproj @@ -9,7 +9,7 @@ Library Properties Neo4jRestNet - Neo4Net + Neo4RestNet v4.0 512