Exemplo n.º 1
0
        public IActionResult addChannel(ModelRelation payload)
        {
            if (!UserIsInRole(UserTypeEnum.Admin, UserTypeEnum.User))
            {
                return(Unauthorized("You are not in role to permit this action"));
            }

            _service.RegisterChannel(payload);
            return(Ok());
        }
        public void InitForRelationChange(ushort id, ushort rid, ushort relatedid, ModificationType modificationtype)
        {
            if (!IsInitialized)
            {
                Id               = id;
                TargetId         = rid;
                TargetItemType   = TargetItemType.Relation;
                ModificationType = modificationtype;

                ModificationDetails = new ModelRelation(rid, relatedid);

                IsInitialized = true;
            }
        }
Exemplo n.º 3
0
        public bool RemoveChannel(ModelRelation paylaod)
        {
            var relation = _relationRepository.RemoveChannel(paylaod);

            return(_repository.SaveChanges());
        }
Exemplo n.º 4
0
 public bool RegisterChannel(ModelRelation paylaod)
 {
     _relationRepository.Create(paylaod);
     return(_relationRepository.SaveChanges());
 }
Exemplo n.º 5
0
        public override List <string> Build()
        {
            var resultErrors    = new List <string>();
            var modelDefinition = this.ModelDefinition;
            var modelSchema     = this.ModelSchema ?? (this.ModelSchema = new ModelSchema());

            this.EdmModel = null;

            IEdmModel edmModel;

            using (var reader = new System.IO.StringReader(modelDefinition.MetaData)) {
                using (var xmlReader = System.Xml.XmlReader.Create(reader)) {
                    IEnumerable <EdmError> edmErrors;
                    var parseResult = Microsoft.Data.Edm.Csdl.EdmxReader.TryParse(xmlReader, out edmModel, out edmErrors);
                    if (!parseResult)
                    {
                        resultErrors.AddRange(edmErrors.Select(_ => _.ErrorMessage));
                        //throw new InvalidOperationException("Failed to load model : " + string.Join(Environment.NewLine, errors.Select(e => e.ErrorMessage)));
                        return(resultErrors);
                    }
                }
            }

            this.EdmModel = edmModel;
            // this.Rules.

            var lstTypeDefinition  = edmModel.SchemaElements.Where(schemaElement => schemaElement.SchemaElementKind == Microsoft.Data.Edm.EdmSchemaElementKind.TypeDefinition).ToList();
            var lstValueTerm       = edmModel.SchemaElements.Where(schemaElement => schemaElement.SchemaElementKind == Microsoft.Data.Edm.EdmSchemaElementKind.ValueTerm).ToList();
            var lstEntityContainer = edmModel.SchemaElements.Where(schemaElement => schemaElement.SchemaElementKind == Microsoft.Data.Edm.EdmSchemaElementKind.EntityContainer).ToList();

            var typeDefinitionsByName = new Dictionary <ModelEntityName, ModelTableType>(ModelUtility.Instance.ModelEntityNameEqualityComparer);
            var entityByName          = new Dictionary <ModelEntityName, ModelEntity>(ModelUtility.Instance.ModelEntityNameEqualityComparer);

            foreach (var typeDefinition in lstTypeDefinition)
            {
                // typeDefinition.FullName
                if (typeDefinition is Microsoft.Data.Edm.IEdmStructuredType structuredType)
                {
                    var modelTableType = new ModelTableType();
                    modelTableType.Name = ConvertToModelEntityName(typeDefinition);
                    modelSchema.TableTypes.Add(modelTableType);
                    typeDefinitionsByName[modelTableType.Name] = modelTableType;
                    var declaredStructuralProperties = structuredType.DeclaredStructuralProperties().ToArray();
                    var declaredProperties           = structuredType.DeclaredProperties.ToArray();
                    foreach (var declaredProperty in declaredProperties)
                    {
                        if (declaredProperty.PropertyKind == EdmPropertyKind.None)
                        {
                            var modelProperty = new ModelProperty();
                            modelProperty.Name = declaredProperty.Name;
                            // modelProperty.Type
                            var declaredPropertyType = declaredProperty.Type;
                            modelTableType.Properties.Add(modelProperty);
                        }
                        else if (declaredProperty.PropertyKind == EdmPropertyKind.Structural)
                        {
                            var modelProperty = new ModelProperty();
                            modelProperty.Name = declaredProperty.Name;
                            // modelProperty.Type
                            var declaredPropertyType = declaredProperty.Type;
                            if (declaredPropertyType is IEdmPrimitiveTypeReference primitiveTypeReference)
                            {
                                var modelScalarType = new ModelScalarType();
                                var definition      = primitiveTypeReference.Definition;
                                if (definition is Microsoft.Data.Edm.IEdmSchemaElement definitionAsSchemaElement)
                                {
                                    var definitionName = ConvertToModelEntityName(definitionAsSchemaElement);
                                    modelScalarType.Name = definitionName;
                                }
                                else
                                {
                                    throw new InvalidOperationException();
                                }
                                modelScalarType.IsNullable = primitiveTypeReference.IsNullable;
                                modelProperty.Type         = modelScalarType;
                            }
                            else
                            {
                            }
                            modelTableType.Properties.Add(modelProperty);
                        }
                        else if (declaredProperty.PropertyKind == EdmPropertyKind.Navigation)
                        {
                            var modelProperty = new ModelProperty();
                            modelProperty.Name = declaredProperty.Name;
                            modelTableType.Properties.Add(modelProperty);
                        }
                    }
                }
            }

            foreach (IEdmSchemaElement schemaElement in lstEntityContainer)
            {
                if (schemaElement is Microsoft.Data.Edm.IEdmEntityContainer entityContainer)
                {
                    bool isDefaultEntityContainer     = false;
                    var  annoIsDefaultEntityContainer = edmModel.DirectValueAnnotationsManager.GetDirectValueAnnotations(entityContainer).FirstOrDefault(_ => _.NamespaceUri == "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" && _.Name == "IsDefaultEntityContainer");
                    if (annoIsDefaultEntityContainer?.Value is Microsoft.Data.Edm.Library.Values.EdmStringConstant stringConstant)
                    {
                        if (string.Equals(stringConstant.Value, "true", StringComparison.OrdinalIgnoreCase) || stringConstant.Value == "1")
                        {
                            isDefaultEntityContainer = true;
                        }
                    }
                    else if (annoIsDefaultEntityContainer?.Value is Microsoft.Data.Edm.Library.Values.EdmBooleanConstant booleanConstant)
                    {
                        if (booleanConstant.Value)
                        {
                            isDefaultEntityContainer = true;
                        }
                    }
                    if (isDefaultEntityContainer)
                    {
                        var entityName = ConvertToModelEntityName(entityContainer);
                        modelSchema.RootEntityName = entityName;
                        //if (modelSchema.Name == null) {
                        //    modelSchema.Name = entityName;
                        //}
                    }

                    var modelEntityContainer = new ModelEntity();
                    modelEntityContainer.Name = ConvertToModelEntityName(entityContainer);
                    modelEntityContainer.Kind = ModelEntityKind.Container;
                    modelSchema.Entities.Add(modelEntityContainer);
                    var modelEntityContainerTableType = new ModelTableType();
                    modelEntityContainerTableType.Name = ConvertToModelEntityName(entityContainer);
                    modelEntityContainer.TableType     = modelEntityContainerTableType;

                    var elements = entityContainer.Elements;

                    var entitySets = entityContainer.EntitySets();
                    foreach (var entitySet in entitySets)
                    {
                        var modelEntitySet = new ModelEntity();
                        modelEntitySet.Name = ConvertToModelEntityName(entitySet, entityContainer);
                        modelEntitySet.Kind = ModelEntityKind.EntitySet;
                        var elementTypeName = ConvertToModelEntityName(entitySet.ElementType);
                        modelEntitySet.TableType = typeDefinitionsByName.GetValueOrDefault(elementTypeName);
                        modelSchema.Entities.Add(modelEntitySet);
                        entityByName[modelEntitySet.Name] = modelEntitySet;

                        //var property = new ModelProperty();
                        //property.Name = entitySet.Name;
                        //property.Type = new ModelT;

                        //modelEntityContainerTableType.Properties.Add(property);
                        //var modelRelation = new ModelRelation();
                        //modelRelation.Name = entitySet.Name;
                        //modelRelation.Master = modelEntityContainer;
                        //modelRelation.Child = modelEntitySet;
                        //modelSchema.Relations.Add(modelRelation);
                        //   entitySet.ContainerElementKind== EdmContainerElementKind.EntitySet
                        //   entitySet.ContainerElementKind == EdmContainerElementKind.FunctionImport
                    }

                    foreach (var entitySet in entitySets)
                    {
                        var entitySetName  = ConvertToModelEntityName(entitySet, entityContainer);
                        var modelEntitySet = entityByName.GetValueOrDefault(entitySetName);

                        var navigationTargets = entitySet.NavigationTargets;
                        foreach (var navigationTarget in navigationTargets)
                        {
                            var navigationProperty  = navigationTarget.NavigationProperty;
                            var targetEntitySet     = navigationTarget.TargetEntitySet;
                            var targetEntitySetName = targetEntitySet.Name;
                            var dependentProperties = navigationProperty.DependentProperties;
                            var partner             = navigationProperty.Partner;
                            var partnerName         = partner.Name;
                            var modelRelation       = new ModelRelation();
                            modelRelation.Name   = navigationTarget.NavigationProperty.Name;
                            modelRelation.Master = modelEntitySet;
                            modelRelation.Child  = null;
                            //if (targetEntitySet.ElementType is IEdmSchemaElement elementType) {
                            //    modelRelation.Child = entityByName.GetValueOrDefault(ConvertToModelEntityName(elementType));
                            //}
                            modelSchema.Relations.Add(modelRelation);
#warning here
                        }

                        //-		[0]	{Microsoft.Data.Edm.Library.EdmNavigationTargetMapping}	Microsoft.Data.Edm.IEdmNavigationTargetMapping {Microsoft.Data.Edm.Library.EdmNavigationTargetMapping}
                    }
                }
            }


            return(resultErrors);
        }
        public ModelSchema ConvertSchema(IEdmModel edmModel)
        {
            var entityContainers       = edmModel.EntityContainers().ToArray();
            var defaultEntityContainer = entityContainers.First(_ => edmModel.IsDefaultEntityContainer(_));
            var entitySets             = defaultEntityContainer.EntitySets().ToArray();

            var modelSchema     = new ModelSchema();
            var modelEntityRoot = new ModelEntity();
            var complexTypeRoot = new ModelComplexType();

            // TODO: add ns
            modelEntityRoot.Name = new ModelEntityName(defaultEntityContainer.Namespace, null, defaultEntityContainer.Name);
            complexTypeRoot.Name = new ModelEntityName(defaultEntityContainer.Namespace, null, defaultEntityContainer.Name);

            modelEntityRoot.EntityType = complexTypeRoot;
            modelSchema.Entities.Add(modelEntityRoot);
            modelEntityRoot.Kind = ModelEntityKind.Container;

            foreach (var entitySet in entitySets)
            {
                var elementType = entitySet.ElementType;
                var complexType = new ModelComplexType();
                var modelEntity = new ModelEntity();
                complexType.Name       = new ModelEntityName(elementType.Namespace, null, elementType.Name);
                modelEntity.Name       = new ModelEntityName(null, null, entitySet.Name);
                modelEntity.EntityType = complexType;
                modelEntity.Kind       = ModelEntityKind.EntitySet;

                modelSchema.ComplexTypes.Add(complexType);
                modelSchema.Entities.Add(modelEntity);


                var declaredStructuralProperties = elementType.DeclaredStructuralProperties().ToArray();
                foreach (var declaredStructuralProperty in declaredStructuralProperties)
                {
                    var declaredStructuralPropertyType           = declaredStructuralProperty.Type;
                    var declaredStructuralPropertyTypeDefinition = declaredStructuralPropertyType.Definition;
                    var modelProperty = new ModelProperty();
                    modelProperty.Name = declaredStructuralProperty.Name;
                    if (declaredStructuralPropertyType.IsPrimitive())
                    {
                        var modelPropertyType = new ModelScalarType();
                        var vas = declaredStructuralProperty.VocabularyAnnotations(edmModel).ToArray();
                        if (declaredStructuralPropertyTypeDefinition is IEdmSchemaElement edmSchemaElement)
                        {
                            modelPropertyType.Name = new ModelEntityName(edmSchemaElement.Namespace, null, edmSchemaElement.Name);
                        }
                        else
                        {
                            modelPropertyType.Name = new ModelEntityName(null, null, declaredStructuralPropertyType.FullName());
                        }
                        modelPropertyType.IsNullable = declaredStructuralPropertyType.IsNullable;
                        modelProperty.Type           = modelPropertyType;
                    }
                    else
                    {
                        throw new NotImplementedException();
                    }
                    complexType.Properties.Add(modelProperty);
                }

                //complexType.Properties
            }

            foreach (var entitySet in entitySets)
            {
                var entityName  = new ModelEntityName(null, null, entitySet.Name);
                var modelEntity = modelSchema.Entities.FirstOrDefault(_ => _.Name == entityName);

                var navigationTargets = entitySet.NavigationTargets.ToArray();
                foreach (var navigationTarget in navigationTargets)
                {
                    //
                    var navigationProperty = navigationTarget.NavigationProperty;

                    if (navigationProperty is IEdmNavigationProperty edmNavigationProperty)
                    {
                        if (navigationProperty.ContainsTarget)
                        {
                        }
                    }
                    var navigationPropertyPartner = navigationProperty.Partner;
                    var targetEntitySet           = navigationTarget.TargetEntitySet;
                    //
                    var naviFromProperties = navigationProperty.DependentProperties?.ToArray();
                    var naviToProperties   = navigationPropertyPartner.DependentProperties?.ToArray();

                    var modelRelation     = new ModelRelation();
                    var targetEntityName  = new ModelEntityName(null, null, targetEntitySet.Name);
                    var targetModelEntity = modelSchema.Entities.FirstOrDefault(_ => _.Name == targetEntityName);
                    modelRelation.Name = navigationTarget.TargetEntitySet.Name;

                    //modelRelation.MasterEntity =
                    //modelRelation.ForeignEntity = targetModelEntity;
                    modelRelation.ForeignName = targetEntityName;
                    modelSchema.Relations.Add(modelRelation);
                }
            }

            return(modelSchema);
        }