public void Ret(UserData user) { log.Debug("Attempting Ret on Bruger with uuid " + user.Uuid); RegistreringType1 registration = GetLatestRegistration(user.Uuid); if (registration == null) { log.Debug("Cannot call Ret on Bruger with uuid " + user.Uuid + " because it does not exist in Organisation"); return; } VirkningType virkning = helper.GetVirkning(user.Timestamp); BrugerPortType channel = StubUtil.CreateChannel <BrugerPortType>(BrugerStubHelper.SERVICE, "Ret", helper.CreatePort()); try { bool changes = false; RetInputType1 input = new RetInputType1(); input.UUIDIdentifikator = user.Uuid; input.AttributListe = registration.AttributListe; input.TilstandListe = registration.TilstandListe; input.RelationListe = registration.RelationListe; // set Tilstand to Active (idempotent change - but it ensures that a previously deactived object is re-activated) changes = helper.SetTilstandToActive(virkning, registration, user.Timestamp) | changes; #region Update attributes // compare latest property to the local object EgenskabType latestProperty = StubUtil.GetLatestProperty(input.AttributListe.Egenskab); if (latestProperty == null || !latestProperty.BrugerNavn.Equals(user.UserId) || (user.ShortKey != null && !latestProperty.BrugervendtNoegleTekst.Equals(user.ShortKey))) { // end the validity of open-ended property if (latestProperty == null) { // create ShortKey if not supplied EnsureKeys(user); } // create a new property EgenskabType newProperty = new EgenskabType(); newProperty.Virkning = helper.GetVirkning(user.Timestamp); newProperty.BrugervendtNoegleTekst = ((user.ShortKey != null) ? user.ShortKey : latestProperty.BrugervendtNoegleTekst); newProperty.BrugerNavn = user.UserId; // create a new set of properties input.AttributListe.Egenskab = new EgenskabType[1]; input.AttributListe.Egenskab[0] = newProperty; changes = true; } #endregion #region Update address relationships // terminate the Virkning on all address relationships that no longer exists locally changes = StubUtil.TerminateObjectsInOrgNoLongerPresentLocally(input.RelationListe.Adresser, user.Addresses, user.Timestamp, true) || changes; // add references to address objects that are new List <string> uuidsToAdd = StubUtil.FindAllObjectsInLocalNotInOrg(input.RelationListe.Adresser, user.Addresses, true); if (uuidsToAdd.Count > 0) { int size = uuidsToAdd.Count + ((input.RelationListe.Adresser != null) ? input.RelationListe.Adresser.Length : 0); AdresseFlerRelationType[] newAdresser = new AdresseFlerRelationType[size]; int i = 0; if (input.RelationListe.Adresser != null) { foreach (var addressInOrg in input.RelationListe.Adresser) { newAdresser[i++] = addressInOrg; } } foreach (string uuidToAdd in uuidsToAdd) { foreach (var addressInLocal in user.Addresses) { if (addressInLocal.Uuid.Equals(uuidToAdd)) { string roleUuid = null; switch (addressInLocal.Type) { case AddressRelationType.EMAIL: roleUuid = UUIDConstants.ADDRESS_ROLE_USER_EMAIL; break; case AddressRelationType.PHONE: roleUuid = UUIDConstants.ADDRESS_ROLE_USER_PHONE; break; case AddressRelationType.LOCATION: roleUuid = UUIDConstants.ADDRESS_ROLE_USER_LOCATION; break; default: log.Warn("Cannot add relationship to address of type " + addressInLocal.Type + " with uuid " + addressInLocal.Uuid + " as the type is unknown"); continue; } AdresseFlerRelationType newAddress = helper.CreateAddressReference(uuidToAdd, (i + 1), roleUuid, virkning); newAdresser[i++] = newAddress; } } } input.RelationListe.Adresser = newAdresser; changes = true; } #endregion #region Update organisation relationship if (registration.RelationListe.Tilhoerer != null) { // make sure that the pointer is set correctly if (!StubUtil.GetMunicipalityOrganisationUUID().Equals(registration.RelationListe.Tilhoerer.ReferenceID.Item)) { registration.RelationListe.Tilhoerer.ReferenceID.Item = StubUtil.GetMunicipalityOrganisationUUID(); changes = true; } // update the Virkning on the Tilhører relationship if needed (undelete feature) object endTime = registration.RelationListe.Tilhoerer.Virkning.TilTidspunkt.Item; if (endTime is DateTime && (DateTime.Compare(DateTime.Now, (DateTime)endTime) >= 0)) { log.Debug("Re-establishing relationship with Organisation for Bruger " + user.Uuid); registration.RelationListe.Tilhoerer.Virkning = virkning; changes = true; } } else { // no existing relationship (should actually never happen, but let us just take care of it) helper.AddOrganisationRelation(StubUtil.GetMunicipalityOrganisationUUID(), virkning, registration); changes = true; } #endregion #region Update person relationship PersonFlerRelationType existingPerson = BrugerStubHelper.GetLatestPersonFlerRelationType(registration.RelationListe.TilknyttedePersoner); if (existingPerson != null) { // It really shouldn't happen that often that the Person reference changes on a User, but we support it nonetheless if (!existingPerson.ReferenceID.Item.Equals(user.PersonUuid)) { // terminiate existing relationship, and add a new one StubUtil.TerminateVirkning(existingPerson.Virkning, user.Timestamp); // create a new person relation PersonFlerRelationType newPerson = new PersonFlerRelationType(); newPerson.Virkning = helper.GetVirkning(user.Timestamp); newPerson.ReferenceID = StubUtil.GetReference <UnikIdType>(user.PersonUuid, ItemChoiceType.UUIDIdentifikator); // create a new set of person references, containing the new user PersonFlerRelationType[] oldPersons = registration.RelationListe.TilknyttedePersoner; input.RelationListe.TilknyttedePersoner = new PersonFlerRelationType[oldPersons.Length + 1]; for (int i = 0; i < oldPersons.Length; i++) { input.RelationListe.TilknyttedePersoner[i] = oldPersons[i]; } input.RelationListe.TilknyttedePersoner[oldPersons.Length] = newPerson; changes = true; } } else { // This really shouldn't have happened, as it means we have an existing User without a Person attached - but // we will just fix it, and create the reference on this user log.Warn("Ret on Bruge with uuid " + user.Uuid + " encountered a registration with NO relationship to a Person - fixing it!"); helper.AddPersonRelationship(user.PersonUuid, virkning, registration); changes = true; } #endregion // if no changes are made, we do not call the service if (!changes) { log.Debug("Ret on Bruger with uuid " + user.Uuid + " cancelled because of no changes"); return; } // send Ret request retRequest request = new retRequest(); request.RetRequest1 = new RetRequestType(); request.RetRequest1.RetInput = input; request.RetRequest1.AuthorityContext = new AuthorityContextType(); request.RetRequest1.AuthorityContext.MunicipalityCVR = OrganisationRegistryProperties.GetMunicipality(); retResponse response = channel.ret(request); int statusCode = Int32.Parse(response.RetResponse1.RetOutput.StandardRetur.StatusKode); if (statusCode != 20) { string message = StubUtil.ConstructSoapErrorMessage(statusCode, "Ret", BrugerStubHelper.SERVICE, response.RetResponse1.RetOutput.StandardRetur.FejlbeskedTekst); log.Error(message); throw new SoapServiceException(message); } log.Debug("Ret succesful on Bruger with uuid " + user.Uuid); } catch (Exception ex) when(ex is CommunicationException || ex is IOException || ex is TimeoutException || ex is WebException) { throw new ServiceNotFoundException("Failed to establish connection to the Ret service on Bruger", ex); } }