Exemple #1
0
        /// <summary>
        /// Checks if the resource has any back references.
        /// </summary>
        /// <param name="resource"></param>
        /// <returns></returns>
        public static bool HasBackReferences(this ResourceClassBase resource)
        {
            if (resource.IsDescriptorEntity())
            {
                return(false);
            }

            return(resource.BackReferencedProperties()
                   .Any());
        }
Exemple #2
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);
        }