Example #1
0
        /// <summary>
        /// Creates a directed Edge and saves to DB
        /// </summary>
        /// <param name="label">Label of created edge</param>
        /// <param name="InVertex">Ingoing vertex to connect</param>
        /// <param name="OutVertex">Outgoing vertex to connect</param>
        /// <param name="Properties">Properties of created edge</param>
        /// <returns>Created IEdge</returns>
        public IEdge AddDirectedEdge(string label, IVertex OutVertex, IVertex InVertex, IEdgeProperties Properties = null)
        {
            IEdge           tmpEdge         = new TitanEdge();
            IVertex         titanOutVertex  = OutVertex;
            IVertex         titanInVertex   = InVertex;
            IEdgeProperties titanProperties = Properties;

            tmpEdge.Label = label;
            tmpEdge.ID    = localId.ToString();
            localId++;
            tmpEdge.InVertexLabel  = InVertex.Label;
            tmpEdge.OutVertexLabel = OutVertex.Label;
            tmpEdge.Properties     = titanProperties;
            tmpEdge.InVertex       = titanInVertex.ID;
            tmpEdge.OutVertex      = titanOutVertex.ID;

            try
            {
                IEdge createdEdge = Gremlin.CreateEdge(tmpEdge.OutVertex, tmpEdge.InVertex, label, (Dictionary <string, object>)Properties);
                logger.Info("Edge " + createdEdge.ID + " has been created.");
                tmpEdge = createdEdge;
            }
            catch (WebSocketException we)
            {
                logger.Error("Can not connect to Server. " + we);
                throw;
            }
            catch (JsonReaderException jre)
            {
                logger.Error("Can not read JSON. " + jre.Data + jre.StackTrace);
                throw;
            }
            catch (Exception e)
            {
                logger.Error("Unhandled Exception occured. " + e);
                throw;
            }
            return(tmpEdge);
        }
Example #2
0
        /// <summary>
        /// Reads incoming Json and parses it to the right implementation
        /// </summary>
        /// <param name="reader">Reader that has json</param>
        /// <param name="objectType">objectType to which should be converted</param>
        /// <param name="existingValue">A potentially existing value</param>
        /// <param name="serializer">Serializer that is in use</param>
        /// <returns>parsed object</returns>
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            var jsonObject = JObject.Load(reader);

            var graphItemType = default(object);

            if (objectType == typeof(TitanVertexProperties) || objectType == typeof(OrientVertexProperties))
            {
                if (objectType == typeof(OrientVertexProperties))
                {
                    graphItemType = new OrientVertexProperties();
                }
                else if (objectType == typeof(TitanVertexProperties))
                {
                    graphItemType = new TitanVertexProperties();
                }
            }
            else if (objectType == typeof(TitanEdgeProperties) || objectType == typeof(OrientEdgeProperties))
            {
                if (objectType == typeof(OrientEdgeProperties))
                {
                    graphItemType = new OrientEdgeProperties();
                }
                else if (objectType == typeof(TitanEdgeProperties))
                {
                    graphItemType = new TitanEdgeProperties();
                }
            }
            else
            {
                JProperty jsonProp = jsonObject.Property("id");
                JToken    token    = jsonProp.Value;
                //Titan
                if (token.Type == JTokenType.Integer || token.Type == JTokenType.String)
                {
                    if (objectType == typeof(IVertex))
                    {
                        graphItemType = new TitanVertex();
                    }
                    else if (objectType == typeof(IEdge))
                    {
                        graphItemType = new TitanEdge();
                    }

                    else if (objectType == typeof(IVertexValue))
                    {
                        graphItemType = new TitanVertexValue();
                    }
                }
                //Orient
                else if (token.Type == JTokenType.Object)
                {
                    if (objectType == typeof(IVertex))
                    {
                        graphItemType = new OrientVertex();
                    }
                    else if (objectType == typeof(IEdge))
                    {
                        graphItemType = new OrientEdge();
                    }
                    else if (objectType == typeof(IVertexValue))
                    {
                        graphItemType = new OrientVertexValue();
                    }
                }
                else
                {
                    throw (new Exceptions.NotSupportedDatabaseException("Not supported GraphDatabase. Titan or OrientDB are supported."));
                }
            }
            serializer.Populate(jsonObject.CreateReader(), graphItemType);
            return(graphItemType);
        }