private void BulkUpsert(IList <CandidateWithHistory> candidatesWithHistory, IDictionary <Guid, CandidateSummary> candidateSummaries)
        {
            //Have to do these one at a time as need to get the id for the inserted person records
            foreach (var candidateWithHistory in candidatesWithHistory.Where(c => c.CandidatePerson.Person.PersonId == 0))
            {
                //Insert any new person records to match with candidate records
                var personId = (int)_targetDatabase.Insert(candidateWithHistory.CandidatePerson.Person);
                candidateWithHistory.CandidatePerson.Candidate.PersonId = personId;
            }

            //Update any existing person records
            _genericSyncRespository.BulkUpdate(_personTable, candidatesWithHistory.Where(c => c.CandidatePerson.Person.PersonId != 0).Select(c => _candidateMappers.MapPersonDictionary(c.CandidatePerson.Person)));

            //Bulk insert any candidates with valid ids that are not already in the database
            _genericSyncRespository.BulkInsert(_candidateTable, candidatesWithHistory.Where(c => c.CandidatePerson.Candidate.CandidateId != 0 && !candidateSummaries.ContainsKey(c.CandidatePerson.Candidate.CandidateGuid)).Select(c => _candidateMappers.MapCandidateDictionary(c.CandidatePerson.Candidate)));

            //Now insert any remaining candidates one at a time
            foreach (var candidateWithHistory in candidatesWithHistory.Where(c => c.CandidatePerson.Candidate.CandidateId == 0))
            {
                //Ensure school attended and candidate histories have the correct candidate id
                var candidateId = (int)_targetDatabase.Insert(candidateWithHistory.CandidatePerson.Candidate);
                if (candidateWithHistory.CandidatePerson.SchoolAttended != null)
                {
                    candidateWithHistory.CandidatePerson.SchoolAttended.CandidateId = candidateId;
                }
                foreach (var candidateHistory in candidateWithHistory.CandidateHistory)
                {
                    candidateHistory.CandidateId = candidateId;
                }
            }

            //Finally, update existing candidates
            _genericSyncRespository.BulkUpdate(_candidateTable, candidatesWithHistory.Where(c => c.CandidatePerson.Candidate.CandidateId != 0 && candidateSummaries.ContainsKey(c.CandidatePerson.Candidate.CandidateGuid)).Select(c => _candidateMappers.MapCandidateDictionary(c.CandidatePerson.Candidate)));

            //Insert new schools attended
            var newSchoolsAttended = candidatesWithHistory.Where(a => a.CandidatePerson.SchoolAttended != null && a.CandidatePerson.SchoolAttended.SchoolAttendedId == 0).Select(a => a.CandidatePerson.SchoolAttended);

            _genericSyncRespository.BulkInsert(_schoolsAttendedTable, newSchoolsAttended.Select(sa => sa.MapSchoolAttendedDictionary()));

            //Update existing schools attended
            var existingSchoolsAttended = candidatesWithHistory.Where(a => a.CandidatePerson.SchoolAttended != null && a.CandidatePerson.SchoolAttended.SchoolAttendedId != 0).Select(a => a.CandidatePerson.SchoolAttended);

            _genericSyncRespository.BulkUpdate(_schoolsAttendedTable, existingSchoolsAttended.Select(sa => sa.MapSchoolAttendedDictionary()));

            //Insert new candidate history records
            var newCandidateHistories = candidatesWithHistory.SelectMany(a => a.CandidateHistory).Where(a => a.CandidateHistoryId == 0);

            _genericSyncRespository.BulkInsert(_candidateHistoryTable, newCandidateHistories.Select(ah => ah.MapCandidateHistoryDictionary()));

            //Update existing candidate history records
            var existingCandidateHistories = candidatesWithHistory.SelectMany(a => a.CandidateHistory).Where(a => a.CandidateHistoryId != 0);

            _genericSyncRespository.BulkUpdate(_candidateHistoryTable, existingCandidateHistories.Select(ah => ah.MapCandidateHistoryDictionary()));
        }
