Example #1
0
        public virtual IEnumerable <IEntity> GetConcepts(Concept concept, IDataRecord reader, KeyMasterOffsetManager offset)
        {
            var personId = reader.GetLong(PersonId);

            if (personId.HasValue)
            {
                var startDate = reader.GetDateTime(StartDate);
                var endDate   = startDate;
                if (!string.IsNullOrEmpty(EndDate))
                {
                    endDate = reader.GetDateTime(EndDate);
                }

                Dictionary <string, string> additionalFields = null;
                if (AdditionalFields != null)
                {
                    additionalFields =
                        new Dictionary <string, string>(AdditionalFields.Length, StringComparer.OrdinalIgnoreCase);
                    foreach (var additionalField in AdditionalFields)
                    {
                        //additionalFields.Add(String.Intern(additionalField.ToLower()), reader.GetString(additionalField));
                        additionalFields.Add(additionalField, reader.GetString(additionalField));
                    }
                }

                foreach (var field in concept.Fields)
                {
                    var sourceValue = field.DefaultSource;
                    if (string.IsNullOrEmpty(sourceValue))
                    {
                        sourceValue = reader.GetString(field.Key);
                    }

                    if (!field.IsNullable && string.IsNullOrEmpty(sourceValue) && field.DefaultConceptId == null &&
                        field.ConceptId == null)
                    {
                        continue;
                    }

                    // Used when: field.Key used for conceptId mapping and
                    // field.SourceKey used for SourceValue (by default field.Key and field.SourceKey are identical)
                    if (!string.IsNullOrEmpty(field.SourceKey))
                    {
                        sourceValue = reader.GetString(field.SourceKey);
                    }

                    foreach (var lookupValue in concept.GetConceptIdValues(Vocabulary, field, reader))
                    {
                        var cId = lookupValue.ConceptId;
                        if (!cId.HasValue && field.DefaultConceptId.HasValue)
                        {
                            cId = field.DefaultConceptId;
                        }

                        if (!concept.IdRequired || cId.HasValue)
                        {
                            var providerIdKey = reader.GetString(ProviderIdKey);
                            if (!string.IsNullOrEmpty(providerIdKey))
                            {
                                providerIdKey = providerIdKey.TrimStart('0');
                            }

                            List <int> ingredients;
                            if (lookupValue.Ingredients == null)
                            {
                                ingredients = new List <int>(1)
                                {
                                    cId ?? 0
                                };
                            }
                            else
                            {
                                ingredients = new List <int>(lookupValue.Ingredients.Count);
                                ingredients.AddRange(lookupValue.Ingredients);
                            }

                            yield return(new Entity
                            {
                                IsUnique = IsUnique,
                                PersonId = personId.Value,
                                SourceValue = sourceValue,
                                ConceptId = cId ?? 0,
                                TypeConceptId = concept.GetTypeId(field, reader),
                                ConceptIdKey = reader.GetString(field.Key),
                                StartDate = startDate,
                                EndDate = endDate == DateTime.MinValue ? (DateTime?)null : endDate,
                                StartTime = reader.GetTime(StartTime),
                                EndTime = reader.GetTime(EndTime),
                                ProviderId = reader.GetInt(ProviderId),
                                ProviderKey = providerIdKey,
                                VisitOccurrenceId = reader.GetLong(VisitOccurrenceId),
                                VisitDetailId = reader.GetLong(VisitDetailId),
                                AdditionalFields = additionalFields,
                                ValidStartDate = lookupValue.ValidStartDate,
                                ValidEndDate = lookupValue.ValidEndDate,
                                SourceConceptId = lookupValue.SourceConceptId,
                                Domain = lookupValue.Domain,
                                //SourceVocabularyId = lookupValue.SourceVocabularyId,
                                VocabularySourceValue = lookupValue.SourceCode,
                                Ingredients = ingredients
                            });
                        }
                    }
                }
            }
        }
