Пример #1
0
        private async Task ShowExistingOptionSets()
        {
            if (!this.IsControlsEnabled)
            {
                return;
            }

            ToggleControls(false, Properties.OutputStrings.LoadingOptionSets);

            this.Dispatcher.Invoke(() =>
            {
                this._itemsSource.Clear();
            });

            IEnumerable <LinkedOptionSetMetadata> list = Enumerable.Empty <LinkedOptionSetMetadata>();

            try
            {
                var service1 = await GetService1();

                var service2 = await GetService2();

                if (service1 != null && service2 != null)
                {
                    var temp = new List <LinkedOptionSetMetadata>();

                    if (!_cacheOptionSetMetadata.ContainsKey(service1.ConnectionData.ConnectionId))
                    {
                        OptionSetRepository repository1 = new OptionSetRepository(service1);

                        var task1List = await repository1.GetOptionSetsAsync();

                        _cacheOptionSetMetadata.Add(service1.ConnectionData.ConnectionId, task1List);
                    }

                    if (!_cacheOptionSetMetadata.ContainsKey(service2.ConnectionData.ConnectionId))
                    {
                        OptionSetRepository repository2 = new OptionSetRepository(service2);

                        var task2List = await repository2.GetOptionSetsAsync();

                        _cacheOptionSetMetadata.Add(service2.ConnectionData.ConnectionId, task2List);
                    }

                    IEnumerable <OptionSetMetadata> list1 = _cacheOptionSetMetadata[service1.ConnectionData.ConnectionId];
                    IEnumerable <OptionSetMetadata> list2 = _cacheOptionSetMetadata[service2.ConnectionData.ConnectionId];

                    string entityName = string.Empty;

                    this.Dispatcher.Invoke(() =>
                    {
                        if (!string.IsNullOrEmpty(cmBEntityName.Text) &&
                            cmBEntityName.Items.Contains(cmBEntityName.Text)
                            )
                        {
                            entityName = cmBEntityName.Text.Trim().ToLower();
                        }
                    });

                    string filterEntity = null;

                    if (service1.ConnectionData.IsValidEntityName(entityName) &&
                        service2.ConnectionData.IsValidEntityName(entityName)
                        )
                    {
                        filterEntity = entityName;
                    }

                    if (!string.IsNullOrEmpty(filterEntity))
                    {
                        {
                            var entityId = service1.ConnectionData.GetEntityMetadataId(filterEntity);

                            if (entityId.HasValue)
                            {
                                var source = GetMetadataSource(service1);

                                var entityMetadata = await source.GetEntityMetadataAsync(entityId.Value);

                                var entityOptionSets = new HashSet <Guid>(entityMetadata
                                                                          .Attributes
                                                                          .OfType <EnumAttributeMetadata>()
                                                                          .Where(a => a.OptionSet != null && a.OptionSet.IsGlobal.GetValueOrDefault())
                                                                          .Select(a => a.OptionSet.MetadataId.Value)
                                                                          );

                                list1 = list1.Where(o => entityOptionSets.Contains(o.MetadataId.Value));
                            }
                        }

                        {
                            var entityId = service2.ConnectionData.GetEntityMetadataId(filterEntity);

                            if (entityId.HasValue)
                            {
                                var source = GetMetadataSource(service2);

                                var entityMetadata = await source.GetEntityMetadataAsync(entityId.Value);

                                var entityOptionSets = new HashSet <Guid>(entityMetadata
                                                                          .Attributes
                                                                          .OfType <EnumAttributeMetadata>()
                                                                          .Where(a => a.OptionSet != null && a.OptionSet.IsGlobal.GetValueOrDefault())
                                                                          .Select(a => a.OptionSet.MetadataId.Value)
                                                                          );

                                list2 = list2.Where(o => entityOptionSets.Contains(o.MetadataId.Value));
                            }
                        }
                    }

                    if (service1.ConnectionData.ConnectionId != service2.ConnectionData.ConnectionId)
                    {
                        foreach (var optionSet1 in list1)
                        {
                            var optionSet2 = list2.FirstOrDefault(e => string.Equals(e.Name, optionSet1.Name, StringComparison.InvariantCultureIgnoreCase));

                            if (optionSet2 == null)
                            {
                                continue;
                            }

                            temp.Add(new LinkedOptionSetMetadata(optionSet1.Name, optionSet1, optionSet2));
                        }
                    }
                    else
                    {
                        foreach (var optionSet1 in list1)
                        {
                            temp.Add(new LinkedOptionSetMetadata(optionSet1.Name, optionSet1, null));
                        }
                    }

                    list = temp;
                }
            }
            catch (Exception ex)
            {
                this._iWriteToOutput.WriteErrorToOutput(null, ex);
            }

            string textName = string.Empty;

            txtBFilter.Dispatcher.Invoke(() =>
            {
                textName = txtBFilter.Text.Trim().ToLower();
            });

            list = FilterList(list, textName);

            LoadEntities(list);

            ToggleControls(true, Properties.OutputStrings.LoadingOptionSetsCompletedFormat1, list.Count());
        }
        private async Task ShowExistingOptionSets()
        {
            if (!this.IsControlsEnabled)
            {
                return;
            }

            var service = await GetService();

            ToggleControls(service.ConnectionData, false, Properties.WindowStatusStrings.LoadingOptionSets);

            this._itemsSource.Clear();

            IEnumerable <OptionSetMetadata> list = Enumerable.Empty <OptionSetMetadata>();

            try
            {
                if (service != null)
                {
                    if (!_cacheOptionSetMetadata.ContainsKey(service.ConnectionData.ConnectionId))
                    {
                        OptionSetRepository repository = new OptionSetRepository(service);

                        var task = repository.GetOptionSetsAsync();

                        var optionSets = await task;

                        _cacheOptionSetMetadata.Add(service.ConnectionData.ConnectionId, optionSets);
                    }

                    list = _cacheOptionSetMetadata[service.ConnectionData.ConnectionId];
                }
            }
            catch (Exception ex)
            {
                this._iWriteToOutput.WriteErrorToOutput(service.ConnectionData, ex);
            }

            string entityName = string.Empty;
            string textName   = string.Empty;

            this.Dispatcher.Invoke(() =>
            {
                textName = txtBFilter.Text.Trim().ToLower();

                if (!string.IsNullOrEmpty(cmBEntityName.Text) &&
                    cmBEntityName.Items.Contains(cmBEntityName.Text)
                    )
                {
                    entityName = cmBEntityName.Text.Trim().ToLower();
                }
            });

            string filterEntity = null;

            if (service.ConnectionData.IsValidEntityName(entityName))
            {
                filterEntity = entityName;
            }

            if (!string.IsNullOrEmpty(filterEntity))
            {
                var entityId = service.ConnectionData.GetEntityMetadataId(filterEntity);

                if (entityId.HasValue)
                {
                    var source = GetMetadataSource(service);

                    var entityMetadata = await source.GetEntityMetadataAsync(entityId.Value);

                    var entityOptionSets = new HashSet <Guid>(entityMetadata
                                                              .Attributes
                                                              .OfType <EnumAttributeMetadata>()
                                                              .Where(a => a.OptionSet != null && a.OptionSet.IsGlobal.GetValueOrDefault())
                                                              .Select(a => a.OptionSet.MetadataId.Value)
                                                              );

                    list = list.Where(o => entityOptionSets.Contains(o.MetadataId.Value));
                }
            }

            list = FilterList(list, textName);

            this.lstVwOptionSets.Dispatcher.Invoke(() =>
            {
                foreach (var entity in list)
                {
                    string name        = entity.Name;
                    string displayName = CreateFileHandler.GetLocalizedLabel(entity.DisplayName);

                    OptionSetMetadataListViewItem item = new OptionSetMetadataListViewItem(name, displayName, entity);

                    this._itemsSource.Add(item);
                }

                if (this.lstVwOptionSets.Items.Count == 1)
                {
                    this.lstVwOptionSets.SelectedItem = this.lstVwOptionSets.Items[0];
                }
            });

            ToggleControls(service.ConnectionData, true, Properties.WindowStatusStrings.LoadingOptionSetsCompletedFormat1, list.Count());
        }