Ejemplo n.º 1
0
        /// <summary>Component from question.</summary>
        /// <param name="format">   Describes the format to use.</param>
        /// <param name="question"> The question.</param>
        /// <param name="itemOrder">[in,out] The item order.</param>
        /// <returns>A Questionnaire.ItemComponent.</returns>
        private static Questionnaire.ItemComponent ComponentFromQuestion(
            IReportingFormat format,
            QuestionnaireQuestion question,
            ref int itemOrder)
        {
            if (!format.Fields.ContainsKey(question.ValueFieldName))
            {
                return(null);
            }

            FormatField valueField = format.Fields[question.ValueFieldName];
            FormatField displayField;

            if (string.IsNullOrEmpty(question.DisplayFieldName) ||
                (!format.Fields.ContainsKey(question.DisplayFieldName)))
            {
                displayField = valueField;
            }
            else
            {
                displayField = format.Fields[question.DisplayFieldName];
            }

            Questionnaire.ItemComponent component = new Questionnaire.ItemComponent()
            {
                LinkId   = valueField.Name,
                Required = valueField.IsRequired == true,
                Repeats  = false,
            };

            if (!string.IsNullOrEmpty(question.FieldSystem))
            {
                component.Code = new List <Coding>()
                {
                    new Coding(question.FieldSystem, valueField.Name),
                };
            }

#if false   // 2020.05.13 - Argonaut extensions aren't valid in R4 yet
            component.AddExtension(
                "http://fhir.org/guides/argonaut/questionnaire/StructureDefinition/extension-itemOrder",
                new FhirDecimal(itemOrder++));
#endif

            if (question.UseTitleOnly)
            {
                component.Text = $"{displayField.Title}";
            }
            else
            {
                component.Text = $"{displayField.Title}: {displayField.Description}";
            }

            int optionOrder = 0;

            switch (valueField.Type)
            {
            case FormatField.FieldType.Date:
                component.Type = Questionnaire.QuestionnaireItemType.Date;
                break;

            case FormatField.FieldType.Count:
                component.Type = Questionnaire.QuestionnaireItemType.Integer;
                break;

            case FormatField.FieldType.Percentage:
                component.Type = Questionnaire.QuestionnaireItemType.Decimal;
                break;

            case FormatField.FieldType.Boolean:
                component.Type = Questionnaire.QuestionnaireItemType.Boolean;
                break;

            case FormatField.FieldType.Choice:
                component.Type         = Questionnaire.QuestionnaireItemType.Choice;
                component.AnswerOption = new List <Questionnaire.AnswerOptionComponent>();

                component.AddExtension(
                    "http://hl7.org/fhir/StructureDefinition/questionnaire-optionExclusive",
                    new FhirBoolean(true),
                    true);

                foreach (FormatFieldOption option in valueField.Options)
                {
                    Element element = new FhirString(option.Text);

#if false   // 2020.05.13 - Argonaut extensions aren't valid in R4 yet
                    element.AddExtension(
                        "http://fhir.org/guides/argonaut/questionnaire/StructureDefinition/extension-itemOrder",
                        new FhirDecimal(optionOrder++));
#endif

                    component.AnswerOption.Add(new Questionnaire.AnswerOptionComponent()
                    {
                        Value = element,
                    });
                }

                break;

            case FormatField.FieldType.MultiSelectChoice:
                component.Type         = Questionnaire.QuestionnaireItemType.Choice;
                component.AnswerOption = new List <Questionnaire.AnswerOptionComponent>();

                foreach (FormatFieldOption option in valueField.Options)
                {
                    Element element = new FhirString(option.Text);

#if false   // 2020.05.13 - Argonaut extensions aren't valid in R4 yet
                    element.AddExtension(
                        "http://fhir.org/guides/argonaut/questionnaire/StructureDefinition/extension-itemOrder",
                        new FhirDecimal(optionOrder++));
#endif

                    component.AnswerOption.Add(new Questionnaire.AnswerOptionComponent()
                    {
                        Value = element,
                    });
                }

                break;

            case FormatField.FieldType.Text:
                component.Type = Questionnaire.QuestionnaireItemType.Text;
                break;

            case FormatField.FieldType.ShortString:
                component.Type = Questionnaire.QuestionnaireItemType.String;
                break;

            case FormatField.FieldType.Display:
            default:
                component.Type = Questionnaire.QuestionnaireItemType.Display;
                break;
            }

            return(component);
        }