示例#2
0
        private void FlushUpdate(bool force)
        {
            if ((!force && _toUpdate.Count < _maxBatchSize) || _toUpdate.Count == 0)
            {
                return;
            }

            try
            {
                _log.Info($"Updating {_toUpdate.Count} records on {_tableDetails.Name}");
                _syncRepository.BulkUpdate(_tableDetails, _toUpdate.Select(r => (IDictionary <string, object>)r));
            }
            finally
            {
                _toUpdate.Clear();
            }
        }
        public void BulkUpsert(IList <ApplicationWithHistory> applicationsWithHistory, IDictionary <Guid, int> applicationIds)
        {
            //Bulk insert any applications with valid ids that are not already in the database
            _genericSyncRespository.BulkInsert(_applicationTable, applicationsWithHistory.Where(a => a.ApplicationWithSubVacancy.Application.ApplicationId != 0 && !applicationIds.ContainsKey(a.ApplicationWithSubVacancy.Application.ApplicationGuid)).Select(a => _applicationMappers.MapApplicationDictionary(a.ApplicationWithSubVacancy.Application)));

            //Now insert any remaining applications one at a time
            foreach (var applicationWithHistory in applicationsWithHistory.Where(a => a.ApplicationWithSubVacancy.Application.ApplicationId == 0))
            {
                //Ensure schools attended and application histories have the correct application id
                var applicationId = (int)_targetDatabase.Insert(applicationWithHistory.ApplicationWithSubVacancy.Application);
                if (applicationWithHistory.ApplicationWithSubVacancy.SchoolAttended != null)
                {
                    applicationWithHistory.ApplicationWithSubVacancy.SchoolAttended.ApplicationId = applicationId;
                }
                foreach (var applicationHistory in applicationWithHistory.ApplicationHistory)
                {
                    applicationHistory.ApplicationId = applicationId;
                }
            }

            //Finally, update existing applications
            _genericSyncRespository.BulkUpdate(_applicationTable, applicationsWithHistory.Where(a => a.ApplicationWithSubVacancy.Application.ApplicationId != 0 && applicationIds.ContainsKey(a.ApplicationWithSubVacancy.Application.ApplicationGuid)).Select(a => _applicationMappers.MapApplicationDictionary(a.ApplicationWithSubVacancy.Application)));

            //Insert new application history records
            var newApplicationHistories = applicationsWithHistory.SelectMany(a => a.ApplicationHistory).Where(a => a.ApplicationHistoryId == 0);

            _genericSyncRespository.BulkInsert(_applicationHistoryTable, newApplicationHistories.Select(ah => ah.MapApplicationHistoryDictionary()));

            //Update existing application history records
            var existingApplicationHistories = applicationsWithHistory.SelectMany(a => a.ApplicationHistory).Where(a => a.ApplicationHistoryId != 0);

            _genericSyncRespository.BulkUpdate(_applicationHistoryTable, existingApplicationHistories.Select(ah => ah.MapApplicationHistoryDictionary()));

            //Insert new schools attended
            var newSchoolsAttended = applicationsWithHistory.Where(a => a.ApplicationWithSubVacancy.SchoolAttended != null && a.ApplicationWithSubVacancy.SchoolAttended.SchoolAttendedId == 0).Select(a => a.ApplicationWithSubVacancy.SchoolAttended);

            _genericSyncRespository.BulkInsert(_schoolsAttendedTable, newSchoolsAttended.Select(sa => sa.MapSchoolAttendedDictionary()));

            //Update existing schools attended
            var existingSchoolsAttended = applicationsWithHistory.Where(a => a.ApplicationWithSubVacancy.SchoolAttended != null && a.ApplicationWithSubVacancy.SchoolAttended.SchoolAttendedId != 0).Select(a => a.ApplicationWithSubVacancy.SchoolAttended);

            _genericSyncRespository.BulkUpdate(_schoolsAttendedTable, existingSchoolsAttended.Select(sa => sa.MapSchoolAttendedDictionary()));

            var subVacancies         = applicationsWithHistory.Where(a => a.ApplicationWithSubVacancy.SubVacancy != null).Select(a => a.ApplicationWithSubVacancy.SubVacancy).ToList();
            var existingSubVacancies = _destinationSubVacancyRepository.GetApplicationSummariesByIds(subVacancies.Select(sv => sv.AllocatedApplicationId));

            //Insert new sub vacancies
            _genericSyncRespository.BulkInsert(_subVacanciesTable, subVacancies.Where(sv => !existingSubVacancies.ContainsKey(sv.AllocatedApplicationId)).Select(sv => _applicationMappers.MapSubVacancyDictionary(sv)));

            //Update existing sub vacancies
            _genericSyncRespository.BulkUpdate(_subVacanciesTable, subVacancies.Where(sv => existingSubVacancies.ContainsKey(sv.AllocatedApplicationId)).Select(sv => _applicationMappers.MapSubVacancyDictionary(sv)));

            //Patch in any changes to the applications in Mongo based on what was discovered through processing
            foreach (var applicationWithSubVacancy in applicationsWithHistory.Select(a => a.ApplicationWithSubVacancy))
            {
                if (applicationWithSubVacancy.UpdateNotes)
                {
                    _updateVacancyApplicationsRepository.UpdateApplicationNotes(applicationWithSubVacancy.Application.ApplicationGuid, applicationWithSubVacancy.Application.AllocatedTo);
                }
                if (applicationWithSubVacancy.UpdateStatusTo.HasValue)
                {
                    _updateVacancyApplicationsRepository.UpdateApplicationStatus(applicationWithSubVacancy.Application.ApplicationGuid, applicationWithSubVacancy.UpdateStatusTo.Value);
                }
            }
        }