public void DeselectAllBoundOperationsForSchemaType(SchemaTypeModel schemaType)
 {
     foreach (var boundOperation in schemaType.BoundOperations)
     {
         boundOperation.IsSelected = false;
     }
 }
        public void ClearBoundOperationList_ShouldResetBoundOperationsForTheSpecificTypeToAnEmptyList()
        {
            var schemaType = new SchemaTypeModel {
                ShortName = "Type1", Name = "Test.Type1", IsSelected = false, BoundOperations = new List <BoundOperationModel>
                {
                    new BoundOperationModel
                    {
                        Name       = "BoundOperation1(Test.Type1)",
                        IsSelected = false
                    }
                }
            };

            using (var objectSelection = new SchemaTypesViewModel
            {
                SchemaTypes = new List <SchemaTypeModel>
                {
                    schemaType,
                    new SchemaTypeModel {
                        ShortName = "Type2", Name = "Test.Type2", IsSelected = true
                    },
                    new SchemaTypeModel {
                        ShortName = "Type3", Name = "Test.Type3", IsSelected = false
                    }
                }
            })
            {
                objectSelection.ClearBoundOperationList(schemaType);

                objectSelection.SchemaTypes.FirstOrDefault(s => s.Name == "Test.Type1")?.BoundOperations.Should().BeEmpty();
            }
        }
 public void ExcludeBoundOperations(SchemaTypeModel schemaType, IEnumerable <string> operationsToExclude)
 {
     foreach (var operationModel in schemaType.BoundOperations)
     {
         operationModel.IsSelected = !operationsToExclude.Contains(operationModel.Name);
     }
 }
        public void ExcludeBoundOperations_ShouldDeselectTheSpecifiedBoundOperations()
        {
            var schemaTypeModel = new SchemaTypeModel
            {
                ShortName       = "Type1",
                Name            = "Test.Type1",
                IsSelected      = true,
                BoundOperations = new List <BoundOperationModel>
                {
                    new BoundOperationModel
                    {
                        Name       = "BoundOperation1(Test.Type1)",
                        IsSelected = true
                    },
                    new BoundOperationModel
                    {
                        Name       = "BoundOperation2(Test.Type1)",
                        IsSelected = true
                    }
                }
            };

            using (var objectSelection = new SchemaTypesViewModel
            {
                SchemaTypes = new List <SchemaTypeModel>
                {
                    schemaTypeModel,
                    new SchemaTypeModel {
                        ShortName = "Type2", Name = "Test.Type2", IsSelected = true
                    },
                    new SchemaTypeModel {
                        ShortName = "Type3", Name = "Test.Type3", IsSelected = false
                    },
                    new SchemaTypeModel {
                        ShortName = "Type4", Name = "Test.Type4", IsSelected = true
                    }
                }
            })
            {
                objectSelection.ExcludeBoundOperations(schemaTypeModel, new string[] { "BoundOperation1(Test.Type1)" });

                schemaTypeModel.BoundOperations.ShouldAllBeEquivalentTo(new List <BoundOperationModel>
                {
                    new BoundOperationModel
                    {
                        Name       = "BoundOperation1(Test.Type1)",
                        IsSelected = false
                    },
                    new BoundOperationModel
                    {
                        Name       = "BoundOperation2(Test.Type1)",
                        IsSelected = true
                    }
                });
            }
        }
        public void Convert_ShouldReturnCollapsed_ForSchemaTypeWithoutBoundOperations()
        {
            var schemaType = new SchemaTypeModel
            {
                Name            = "Type1",
                IsSelected      = false,
                BoundOperations = new List <BoundOperationModel>()
            };

            var result = new SchemaTypeModelToVisibilityConverter()
                         .Convert(schemaType, typeof(Visibility), null, CultureInfo.CurrentCulture);

            result.Should()
            .Be(Visibility.Collapsed);
        }
        public void Convert_ShouldReturnVisible_ForSchemaTypeWithBoundOperations()
        {
            var schemaType = new SchemaTypeModel {
                Name = "Type1", IsSelected = false, BoundOperations = new List <BoundOperationModel>
                {
                    new BoundOperationModel
                    {
                        IsSelected = true,
                        Name       = "TestOperation(Type1)"
                    }
                }
            };

            var result = new SchemaTypeModelToVisibilityConverter()
                         .Convert(schemaType, typeof(Visibility), null, CultureInfo.CurrentCulture);

            result.Should()
            .Be(Visibility.Visible);
        }
