Example #1
0
        public Task <bool> Process(XmlLookupWorkItem item)
        {
            var hashBytes = _hashProvider.Hash(item.LookupXElement.ToString());
            var hash      = _hashProvider.BytesToStr(hashBytes);

            item.HashString = hash;
            return(Task.FromResult(true));
        }
        /// <summary>
        ///     Starting with the innermost nodes, replace all lookups with identities
        /// </summary>
        /// <param name="xElement"></param>
        private void ReplaceReferenceLookupsWithIdentities(XElement xElement)
        {
            XElement newIdentity = null;

            var ns = xElement.Name.Namespace;

            if (_regex.IsMatch(xElement.Name.LocalName))
            {
                var typeName = _regex.Match(xElement.Name.LocalName).Groups["TypeName"].Value;
                var lookup   = xElement.Element(ns + $"{typeName}Lookup");
                var identity = xElement.Element(ns + $"{typeName}Identity");

                if (lookup != null && identity == null)
                {
                    var hashBytes = _hashProvider.Hash(lookup.ToString());
                    var hash      = _hashProvider.BytesToStr(hashBytes);

                    if (_hashIdentities.ContainsKey(hash) && _hashIdentities[hash] != null)
                    {
                        newIdentity = XElement.Parse(_hashIdentities[hash].ToString());

                        lookup.AddBeforeSelf(
                            new XComment(
                                $"This {typeName}Lookup entity was used to populate its {typeName}Identity sibling"));
                    }
                    else
                    {
                        lookup.AddBeforeSelf(
                            new XComment(
                                $"No {typeName}Identity could be retrieved for this {typeName}Lookup entity"));
                    }
                }

                foreach (var element in xElement.Elements())
                {
                    ReplaceReferenceLookupsWithIdentities(element);
                }

                if (newIdentity == null)
                {
                    return;
                }

                xElement.Add(newIdentity);

                newIdentity.AddBeforeSelf(
                    new XComment(
                        $"This {typeName}Identity entity was retrieved from the API using the provided {typeName}Lookup information"));
            }
            else
            {
                foreach (var element in xElement.Elements())
                {
                    ReplaceReferenceLookupsWithIdentities(element);
                }
            }
        }