/// <summary> /// Map the incoming FHIR resource to a MODEL resource /// </summary> /// <param name="resource">The resource to be mapped</param> /// <returns></returns> protected override Place MapToModel(Location resource) { Place place = null; if (Guid.TryParse(resource.Id, out Guid key)) { place = this.m_repository.Get(key); if (place == null) { place = new Place { Key = key }; } } else if (resource.Identifier.Any()) { foreach (var ii in resource.Identifier.Select(DataTypeConverter.ToEntityIdentifier)) { if (ii.LoadProperty(o => o.Authority).IsUnique) { place = this.m_repository.Find(o => o.Identifiers.Where(i => i.AuthorityKey == ii.AuthorityKey).Any(i => i.Value == ii.Value)).FirstOrDefault(); } if (place != null) { break; } } if (place == null) { place = new Place { Key = Guid.NewGuid() }; } } else { place = new Place { Key = Guid.NewGuid() }; } switch (resource.Status) { case Location.LocationStatus.Active: place.StatusConceptKey = StatusKeys.Active; break; case Location.LocationStatus.Suspended: throw new NotSupportedException(this.m_localizationService.GetString("error.type.NotSupportedException")); case Location.LocationStatus.Inactive: place.StatusConceptKey = StatusKeys.Inactive; break; } // add the textual representation of the name of the place as the address text property for search purposes // see the BirthPlaceExtension class if (!string.IsNullOrEmpty(resource.Address?.Text)) { place.Names.Add(new EntityName(NameUseKeys.Search, resource.Address.Text)); } place.Names.Add(new EntityName(NameUseKeys.OfficialRecord, resource.Name)); place.Names.AddRange(resource.Alias.Select(o => new EntityName(NameUseKeys.Pseudonym, o))); if (resource.Mode == Location.LocationMode.Kind) { place.DeterminerConceptKey = DeterminerKeys.Described; } else { place.DeterminerConceptKey = DeterminerKeys.Specific; } place.TypeConcept = DataTypeConverter.ToConcept(resource.Type.FirstOrDefault()); place.Telecoms = resource.Telecom.Select(DataTypeConverter.ToEntityTelecomAddress).OfType <EntityTelecomAddress>().ToList(); place.Identifiers = resource.Identifier.Select(DataTypeConverter.ToEntityIdentifier).ToList(); if (resource.Address != null) { place.Addresses = new List <EntityAddress>() { DataTypeConverter.ToEntityAddress(resource.Address) } } ; if (resource.Position != null) { place.GeoTag = new GeoTag { Lat = (double)resource.Position.Latitude, Lng = (double)resource.Position.Longitude }; } if (resource.PartOf != null) { var reference = DataTypeConverter.ResolveEntity <Place>(resource.PartOf, resource); if (reference == null) { this.m_tracer.TraceError($"Could not resolve {resource.PartOf.Reference}"); throw new KeyNotFoundException(m_localizationService.FormatString("error.type.KeyNotFoundException.couldNotResolve", new { param = resource.PartOf.Reference })); } // point the child place entity at the target place entity with a relationship of parent place.Relationships.Add(new EntityRelationship(EntityRelationshipTypeKeys.Parent, reference)); } return(place); }