private ListsSubResult SortCompaniesByNewAndExisting(List <APICompany> listToSort, List <APICompany> listToCompareWith) { var newCompanies = new List <APICompany>(); var existingCompanies = new List <APICompany>(); listToSort.ForEach(x => { var match = false; foreach (var y in listToCompareWith) { if (String.IsNullOrWhiteSpace(y.CompanyDirectoryEntryReffNumber)) { continue; } if (x.CompanyDirectoryEntryReffNumber.Equals(y.CompanyDirectoryEntryReffNumber)) { if (0 < (DateTime.Compare(x.Created, y.Created))) { x.Id = y.Id; existingCompanies.Add(x); match = true; break; } else { //We sets match to true so we wont add it to new companies. //The reason is: X is older and we dont want older data overiding new data. //We therefore skip it completely. match = true; break; } } } if (match == false) { newCompanies.Add(x); } }); var subResult = new ListsSubResult(); subResult.ExistingCompanies = existingCompanies; subResult.NewCompanies = newCompanies; return(subResult); }
/// <summary> /// Goes trough companies from stream and compares them with API companies. /// Splits stream companies into existing companies and new companies. /// </summary> /// <param name="dataStreamCompanies">Companies from stream.</param> /// <param name="apiCompanies">Companies from out API</param> /// <returns></returns> public ResultObject PrepareCompanies( List <DataStreamCompany> dataStreamCompanies, List <APICompany> apiCompanies, List <SupplyChainRole> SupplyChainRoles, List <SupplyChainCategory> SuppleChainCategories) { if (!dataStreamCompanies.Any() || !apiCompanies.Any() || !SupplyChainRoles.Any() || !SuppleChainCategories.Any()) { this._badResult.IsSuccesFull = false; this._badResult.ErrorMessage = "One of the lists are empty. Please restart applikation."; return(this._badResult); } //Converts stream data to API Company format var convertedStreamCompanies = ConvertStreamCompanies(dataStreamCompanies, SupplyChainRoles, SuppleChainCategories); //Preperes to sort throug the stream data. var existingCompanies = new List <APICompany>(); var newCompanies = new List <APICompany>(); foreach (var convertedStreamCompany in convertedStreamCompanies) { var streamNameCountryKey = (convertedStreamCompany.CompanyName + convertedStreamCompany.Country).Trim().ToLower(); var added = false; foreach (var apiCompany in apiCompanies) { var apiNameCountryKey = (apiCompany.CompanyName + apiCompany.Country).Trim().ToLower(); //Incase of first run and Reff Key is not yet set into DB. if (apiCompany.CompanyDirectoryEntryReffNumber == null && streamNameCountryKey.Equals(apiNameCountryKey)) { //Adds if exists in DB. convertedStreamCompany.Id = apiCompany.Id; existingCompanies.Add(convertedStreamCompany); added = true; break; } //If Reff Key have been set and it matches. if (apiCompany.CompanyDirectoryEntryReffNumber != null && apiCompany.CompanyDirectoryEntryReffNumber.Equals(convertedStreamCompany.CompanyDirectoryEntryReffNumber)) { //Adds if exists in DB. convertedStreamCompany.Id = apiCompany.Id; existingCompanies.Add(convertedStreamCompany); added = true; break; } } //If none of the above this is the case. if (added == false) { //Adds if not exists in DB. newCompanies.Add(convertedStreamCompany); } } //creating the return object. var subResult = new ListsSubResult(); subResult.ExistingCompanies = existingCompanies; subResult.NewCompanies = newCompanies; this._succesResult.IsSuccesFull = true; this._succesResult.Payload = subResult; return(this._succesResult); }