public void TestRelationshipToString() { var you = YouInARoom(out IWorld world); var bob = new Npc("Bob", you.CurrentLocation); var relationship = new PersonalRelationship(bob, you) { Attitude = 500 }; Assert.AreEqual("Bob to Test Wanderer of 500", relationship.ToString()); }
/// <summary> /// Build the filter /// </summary> public string BuildFilter(IComponent data, bool forceExact) { PersonalRelationship prs = data as PersonalRelationship; // Subject relative Person subjectRelative = new Person() { RoleCode = PersonRole.PRS | PersonRole.PAT, Site = data.Site }; if (prs.LegalName != null) { subjectRelative.Names = new List <NameSet>() { prs.LegalName } } ; if (prs.AlternateIdentifiers != null && prs.AlternateIdentifiers.Count > 0) { subjectRelative.AlternateIdentifiers = new List <DomainIdentifier>(prs.AlternateIdentifiers); } PersonPersister prsp = new PersonPersister(); var filterString = prsp.BuildFilter(subjectRelative, forceExact); var registrationEvent = DbUtil.GetRegistrationEvent(data); StringBuilder sb = new StringBuilder(); if (registrationEvent != null) // We don't discriminate on queries for related persons { if (registrationEvent.EventClassifier == RegistrationEventType.Register || registrationEvent.EventClassifier == RegistrationEventType.Replace) { return(""); // We don't discriminate against registration events based on who they're related to } else { sb.Append("SELECT DISTINCT REG_VRSN_ID FROM PSN_VRSN_TBL INNER JOIN PSN_RLTNSHP_TBL ON (PSN_VRSN_TBL.PSN_ID = PSN_RLTNSHP_TBL.TRG_PSN_ID) WHERE PSN_VRSN_TBL.OBSLT_UTC IS NULL AND PSN_VRSN_TBL.PSN_VRSN_ID BETWEEN PSN_RLTNSHP_TBL.EFFT_VRSN_ID AND COALESCE(PSN_RLTNSHP_TBL.OBSLT_VRSN_ID, PSN_VRSN_TBL.PSN_VRSN_ID) "); } } else { sb.Append("SELECT DISTINCT TRG_PSN_ID AS PSN_ID FROM PSN_RLTNSHP_TBL WHERE OBSLT_UTC IS NULL "); } sb.AppendFormat(" AND KIND_CS = '{0}' AND SRC_PSN_ID IN (SELECT PSN_TBL.PSN_ID FROM ({1}) AS PSN_TBL) ", prs.RelationshipKind.Replace("'", "''"), filterString); return(sb.ToString()); //return String.Empty; }
/// <summary> /// Create a personal relationship component /// </summary> protected PersonalRelationship CreatePersonalRelationshipComponent(MARC.Everest.RMIM.CA.R020403.COCT_MT910108CA.PersonalRelationship personalRelationship, List <IResultDetail> dtls) { PersonalRelationship retVal = new PersonalRelationship(); // Identifier if (personalRelationship.Id == null || personalRelationship.Id.IsNull) { dtls.Add(new MandatoryElementMissingResultDetail(ResultDetailType.Error, this.m_localeService.GetString("MSGE02C"), null)); return(null); } retVal.AlternateIdentifiers.Add(new DomainIdentifier() { Domain = personalRelationship.Id.Root, Identifier = personalRelationship.Id.Extension }); // Name if (personalRelationship.RelationshipHolder == null || personalRelationship.RelationshipHolder.NullFlavor != null) { dtls.Add(new MandatoryElementMissingResultDetail(ResultDetailType.Error, this.m_localeService.GetString("MSGE02D"), null)); return(null); } retVal.LegalName = CreateNameSet(personalRelationship.RelationshipHolder.Name, dtls); // Type if (personalRelationship.Code == null || personalRelationship.Code.IsNull || personalRelationship.Code.Code.IsAlternateCodeSpecified) { dtls.Add(new MandatoryElementMissingResultDetail(ResultDetailType.Error, this.m_localeService.GetString("MSGE02E"), null)); return(null); } retVal.RelationshipKind = Util.ToWireFormat(personalRelationship.Code); // Telecom addresses if (personalRelationship.RelationshipHolder.Telecom != null && !personalRelationship.RelationshipHolder.Telecom.IsEmpty) { foreach (var telecom in personalRelationship.RelationshipHolder.Telecom) { retVal.TelecomAddresses.Add(new TelecommunicationsAddress() { Use = telecom.Use == null ? null : Util.ToWireFormat(telecom.Use), Value = telecom.Value }); } } return(retVal); }
public void SetUp() { _mockDatabaseGateway = new Mock <IDatabaseGateway>(); _personalRelationshipsUseCase = new PersonalRelationshipsUseCase(_mockDatabaseGateway.Object); _mockDatabaseGateway.Setup(x => x.GetPersonalRelationshipTypeByDescription(_typeInRequest.Description)) .Returns(_typeInRequest); _request = PersonalRelationshipsHelper.CreatePersonalRelationshipRequest(type: _typeInRequest.Description); _person = TestHelpers.CreatePerson(_request.PersonId); _otherPerson = TestHelpers.CreatePerson(_request.OtherPersonId); _typeInExistingRelationship = PersonalRelationshipsHelper.CreatePersonalRelationshipType("partner"); _relationship = PersonalRelationshipsHelper.CreatePersonalRelationship(_person, _otherPerson, _typeInExistingRelationship); _rel_details = PersonalRelationshipsHelper.CreatePersonalRelationshipDetail(_relationship.Id, "some details for the relationship"); }
public void TestLeadership_NoTargets() { var you = YouInARoom(out IWorld w); //nobody else in room Assert.IsFalse(new LeadershipAction(you).HasTargets(you)); //enemy in room var npc = new Npc("Chaos Bob", you.CurrentLocation); //he doesnt like you var rel = new PersonalRelationship(npc, you) { Attitude = -10 }; w.Relationships.Add(rel); Assert.IsFalse(new LeadershipAction(you).HasTargets(you)); //he is your friend rel.Attitude = 10; Assert.IsTrue(new LeadershipAction(you).HasTargets(you)); }
/// <summary> /// Create personal relationship /// </summary> private MARC.Everest.RMIM.UV.NE2008.PRPA_MT201303UV02.PersonalRelationship CreatePersonalRelationship(PersonalRelationship rel, List <IResultDetail> details) { var retVal = new MARC.Everest.RMIM.UV.NE2008.PRPA_MT201303UV02.PersonalRelationship(); // Identifier retVal.Id = new SET <II>(new II(this.m_configService.OidRegistrar.GetOid("CR_PRID").Oid, rel.Id.ToString())); if (!String.IsNullOrEmpty(rel.RelationshipKind)) { retVal.Code = new CE <string>(rel.RelationshipKind, "2.16.840.1.113883.5.111"); } // Effective time var efftTsExt = rel.FindExtension(o => o.Name == "EffectiveTime"); if (efftTsExt != null) { retVal.EffectiveTime = CreateIVL(efftTsExt.Value as TimestampSet, details); } // Relationship holder var holder = new MARC.Everest.RMIM.UV.NE2008.COCT_MT030007UV.Person(); // Subject identifier var person = rel.FindComponent(HealthServiceRecordSiteRoleType.SubjectOf) as Person; if (person == null) { holder.NullFlavor = NullFlavor.NoInformation; } else { if (person.GenderCode != null) { holder.AdministrativeGenderCode = Util.Convert <AdministrativeGender>(person.GenderCode); } // Birth if (person.BirthTime != null) { holder.BirthTime = CreateTS(person.BirthTime, details); } holder.Id = CreateIISet(person.AlternateIdentifiers, details); // Names holder.Name = new BAG <EN>(); foreach (var name in person.Names) { holder.Name.Add(CreatePN(rel.LegalName, details)); } // Address retVal.Addr = new BAG <AD>(); foreach (var addr in person.Addresses) { retVal.Addr.Add(CreateAD(rel.PerminantAddress, details)); } // Telecom if (person.TelecomAddresses != null) { retVal.Telecom = new BAG <TEL>(); foreach (var tel in person.TelecomAddresses) { retVal.Telecom.Add(CreateTEL(tel, details)); } } retVal.SetRelationshipHolder1(holder); } return(retVal); }
/// <summary> /// Create a personal relationship /// </summary> /// <param name="psn"></param> /// <returns></returns> private Everest.RMIM.UV.NE2008.PRPA_MT201303UV02.PersonalRelationship CreatePersonalRelationship(PersonalRelationship psn) { var retVal = new Everest.RMIM.UV.NE2008.PRPA_MT201303UV02.PersonalRelationship( CreateIISet(psn.AlternateIdentifiers), false, new CE <string>(psn.RelationshipKind, "2.16.840.1.113883.5.111"), psn.PerminantAddress != null ? BAG <AD> .CreateBAG(CreateAD(psn.PerminantAddress)) : null, null, ConvertRoleStatusCode(psn.Status), null, null); // Now to determine the additional parameters if (psn.TelecomAddresses != null) { foreach (var tel in psn.TelecomAddresses) { retVal.Telecom.Add(CreateTEL(tel)); } } // efft time stored as an extension var efftTs = psn.FindExtension(o => o.Name == "EffectiveTime"); if (efftTs != null) { retVal.EffectiveTime = CreateIVL(efftTs.Value as TimestampSet); } // Relationship holder if (psn.GenderCode != null || psn.BirthTime != null || psn.LegalName != null) { var rh = new Everest.RMIM.UV.NE2008.COCT_MT030007UV.Person(); if (psn.GenderCode != null) { rh.AdministrativeGenderCode = psn.GenderCode == "M" ? AdministrativeGender.Male : psn.GenderCode == "F" ? AdministrativeGender.Female : AdministrativeGender.Undifferentiated; } if (psn.BirthTime != null) { rh.BirthTime = CreateTS(psn.BirthTime); } if (psn.LegalName != null) { rh.Name = BAG <EN> .CreateBAG(CreatePN(psn.LegalName)); } } return(retVal); }
/// <summary> /// Persist a component to the database /// </summary> public MARC.HI.EHRS.SVC.Core.DataTypes.VersionedDomainIdentifier Persist(System.Data.IDbConnection conn, System.Data.IDbTransaction tx, System.ComponentModel.IComponent data, bool isUpdate) { // Get config service ISystemConfigurationService configService = ApplicationContext.ConfigurationService; //ApplicationContext.Current.GetService(typeof(ISystemConfigurationService)) as ISystemConfigurationService; // Create the personal relationship in a strongly typed fashion PersonalRelationship pr = data as PersonalRelationship; Person clientContainer = pr.Site.Container as Person, relationshipPerson = null; // Get the person persister var persister = new PersonPersister(); // First, let's see if we can fetch the client if (pr.Id != default(decimal)) { var personalRelationship = this.DePersist(conn, pr.Id, pr.Site.Container, (pr.Site as HealthServiceRecordSite).SiteRoleType, true) as PersonalRelationship; relationshipPerson = personalRelationship.FindComponent(HealthServiceRecordSiteRoleType.SubjectOf) as Person; } else if (pr.AlternateIdentifiers != null) { int i = 0; while (relationshipPerson == null && i < pr.AlternateIdentifiers.Count) { relationshipPerson = persister.GetPerson(conn, tx, pr.AlternateIdentifiers[i++], true); } } if (relationshipPerson == null) { List <DomainIdentifier> candidateId = new List <DomainIdentifier>(); // Is this an existing person (same name and relation) using (IDbCommand cmd = DbUtil.CreateCommandStoredProc(conn, tx)) { cmd.CommandText = "get_psn_rltnshps"; cmd.Parameters.Add(DbUtil.CreateParameterIn(cmd, "src_psn_id_in", DbType.Decimal, clientContainer.Id)); cmd.Parameters.Add(DbUtil.CreateParameterIn(cmd, "src_psn_vrsn_id_in", DbType.Decimal, clientContainer.VersionId)); using (IDataReader rdr = cmd.ExecuteReader()) { while (rdr.Read()) { if (rdr["kind_cs"].ToString() == pr.RelationshipKind) { candidateId.Add(new DomainIdentifier() { Domain = configService.OidRegistrar.GetOid("CR_CID").Oid, Identifier = rdr["src_psn_id"].ToString() }); } } } } // Now load candidates and check foreach (var id in candidateId) { var candidate = persister.GetPerson(conn, tx, id, true); if (NON_DUPLICATE_REL.Contains(pr.RelationshipKind)) { relationshipPerson = candidate; break; } else if (candidate.Names.Exists(n => n.SimilarityTo(pr.LegalName) >= DatabasePersistenceService.ValidationSettings.PersonNameMatch)) { relationshipPerson = candidate; break; } } } // Did we get one? // If not, then we need to register a patient in the database if (relationshipPerson == null) { if (pr.LegalName == null) { throw new DataException(ApplicationContext.LocaleService.GetString("DBCF00B")); } relationshipPerson = new Person() { AlternateIdentifiers = pr.AlternateIdentifiers, Names = new List <NameSet>() { pr.LegalName }, Addresses = new List <AddressSet>() { pr.PerminantAddress }, GenderCode = pr.GenderCode, BirthTime = pr.BirthTime, TelecomAddresses = pr.TelecomAddresses, Status = StatusType.Active, RoleCode = PersonRole.PRS }; var registrationEvent = DbUtil.GetRegistrationEvent(pr).Clone() as RegistrationEvent; registrationEvent.Id = default(decimal); registrationEvent.EventClassifier = RegistrationEventType.ComponentEvent; registrationEvent.RemoveAllFromRole(HealthServiceRecordSiteRoleType.SubjectOf); registrationEvent.Add(relationshipPerson, "SUBJ", HealthServiceRecordSiteRoleType.SubjectOf, null); registrationEvent.Status = StatusType.Completed; // Persist or merge? new RegistrationEventPersister().Persist(conn, tx, registrationEvent, isUpdate); //var clientIdentifier = persister.Persist(conn, tx, relationshipPerson, isUpdate); // Should persist } else if (relationshipPerson.RoleCode != PersonRole.PAT) { var updatedPerson = new Person() { Id = relationshipPerson.Id, AlternateIdentifiers = pr.AlternateIdentifiers, Names = new List <NameSet>() { pr.LegalName }, Addresses = new List <AddressSet>() { }, GenderCode = pr.GenderCode, BirthTime = pr.BirthTime, TelecomAddresses = pr.TelecomAddresses, Status = StatusType.Active, RoleCode = relationshipPerson.RoleCode }; if (pr.PerminantAddress != null) { updatedPerson.Addresses.Add(pr.PerminantAddress); } persister.MergePersons(updatedPerson, relationshipPerson, true); relationshipPerson = updatedPerson; var registrationEvent = DbUtil.GetRegistrationEvent(pr).Clone() as RegistrationEvent; registrationEvent.Id = default(decimal); registrationEvent.EventClassifier = RegistrationEventType.ComponentEvent; registrationEvent.RemoveAllFromRole(HealthServiceRecordSiteRoleType.SubjectOf); registrationEvent.Add(relationshipPerson, "SUBJ", HealthServiceRecordSiteRoleType.SubjectOf, null); registrationEvent.Status = StatusType.Completed; new RegistrationEventPersister().Persist(conn, tx, registrationEvent, isUpdate); } // Validate if (!NON_DUPLICATE_REL.Contains(pr.RelationshipKind) && pr.AlternateIdentifiers.Count == 0 && !relationshipPerson.Names.Exists(o => QueryUtil.MatchName(pr.LegalName, o) >= DatabasePersistenceService.ValidationSettings.PersonNameMatch)) { throw new DataException(ApplicationContext.LocaleService.GetString("DBCF00A")); } // If the container for this personal relationship is a client, then we'll need to link that // personal relationship with the client to whom they have a relation with. if (clientContainer != null) // We need to do some linking { pr.Id = LinkClients(conn, tx, relationshipPerson.Id, clientContainer.Id, pr.RelationshipKind, pr.Status); } else if (clientContainer == null) // We need to do some digging to find out "who" this person is related to (the record target) { throw new ConstraintException(ApplicationContext.LocaleService.GetString("DBCF003")); } // todo: Container is a HSR return(new VersionedDomainIdentifier() { Domain = configService.OidRegistrar.GetOid(ClientRegistryOids.RELATIONSHIP_OID).Oid, Identifier = pr.Id.ToString() }); }
/// <summary> /// De-persist a record from the database /// </summary> public System.ComponentModel.IComponent DePersist(System.Data.IDbConnection conn, decimal identifier, IContainer container, HealthServiceRecordSiteRoleType?roleType, bool loadFast) { // Return value PersonalRelationship retVal = new PersonalRelationship(); // De-persist the observation record ISystemConfigurationService sysConfig = ApplicationContext.ConfigurationService; // De-persist using (IDbCommand cmd = DbUtil.CreateCommandStoredProc(conn, null)) { cmd.CommandText = "get_psn_rltnshp"; cmd.Parameters.Add(DbUtil.CreateParameterIn(cmd, "psn_rltnshp_id_in", DbType.Decimal, identifier)); String clientId = String.Empty; // Execute the reader using (IDataReader rdr = cmd.ExecuteReader()) { if (rdr.Read()) { retVal.Id = identifier; retVal.RelationshipKind = Convert.ToString(rdr["kind_cs"]); // Add prs retVal.AlternateIdentifiers.Add(new DomainIdentifier() { Domain = sysConfig.OidRegistrar.GetOid(ClientRegistryOids.RELATIONSHIP_OID).Oid, Identifier = rdr["rltnshp_id"].ToString() }); retVal.Id = Convert.ToDecimal(rdr["rltnshp_id"]); clientId = rdr["src_psn_id"].ToString(); } else { return(null); } } // First, de-persist the client portions var clientDataRetVal = new PersonPersister().GetPerson(conn, null, new DomainIdentifier() { Domain = sysConfig.OidRegistrar.GetOid(ClientRegistryOids.CLIENT_CRID).Oid, Identifier = clientId.ToString() }, true); if (clientDataRetVal.Names != null) { retVal.LegalName = clientDataRetVal.Names.Find(o => o.Use == NameSet.NameSetUse.Legal) ?? clientDataRetVal.Names[0]; } if (clientDataRetVal.RoleCode == PersonRole.PAT) { retVal.AlternateIdentifiers.AddRange(clientDataRetVal.AlternateIdentifiers.Where(o => o.Domain != sysConfig.OidRegistrar.GetOid(ClientRegistryOids.RELATIONSHIP_OID).Oid)); } else { retVal.AlternateIdentifiers.AddRange(clientDataRetVal.AlternateIdentifiers.Where(o => o.Domain != sysConfig.OidRegistrar.GetOid(ClientRegistryOids.CLIENT_CRID).Oid)); } retVal.Add(clientDataRetVal, "SUBJ", HealthServiceRecordSiteRoleType.SubjectOf, null); } return(retVal); }
/// <summary> /// Update the NK1 segment /// </summary> private void UpdateNK1(PersonalRelationship subject, NK1 nk1) { if (subject.AlternateIdentifiers != null) { foreach (var altId in subject.AlternateIdentifiers) { UpdateCX(altId, nk1.GetNextOfKinAssociatedPartySIdentifiers(nk1.NextOfKinAssociatedPartySIdentifiersRepetitionsUsed)); } } // Birth time if (subject.BirthTime != null) { MARC.Everest.DataTypes.TS ts = new Everest.DataTypes.TS(subject.BirthTime.Value, ReverseLookup(ComponentUtility.TS_PREC_MAP, subject.BirthTime.Precision)); nk1.DateTimeOfBirth.Time.Value = MARC.Everest.Connectors.Util.ToWireFormat(ts); } if (subject.GenderCode != null) { nk1.AdministrativeSex.Value = subject.GenderCode; } if (subject.LegalName != null) { UpdateXPN(subject.LegalName, nk1.GetName(0)); } if (subject.PerminantAddress != null) { UpdateAD(subject.PerminantAddress, nk1.GetAddress(0)); } if (subject.RelationshipKind != null) { nk1.Relationship.Identifier.Value = subject.RelationshipKind; } if (subject.TelecomAddresses != null) { foreach (var tel in subject.TelecomAddresses) { if (tel.Use == "HP" && tel.Value.StartsWith("tel")) { MessageUtil.XTNFromTel(new TEL() { Value = tel.Value, Use = MARC.Everest.Connectors.Util.Convert <SET <CS <TelecommunicationAddressUse> > >(tel.Use), Capabilities = MARC.Everest.Connectors.Util.Convert <SET <CS <TelecommunicationCabability> > >(tel.Capability) }, nk1.GetContactPersonSTelephoneNumber(nk1.ContactPersonSTelephoneNumberRepetitionsUsed)); } else if (tel.Use == "HP") { nk1.GetPhoneNumber(nk1.ContactPersonSTelephoneNumberRepetitionsUsed).EmailAddress.Value = tel.Value; } else if (tel.Use == "WP" && tel.Value.StartsWith("tel")) { MessageUtil.XTNFromTel(new TEL() { Value = tel.Value, Use = MARC.Everest.Connectors.Util.Convert <SET <CS <TelecommunicationAddressUse> > >(tel.Use), Capabilities = MARC.Everest.Connectors.Util.Convert <SET <CS <TelecommunicationCabability> > >(tel.Capability) }, nk1.GetContactPersonSTelephoneNumber(nk1.ContactPersonSTelephoneNumberRepetitionsUsed)); } else if (tel.Use == "WP") { nk1.GetPhoneNumber(nk1.ContactPersonSTelephoneNumberRepetitionsUsed).EmailAddress.Value = tel.Value; } } } }