public CprBroker.Schemas.Part.RegistreringType1 Read(PersonIdentifier uuid, CprBroker.Schemas.Part.LaesInputType input, Func <string, Guid> cpr2uuidFunc, out QualityLevel?ql) { Schemas.Part.RegistreringType1 ret = null; var fromRegistrationDate = TidspunktType.ToDateTime(input.RegistreringFraFilter); var toRegistrationDate = TidspunktType.ToDateTime(input.RegistreringTilFilter); var fromEffectDate = TidspunktType.ToDateTime(input.VirkningFraFilter); var ToEffectDate = TidspunktType.ToDateTime(input.VirkningTilFilter); using (var dataContext = new PartDataContext()) { Data.Part.PersonRegistration.SetChildLoadOptions(dataContext); ret = ( from personReg in dataContext.PersonRegistrations where personReg.UUID == uuid.UUID // Filter by registration date && (!fromRegistrationDate.HasValue || personReg.RegistrationDate >= fromRegistrationDate) && (!toRegistrationDate.HasValue || personReg.RegistrationDate <= toRegistrationDate) // TODO: Filter by effect date orderby personReg.RegistrationDate descending, personReg.BrokerUpdateDate descending select Data.Part.PersonRegistration.ToXmlType(personReg) ).FirstOrDefault(); } ql = QualityLevel.LocalCache; return(ret); }
public static bool MergePersonRegistration(PersonIdentifier personIdentifier, Schemas.Part.RegistreringType1 oioRegistration, out Guid?personRegistrationId) { using (var dataContext = new PartDataContext()) { // Load possible equal registrations bool dataChanged; personRegistrationId = null; var existingInDb = MatchPersonRegistration(personIdentifier, oioRegistration, dataContext, out dataChanged); Func <PersonRegistration[], Guid?> latestRegistrationFunc = (dbRegs) => { if (dbRegs.Count() > 0) { return(dbRegs.OrderByDescending(db => db.RegistrationDate).ThenByDescending(db => db.BrokerUpdateDate).Select(db => db.PersonRegistrationId).FirstOrDefault()); } return(null); }; // If key & contents match was not found if (existingInDb.Length == 0) { var dbPerson = EnsurePersonExists(dataContext, personIdentifier); var dbReg = InsertPerson(dataContext, dbPerson, oioRegistration); dataContext.SubmitChanges(); personRegistrationId = dbReg.PersonRegistrationId; return(true); } else // key & content match found { if (string.IsNullOrEmpty(oioRegistration.SourceObjectsXml)) { // No need to update anything - just commit if needed if (dataChanged) { dataContext.SubmitChanges(); personRegistrationId = latestRegistrationFunc(existingInDb); } return(dataChanged); } else { var existinginDbWithoutSource = existingInDb.Where(db => db.SourceObjects == null || db.SourceObjects.IsEmpty).ToArray(); // If existing registration(s) has exactly same keys and contents , with empty SourceObjects, update source objects if (existinginDbWithoutSource.Length > 0) { Array.ForEach <PersonRegistration>( existinginDbWithoutSource, db => db.SourceObjects = XElement.Parse(oioRegistration.SourceObjectsXml)); dataContext.SubmitChanges(); personRegistrationId = latestRegistrationFunc(existinginDbWithoutSource); return(true); } else { var xml = XElement.Parse(oioRegistration.SourceObjectsXml).ToString(); var existinginDbWithEqualSource = existingInDb .Where(db => db.SourceObjects != null && !db.SourceObjects.IsEmpty && db.SourceObjects.ToString().Equals(xml)) .ToArray(); // If existing registration has exactly same keys and contents and SourceObjects, return without doing anything - no need to update if (existinginDbWithEqualSource.Count() > 0) { // Already up to date - just save updated records if needed if (dataChanged) { dataContext.SubmitChanges(); personRegistrationId = latestRegistrationFunc(existinginDbWithEqualSource); } return(dataChanged); } else { // Otherwise : insert new registration var dbPerson = EnsurePersonExists(dataContext, personIdentifier); var dbReg = InsertPerson(dataContext, dbPerson, oioRegistration); dataContext.SubmitChanges(); personRegistrationId = dbReg.PersonRegistrationId; return(true); } } } } } }
/// <summary> /// Updates the system database with person registration objects /// </summary> /// <param name="personIdentifier"></param> /// <param name="personRegistraion"></param> public static void UpdatePersonRegistration(PersonIdentifier personIdentifier, Schemas.Part.RegistreringType1 personRegistraion) { // TODO: differentiate the 'true' returned between cases when new data is inserted or just SourceObjects is updated Guid?personRegistrationId = null; if (MergePersonRegistration(personIdentifier, personRegistraion, out personRegistrationId)) { // TODO: move this call to a separate phase in request processing NotifyPersonRegistrationUpdate(personIdentifier.UUID.Value, personRegistrationId.Value); } }