/// <summary> /// Adds suitable listeners to capture all given glob patterns for the given folder, /// and - if subfolders is set to true - all its subfolders. /// Does nothing if no folder with the give path exists. /// </summary> public Task ListenAsync(string folder, bool subfolders, Action <ImmutableDictionary <Uri, ImmutableHashSet <string> > > onInitialState, params string[] globPatterns) { if (!Directory.Exists(folder)) { return(Task.CompletedTask); } folder = folder.TrimEnd(Path.DirectorySeparatorChar); globPatterns = globPatterns.Distinct().ToArray(); this.globPatterns.AddOrUpdate(new Uri(folder), globPatterns, (_, currentPatterns) => currentPatterns.Concat(globPatterns).Distinct().ToArray()); var filters = globPatterns.Select(p => (p, NotifyFilters.FileName | NotifyFilters.LastWrite)); if (subfolders) { filters = filters.Concat(new (string, NotifyFilters)[] { (string.Empty, NotifyFilters.DirectoryName) });
/// <summary> /// Suggests other potential options based on the entities passed /// </summary> /// <param name="rows">The rows being imported</param> /// <returns>Entites with suggestions</returns> public Suggestions SuggestEntites(ImportRow[] rows) { var rowSuggestionsConcurrentDictionary = new ConcurrentDictionary<int, RowSuggestions>(); var suggestionsToReturn = new Suggestions(); var clients = new ConcurrentDictionary<Guid, FoundOps.Api.Models.Client>(); var locations = new ConcurrentDictionary<Guid, FoundOps.Api.Models.Location>(); var contactInfoSets = new ConcurrentDictionary<Guid, FoundOps.Api.Models.ContactInfo>(); Parallel.For((long)0, rows.Count(), rowIndex => { var row = rows[rowIndex]; var rowSuggestions = new RowSuggestions(); #region Location if (row.Location != null) { //Find all the Locations to be suggested by finding all Locations for the Client of the row var locationSuggestions = row.Client != null ? _locations.Where(l => l.Value.ClientId == row.Client.Id).ToArray() : null; if (locationSuggestions != null) { //Add any of the suggestions to the rows suggestions rowSuggestions.LocationSuggestions.AddRange(locationSuggestions.Select(l => l.Key)); var convertedLocationSuggestions = locationSuggestions.Select(l => l.Value).Select(FoundOps.Api.Models.Location.ConvertModel); //Add all suggested Locations to the list of Locations to be returned foreach (var location in convertedLocationSuggestions) locations.GetOrAdd(location.Id, location); } //Add the matched/new location as the first suggestion rowSuggestions.LocationSuggestions.Add(row.Location.Id); //Add the location passed to the list of location entites locations.GetOrAdd(row.Location.Id, row.Location); } #endregion #region Client if (row.Client != null) { //Find all the Clients to be suggested by finding all Clients for the Location of the row var clientSuggestions = row.Location != null ? _clients.Where(c => c.Key == row.Location.ClientId).ToArray() : null; if (clientSuggestions != null) { //Add any of the suggestions to the rows suggestions rowSuggestions.ClientSuggestions.AddRange(clientSuggestions.Select(c => c.Key)); var convertedClientSuggestions = clientSuggestions.Select(c => c.Value).Select(FoundOps.Api.Models.Client.ConvertModel); //Add all suggested Clients to the list of Clients to be returned foreach (var client in convertedClientSuggestions) clients.GetOrAdd(client.Id, client); } //Add the matched/new client as the first suggestion rowSuggestions.ClientSuggestions.Add(row.Client.Id); //Add the Client passed to the list of client entites clients.GetOrAdd(row.Client.Id, row.Client); } #endregion //Repeat if (row.Repeat != null) rowSuggestions.Repeats.Add(row.Repeat); //Contact Info if (row.ContactInfoSet.Count != 0) { rowSuggestions.ContactInfoSuggestions.AddRange(row.ContactInfoSet.Select(ci => ci.Id)); foreach (var contactInfoSet in row.ContactInfoSet) contactInfoSets.GetOrAdd(contactInfoSet.Id, contactInfoSet); } //Add this row's suggestions to the list to be returned rowSuggestionsConcurrentDictionary.GetOrAdd((int)rowIndex, rowSuggestions); }); //Order the row suggestions by rowIndex suggestionsToReturn.RowSuggestions.AddRange(rowSuggestionsConcurrentDictionary.OrderBy(kvp => kvp.Key).Select(kvp => kvp.Value)); //Only add distinct Clients var distinctClients = clients.Distinct(); suggestionsToReturn.Clients.AddRange(distinctClients.Select(dc => dc.Value)); //Only add distinct Locations var distinctLocations = locations.Distinct(); suggestionsToReturn.Locations.AddRange(distinctLocations.Select(dl => dl.Value)); //Only add distinct ContactInfo var distinctContactInfo = contactInfoSets.Select(ci => ci.Value).Distinct(); suggestionsToReturn.ContactInfoSet.AddRange(distinctContactInfo); return suggestionsToReturn; }