Esempio n. 7
0
        /// <summary>
        /// Creates a list of types that needs to be loaded on the UI.
        /// Initially all the types are loaded
        /// </summary>
        /// <param name="schemaTypes">A list of schema types that need to be laoded.</param>
        /// <param name="boundOperations">The associated bound operations.</param>
        public void LoadSchemaTypes(
            IEnumerable <IEdmSchemaType> schemaTypes, IDictionary <IEdmStructuredType, List <IEdmOperation> > boundOperations)
        {
            var toLoad = new List <SchemaTypeModel>();

            foreach (var type in schemaTypes)
            {
                if (!SchemaTypeModelMap.ContainsKey(type.FullName()) || SchemaTypeModelMap.Count() != schemaTypes.Count())
                {
                    SchemaTypes = toLoad;
                    SchemaTypeModelMap.Clear();
                    break;
                }
            }

            if (SchemaTypes.Any())
            {
                return;
            }

            foreach (var schemaType in schemaTypes)
            {
                var schemaTypeModel = new SchemaTypeModel()
                {
                    Name      = schemaType.FullTypeName(),
                    ShortName = EdmHelper.GetTypeNameFromFullName(schemaType.FullTypeName())
                };

                // Create propertyChange handler.
                // Anytime a property is selected/unslected, the handler ensures
                // all the related types and operations are selected/unselected
                schemaTypeModel.PropertyChanged += (s, args) =>
                {
                    if (schemaTypeModel.IsSelected && schemaType is IEdmStructuredType structuredType)
                    {
                        //Check for the base type and automatically select it if not selected already.
                        string baseTypeFullName = structuredType.BaseType?.FullTypeName();

                        if (baseTypeFullName != null)
                        {
                            AddRelatedType(baseTypeFullName, structuredType.FullTypeName());

                            if (SchemaTypeModelMap.TryGetValue(baseTypeFullName, out SchemaTypeModel baseTypeSchemaTypeModel) &&
                                !baseTypeSchemaTypeModel.IsSelected)
                            {
                                baseTypeSchemaTypeModel.IsSelected = true;
                            }
                        }

                        // Check the required navigational property types and ensure they are selected as well
                        foreach (var property in structuredType.DeclaredProperties)
                        {
                            string propertyName = property is IEdmNavigationProperty?property.Type.ToStructuredType().FullTypeName() : property.Type.FullName();

                            if (property.Type.ToStructuredType() != null || property.Type.IsEnum())
                            {
                                propertyName = property.Type.ToStructuredType()?.FullTypeName() ?? property.Type.FullName();
                                AddRelatedType(propertyName, structuredType.FullTypeName());

                                bool hasProperty = SchemaTypeModelMap.TryGetValue(propertyName, out SchemaTypeModel navigationPropertyModel);

                                if (hasProperty && !navigationPropertyModel.IsSelected)
                                {
                                    navigationPropertyModel.IsSelected = true;
                                }
                            }
                        }

                        // Check for bound operations and ensure related types are also selected.
                        // In this case related types means return types and parameter types.
                        if (boundOperations.TryGetValue(structuredType, out List <IEdmOperation> operations))
                        {
                            foreach (var operation in operations)
                            {
                                // Check if return type of associated bound operation has been selected.
                                if (operation.ReturnType != null && (operation.ReturnType.ToStructuredType() != null || operation.ReturnType.IsEnum()))
                                {
                                    string returnTypeFullName = operation.ReturnType.ToStructuredType()?.FullTypeName() ?? operation.ReturnType.FullName();
                                    AddRelatedType(returnTypeFullName, structuredType.FullTypeName());

                                    if (SchemaTypeModelMap.TryGetValue(returnTypeFullName, out SchemaTypeModel referencedSchemaTypeModel) &&
                                        !referencedSchemaTypeModel.IsSelected)
                                    {
                                        referencedSchemaTypeModel.IsSelected = true;
                                    }
                                }

                                // Check if parameter types of associated bound operations has been selected.
                                IEnumerable <IEdmOperationParameter> parameters = operation.Parameters;

                                foreach (var parameter in parameters)
                                {
                                    if (parameter.Type.ToStructuredType() != null || parameter.Type.IsEnum())
                                    {
                                        string parameterFullName = parameter.Type.ToStructuredType()?.FullTypeName() ?? parameter.Type.FullName();
                                        AddRelatedType(parameterFullName, structuredType.FullTypeName());

                                        if (SchemaTypeModelMap.TryGetValue(parameterFullName, out SchemaTypeModel model) && !model.IsSelected)
                                        {
                                            model.IsSelected = true;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    else if (!schemaTypeModel.IsSelected)
                    {
                        // automatically deselect related types for deselected type
                        if (RelatedTypes.TryGetValue(schemaTypeModel.Name, out ICollection <string> relatedTypes))
                        {
                            foreach (var relatedType in relatedTypes)
                            {
                                if (SchemaTypeModelMap.TryGetValue(relatedType, out SchemaTypeModel relatedSchemaTypeModel) &&
                                    relatedSchemaTypeModel.IsSelected)
                                {
                                    relatedSchemaTypeModel.IsSelected = false;
                                }
                            }
                        }
                    }
                };

                toLoad.Add(schemaTypeModel);
                schemaTypeModel.IsSelected = true;
                SchemaTypeModelMap.Add(schemaType.FullTypeName(), schemaTypeModel);
            }

            SchemaTypes = toLoad.OrderBy(o => o.Name).ToList();
        }
        public void DeselectAllBoundOperationsForSchemaType_ShouldMarkBoundOperationsForTheSpecificTypeAsNotSelected()
        {
            var schemaType = new SchemaTypeModel
            {
                ShortName       = "Type1",
                Name            = "Test.Type1",
                IsSelected      = false,
                BoundOperations = new List <BoundOperationModel>
                {
                    new BoundOperationModel
                    {
                        Name       = "BoundOperation1(Test.Type1)",
                        IsSelected = true
                    },
                    new BoundOperationModel
                    {
                        Name       = "BoundOperation2(Test.Type1)",
                        IsSelected = true
                    }
                }
            };

            using (var objectSelection = new SchemaTypesViewModel
            {
                SchemaTypes = new List <SchemaTypeModel>
                {
                    schemaType,
                    new SchemaTypeModel {
                        ShortName = "Type2", Name = "Test.Type2", IsSelected = true, BoundOperations = new List <BoundOperationModel>
                        {
                            new BoundOperationModel
                            {
                                Name = "BoundOperation3(Test.Type2)",
                                IsSelected = false
                            }
                        }
                    },
                    new SchemaTypeModel {
                        ShortName = "Type3", Name = "Test.Type3", IsSelected = false
                    }
                }
            })
            {
                objectSelection.DeselectAllBoundOperationsForSchemaType(schemaType);

                objectSelection.SchemaTypes.FirstOrDefault(s => s.Name == "Test.Type1")?.BoundOperations.ShouldAllBeEquivalentTo(new List <BoundOperationModel>
                {
                    new BoundOperationModel
                    {
                        Name       = "BoundOperation1(Test.Type1)",
                        IsSelected = false
                    },
                    new BoundOperationModel
                    {
                        Name       = "BoundOperation2(Test.Type1)",
                        IsSelected = false
                    }
                });
                objectSelection.SchemaTypes.FirstOrDefault(s => s.Name == "Test.Type2")?.BoundOperations.ShouldAllBeEquivalentTo(new List <BoundOperationModel>
                {
                    new BoundOperationModel
                    {
                        Name       = "BoundOperation3(Test.Type2)",
                        IsSelected = false
                    }
                });
            }
        }
 public void ClearBoundOperationList(SchemaTypeModel schemaType)
 {
     schemaType.BoundOperations = new List <BoundOperationModel>();
 }
        /// <summary>
        /// Loads bound operations except the ones that require a type that is excluded
        /// </summary>
        /// <param name="schemaType">a schema type model.</param>
        /// <param name="boundOperations">a list of all the bound operations.</param>
        /// <param name="excludedSchemaTypes">A collection of schema types that will be excluded from generated code.</param>
        /// <param name="schemaTypeModels">a dictionary of schema type and the associated schematypemodel.</param>
        private void LoadBoundOperations(SchemaTypeModel schemaType,
                                         IDictionary <IEdmType, List <IEdmOperation> > boundOperations, ICollection <string> excludedSchemaTypes,
                                         IDictionary <string, SchemaTypeModel> schemaTypeModels)
        {
            var toLoad       = new ObservableCollection <BoundOperationModel>();
            var alreadyAdded = new HashSet <string>();

            foreach (KeyValuePair <IEdmType, List <IEdmOperation> > boundOperation in boundOperations)
            {
                IEdmType edmType = boundOperation.Key;
                foreach (IEdmOperation operation in boundOperation.Value)
                {
                    string name = $"{operation.Name}({edmType.FullTypeName()})";
                    if (!alreadyAdded.Contains(name))
                    {
                        var boundOperationModel = new BoundOperationModel
                        {
                            Name       = name,
                            ShortName  = operation.Name,
                            IsSelected = IsBoundOperationIncluded(operation, excludedSchemaTypes)
                        };

                        boundOperationModel.PropertyChanged += (s, args) =>
                        {
                            if (s is BoundOperationModel currentBoundOperationModel)
                            {
                                IEnumerable <IEdmOperationParameter> parameters = operation.Parameters;

                                foreach (var parameter in parameters)
                                {
                                    var parameterTypeName = parameter.Type.IsCollection()
                                        ? parameter.Type.AsCollection()?.ElementType()?.FullName()
                                        : parameter.Type.FullName();

                                    if (parameterTypeName != null &&
                                        schemaTypeModels.TryGetValue(parameterTypeName, out SchemaTypeModel model) &&
                                        !model.IsSelected && model.IsSelected != currentBoundOperationModel.IsSelected)
                                    {
                                        model.IsSelected = currentBoundOperationModel.IsSelected;
                                    }
                                }

                                string returnTypeName = operation.ReturnType?.IsCollection() == true
                                    ? operation.ReturnType?.AsCollection()?.ElementType()?.FullName()
                                    : operation.ReturnType?.FullName();

                                if (returnTypeName != null &&
                                    schemaTypeModels.TryGetValue(returnTypeName, out SchemaTypeModel schemaTypeModel) &&
                                    !schemaTypeModel.IsSelected &&
                                    schemaTypeModel.IsSelected != currentBoundOperationModel.IsSelected)
                                {
                                    schemaTypeModel.IsSelected = currentBoundOperationModel.IsSelected;
                                }

                                if (this.View is SchemaTypes view)
                                {
                                    view.SelectedBoundOperationsCount.Text = SchemaTypes
                                                                             .Where(x => x.IsSelected && x.BoundOperations?.Any() == true)
                                                                             .SelectMany(x => x.BoundOperations).Count(x => x.IsSelected).ToString(CultureInfo.InvariantCulture);
                                }
                            }
                        };
                        toLoad.Add(boundOperationModel);

                        alreadyAdded.Add(name);
                    }
                }
            }

            schemaType.BoundOperations = toLoad.OrderBy(o => o.Name).ToList();
        }
        /// <summary>
        /// Creates a list of types that needs to be loaded on the UI.
        /// Initially all the types are loaded
        /// </summary>
        /// <param name="schemaTypes">A list of schema types that need to be laoded.</param>
        /// <param name="boundOperations">The associated bound operations.</param>
        public void LoadSchemaTypes(
            IEnumerable <IEdmSchemaType> schemaTypes, IDictionary <IEdmType, List <IEdmOperation> > boundOperations)
        {
            var toLoad = new List <SchemaTypeModel>();

            foreach (var type in schemaTypes)
            {
                if (!SchemaTypeModelMap.ContainsKey(type.FullName()) ||
                    SchemaTypeModelMap.Count != schemaTypes.Count())
                {
                    SchemaTypes = toLoad;
                    SchemaTypeModelMap.Clear();
                    break;
                }
            }

            if (SchemaTypes.Any())
            {
                return;
            }

            foreach (var schemaType in schemaTypes)
            {
                var schemaTypeModel = new SchemaTypeModel
                {
                    Name      = schemaType.FullTypeName(),
                    ShortName = EdmHelper.GetTypeNameFromFullName(schemaType.FullTypeName())
                };

                // Create propertyChange handler.
                // Anytime a property is selected/unslected, the handler ensures
                // all the related types and operations are selected/unselected
                schemaTypeModel.PropertyChanged += (s, args) =>
                {
                    if (schemaTypeModel.IsSelected && schemaType is IEdmStructuredType structuredType)
                    {
                        //Check for the base type and automatically select it if not selected already.
                        string baseTypeFullName = structuredType.BaseType?.FullTypeName();

                        if (baseTypeFullName != null)
                        {
                            AddRelatedType(baseTypeFullName, structuredType.FullTypeName());

                            if (SchemaTypeModelMap.TryGetValue(baseTypeFullName,
                                                               out SchemaTypeModel baseTypeSchemaTypeModel) &&
                                !baseTypeSchemaTypeModel.IsSelected)
                            {
                                baseTypeSchemaTypeModel.IsSelected = true;
                            }
                        }

                        // Check the required property types and ensure they are selected as well
                        foreach (var property in structuredType.DeclaredProperties)
                        {
                            IEdmTypeReference propertyType = property.Type.IsCollection()
                                ? property.Type.AsCollection().ElementType()
                                : property.Type;

                            if (propertyType.ToStructuredType() != null || propertyType.IsEnum())
                            {
                                string propertyTypeName = propertyType.ToStructuredType()?.FullTypeName() ?? propertyType.FullName();
                                AddRelatedType(propertyTypeName, structuredType.FullTypeName());

                                bool hasProperty = SchemaTypeModelMap.TryGetValue(propertyTypeName, out SchemaTypeModel propertySchemaTypeModel);

                                if (hasProperty && !propertySchemaTypeModel.IsSelected)
                                {
                                    propertySchemaTypeModel.IsSelected = true;
                                }
                            }
                        }

                        // Check for bound operations and ensure related types are also selected.
                        // In this case related types means return types and parameter types.
                        if (boundOperations.TryGetValue(structuredType, out List <IEdmOperation> operations))
                        {
                            foreach (var operation in operations)
                            {
                                // Check if return type of associated bound operation has been selected.
                                if (operation.ReturnType != null &&
                                    (operation.ReturnType.ToStructuredType() != null || operation.ReturnType.IsEnum()))
                                {
                                    string returnTypeFullName =
                                        operation.ReturnType.ToStructuredType()?.FullTypeName() ??
                                        operation.ReturnType.FullName();
                                    AddRelatedType(returnTypeFullName, structuredType.FullTypeName());

                                    if (SchemaTypeModelMap.TryGetValue(returnTypeFullName,
                                                                       out SchemaTypeModel referencedSchemaTypeModel) &&
                                        !referencedSchemaTypeModel.IsSelected)
                                    {
                                        referencedSchemaTypeModel.IsSelected = true;
                                    }
                                }

                                // Check if parameter types of associated bound operations has been selected.
                                IEnumerable <IEdmOperationParameter> parameters = operation.Parameters;

                                foreach (var parameter in parameters)
                                {
                                    if (parameter.Type.ToStructuredType() != null || parameter.Type.IsEnum())
                                    {
                                        string parameterFullName =
                                            parameter.Type.ToStructuredType()?.FullTypeName() ??
                                            parameter.Type.FullName();
                                        AddRelatedType(parameterFullName, structuredType.FullTypeName());

                                        if (SchemaTypeModelMap.TryGetValue(parameterFullName,
                                                                           out SchemaTypeModel model) && !model.IsSelected)
                                        {
                                            model.IsSelected = true;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    else if (!schemaTypeModel.IsSelected)
                    {
                        // automatically deselect related types for deselected type
                        if (RelatedTypes.TryGetValue(schemaTypeModel.Name, out ICollection <string> relatedTypes))
                        {
                            foreach (var relatedType in relatedTypes)
                            {
                                if (SchemaTypeModelMap.TryGetValue(relatedType,
                                                                   out SchemaTypeModel relatedSchemaTypeModel) &&
                                    relatedSchemaTypeModel.IsSelected)
                                {
                                    relatedSchemaTypeModel.IsSelected = false;
                                }
                            }
                        }

                        // deselect all related bound operations
                        foreach (var boundOperation in schemaTypeModel.BoundOperations)
                        {
                            boundOperation.IsSelected = false;
                        }
                    }

                    if (this.View is SchemaTypes view)
                    {
                        view.SelectedSchemaTypesCount.Text = SchemaTypes.Count(x => x.IsSelected).ToString(CultureInfo.InvariantCulture);
                    }
                };

                // load bound operations that require the schema type
                var boundOperationsToLoad = boundOperations
                                            .Where(x => x.Key == schemaType || x.Key.AsElementType() == schemaType)
                                            .ToDictionary(x => x.Key, x => x.Value);
                LoadBoundOperations(schemaTypeModel, boundOperationsToLoad,
                                    ExcludedSchemaTypeNames.ToList(), SchemaTypeModelMap);

                toLoad.Add(schemaTypeModel);
                schemaTypeModel.IsSelected = true;
                SchemaTypeModelMap.Add(schemaType.FullTypeName(), schemaTypeModel);
            }

            SchemaTypes = toLoad.OrderBy(o => o.Name).ToList();

            _schemaTypesCount     = SchemaTypes.Count();
            _boundOperationsCount = SchemaTypes.Where(x => x?.BoundOperations?.Any() == true)
                                    .SelectMany(x => x.BoundOperations).Count();
        }