Beispiel #1
0
            public void EnsurePersonExists_New_Saved()
            {
                var uuid = Guid.NewGuid();
                var pnr  = Utilities.RandomCprNumber();

                using (var dataContext = new PartDataContext())
                {
                    var person = CprBroker.Engine.Local.UpdateDatabase.EnsurePersonExists(dataContext, new CprBroker.Schemas.PersonIdentifier()
                    {
                        CprNumber = pnr, UUID = uuid
                    });
                    Assert.NotNull(person);
                    Assert.AreEqual(uuid, person.UUID);
                    Assert.AreEqual(pnr, person.UserInterfaceKeyText);

                    Assert.AreEqual(1, dataContext.GetChangeSet().Inserts.Count);
                    Assert.AreEqual(0, dataContext.GetChangeSet().Updates.Count);
                    Assert.AreEqual(0, dataContext.GetChangeSet().Deletes.Count);
                    dataContext.SubmitChanges();
                }

                using (var dataContext = new PartDataContext())
                {
                    var person = dataContext.Persons.Where(p => p.UUID == uuid).First();
                    Assert.NotNull(person);
                    Assert.AreEqual(pnr, person.UserInterfaceKeyText);
                }
            }
Beispiel #2
0
        public static void UpdatePersonUuidArray(string[] cprNumbers, Guid?[] uuids)
        {
            using (var dataContext = new PartDataContext())
            {
                var all = new Dictionary <string, Guid>(cprNumbers.Length);

                for (int i = 0; i < cprNumbers.Length; i++)
                {
                    var uuid = uuids[i];
                    if (uuid.HasValue)
                    {
                        var cprNumber = cprNumbers[i];
                        if (!all.ContainsKey(cprNumber))
                        {
                            all[cprNumber] = uuid.Value;
                        }
                    }
                }
                var mappings = all
                               .Select(kvp => new PersonMapping()
                {
                    CprNumber = kvp.Key,
                    UUID      = kvp.Value
                });

                dataContext.PersonMappings.InsertAllOnSubmit(mappings);
                dataContext.SubmitChanges();
            }
        }
Beispiel #3
0
 public static void UpdatePersonUuid(string cprNumber, Guid uuid)
 {
     using (var dataContext = new PartDataContext())
     {
         PersonMapping map = new PersonMapping()
         {
             CprNumber = cprNumber,
             UUID      = uuid
         };
         dataContext.PersonMappings.InsertOnSubmit(map);
         dataContext.SubmitChanges();
     }
 }
        public override void ProcessPerson(string pnr)
        {
            var  arr  = pnr.Split(',');
            Guid uuid = new Guid(arr[0]);
            Guid personRegistrationId = new Guid(arr[1]);

            using (var dataContext = new PartDataContext(this.BrokerConnectionString))
            {
                var dbReg = dataContext.PersonRegistrations.Where(pr => pr.UUID == uuid && pr.PersonRegistrationId == personRegistrationId).FirstOrDefault();
                var oio   = PersonRegistration.ToXmlType(dbReg);
                oio.OrderByStartDate(false);
                dbReg.SetContents(oio);
                dataContext.SubmitChanges();
            }
        }
Beispiel #5
0
            public void InsertPerson_New_CorrectOutput()
            {
                using (var dataContext = new PartDataContext())
                {
                    var dbPerson = new Person()
                    {
                        UserInterfaceKeyText = Utilities.RandomCprNumber(), UUID = Guid.NewGuid()
                    };
                    var oioRegistration   = Utilities.CreateFakePerson();
                    var newDbRegistration = CprBroker.Engine.Local.UpdateDatabase.InsertPerson(dataContext, dbPerson, oioRegistration);

                    Assert.Greater(dataContext.GetChangeSet().Inserts.Count, 0);
                    Assert.AreEqual(0, dataContext.GetChangeSet().Updates.Count);
                    Assert.AreEqual(0, dataContext.GetChangeSet().Deletes.Count);

                    dataContext.SubmitChanges();
                }
            }
        public override void ProcessPerson(string personRegId)
        {
            BrokerContext.Initialize(this.ApplicationToken, this.UserToken);

            var personRegistrationId = new Guid(personRegId);

            using (var dataContext = new PartDataContext(this.BrokerConnectionString))
            {
                CprBroker.Engine.Local.Admin.AddNewLog(System.Diagnostics.TraceEventType.Information, GetType().Name, string.Format("Processing registration {0}", personRegId), "", "");

                try
                {
                    var dbReg = dataContext.PersonRegistrations.Where(pr => pr.PersonRegistrationId == personRegistrationId).First();
                    var pnr   = dataContext.PersonMappings.Where(pm => pm.UUID == dbReg.UUID).Select(pm => pm.CprNumber).First();
                    Func <string, Guid> cpr2uuidFunc = relPnr =>
                    {
                        relPnr = relPnr.PadLeft(10, ' ');
                        return(dataContext.PersonMappings.Where(pm => pm.CprNumber == relPnr).Select(pm => pm.UUID).First());
                    };

                    //string oldContentsXml, oldSourceXml, newContentsXml, newSourceXml;

                    //TakeCopies(dbReg, out oldContentsXml, out oldSourceXml);

                    var oioReg = CreateXmlType(pnr, dbReg, cpr2uuidFunc);

                    dbReg.SourceObjects = System.Xml.Linq.XElement.Parse(oioReg.SourceObjectsXml);
                    dbReg.SetContents(oioReg);

                    //TakeCopies(dbReg, out newContentsXml, out newSourceXml);

                    //CompareContents(oioReg, oldContentsXml, newContentsXml);
                    //CompareSource(oldSourceXml, newSourceXml);

                    dataContext.SubmitChanges();
                    CprBroker.Engine.Local.Admin.AddNewLog(System.Diagnostics.TraceEventType.Information, GetType().Name, string.Format("Finished registration {0}", personRegId), "", "");
                }
                catch (Exception ex)
                {
                    CprBroker.Engine.Local.Admin.LogException(ex);
                    throw ex;
                }
            }
        }