Ejemplo n.º 2
0
        /// <summary>Builds a questionnaire.</summary>
        /// <exception cref="ArgumentNullException">Thrown when one or more required arguments are null.</exception>
        /// <param name="format">Describes the format to use.</param>
        /// <returns>A Questionnaire.</returns>
        private static Questionnaire BuildQuestionnaire(
            IReportingFormat format)
        {
            if (format == null)
            {
                throw new ArgumentNullException(nameof(format));
            }

            if (string.IsNullOrEmpty(format.Name))
            {
                throw new ArgumentNullException(nameof(format), $"Invalid IReportingFormat.Name: {format.Name}");
            }

            if ((format.Fields == null) || (format.Fields.Count == 0))
            {
                throw new ArgumentNullException(nameof(format), $"Invalid IReportingFormat.Fields: {format.Fields}");
            }

            if ((format.QuestionnaireSections == null) || (format.QuestionnaireSections.Count == 0))
            {
                throw new ArgumentNullException(nameof(format), $"Invalid IReportingFormat.QuestionnaireSections: {format.QuestionnaireSections}");
            }

            Questionnaire questionnaire = new Questionnaire()
            {
                Meta = new Meta()
                {
                    Profile = new string[]
                    {
                        FhirSystems.Questionnaire,
                    },
                },
                Id           = format.Name,
                Name         = format.Name,
                Url          = $"{CanonicalUrl}/Questionnaire/{format.Name}",
                Version      = QuestionnaireVersion,
                Title        = format.Title,
                Description  = new Markdown(format.Description),
                Status       = PublicationStatus.Draft,
                Date         = PublicationDate,
                Publisher    = Publisher,
                Jurisdiction = new List <CodeableConcept>()
                {
                    FhirTriplet.UnitedStates.GetConcept(),
                },
                UseContext = new List <UsageContext>()
                {
                    new UsageContext()
                    {
                        Code  = FhirTriplet.GetCode(FhirSystems.UsageContextType, CommonLiterals.ContextFocus),
                        Value = FhirTriplet.SctCovid.GetConcept(),
                    },
                },
                Item = new List <Questionnaire.ItemComponent>(),
            };

            int sectionNumber = -1;
            int itemNumber    = 0;

            foreach (QuestionnaireSection questionnaireSection in format.QuestionnaireSections)
            {
                sectionNumber++;
                itemNumber = 0;

                Questionnaire.ItemComponent section = new Questionnaire.ItemComponent()
                {
                    LinkId  = $"section_{sectionNumber}",
                    Type    = Questionnaire.QuestionnaireItemType.Group,
                    Item    = new List <Questionnaire.ItemComponent>(),
                    Repeats = false,
                };

#if false   // 2020.05.13 - Argonaut extensions aren't valid in R4 yet
                section.AddExtension(
                    "http://fhir.org/guides/argonaut/questionnaire/StructureDefinition/extension-itemOrder",
                    new FhirDecimal(sectionNumber));
#endif

                if (format.Fields.ContainsKey(questionnaireSection.Title))
                {
                    section.Text = $"{format.Fields[questionnaireSection.Title].Title}: {format.Fields[questionnaireSection.Title].Description}";
                }
                else
                {
                    section.Text = questionnaireSection.Title;
                }

                foreach (QuestionnaireQuestion question in questionnaireSection.Fields)
                {
                    Questionnaire.ItemComponent component = ComponentFromQuestion(
                        format,
                        question,
                        ref itemNumber);

                    if (component == null)
                    {
                        continue;
                    }

                    section.Item.Add(component);
                }

                questionnaire.Item.Add(section);
            }

            return(questionnaire);
        }
