private void AddItemToMap(Model.ModelBase item) { if (item == null || string.IsNullOrWhiteSpace(item.Id)) { return; } AddItemToMap(item.Parent); List <string> parentList; if (_ItemMap.ContainsKey(item.Id)) { parentList = _ItemMap[item.Id]; } else { parentList = new List <string>(); _ItemMap.Add(item.Id, parentList); } if (!parentList.Contains(item.ParentId)) { parentList.Add(item.ParentId); } }
private Model.ModelBase GetItemFromID(string id) { if (string.Equals(id, "0", StringComparison.OrdinalIgnoreCase)) { return(new Model.Root(CurrentUser)); } if (_ItemMap.ContainsKey(id)) { IEnumerable <string> parentList; //the order by length is a trick to make sure we avoid using the All Videos All Music folder if possible parentList = _ItemMap[id].OrderByDescending(i => i.Length); Model.ModelBase result = null; foreach (var parentID in parentList) { var parentItem = GetItemFromID(parentID); if (parentItem != null) { result = parentItem.Children.FirstOrDefault(i => string.Equals(i.Id, id, StringComparison.OrdinalIgnoreCase)); if (result != null) { break; } } } //this shouldn't happen, if the item is in the map, it should exist if (result == null) { Logger.Debug("GetItemFromID couldn't find parent for item: {0}", id); } return(result); } else { //find it the slow way by recursing through the object model var result = new Model.Root(CurrentUser).GetChildRecursive(id); //add it to the map so next time we don't have to find it this way AddItemToMap(result); return(result); } }