Exemplo n.º 1
0
        private static Questionnaire.ItemComponent CreateItemComponentV1(QuestionnaireItem 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)
                    }
                };
            }

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

            if (!string.IsNullOrWhiteSpace(item.ValidationText))
            {
                itemComponent.SetStringExtension(Constants.ValidationTextUri, item.ValidationText);
            }
            if (!string.IsNullOrWhiteSpace(item.ReferenceValue) && item.ReferenceValue.IndexOf('#') == 0)
            {
                itemComponent.AnswerValueSetElement = new Canonical($"#{item.ReferenceValue.Substring(1)}");
            }
            if (!string.IsNullOrWhiteSpace(item.EntryFormat))
            {
                itemComponent.SetStringExtension(Constants.EntryFormatUri, item.EntryFormat);
            }
            if (item.MaxValue.HasValue)
            {
                itemComponent.SetIntegerExtension(Constants.MaxValueUri, item.MaxValue.Value);
            }
            if (item.MinValue.HasValue)
            {
                itemComponent.SetIntegerExtension(Constants.MinValueUri, item.MinValue.Value);
            }

            return(itemComponent);
        }
Exemplo n.º 2
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);
        }