public async Task <TNDEntityItem[]> SearchTerm(TNDMetaEntity metaEntity, string searchTerm, string emails, string emailDomains) { var configuration = this._ApplicationBuis.LoadConfiguration(false); var metaSource = metaEntity?.MetaSource; if (metaSource == null) { var tasksSearchTerm = new List <Task <TNDEntityItem[]> >(); foreach (var metaEnt in configuration.MetaEntities) { var sourceBuis = metaEnt?.MetaSource.SourceBuis; if (sourceBuis != null) { var taskSearchTerm = sourceBuis.SearchTerm(metaEnt, searchTerm, emails, emailDomains); tasksSearchTerm.Add(taskSearchTerm); } } if (tasksSearchTerm.Any()) { var taskWhenAll = Task.WhenAll(tasksSearchTerm); TNDEntityItem[][] results = null; try { results = await taskWhenAll; } catch (Exception exception) { throw; } // flatten var result = new List <TNDEntityItem>(); foreach (var r in results) { if (r != null && r.Any()) { result.AddRange(r); } } return(SortHelper.Sort(result.ToArray())); } return(new TNDEntityItem[0]); } else { var sourceBuis = metaSource.SourceBuis; if (sourceBuis != null) { var taskSearchTerm = sourceBuis.SearchTerm(metaEntity, searchTerm, emails, emailDomains); TNDEntityItem[] result = null; try { result = await taskSearchTerm; } catch (Exception exception) { throw; } return(result); //metaSource.Configuration; } } return(null); }
private static void addEntityItems(BulkObservableCollection <TNDEntityItem> entityItems, BulkObservableCollection <TNDEntityItem> checkedItems, TNDEntityItem[] arrSearchResults) { if (arrSearchResults == null || !arrSearchResults.Any()) { //entityItems.Clear(); } else { //entityItems.Clear(); var dictCheckedItems = checkedItems.ToDictionary(_ => _); var dictEntityItems = new Dictionary <TNDEntityItem, int>(); { int idx = 0; foreach (var entityItem in entityItems) { dictEntityItems[entityItem] = idx; idx++; } } // var oldEntityItems = new List <TNDEntityItem>(entityItems); var newCheckedItems = new List <TNDEntityItem>(); // add the found ones - uses the found. foreach (var searchResult in arrSearchResults) { int idx; if (dictEntityItems.TryGetValue(searchResult, out idx)) { var oldMatch = oldEntityItems[idx]?.GetProperty("Match")?.GetValueAsInt(); var currentMatch = searchResult.GetProperty("Match")?.GetValueAsInt(); oldEntityItems[idx] = null; if (oldMatch.HasValue && (oldMatch.GetValueOrDefault() > currentMatch.GetValueOrDefault())) { searchResult.SetProperty <int>("Match", oldMatch.GetValueOrDefault()); } } if (dictCheckedItems.Remove(searchResult)) { searchResult.IsChecked = true; newCheckedItems.Add(searchResult); } } // add the not found ones - reuse the old. newCheckedItems.AddRange(dictCheckedItems.Keys); var lstSearchResults = new List <TNDEntityItem>(oldEntityItems.Count + arrSearchResults.Length); lstSearchResults.AddRange(oldEntityItems.Where(_ => _ != null)); lstSearchResults.AddRange(arrSearchResults); arrSearchResults = SortHelper.Sort(lstSearchResults.ToArray()); // var sortedNewCheckedItems = newCheckedItems .OrderBy(_ => _.MetaEntity.MetaEntityLevel) .ThenBy(_ => _.MetaEntityName) .ThenBy(_ => _.Name) .ToArray(); lock (entityItems) { checkedItems.Replace(newCheckedItems); entityItems.Replace(arrSearchResults); } } }