예제 #1
0
        // Define other methods and classes here
        private static void BuildTreeNode(PrefetchElementStringRepresentation iteratorNode, string[] nodeLeaf, int index)
        {
            if (index >= nodeLeaf.Length)
            {
                return; // we're done
            }
            var nodeStr    = nodeLeaf[index];
            var nodeStrArr = nodeStr.Split(':');
            var name       = nodeStrArr[0];
            var max        = 0;

            if (nodeStrArr.Length == 2)
            {
                int.TryParse(nodeStrArr[1], out max);
            }

            var n = iteratorNode.Children.FirstOrDefault(c => c.Name.Equals(name, StringComparison.OrdinalIgnoreCase));

            if (n == null)
            {
                n = new PrefetchElementStringRepresentation {
                    Name = name, MaxNumberOfItemsToReturn = max
                };
                iteratorNode.Children.Add(n);
            }
            else if (n.MaxNumberOfItemsToReturn < max)
            {
                n.MaxNumberOfItemsToReturn = max;
            }
            BuildTreeNode(n, nodeLeaf, ++index);
        }
예제 #2
0
        internal static IPrefetchPath2 ConvertStringToPrefetchPath(EntityType entityType, string prefetchStr, string selectStr)
        {
            if (string.IsNullOrWhiteSpace(prefetchStr))
            {
                return(null);
            }

            // Break up the selectStr into a dictionary of keys and values
            // where the key is the path (i.e.: products.supplier)
            // and the value is the field name (i.e.: companyname)
            var prefetchFieldKeysAndNames = (selectStr ?? "")
                                            .Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
                                            .Where(s => s.IndexOf('.') > 0)
                                            .Select(s => s.Trim('.').ToLowerInvariant());
            var prefetchFieldNamesDictionary = new Dictionary <string, List <string> >();

            foreach (var s in prefetchFieldKeysAndNames)
            {
                var key   = s.Substring(0, s.LastIndexOf('.'));
                var value = s.Substring(s.LastIndexOf('.') + 1);
                if (prefetchFieldNamesDictionary.ContainsKey(key))
                {
                    prefetchFieldNamesDictionary[key].AddIfNotExists(value);
                }
                else
                {
                    prefetchFieldNamesDictionary.Add(key, new List <string>(new[] { value }));
                }
            }

            var prefetchStrs = prefetchStr.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            var nodeLeaves   =
                prefetchStrs.Select(n => n.Split(new[] { '.' }, StringSplitOptions.RemoveEmptyEntries)).ToArray();
            var rootNode = new PrefetchElementStringRepresentation {
                Name = "root", MaxNumberOfItemsToReturn = 0
            };

            foreach (var nodeLeaf in nodeLeaves)
            {
                BuildTreeNode(rootNode, nodeLeaf, 0);
            }

            var prefetch = new PrefetchPath2(entityType);

            foreach (var prefetchRepresentation in rootNode.Children)
            {
                ExcludeIncludeFieldsList prefetchPathElementIncludeFieldList;
                IPrefetchPathElement2    prefetchPathElement =
                    ConvertPrefetchRepresentationToPrefetchPathElement(entityType, prefetchRepresentation,
                                                                       prefetchFieldNamesDictionary,
                                                                       prefetchRepresentation.Name.ToLowerInvariant(),
                                                                       out prefetchPathElementIncludeFieldList);
                if (prefetchPathElement != null)
                {
                    prefetch.Add(prefetchPathElement, prefetchPathElementIncludeFieldList);
                }
            }
            return(prefetch.Count > 0 ? prefetch : null);
        }
        internal static IPrefetchPath2 ConvertStringToPrefetchPath(EntityType entityType, string prefetchStr, string selectStr)
        {
            if (string.IsNullOrWhiteSpace(prefetchStr))
                return null;

            // Break up the selectStr into a dictionary of keys and values
            // where the key is the path (i.e.: products.supplier)
            // and the value is the field name (i.e.: companyname)
            var prefetchFieldKeysAndNames = (selectStr ?? "")
                .Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
                .Where(s => s.IndexOf('.') > 0)
                .Select(s => s.Trim('.').ToLowerInvariant());
            var prefetchFieldNamesDictionary = new Dictionary<string, List<string>>();
            foreach (var s in prefetchFieldKeysAndNames)
            {
                var key = s.Substring(0, s.LastIndexOf('.'));
                var value = s.Substring(s.LastIndexOf('.') + 1);
                if (prefetchFieldNamesDictionary.ContainsKey(key))
                    prefetchFieldNamesDictionary[key].AddIfNotExists(value);
                else
                    prefetchFieldNamesDictionary.Add(key, new List<string>(new[] { value }));
            }

            var prefetchStrs = prefetchStr.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            var nodeLeaves =
                prefetchStrs.Select(n => n.Split(new[] { '.' }, StringSplitOptions.RemoveEmptyEntries)).ToArray();
            var rootNode = new PrefetchElementStringRepresentation { Name = "root", MaxNumberOfItemsToReturn = 0 };
            foreach (var nodeLeaf in nodeLeaves)
                BuildTreeNode(rootNode, nodeLeaf, 0);

            var prefetch = new PrefetchPath2(entityType);
            foreach (var prefetchRepresentation in rootNode.Children)
            {
                ExcludeIncludeFieldsList prefetchPathElementIncludeFieldList;
                IPrefetchPathElement2 prefetchPathElement =
                    ConvertPrefetchRepresentationToPrefetchPathElement(entityType, prefetchRepresentation,
                                                                       prefetchFieldNamesDictionary,
                                                                       prefetchRepresentation.Name.ToLowerInvariant(),
                                                                       out prefetchPathElementIncludeFieldList);
                if (prefetchPathElement != null)
                    prefetch.Add(prefetchPathElement, prefetchPathElementIncludeFieldList);
            }
            return prefetch.Count > 0 ? prefetch : null;
        }
        private static IPrefetchPathElement2 ConvertPrefetchRepresentationToPrefetchPathElement(EntityType parentEntityType,
                                                                                         PrefetchElementStringRepresentation
                                                                                             prefetchRepresentation,
                                                                                         Dictionary
                                                                                             <string, List<string>>
                                                                                             prefetchFieldNamesDictionary,
                                                                                         string fieldNamesKey,
                                                                                         out ExcludeIncludeFieldsList
                                                                                             includeFieldList)
        {
            includeFieldList = null;

            if (prefetchRepresentation == null)
                return null;

            var includeMap = GetEntityTypeIncludeMap(parentEntityType);

            IPrefetchPathElement2 newElement = null;
            var rr =
                includeMap.FirstOrDefault(
                    rm => rm.Key.Equals(prefetchRepresentation.Name, StringComparison.InvariantCultureIgnoreCase));
            if (!rr.Equals(default(KeyValuePair<string, IPrefetchPathElement2>)))
            {
                newElement = rr.Value;

                foreach (var childRepresentation in prefetchRepresentation.Children)
                {
                    ExcludeIncludeFieldsList subElementIncludeFieldList;
                    var subElement =
                        ConvertPrefetchRepresentationToPrefetchPathElement((EntityType)newElement.ToFetchEntityType,
                                                                           childRepresentation,
                                                                           prefetchFieldNamesDictionary,
                                                                           string.Concat(fieldNamesKey, ".",
                                                                                         childRepresentation.Name
                                                                                                            .ToLowerInvariant
                                                                                             ()),
                                                                           out subElementIncludeFieldList);
                    if (subElement != null)
                        newElement.SubPath.Add(subElement, subElementIncludeFieldList);
                }
            }

            if (newElement != null)
            {
                // Determin if there is a max amount of records to return for this prefetch element
                if (newElement.MaxAmountOfItemsToReturn < prefetchRepresentation.MaxNumberOfItemsToReturn)
                    newElement.MaxAmountOfItemsToReturn = prefetchRepresentation.MaxNumberOfItemsToReturn;

                // Determine if there are field name restrictions applied to the prefetched item
                if (prefetchFieldNamesDictionary.ContainsKey(fieldNamesKey))
                {
                    includeFieldList = ConvertEntityTypeFieldNamesToExcludedIncludedFields(
                        (EntityType)newElement.ToFetchEntityType, prefetchFieldNamesDictionary[fieldNamesKey]);
                }
            }

            return newElement;
        }
        // Define other methods and classes here
        private static void BuildTreeNode(PrefetchElementStringRepresentation iteratorNode, string[] nodeLeaf, int index)
        {
            if (index >= nodeLeaf.Length)
                return; // we're done

            var nodeStr = nodeLeaf[index];
            var nodeStrArr = nodeStr.Split(':');
            var name = nodeStrArr[0];
            var max = 0;
            if (nodeStrArr.Length == 2) int.TryParse(nodeStrArr[1], out max);

            var n = iteratorNode.Children.FirstOrDefault(c => c.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
            if (n == null)
            {
                n = new PrefetchElementStringRepresentation { Name = name, MaxNumberOfItemsToReturn = max };
                iteratorNode.Children.Add(n);
            }
            else if (n.MaxNumberOfItemsToReturn < max)
            {
                n.MaxNumberOfItemsToReturn = max;
            }
            BuildTreeNode(n, nodeLeaf, ++index);
        }
예제 #6
0
        private static IPrefetchPathElement2 ConvertPrefetchRepresentationToPrefetchPathElement(EntityType parentEntityType,
                                                                                                PrefetchElementStringRepresentation
                                                                                                prefetchRepresentation,
                                                                                                Dictionary
                                                                                                <string, List <string> >
                                                                                                prefetchFieldNamesDictionary,
                                                                                                string fieldNamesKey,
                                                                                                out ExcludeIncludeFieldsList
                                                                                                includeFieldList)
        {
            includeFieldList = null;

            if (prefetchRepresentation == null)
            {
                return(null);
            }

            var includeMap = GetEntityTypeIncludeMap(parentEntityType);

            IPrefetchPathElement2 newElement = null;
            var rr =
                includeMap.FirstOrDefault(
                    rm => rm.Key.Equals(prefetchRepresentation.Name, StringComparison.InvariantCultureIgnoreCase));

            if (!rr.Equals(default(KeyValuePair <string, IPrefetchPathElement2>)))
            {
                newElement = rr.Value;

                foreach (var childRepresentation in prefetchRepresentation.Children)
                {
                    ExcludeIncludeFieldsList subElementIncludeFieldList;
                    var subElement =
                        ConvertPrefetchRepresentationToPrefetchPathElement((EntityType)newElement.ToFetchEntityType,
                                                                           childRepresentation,
                                                                           prefetchFieldNamesDictionary,
                                                                           string.Concat(fieldNamesKey, ".",
                                                                                         childRepresentation.Name
                                                                                         .ToLowerInvariant
                                                                                             ()),
                                                                           out subElementIncludeFieldList);
                    if (subElement != null)
                    {
                        newElement.SubPath.Add(subElement, subElementIncludeFieldList);
                    }
                }
            }

            if (newElement != null)
            {
                // Determin if there is a max amount of records to return for this prefetch element
                if (newElement.MaxAmountOfItemsToReturn < prefetchRepresentation.MaxNumberOfItemsToReturn)
                {
                    newElement.MaxAmountOfItemsToReturn = prefetchRepresentation.MaxNumberOfItemsToReturn;
                }

                // Determine if there are field name restrictions applied to the prefetched item
                if (prefetchFieldNamesDictionary.ContainsKey(fieldNamesKey))
                {
                    includeFieldList = ConvertEntityTypeFieldNamesToExcludedIncludedFields(
                        (EntityType)newElement.ToFetchEntityType, prefetchFieldNamesDictionary[fieldNamesKey]);
                }
            }

            return(newElement);
        }