Esempio n. 1
0
        public object AssembleReferenceFullyDefined(ResourceClassBase resource, AssociationView association = null)
        {
            if (resource == null)
            {
                throw new ArgumentNullException(nameof(resource));
            }

            // populates the reference is fully defined method for an aggregate reference.
            if (resource.HasBackReferences() && resource.IsAggregateRoot() || !resource.IdentifyingProperties.Any())
            {
                return(ResourceRenderer.DoNotRenderProperty);
            }

            var ids = association != null
                ? Resources.GetIdentifyingPropertiesWithAttributes(resource, association)
                      .OrderBy(x => x.Target.PropertyName)
                      .ToList()
                : resource.IdentifyingProperties.Where(x => !x.IsLocallyDefined)
                      .OrderBy(x => x.PropertyName)
                      .Select(
                x => new HrefData
            {
                Source    = x,
                Target    = x,
                IsUnified = false
            })
                      .ToList();

            if (!ids.Any())
            {
                return(ResourceRenderer.DoNotRenderProperty);
            }

            ResourceProperty first = ids.First()
                                     .Source;

            ResourceProperty last = ids.Last()
                                    .Source;

            return(ids
                   .Select(
                       x =>
                       new PropertyData(x.Source)
            {
                IsFirstProperty = x.Source == first,
                IsLastProperty = x.Source == last,
                IsReferencedProperty = x.IsUnified
            }.Render())
                   .ToList());
        }
        public bool HasNavigableChildren(ResourceClassBase resource)
        {
            if (resource == null)
            {
                throw new ArgumentNullException(nameof(resource));
            }

            if (!HasProfile || IsBaseResource)
            {
                return(resource.Entity.NavigableChildren.Any());
            }

            if (resource.Name == SuppliedResource.Name)
            {
                return(SuppliedResource.Collections.Any());
            }

            return(resource.Collections.Any() || resource.HasBackReferences());
        }
        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);
        }
Esempio n. 4
0
        public object AssemblePrimaryKeys(
            ResourceProfileData profileData,
            ResourceClassBase resourceClass,
            TemplateContext templateContext)
        {
            if (profileData == null)
            {
                throw new ArgumentNullException(nameof(profileData));
            }

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

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

            var pks = new List <object>();

            var activeResource = profileData.GetProfileActiveResource(resourceClass);

            var filteredResource = profileData.GetContainedResource(resourceClass) ?? resourceClass;

            if (resourceClass is ResourceChildItem)
            {
                pks.Add(
                    new
                {
                    HasParent = ResourceRenderer.DoRenderProperty,
                    Property  = new
                    {
                        PropertyName =
                            resourceClass.GetResourceInterfaceName(
                                templateContext.GetSchemaProperCaseNameForResource(resourceClass),
                                templateContext.IsProfiles,
                                templateContext.IsExtension),
                        ReferencesWithUnifiedKey =
                            resourceClass.References
                            .Where(@ref => @ref.Properties.Any(rp => rp.IsUnified()))
                            .Select(@ref => new
                        {
                            ReferencePropertyName = @ref.PropertyName,
                            ReferenceFieldName    = @ref.PropertyName.ToCamelCase(),
                            UnifiedKeyProperties  = @ref.Properties
                                                    .Where(rp => rp.IsUnified())
                                                    .Select(rp => new
                            {
                                UnifiedKeyPropertyName = rp.PropertyName
                            })
                        })
                    }
                });
            }

            var props = new List <PropertyData>();

            foreach (var property in activeResource.AllProperties
                     .Where(x => x.IsIdentifying)
                     .OrderBy(x => x.PropertyName))
            {
                if (activeResource.IsDescriptorEntity())
                {
                    props.Add(PropertyData.CreateDerivedProperty(property));
                    continue;
                }

                if (activeResource.IsAggregateRoot())
                {
                    if (resourceClass.IsDerived)
                    {
                        props.Add(AssembleDerivedProperty(property));
                    }
                    else
                    {
                        props.Add(
                            property.IsPersonOrUsi() && !templateContext.IsExtension
                                ? PropertyData.CreateUsiPrimaryKey(property)
                                : property.HasAssociations() && !property.IsDirectLookup
                                    ? PropertyData.CreateReferencedProperty(property, resource: filteredResource)
                                    : PropertyData.CreateStandardProperty(property));
                    }
                }
                else
                {
                    // non aggregate root
                    if (activeResource.References.Any())
                    {
                        if (property.IsPersonOrUsi())
                        {
                            props.Add(PropertyData.CreateUsiPrimaryKey(property));
                            continue;
                        }

                        if (resourceClass.HasBackReferences() &&
                            resourceClass.BackReferencedProperties()
                            .Contains(property, ModelComparers.ResourcePropertyNameOnly))
                        {
                            continue;
                        }

                        props.Add(
                            property.HasAssociations() && !property.IsDirectLookup
                                ? PropertyData.CreateReferencedProperty(property)
                                : PropertyData.CreateStandardProperty(property));
                    }
                    else
                    {
                        props.Add(PropertyData.CreateStandardProperty(property));
                    }
                }
            }

            pks.AddRange(CreatePropertyDtos(props));

            return(pks.Any()
                ? new { Properties = pks }
                : ResourceRenderer.DoNotRenderProperty);
        }
Esempio n. 5
0
        private object CreateContextSpecificResourceReferences(ResourceProfileData profileData, ResourceClassBase resource)
        {
            if (resource.IsAggregateRoot() || !resource.HasBackReferences())
            {
                return(ResourceRenderer.DoNotRenderProperty);
            }

            var externalAssociations = resource.ExternalReferenceAssociations()
                                       .ToList();

            var refs = externalAssociations.Select(
                av => new
            {
                ContextSpecificResourceReference = new
                {
                    NamespacePrefix = resource.GetNamespacePrefix(),
                    ReferenceName   = string.Format(
                        "{0}To{1}",
                        av.ThisEntity.Name,
                        av.OtherEntity.Name),
                    // Leaving this member definition inline as this entire method
                    // is to be deprecated in the future.
                    ContextualReferenceIdentifier =
                        av.ThisProperties
                        .Where(p => (p.IsUnified && p.IsIdentifying))
                        .Select(
                            p =>
                            av.PropertyMappingByThisName[p.PropertyName]
                            .OtherProperty
                            .ToResourceProperty(resource))
                        .OrderBy(p => p.PropertyName)
                        .Select(p => new
                    {
                        PropertyName     = p.PropertyName,
                        JsonPropertyName = p.JsonPropertyName,
                        PropertyType     = p.PropertyType.ToCSharp(),
                        // Use GetLineage to build the property path, in reverse order, skipping the first since that's the BackReference itself
                        PropertyPathToRoot = "BackReference." +
                                             string.Join(".", ((ResourceChildItem)resource).GetLineage().Reverse().Skip(1).Select(l => l.Name))
                    }),
                    ReferenceIdentifiers =
                        _resourcePropertyRenderer.AssembleIdentifiers(
                            profileData,
                            resource,
                            av),
                    Href             = AssembleHref(profileData, resource, av),
                    HasDiscriminator = av.OtherEntity.HasDiscriminator(),
                    ThisEntityName   = av.ThisEntity.Name,
                    OtherEntityName  = av.OtherEntity.Name,
                    BackReference    =
                        string.Format(
                            "backreference.{0}.",
                            (resource as ResourceChildItem)?.Parent.Name),
                    ReferenceFullyDefined =
                        _resourcePropertyRenderer
                        .AssembleReferenceFullyDefined(
                            resource,
                            av)
                }
            }
                )
                       .ToList();

            return(refs);
        }