public object InheritedCollections(
            ResourceProfileData profileData,
            ResourceClassBase resource,
            TemplateContext templateContext)
        {
            if (resource.IsDescriptorEntity())
            {
                return(ResourceRenderer.DoRenderProperty);
            }

            string ns = profileData.GetProfileNamespace(resource);

            var activeResource = profileData.GetProfileActiveResource(resource);

            return(activeResource.IsDerived && activeResource.Collections.Any()
                ? new
            {
                Inherited =
                    activeResource.Collections
                    .Where(x => x.IsInherited)
                    .OrderBy(x => x.PropertyName)
                    .Select(
                        x => new
                {
                    BaseEntity =
                        $"{resource.Entity.BaseEntity.Name}.{resource.Entity.BaseEntity.SchemaProperCaseName()}",
                    BaseEntityInterfaceName = $"I{resource.Entity.BaseEntity.Name}",
                    ItemTypeNamespacePrefix = GetContextualNamespacePrefix(x.ItemType),
                    ItemType = x.ItemType.Name,
                    Collection = x.ItemType.PluralName,
                    PropertyName = x.PropertyName,
                    JsonPropertyName =
                        x.IsDerivedEntityATypeEntity()
                                            ? x.Association.OtherEntity.PluralName.ToCamelCase()
                                            : x.JsonPropertyName,
                    PropertyFieldName = x.ItemType.PluralName.ToCamelCase(),
                    ParentName = x.ParentFullName.Name,
                    PropertyNamespace = ns,
                    ResourceName = resource.Name,
                    Standard =
                        profileData.IsIncluded(resource, x) &&
                        !profileData.IsFilteredCollection(resource, x),
                    Null = !profileData.IsIncluded(resource, x),
                    Filtered = profileData.IsIncluded(resource, x) &&
                               profileData.IsFilteredCollection(
                        resource,
                        x)
                })
                    .ToList()
            }
                : ResourceRenderer.DoNotRenderProperty);
        }