Ejemplo n.º 3
0
        private Questionnaire.ItemComponent CreateItemComponentV2(QuestionnaireItem2 item)
        {
            Questionnaire.QuestionnaireItemType?itemType = EnumUtility.ParseLiteral <Questionnaire.QuestionnaireItemType>(item.Type);
            if (!itemType.HasValue)
            {
                throw new Exception($"QuestionnaireItemType at question with linkId: {item.LinkId} is not conforming to any valid literals. QuestionnaireItemType: {item.Type}");
            }

            Questionnaire.ItemComponent itemComponent = new Questionnaire.ItemComponent
            {
                Type = itemType,
            };

            itemComponent.LinkId = string.IsNullOrWhiteSpace(item.LinkId) ? null : item.LinkId;
            itemComponent.Prefix = string.IsNullOrWhiteSpace(item.Prefix) ? null : item.Prefix;
            itemComponent.Text   = string.IsNullOrWhiteSpace(item.Text) ? null : item.Text;
            if (!string.IsNullOrWhiteSpace(item.EnableWhen))
            {
                itemComponent.EnableWhen = ParseEnableWhen(item.EnableWhen).ToList();
                // TODO: Defaults to 'any' in the first iteration of "migrate to R4".
                itemComponent.EnableBehavior = Questionnaire.EnableWhenBehavior.Any;
            }
            if (itemType != Questionnaire.QuestionnaireItemType.Group && itemType != Questionnaire.QuestionnaireItemType.Display)
            {
                itemComponent.Required = item.Required.HasValue ? item.Required : null;
                itemComponent.ReadOnly = item.ReadOnly;
                itemComponent.Initial  = string.IsNullOrEmpty(item.Initial)
                    ? null
                    : new List <Questionnaire.InitialComponent> {
                    new Questionnaire.InitialComponent {
                        Value = GetElement(itemType.Value, item.Initial)
                    }
                };
                itemComponent.MaxLength = item.MaxLength.HasValue ? item.MaxLength : null;
            }

            if (itemType != Questionnaire.QuestionnaireItemType.Display)
            {
                itemComponent.Repeats = item.Repeats;
            }

            if (!string.IsNullOrWhiteSpace(item.ValidationText))
            {
                itemComponent.SetStringExtension(Constants.ValidationTextUri, item.ValidationText);
            }

            if (!string.IsNullOrWhiteSpace(item.Options) && item.Options.IndexOf('#') == 0)
            {
                itemComponent.AnswerValueSetElement = new Canonical($"#{item.Options.Substring(1)}");
            }
            else if (!string.IsNullOrWhiteSpace(item.Options) &&
                     (item.Options.IndexOf("http://") == 0 || item.Options.IndexOf("https://") == 0))
            {
                itemComponent.AnswerValueSetElement = new Canonical(item.Options);
            }

            if (!string.IsNullOrWhiteSpace(item.EntryFormat))
            {
                itemComponent.SetStringExtension(Constants.EntryFormatUri, item.EntryFormat);
            }

            if (item.MaxValueInteger.HasValue)
            {
                itemComponent.SetIntegerExtension(Constants.MaxValueUri, item.MaxValueInteger.Value);
            }
            if (item.MinValueInteger.HasValue)
            {
                itemComponent.SetIntegerExtension(Constants.MinValueUri, item.MinValueInteger.Value);
            }

            if (item.MaxValueDate.HasValue)
            {
                itemComponent.SetExtension(Constants.MaxValueUri, new FhirDateTime(new DateTimeOffset(item.MaxValueDate.Value.ToUniversalTime())));
            }
            if (item.MinValueDate.HasValue)
            {
                itemComponent.SetExtension(Constants.MinValueUri, new FhirDateTime(new DateTimeOffset(item.MinValueDate.Value.ToUniversalTime())));
            }

            if (item.MinLength.HasValue)
            {
                itemComponent.SetIntegerExtension(Constants.MinLenghtUri, item.MinLength.Value);
            }

            if (item.MaxDecimalPlaces.HasValue)
            {
                itemComponent.SetIntegerExtension(Constants.MaxDecimalPlacesUri, item.MaxDecimalPlaces.Value);
            }

            if (!string.IsNullOrWhiteSpace(item.RepeatsText))
            {
                itemComponent.SetStringExtension(Constants.RepeatsTextUri, item.RepeatsText);
            }

            if (!string.IsNullOrWhiteSpace(item.ItemControl))
            {
                CodeableConcept codeableConcept = new CodeableConcept
                {
                    Coding = new List <Coding> {
                        new Coding
                        {
                            System = Constants.ItemControlSystem,
                            Code   = item.ItemControl
                        }
                    }
                };

                itemComponent.SetExtension(Constants.ItemControlUri, codeableConcept);
            }

            if (item.MaxOccurs.HasValue)
            {
                itemComponent.SetIntegerExtension(Constants.MaxOccursUri, item.MaxOccurs.Value);
            }
            if (item.MinOccurs.HasValue)
            {
                itemComponent.SetIntegerExtension(Constants.MinOccursUri, item.MinOccurs.Value);
            }

            if (!string.IsNullOrWhiteSpace(item.Regex))
            {
                itemComponent.SetStringExtension(Constants.RegexUri, item.Regex);
            }

            if (!string.IsNullOrWhiteSpace(item.Markdown))
            {
                if (itemComponent.Text == null)
                {
                    throw new MissingRequirementException($"Question with linkId: {item.LinkId}. The 'Text' attribute is required when setting the 'Markdown' extension so that form fillers which do not support the 'Markdown' extension still can display informative text to the user.");
                }
                itemComponent.TextElement.SetExtension(Constants.RenderingMarkdownUri, new Markdown(item.Markdown));
            }
            if (!string.IsNullOrWhiteSpace(item.Unit))
            {
                Coding unitCoding = ParseCoding(item.Unit);
                itemComponent.SetExtension(Constants.QuestionnaireUnitUri, unitCoding);
            }

            if (!string.IsNullOrWhiteSpace(item.Code))
            {
                itemComponent.Code = ParseArrayOfCoding(item.Code);
            }

            if (!string.IsNullOrWhiteSpace(item.Option))
            {
                List <Element> options = ParseArrayOfElement(item.Option);
                foreach (DataType element in options)
                {
                    if (element is ResourceReference)
                    {
                        itemComponent.AddExtension(Constants.OptionReferenceUri, element);
                    }
                    else
                    {
                        itemComponent.AnswerOption.Add(new Questionnaire.AnswerOptionComponent {
                            Value = element
                        });
                    }
                }
            }

            if (!string.IsNullOrWhiteSpace(item.FhirPathExpression))
            {
                itemComponent.SetStringExtension(Constants.FhirPathUri, item.FhirPathExpression);
            }

            if (item.Hidden)
            {
                itemComponent.SetBoolExtension(Constants.QuestionnaireItemHidden, item.Hidden);
            }

            if (item.AttachmentMaxSize.HasValue && itemComponent.Type == Questionnaire.QuestionnaireItemType.Attachment)
            {
                itemComponent.SetExtension(Constants.QuestionnaireAttachmentMaxSize, new FhirDecimal(item.AttachmentMaxSize));
            }

            if (!string.IsNullOrWhiteSpace(item.CalculatedExpression))
            {
                itemComponent.SetStringExtension(Constants.CalculatedExpressionUri, item.CalculatedExpression);
            }

            if (!string.IsNullOrWhiteSpace(item.GuidanceAction))
            {
                itemComponent.SetStringExtension(Constants.GuidanceActionUri, item.GuidanceAction.Trim());
            }

            if (!string.IsNullOrWhiteSpace(item.GuidanceParameter))
            {
                itemComponent.SetStringExtension(Constants.GuidanceParameterUri, $"hn_frontend_{item.GuidanceParameter.Trim()}");
            }

            if (!string.IsNullOrWhiteSpace(item.FhirPathValidation))
            {
                itemComponent.SetStringExtension(Constants.FhirPathValidationUri, item.FhirPathValidation);
            }

            if (!string.IsNullOrWhiteSpace(item.FhirPathMaxValue))
            {
                itemComponent.SetStringExtension(Constants.SdfMaxValueUri, item.FhirPathMaxValue);
            }

            if (!string.IsNullOrWhiteSpace(item.FhirPathMinValue))
            {
                itemComponent.SetStringExtension(Constants.SdfMinValueUri, item.FhirPathMinValue);
            }

            if (!string.IsNullOrWhiteSpace(item.EnableBehavior))
            {
                itemComponent.EnableBehavior = EnumUtility.ParseLiteral <Questionnaire.EnableWhenBehavior>(item.EnableBehavior.ToLowerInvariant());
            }

            return(itemComponent);
        }