Esempio n. 1
0
        private static byte[] GetFullByteBuffer(BasicNodeModel model)
        {
            List <byte> buffer = new List <byte>();

            // Add the contentType
            buffer.Add((byte)model.ContentType);
            // Add the common name
            buffer.AddRange(Encoding.UTF8.GetBytes(model.CommonName));
            // Add each of the other names
            foreach (string name in model.OtherNames.OrderBy(x => x))
            {
                buffer.AddRange(Encoding.UTF8.GetBytes(name));
            }
            // Add the release date
            if (model.ReleaseDate.HasValue)
            {
                buffer.AddRange(Encoding.UTF8.GetBytes(DateValueConverter.ToDateTime(model.ReleaseDate.Value).ToString("yyyy-MM-dd")));
            }
            else
            {
                buffer.AddRange(Encoding.UTF8.GetBytes("0000-00-00"));
            }
            // Add the death date
            if (model.DeathDate.HasValue)
            {
                buffer.AddRange(Encoding.UTF8.GetBytes(DateValueConverter.ToDateTime(model.DeathDate.Value).ToString("yyyy-MM-dd")));
            }
            else
            {
                buffer.AddRange(Encoding.UTF8.GetBytes("0000-00-00"));
            }
            // Add the id
            buffer.AddRange(model.Id.ToByteArray());

            // Add node type content
            if (model.ContentType == NodeContentType.Company)
            {
                AddCompanyInformationToBuffer(((CompanyNodeModel)model), ref buffer);
            }
            else if (model.ContentType == NodeContentType.Media)
            {
                AddMediaInformationToBuffer(((MediaNodeModel)model), ref buffer);
            }
            else if (model.ContentType == NodeContentType.Person)
            {
                AddPersonInformationToBuffer(((PersonNodeModel)model), ref buffer);
            }

            return(buffer.ToArray());
        }
Esempio n. 2
0
        private static byte[] GetPartialByteBuffer(BasicNodeModel model)
        {
            List <byte> buffer = new List <byte>();

            // Add the common name of the node
            buffer.AddRange(Encoding.UTF8.GetBytes(model.CommonName));
            // Add the content type
            buffer.Add((byte)model.ContentType);
            // Add the release date
            if (model.ReleaseDate.HasValue)
            {
                buffer.AddRange(Encoding.UTF8.GetBytes(DateValueConverter.ToDateTime(model.ReleaseDate.Value).ToString("yyyy-MM-dd")));
            }
            else
            {
                buffer.AddRange(Encoding.UTF8.GetBytes("0000-00-00"));
            }
            // Finally, add the id
            buffer.AddRange(model.Id.ToByteArray());

            return(buffer.ToArray());
        }
Esempio n. 3
0
        /// <summary>
        /// Creates a BasicNodeModel based on the given INode.
        /// </summary>
        /// <exception cref="ArgumentException">If the INode could not be parsed</exception>
        /// <param name="node">The INode from which to create the BasicNodeModel</param>
        /// <returns>The created BasicNodeModel or null if an error occurred</returns>
        public static BasicNodeModel FromINode(INode node)
        {
            BasicNodeModel result = null;

            try
            {
                NodeContentType contentType = (NodeContentType)Enum.Parse(typeof(NodeContentType), node.Labels[0]);
                if (contentType == NodeContentType.Company)
                {
                    result = new CompanyNodeModel();
                }
                else if (contentType == NodeContentType.Media)
                {
                    result = new MediaNodeModel
                    {
                        // Media properties
                        MediaType = node.Labels.Count > 2 ? (NodeMediaType)Enum.Parse(typeof(NodeMediaType), node.Labels[1]) : 0,
                        Franchise = node.Properties.ContainsKey("franchise") ? node.Properties["franchise"].As <string>() : null,
                        Genres    = node.Properties.ContainsKey("genres") ? node.Properties["genres"].As <List <string> >() : new List <string>()
                    };
                }
                else if (contentType == NodeContentType.Person)
                {
                    result = new PersonNodeModel
                    {
                        // Person properties
                        FamilyName = node.Properties.ContainsKey("familyName") ? node.Properties["familyName"].As <string>() : null,
                        GivenName  = node.Properties.ContainsKey("givenName") ? node.Properties["givenName"].As <string>() : null,
                        Status     = node.Properties.ContainsKey("status") ? (PersonStatus)Enum.Parse(typeof(PersonStatus), node.Properties["status"].As <string>()) : 0
                    };

                    // If the family and given name was not populated and there is a common name,
                    if (((PersonNodeModel)result).FamilyName == null && ((PersonNodeModel)result).GivenName == null &&
                        node.Properties.ContainsKey("commonName"))
                    {
                        // Parse the names out of the common name
                        string[] nameParts  = node.Properties["commonName"].As <string>().Split(' ');
                        string   givenName  = "";
                        string   familyName = "";
                        for (int i = 0; i < nameParts.Length; i++)
                        {
                            if (i != nameParts.Length - 1)
                            {
                                givenName += $"{nameParts[i]} ";
                            }
                            else
                            {
                                familyName = nameParts[i];
                            }
                        }

                        ((PersonNodeModel)result).GivenName  = givenName.Trim();
                        ((PersonNodeModel)result).FamilyName = familyName;
                    }
                }
                // Basic properties
                result.Id          = Guid.Parse(node.Properties["id"].As <string>());
                result.ContentType = contentType;
                result.ReleaseDate = node.Properties.ContainsKey("releaseDate") ? node.Properties["releaseDate"].As <long>() : default(long?);
                result.DeathDate   = node.Properties.ContainsKey("deathDate") ? node.Properties["deathDate"].As <long>() : default(long?);
                result.CommonName  = node.Properties.ContainsKey("commonName") ? node.Properties["commonName"].As <string>() : null;
                result.OtherNames  = node.Properties.ContainsKey("otherNames") ? node.Properties["otherNames"].As <List <string> >() : new List <string>();
                result.PartialHash = node.Properties.ContainsKey("partialHash") ? node.Properties["partialHash"].As <string>() : "";
                result.FullHash    = node.Properties.ContainsKey("fullHash") ? node.Properties["fullHash"].As <string>() : "";
            }
            catch (Exception e)
            {
                throw new ArgumentException("Failed to parse the given node: (Type: " + node.Labels[0] + " Id: " + node.Properties["id"].As <string>(), e);
            }

            return(result);
        }
Esempio n. 4
0
 public static string CalculateFullHash(BasicNodeModel model)
 {
     byte[] hash = Hash(GetFullByteBuffer(model));
     return(hash != null?ConvertToHexString(hash) : null);
 }