Example #2
0
        public override IEnumerable <IEntity> GetConcepts(Concept concept, IDataRecord reader, KeyMasterOffsetManager offset)
        {
            var obsConcepts = base.GetConcepts(concept, reader, offset).ToList();

            if (obsConcepts.Count > 0)
            {
                var unitConcept = GetUnitConcept(reader);
                int?relevantConditionConceptId = null;

                if (Concepts.Length == 3)
                {
                    var relevantConditionConcepts = Concepts[2].GetConceptIdValues(Vocabulary, Concepts[2].Fields[0], reader);

                    if (relevantConditionConcepts.Count > 0)
                    {
                        relevantConditionConceptId = relevantConditionConcepts.Min(c => c.ConceptId);
                    }
                }

                var createObservation = new Func <Observation>(() => new Observation(obsConcepts[0])
                {
                    Id                         = KeyMasterOffsetManager.GetKeyOffset(obsConcepts[0].PersonId).ObservationId,
                    SourceValue                = string.IsNullOrWhiteSpace(obsConcepts[0].SourceValue) ? null : obsConcepts[0].SourceValue,
                    RangeLow                   = reader.GetDecimal(RangeLow),
                    RangeHigh                  = reader.GetDecimal(RangeHigh),
                    UnitsConceptId             = unitConcept.Key ?? 0,
                    UnitsSourceValue           = string.IsNullOrWhiteSpace(unitConcept.Value) ? null : unitConcept.Value,
                    ValueAsConceptId           = reader.GetInt(ValueAsConceptId) ?? 0,
                    QualifierConceptId         = reader.GetInt(QualifierConceptId) ?? 0,
                    QualifierSourceValue       = reader.GetString(QualifierSourceValue),
                    Time                       = reader.GetTime(Time),
                    RelevantConditionConceptId = relevantConditionConceptId ?? 0
                });

                if (ValuesAsNumber != null && ValuesAsNumber.Length > 1)
                {
                    foreach (var valueAsNumber in ValuesAsNumber)
                    {
                        var value = reader.GetDecimal(valueAsNumber);
                        if (value != null)
                        {
                            var observation = createObservation();

                            observation.ValueAsNumber = Math.Round(value.Value, 3);
                            observation.SourceValue   = observation.ValueAsNumber.ToString();
                            observation.TypeConceptId = 38000277;
                            yield return(observation);
                        }
                    }
                }
                else if (ValuesAsString != null && ValuesAsString.Length > 1)
                {
                    foreach (var valueAsString in ValuesAsString)
                    {
                        var value = reader.GetString(valueAsString);
                        if (!string.IsNullOrEmpty(value) && value != "-" && value != "9") //TMP
                        {
                            var observation = createObservation();
                            observation.ValueAsString = reader.GetString(valueAsString);
                            observation.SourceValue   = valueAsString;
                            observation.TypeConceptId = 38000278;
                            yield return(observation);
                        }
                    }
                }
                else if (obsConcepts[0].ConceptId > 0 || !string.IsNullOrEmpty(obsConcepts[0].SourceValue))
                {
                    var observation = createObservation();
                    if (ValuesAsNumber != null && ValuesAsNumber.Length > 0)
                    {
                        observation.ValueAsNumber = reader.GetDecimal(ValuesAsNumber[0]);
                        if (observation.ValueAsNumber.HasValue)
                        {
                            observation.ValueAsNumber = Math.Round(observation.ValueAsNumber.Value, 3);
                        }
                    }
                    if (ValuesAsString != null && ValuesAsString.Length > 0)
                    {
                        observation.ValueAsString = reader.GetString(ValuesAsString[0]);

                        // Workaround, map ValueAsString (usind SourceLookup) and reset SourceValue to original value
                        if (observation.AdditionalFields != null && observation.AdditionalFields.ContainsKey("original_source"))
                        {
                            observation.ValueAsString = observation.SourceValue;
                            observation.SourceValue   = observation.AdditionalFields["original_source"];
                        }
                    }

                    yield return(observation);
                }
            }
        }