public static Dictionary<int, List<aim4_dotnet.ImagingObservationCharacteristic>> SelectedImagingObservationCharacteristicsDictionary(AimTemplateTreeImagingObservationNode imagingObservation)
 {
     return imagingObservation.SelectedImagingObservationCharacteristicTreeNodes.ToDictionary(ioc => ioc.ItemNumber,
         ioc => new List<aim4_dotnet.ImagingObservationCharacteristic>(SelectedImagingObservationCharacteristics(ioc)));
 }
        public static List<aim4_dotnet.ImagingObservationEntity> ToSelectedImagingObservationList(AimTemplateTreeImagingObservationNode node)
        {
            var imagingObservations = new List<aim4_dotnet.ImagingObservationEntity>();
            bool isValid = node.Valid;
            foreach (var term in node.SelectedAllowedTerms)
            {
                //var imagingObservation = ToImagingObservation(ToStandardCodeSequence(term), node.Label);
                var imagingObservation = new aim4_dotnet.ImagingObservationEntity
                    {
                        UniqueIdentifier = NewUid,
                        TypeCode = ToNativeCodeList(term),
                        QuestionTypeCode = ToNativeCodeList(node.QuestionType),
                        QuestionIndex = node.ItemNumber,
                        Label = node.Label,
                        IsPresent = true
                    };

                if (node.HasConfidence)
                    imagingObservation.AnnotatorConfidence = node.ConfidenceValue;

                //node
                var selectedImagingObservationCharacteristics = SelectedImagingObservationCharacteristicsDictionary(node);

                // Sort characteristics first according to their ItemNumber
                List<int> imagingObservationCharacteristicIndices =
                    CollectionUtils.Sort(selectedImagingObservationCharacteristics.Keys, (i1, i2) => i1.CompareTo(i2));
                foreach (int characteristicIndex in imagingObservationCharacteristicIndices)
                {
                    // Find definition of the selected characteristic in the component
                    int index = characteristicIndex;
                    AimTemplateTreeImagingObservationCharacteristicNode templateImagingObservationCharacteristic =
                        CollectionUtils.SelectFirst(node.ImagingObservationCharacteristicTreeNodes, item => item.ItemNumber == index);
                    isValid = templateImagingObservationCharacteristic != null &&
                              selectedImagingObservationCharacteristics[characteristicIndex].Count >=
                              Math.Min(templateImagingObservationCharacteristic.CharacteristicQuantificationAllowedTerms.Count, templateImagingObservationCharacteristic.MinCardinality) &&
                              selectedImagingObservationCharacteristics[characteristicIndex].Count <= templateImagingObservationCharacteristic.MaxCardinality;
                    if (!isValid)
                        break;

                    // Put all Anatomic Entity Characteristics into a single collection
                    imagingObservation.ImagingObservationCharacteristicCollection =
                        CollectionUtils.Concat<aim4_dotnet.ImagingObservationCharacteristic>(
                            imagingObservation.ImagingObservationCharacteristicCollection ?? new List<aim4_dotnet.ImagingObservationCharacteristic>(),
                            selectedImagingObservationCharacteristics[characteristicIndex]
                            );
                }
                if (!isValid)
                    break;

                imagingObservations.Add(imagingObservation);
            }
            return imagingObservations;
        }
        public ImagingObservationWpfControl(AimTemplateTreeImagingObservationNode imagingObservation)
        {
            ImagingObservation = imagingObservation;
            InitializeComponent();

            _originalBackground = Background;
            ChildControls = new List<IValidTreeControl>();

            // Hide the allowed terms control that is not being used
            if (MaxNumberOfAnswers > 1)
            {
                _comboBox.Visibility = Visibility.Hidden;
                _comboBox.Height = 0;

                var cardinalityString = String.Empty;

                if (ImagingObservation.MinCardinality > 0 && ImagingObservation.MaxCardinality >= ImagingObservation.AllowedTerms.Count)
                    cardinalityString = "Select at least " + ImagingObservation.MinCardinality;
                else if (ImagingObservation.MaxCardinality < ImagingObservation.AllowedTerms.Count && ImagingObservation.MinCardinality <= 0)
                    cardinalityString = "Select less than " + (ImagingObservation.MaxCardinality + 1);
                else if (ImagingObservation.MinCardinality > 0 && ImagingObservation.MaxCardinality < ImagingObservation.AllowedTerms.Count)
                    cardinalityString = "Select at least " + ImagingObservation.MinCardinality + " and less than " +
                                        (ImagingObservation.MaxCardinality + 1);

                if (!String.IsNullOrEmpty(cardinalityString))
                    ((TextBlock)_label.Content).Text += Environment.NewLine + cardinalityString;
            }
            else
            {
                _itemsControl.Visibility = Visibility.Hidden;
                _itemsControl.Height = 0;
            }

            // Add confidence control
            if (ImagingObservation.ShouldDisplay && ImagingObservation.HasConfidence)
            {
                NodeConfidenceWpfControl = new NodeConfidenceWpfControl(ImagingObservation);
                _stackPanel.Children.Add(NodeConfidenceWpfControl);
            }

            var groupedNodes = new List<AimTemplateTreeNode>();

            foreach (var characteristic in imagingObservation.ImagingObservationCharacteristicTreeNodes)
            {
                // Start or add to group label control
                if (!String.IsNullOrEmpty(characteristic.GroupLabel))
                {
                    if (groupedNodes.Count > 0 && characteristic.GroupLabel == groupedNodes[0].GroupLabel)
                        groupedNodes.Add(characteristic);
                    else
                    {
                        // Finish and add the group label control
                        if (groupedNodes.Count > 0)
                        {
                            var groupLabel = new GroupLabelWpfControl(groupedNodes)
                            {
                                Header = groupedNodes[0].GroupLabel
                            };
                            _stackPanel.Children.Add(groupLabel);
                            ChildControls.Add(groupLabel);
                        }

                        groupedNodes = new List<AimTemplateTreeNode> { characteristic };
                    }
                }
                else
                {
                    // Finish and add the group label control
                    if (groupedNodes.Count > 0)
                    {
                        var groupLabel = new GroupLabelWpfControl(groupedNodes)
                        {
                            Header = groupedNodes[0].GroupLabel
                        };
                        groupedNodes.Clear();
                        _stackPanel.Children.Add(groupLabel);
                        ChildControls.Add(groupLabel);
                    }

                    var characteristicControl = new ImagingObservationCharacteristicWpfControl(characteristic);
                    _stackPanel.Children.Add(characteristicControl);
                    ChildControls.Add(characteristicControl);
                }
            }

            // Finish and add the group label control
            if (groupedNodes.Count > 0)
            {
                var groupLabel = new GroupLabelWpfControl(groupedNodes)
                {
                    Header = groupedNodes[0].GroupLabel
                };
                _stackPanel.Children.Add(groupLabel);
                ChildControls.Add(groupLabel);
            }

            Loaded += ImagingObservationWpfControlLoaded;

            ImagingObservation.SelectedAllowedTermsChanged += SelectedAllowedTermsCollectionChanged;
            CheckBoxControls = new Dictionary<AimTemplateTreeAllowedTerm, CheckBox>();

            if (!ImagingObservation.ShouldDisplay)
            {
                _label.Visibility = Visibility.Hidden;
                _label.Height = 0;
                _itemsControl.Visibility = Visibility.Hidden;
                _itemsControl.Height = 0;
                _comboBox.Visibility = Visibility.Hidden;
                _comboBox.Height = 0;
                if (imagingObservation.ImagingObservationCharacteristicTreeNodes.TrueForAll(item => !item.ShouldDisplay))
                {
                    Visibility = Visibility.Hidden;
                    Height = 0;
                }
            }
        }