Beispiel #2
0
        private object AssembleConstructor(ResourceProfileData profileData, ResourceClassBase resource)
        {
            string ns = profileData.GetProfileNamespace(resource);

            var activeResource = profileData.GetProfileActiveResource(resource);

            return(activeResource.Collections.Any()
                ? new
            {
                Inherited = resource.IsDerived && activeResource.Collections.Any(x => x.IsInherited)
                        ? ResourceRenderer.DoRenderProperty
                        : ResourceRenderer.DoNotRenderProperty,
                InheritedCollections = resource.Collections
                                       .Where(x => x.IsInherited)
                                       .Select(
                    x =>
                    new
                {
                    BaseEntity =
                        $"{resource.Entity.BaseEntity.Name}.{resource.Entity.BaseEntity.SchemaProperCaseName()}",
                    PropertyName = x.ItemType.PluralName,
                    PropertyNamespace = ns,
                    CollectionName = x.ItemType.Name
                })
                                       .ToList(),
                Standard = resource.Collections.Any(x => !x.IsInherited)
                        ? ResourceRenderer.DoRenderProperty
                        : ResourceRenderer.DoNotRenderProperty,
                Collections = resource.Collections
                              .Where(
                    x =>
                    !x.IsInherited &&
                    profileData.IsIncluded(resource, x) &&
                    TemplateContext.ShouldRenderResourceClass(
                        x.ItemType))
                              .Select(
                    x => new
                {
                    PropertyName = x.ItemType.PluralName,
                    PropertyNamespace = ns,
                    CollectionName = x.ItemType.Name
                })
                              .ToList()
            }
                : ResourceRenderer.DoNotRenderProperty);
        }
        public object OnDeserialize(ResourceProfileData profileData, ResourceClassBase resource, TemplateContext TemplateContext)
        {
            bool shouldRender = !(profileData.HasProfile && !profileData.HasNavigableChildren(resource));

            if (!profileData.HasProfile &&
                !(resource.Collections.Any() || !resource.IsAggregateRoot() && resource.HasBackReferences()))
            {
                shouldRender = false;
            }

            if (!shouldRender)
            {
                return(ResourceRenderer.DoNotRenderProperty);
            }

            if (resource.IsDerived && !resource.IsDescriptorEntity())
            {
                return(new
                {
                    Inherited = resource.Collections.Any(x => x.IsInherited)
                        ? ResourceRenderer.DoRenderProperty
                        : ResourceRenderer.DoNotRenderProperty,
                    InheritedCollections = resource.Collections.Where(x => x.IsInherited)
                                           .OrderBy(x => x.PropertyName)
                                           .Select(
                        x => new
                    {
                        PropertyFieldName =
                            x.ItemType.PluralName.ToCamelCase()
                    })
                                           .ToList(),
                    Standard = resource.Collections.Any(x => !x.IsInherited)
                        ? ResourceRenderer.DoRenderProperty
                        : ResourceRenderer.DoNotRenderProperty,
                    Collections = resource.Collections
                                  .Where(x => !x.IsInherited && TemplateContext.ShouldRenderEntity(x.ItemType.Entity))
                                  .OrderBy(x => x.PropertyName)
                                  .Select(
                        x => new
                    {
                        ItemType = x.ItemType.Name,
                        Collection = x.ItemType.PluralName,
                        PropertyName = x.PropertyName,

                        // using the property name so we do not break the data member contract
                        // from the original template.
                        JsonPropertyName = x.PropertyName
                                           .TrimPrefix(x.ParentFullName.Name)
                                           .ToCamelCase(),
                        PropertyFieldName = x.ItemType.PluralName.ToCamelCase(),
                        ParentName = x.ParentFullName.Name
                    })
                                  .ToList()
                });
            }

            if (resource.Collections.Any(c => profileData.IsIncluded(resource, c)) ||
                !resource.IsAggregateRoot() && resource.References.Any(x => profileData.IsIncluded(resource, x)))
            {
                return(new
                {
                    BackRef = !resource.IsAggregateRoot() && resource.HasBackReferences()
                        ? ResourceRenderer.DoRenderProperty
                        : ResourceRenderer.DoNotRenderProperty,
                    BackRefCollections = resource.References
                                         .Where(x => profileData.IsIncluded(resource, x))
                                         .OrderBy(x => x.PropertyName)
                                         .Select(
                        x => new
                    {
                        ReferenceTypeName = x.ReferenceTypeName,
                        PropertyFieldName = x.PropertyName.ToCamelCase()
                    })
                                         .ToList(),
                    Standard = resource.Collections.Any(x => !x.IsInherited)
                        ? ResourceRenderer.DoRenderProperty
                        : ResourceRenderer.DoNotRenderProperty,
                    Collections = resource.Collections
                                  .OrderBy(x => x.PropertyName)
                                  .Select(
                        x => new
                    {
                        ItemType = x.ItemType.Name,
                        Collection = x.ItemType.PluralName,
                        PropertyName = x.PropertyName,

                        // using the property name so we do not break the data member contract
                        // from the original template.
                        JsonPropertyName = x.PropertyName
                                           .TrimPrefix(x.ParentFullName.Name)
                                           .ToCamelCase(),
                        PropertyFieldName = x.ItemType.PluralName.ToCamelCase(),
                        ParentName = x.ParentFullName.Name
                    })
                                  .ToList(),
                    Inherited = resource.Collections.Any(x => x.IsInherited)
                        ? ResourceRenderer.DoRenderProperty
                        : ResourceRenderer.DoNotRenderProperty,
                    InheritedCollections = resource.Collections.Where(x => x.IsInherited)
                                           .OrderBy(x => x.PropertyName)
                                           .Select(x => new { PropertyFieldName = x.ItemType.PluralName.ToCamelCase() })
                                           .ToList()
                });
            }

            return(ResourceRenderer.DoNotRenderProperty);
        }
        public object References(ResourceProfileData profileData, ResourceClassBase resource, TemplateContext TemplateContext)
        {
            var activeResource = profileData.GetProfileActiveResource(resource);

            var references = activeResource.References
                             .ToList();

            if (!references.Any(x => profileData.IsIncluded(resource, x)))
            {
                return(ResourceRenderer.DoNotRenderProperty);
            }

            if (activeResource.IsAggregateRoot() || !activeResource.HasBackReferences())
            {
                return(new
                {
                    Collections = references
                                  .Where(x => profileData.IsIncluded(resource, x))
                                  .OrderBy(x => x.PropertyName)
                                  .Select(
                        x =>
                    {
                        bool renderNamespace =
                            !(x.Association.IsSelfReferencing ||
                              x.Association.IsSelfReferencingManyToMany) ||
                            x.Association.OtherEntity.IsAbstract ||
                            !x.Association.OtherEntity.IsSameAggregate(
                                resource.Entity);

                        string referenceName = !renderNamespace
                                    ? x.ReferenceTypeName
                                    : $"{x.ReferencedResourceName}.{x.ReferencedResource.Entity.SchemaProperCaseName()}.{x.ReferenceTypeName}";

                        return new
                        {
                            Reference = new
                            {
                                ReferencedResourceName = x.ReferencedResourceName,
                                ReferenceTypeName =
                                    !x.ReferencedResource.Entity
                                    .IsExtensionEntity &&
                                    !x.ReferenceTypeName.Replace("Reference", string.Empty)
                                    .Equals(resource.Name) &&
                                    !x.ReferenceTypeName.Replace("Reference", string.Empty)
                                    .Equals((resource as ResourceChildItem)?.Parent.Name)
                                                ? $"{x.ReferencedResourceName}.{EdFiConventions.ProperCaseName}.{x.ReferenceTypeName}"
                                                : referenceName,
                                Name = x.ParentFullName.Name,

                                // using the property name so we do not break the data member contract
                                // from the original template.
                                JsonPropertyName = x.PropertyName.ToCamelCase(),
                                PropertyName = x.PropertyName,
                                PropertyFieldName = x.PropertyName.ToCamelCase(),
                                IsRequired = x.IsRequired,
                                IsIdentifying = x.Association.IsIdentifying
                            },
                            Standard = ResourceRenderer.DoRenderProperty
                        };
                    }
                        )
                                  .ToList()
                });
            }

            return(new
            {
                Collections = activeResource.References
                              .OrderBy(x => x.PropertyName)
                              .Select(
                    x => new
                {
                    Reference = new
                    {
                        ReferencedResourceName = x.ReferencedResourceName,
                        ReferenceTypeName = x.ReferenceTypeName,
                        Name = x.ParentFullName.Name,
                        JsonPropertyName = x.JsonPropertyName,
                        PropertyName = x.PropertyName,
                        PropertyFieldName = x.PropertyName.ToCamelCase(),
                        IsRequired = x.IsRequired,
                        IsIdentifying = x.Association.IsIdentifying,
                        BackReferenceType =
                            string.Format(
                                "{0}To{1}Reference",
                                x.Association.ThisEntity.Name,
                                x.Association.OtherEntity.Name)
                    },
                    Backref = ResourceRenderer.DoRenderProperty
                }
                    )
                              .ToList()
            });
        }
        public object CreatePutPostRequestValidator(
            ResourceProfileData profileData,
            ResourceClassBase resource,
            TemplateContext templateContext)
        {
            var resourceChildItem = resource as ResourceChildItem;

            return(new PutPostRequestValidator
            {
                EntityName = resource.Name,
                Collections = resource.Collections
                              // TODO: Remove this filter with dynamic profiles
                              .Where(collection => !profileData.HasProfile || profileData.IsIncluded(resource, collection))
                              .Select(
                    collection => new PutPostRequestValidatorCollectionProperty
                {
                    PropertyName = collection.PropertyName,
                    PropertyFieldNamePrefix = collection.PropertyName.ToCamelCase(),
                    ItemTypeName = collection.ItemType.Name,
                    Namespace = collection.ParentFullName.Name != resource.Name
                                ? string.Format(
                        "{0}.{1}.{2}",
                        collection.ParentFullName.Name,
                        profileData.HasProfile ? resource.SchemaProperCaseName : resource.Entity.BaseEntity.SchemaProperCaseName(),
                        profileData.ProfilePropertyNamespaceSection)
                                : ResourceRenderer.DoNotRenderProperty
                }),
                HasProfileItemFilterValidations = profileData.HasProfile &&
                                                  profileData.IsWritable &&
                                                  profileData.HasFilteredCollection() &&
                                                  GetItemFilterValidations().Any(),
                ProfileItemFilterValidations = GetItemFilterValidations(),
                KeyUnificationValidations = new KeyUnificationValidation
                {
                    ResourceClassName = resource.Name,
                    ParentResourceClassName = resourceChildItem?.Parent.Name,
                    UnifiedProperties = resource.AllProperties
                                        // TODO: Remove this filter with dynamic profiles
                                        .Where(rp => !profileData.HasProfile || profileData.IsIncluded(resource, rp))
                                        .Where(rp => rp.IsUnified())
                                        .Select(rp => new UnifiedProperty
                    {
                        UnifiedPropertyName = rp.PropertyName,
                        UnifiedJsonPropertyName = rp.JsonPropertyName,
                        UnifiedCSharpPropertyType = rp.PropertyType.ToCSharp(),
                        UnifiedPropertyIsFromParent = rp.EntityProperty.IncomingAssociations
                                                      .Any(a => a.IsNavigable),
                        UnifiedPropertyParentPath = resourceChildItem != null && resourceChildItem.IsResourceExtension
                                ? string.Join(
                            string.Empty,
                            resourceChildItem.GetLineage().TakeWhile(l => !l.IsResourceExtension)
                            .Select(l => "." + l.Name))
                                : null,
                        References = rp.EntityProperty.IncomingAssociations
                                     .Where(a => !a.IsNavigable && rp.Parent.ReferenceByName.ContainsKey(a.Name + "Reference"))
                                     .Select(a => new
                        {
                            Reference = rp.Parent.ReferenceByName[a.Name + "Reference"],
                            OtherEntityPropertyName = a.PropertyMappings.Where(pm => pm.ThisProperty.Equals(rp.EntityProperty)).Select(pm => pm.OtherProperty.PropertyName).Single(),
                        })
                                     // TODO: Remove this filter with dynamic profiles
                                     .Where(x => !profileData.HasProfile || profileData.IsIncluded(resource, x.Reference))
                                     .Select(x => new
                        {
                            Reference = x.Reference,
                            ReferenceProperty =
                                (x.Reference.ReferenceTypeProperties
                                 .SingleOrDefault(rtp => rtp.EntityProperty.PropertyName == x.OtherEntityPropertyName)
                                 // Deal with the special case of the re-pointing of the identifying property from USI to UniqueId in Person entities
                                 ?? x.Reference.ReferenceTypeProperties
                                 .Single(rtp => rtp.EntityProperty.PropertyName ==
                                         EdFi.Ods.Common.Specifications.UniqueIdSpecification.GetUniqueIdPropertyName(x.OtherEntityPropertyName)))
                        })
                                     .Select(x => new UnifiedReferenceProperty
                        {
                            ReferenceName = x.Reference.PropertyName,
                            ReferenceJsonName = x.Reference.JsonPropertyName,
                            ReferencePropertyName = x.ReferenceProperty.PropertyName,
                            ReferenceJsonPropertyName = x.ReferenceProperty.JsonPropertyName
                        })
                    })
                }
            });

            IEnumerable <ItemFilterValidation> GetItemFilterValidations()
            {
                return(resource.Collections
                       .Where(x => profileData.IsFilteredCollection(resource, x))
                       .OrderBy(x => x.PropertyName)
                       .SelectMany(
                           x => profileData.GetFilteredCollection(resource, x)
                           .ValueFilters
                           .Select(
                               y => new ItemFilterValidation
                {
                    PropertyName = x.PropertyName,
                    ValidatorName = x.ItemType.Name,
                    ValidatorPropertyName = y.PropertyName,
                    PropertyFieldName = x.ItemType.Name.ToCamelCase(),
                    Filters = string.Join(", ", y.Values.Select(s => string.Format("'{0}'", s))),
                    ProfileName = profileData.ProfileName
                })));
            }
        }