Beispiel #7
0
 public override void ProcessPerson(string cprNumberOrUuid)
 {
     using (var dataContext = new PartDataContext(this.BrokerConnectionString))
     {
         CprBroker.Engine.BrokerContext.Initialize(this.ApplicationToken, this.UserToken);
         CprBroker.Engine.Local.Admin.AddNewLog(System.Diagnostics.TraceEventType.Information, GetType().Name, string.Format("Preparing dummy data for failed PNR: {0}", cprNumberOrUuid), "", "");
         RegistreringType1 reg  = new RegistreringType1();
         UnikIdType        uuid = new UnikIdType();
         uuid.Item     = CprBroker.Utilities.Constants.FakeActorId.ToString();
         reg.AktoerRef = uuid;
         reg.Tidspunkt = TidspunktType.Create(new DateTime(2013, 9, 24));
         var dbReg = CprBroker.Data.Part.PersonRegistration.FromXmlType(reg);
         dbReg.BrokerUpdateDate = new DateTime(2013, 9, 24);
         dbReg.UUID             = dataContext.PersonMappings.Where(pm => pm.CprNumber == cprNumberOrUuid).First().UUID;
         var pers = dataContext.Persons.Where(p => p.UUID == dbReg.UUID).FirstOrDefault();
         if (pers == null)
         {
             dataContext.Persons.InsertOnSubmit(new Person()
             {
                 UUID = dbReg.UUID,
                 UserInterfaceKeyText = cprNumberOrUuid,
             }
                                                );
             CprBroker.Engine.Local.Admin.AddNewLog(System.Diagnostics.TraceEventType.Information, GetType().Name, string.Format("Adding {0} to Person", cprNumberOrUuid), "", "");
         }
         else
         {
             CprBroker.Engine.Local.Admin.AddNewLog(System.Diagnostics.TraceEventType.Information, GetType().Name, string.Format("Ignoring {0} - is in Person", cprNumberOrUuid), "", "");
         }
         var persReg = dataContext.PersonRegistrations.Where(p => p.UUID == dbReg.UUID).FirstOrDefault();
         if (persReg == null)
         {
             CprBroker.Engine.Local.Admin.AddNewLog(System.Diagnostics.TraceEventType.Information, GetType().Name, string.Format("Adding {0} to PersonRegistration", cprNumberOrUuid), "", "");
             dataContext.PersonRegistrations.InsertOnSubmit(dbReg);
             dataContext.SubmitChanges();
             CprBroker.Engine.Local.Admin.AddNewLog(System.Diagnostics.TraceEventType.Information, GetType().Name, string.Format("Added dummy data for {0}", cprNumberOrUuid), "", "");
         }
         else
         {
             CprBroker.Engine.Local.Admin.AddNewLog(System.Diagnostics.TraceEventType.Information, GetType().Name, string.Format("Ignoring {0} - is in PersonRegistration", cprNumberOrUuid), "", "");
         }
     }
 }
            public void UpdatePersonLists_NotMatching_NotFound([ValueSource("MunicipalityCodes")] string municipalityCode)
            {
                Guid uuid        = Guid.NewGuid();
                Guid personRegId = Guid.NewGuid();

                var pers            = Utils.CreatePerson(uuid);
                var dbReg           = Utils.CreatePersonRegistration(pers, municipalityCode, personRegId, uuid);
                var dataChangeEvent = Utils.CreateDataChangeEvent(dbReg);
                var subscription    = Utils.CreateCriteriaSubscription(municipalityCode + "222");

                using (var cprDataContext = new PartDataContext())
                {
                    cprDataContext.Persons.InsertOnSubmit(pers);
                    cprDataContext.PersonRegistrations.InsertOnSubmit(dbReg);
                    cprDataContext.SubmitChanges();
                }

                var sss = subscription.GetDataChangeEventMatches(new DataChangeEvent[] { dataChangeEvent }).ToArray();

                Assert.IsEmpty(sss);
            }
        public override void ProcessPerson(string pnr)
        {
            using (var dataContext = new PartDataContext())
            {
                var pm  = dataContext.PersonMappings.Where(p => p.CprNumber == pnr).Single();
                var per = dataContext.Persons.Where(p => p.UUID == pm.UUID).SingleOrDefault();
                PersonRegistration[] regs = null;
                var actros = new List <ActorRef>();
                if (per != null)
                {
                    regs = per.PersonRegistrations.ToArray();
                    foreach (var reg in regs)
                    {
                        if (reg.SourceObjects != null)
                        {
                            throw new Exception("Source not null");
                        }

                        var oio = PersonRegistration.ToXmlType(reg);
                        if (oio.AttributListe != null || oio.RelationListe != null || oio.TilstandListe != null)
                        {
                            throw new Exception("Contents not empty");
                        }

                        actros.Add(reg.ActorRef);
                    }


                    // Now delete the records
                    dataContext.PersonRegistrations.DeleteAllOnSubmit(regs);
                    dataContext.ActorRefs.DeleteAllOnSubmit(actros);
                    dataContext.Persons.DeleteOnSubmit(per);
                    dataContext.PersonMappings.DeleteOnSubmit(pm);

                    dataContext.SubmitChanges();
                }
            }
        }
Beispiel #10
0
        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);
                            }
                        }
                    }
                }
            }
        }