Beispiel #6
0
        public object AssembleInheritedProperties(
            ResourceProfileData profileData,
            ResourceClassBase resource)
        {
            if (profileData == null)
            {
                throw new ArgumentNullException(nameof(profileData));
            }

            if (resource == null)
            {
                throw new ArgumentNullException(nameof(resource));
            }

            IList <ResourcePropertyData> includedProperties = null;

            if (profileData.HasProfile)
            {
                includedProperties = (resource.Name == profileData.SuppliedResource.Name
                        ? profileData.UnfilteredResource
                        : resource).InheritedProperties()
                                     .Where(x => !x.IsIdentifying && !x.PropertyName.Equals("Id"))
                                     .OrderBy(x => x.PropertyName)
                                     .Select(
                    x =>
                    new ResourcePropertyData
                {
                    Property           = x,
                    IsStandardProperty = !profileData.IsIncluded(resource, x)
                })
                                     .ToList();
            }
            else
            {
                includedProperties = resource.InheritedProperties()
                                     .OrderBy(x => x.PropertyName)
                                     .Where(x => !x.IsIdentifying && !x.PropertyName.Equals("Id"))
                                     .Select(
                    x => new ResourcePropertyData
                {
                    Property           = x,
                    IsStandardProperty = false
                })
                                     .ToList();
            }

            var propertiesToRender = new List <PropertyData>();

            if (resource.IsDescriptorEntity() &&
                resource.InheritedProperties()
                .Any(x => !x.IsIdentifying && !x.PropertyName.Equals("Id")))
            {
                propertiesToRender.AddRange(
                    includedProperties.Select(
                        x =>
                {
                    var propertyData = x.IsStandardProperty
                                ? PropertyData.CreateNullProperty(x.Property)
                                : PropertyData.CreateStandardProperty(x.Property);

                    propertyData[ResourceRenderer.MiscellaneousComment] = "// NOT in a reference, NOT a lookup column ";
                    return(propertyData);
                }));
            }
            else if (resource.IsDerived)
            {
                propertiesToRender.AddRange(
                    includedProperties.Select(
                        x => x.IsStandardProperty
                            ? PropertyData.CreateNullProperty(x.Property)
                            : PropertyData.CreateStandardProperty(x.Property)));
            }

            return(propertiesToRender.Any()
                ? new { Properties = CreatePropertyDtos(propertiesToRender) }
                : ResourceRenderer.DoNotRenderProperty);
        }