Example #1
0
        /// <summary>
        /// Gets the ResourceAssociationSet instance when given the source association end.
        /// </summary>
        /// <param name="resourceSet">Resource set of the source association end.</param>
        /// <param name="resourceType">Resource type of the source association end.</param>
        /// <param name="resourceProperty">Resource property of the source association end.</param>
        /// <returns>ResourceAssociationSet instance.</returns>
        /// <remarks>This method returns a ResourceAssociationSet representing a reference which is specified
        /// by the <paramref name="resourceProperty"/> on the <paramref name="resourceType"/> for instances in the <paramref name="resourceSet"/>.</remarks>
        public virtual ResourceAssociationSet GetResourceAssociationSet(ResourceSet resourceSet, ResourceType resourceType, ResourceProperty resourceProperty)
        {
            ResourceAssociationSet resourceAssociationSet;

            if (this.associationSets.TryGetValue(resourceSet.Name + "_" + resourceType.FullName + "_" + resourceProperty.Name, out resourceAssociationSet))
            {
                Debug.Assert(resourceAssociationSet != null, "resourceAssociationSet != null");

                // Just few verification to show what is expected of the returned resource association set.
                Debug.Assert(
                    (resourceAssociationSet.End1.ResourceSet == resourceSet &&
                     resourceAssociationSet.End1.ResourceType == resourceType &&
                     resourceAssociationSet.End1.ResourceProperty == resourceProperty) ||
                    (resourceAssociationSet.End2.ResourceSet == resourceSet &&
                     resourceAssociationSet.End2.ResourceType == resourceType &&
                     resourceAssociationSet.End2.ResourceProperty == resourceProperty),
                    "The precreated resource association set doesn't match the specified resource set.");
            }
            else
            {
                // We have the resource association set precreated on the property annotation, so no need to compute anything in here
                Debug.Assert(resourceProperty.GetAnnotation().ResourceAssociationSet != null, "resourceProperty.GetAnnotation().ResourceAssociationSet != null");
                resourceAssociationSet = resourceProperty.GetAnnotation().ResourceAssociationSet();

                // Just few verification to show what is expected of the returned resource association set.
                Debug.Assert(resourceAssociationSet.End1.ResourceSet == resourceSet, "The precreated resource association set doesn't match the specified resource set.");
                Debug.Assert(resourceAssociationSet.End1.ResourceType == resourceType, "The precreated resource association set doesn't match the specified resource type.");
                Debug.Assert(resourceAssociationSet.End1.ResourceProperty == resourceProperty, "The precreated resource association set doesn't match its resource property.");
            }

            return(resourceAssociationSet);
        }
Example #2
0
        /// <summary>Helper method to add a reference property.</summary>
        /// <param name="resourceType">The resource type to add the property to.</param>
        /// <param name="name">The name of the property to add.</param>
        /// <param name="targetResourceSet">The resource set the resource reference property points to.</param>
        /// <param name="targetResourceType">The resource type the resource set reference property points to.</param>
        /// <param name="resourceSetReference">true if the property should be a resource set reference, false if it should be resource reference.</param>
        private void AddReferenceProperty(ResourceType resourceType, string name, ResourceSet targetResourceSet, ResourceType targetResourceType, bool resourceSetReference)
        {
            PropertyInfo propertyInfo = resourceType.InstanceType.GetProperty(name);

            targetResourceType = targetResourceType ?? targetResourceSet.ResourceType;
            ResourceProperty property = AddResourceProperty(
                resourceType,
                name,
                resourceSetReference ? ResourcePropertyKind.ResourceSetReference : ResourcePropertyKind.ResourceReference,
                targetResourceType,
                propertyInfo);

            // We don't support MEST, that is having two resource sets with the same resource type, so we can determine
            //   the resource set from the resource type. That also means that the property can never point to different resource sets
            //   so we can precreate the ResourceAssociationSet for this property right here as we have all the information.
            property.GetAnnotation().ResourceAssociationSet = () =>
            {
                ResourceSet  sourceResourceSet = resourceType.GetAnnotation().ResourceSet;
                ResourceType baseResourceType  = resourceType.BaseType;
                while (sourceResourceSet == null && baseResourceType != null)
                {
                    sourceResourceSet = baseResourceType.GetAnnotation().ResourceSet;
                    baseResourceType  = baseResourceType.BaseType;
                }

                return(new ResourceAssociationSet(
                           resourceType.Name + "_" + name + "_" + targetResourceSet.Name,
                           new ResourceAssociationSetEnd(sourceResourceSet, resourceType, property),
                           new ResourceAssociationSetEnd(targetResourceSet, targetResourceType, null)));
            };
        }
Example #3
0
        internal static void AddResourcePropertyFromInstanceCollectionResourceType(this ResourceType resourceType, ResourceType propertyCollection)
        {
            var resourceProperty = new ResourceProperty(
                propertyCollection.Name,
                ResourcePropertyKind.Collection,
                ResourceType.GetCollectionResourceType(propertyCollection));

            resourceProperty.CanReflectOnInstanceTypeProperty = false;
            resourceProperty.GetAnnotation().InstanceProperty = (PropertyInfo)propertyCollection.CustomState;
            resourceType.AddProperty(resourceProperty);
        }
Example #4
0
        ResourceAssociationSet IDataServiceMetadataProvider.GetResourceAssociationSet(
            ResourceSet resourceSet,
            ResourceType resourceType,
            ResourceProperty resourceProperty)
        {
            // We have the resource association set precreated on the property annotation,
            // so no need to compute anything in here
            ResourceAssociationSet resourceAssociationSet = resourceProperty.GetAnnotation().ResourceAssociationSet;

            return(resourceAssociationSet);
        }
Example #5
0
        /// <summary>
        /// Gets the ResourceAssociationSet instance when given the source association end.
        /// </summary>
        /// <param name="resourceSet">Resource set of the source association end.</param>
        /// <param name="resourceType">Resource type of the source association end.</param>
        /// <param name="resourceProperty">Resource property of the source association end.</param>
        /// <returns>ResourceAssociationSet instance.</returns>
        /// <remarks>This method returns a ResourceAssociationSet representing a reference which is specified
        /// by the <paramref name="resourceProperty"/> on the <paramref name="resourceType"/> for instances in the <paramref name="resourceSet"/>.</remarks>
        public ResourceAssociationSet GetResourceAssociationSet(ResourceSet resourceSet, ResourceType resourceType, ResourceProperty resourceProperty)
        {
            // We have the resource association set precreated on the property annotation, so no need to compute anything in here
            ResourceAssociationSet resourceAssociationSet = resourceProperty.GetAnnotation().ResourceAssociationSet;

            // Just few verification to show what is expected of the returned resource association set.
            Debug.Assert(resourceAssociationSet.End1.ResourceSet == resourceSet, "The precreated resource association set doesn't match the specified resource set.");
            Debug.Assert(resourceAssociationSet.End1.ResourceType == resourceType, "The precreated resource association set doesn't match the specified resource type.");
            Debug.Assert(resourceAssociationSet.End1.ResourceProperty == resourceProperty, "The precreated resource association set doesn't match its resource property.");

            return(resourceAssociationSet);
        }
Example #6
0
 object IDataServiceQueryProvider.GetPropertyValue(object target, ResourceProperty resourceProperty)
 {
     if (target is ContentItem)
     {
         var contentItem = (ContentItem)target;
         var contentPart = contentItem.Parts.FirstOrDefault(x => x.PartDefinition.Name == resourceProperty.Name);
         if (null != contentPart)
         {
             return(contentPart);
         }
         var resourceField = contentItem.Parts.FirstOrDefault(x => x.PartDefinition.Name == contentItem.ContentType);
         if (null == resourceField)
         {
             return(null);
         }
         var contentField = resourceField.Fields.FirstOrDefault(x => x.Name == resourceProperty.Name);
         return(contentField);
     }
     if (target is ContentPart)
     {
         var partProperty = resourceProperty.GetAnnotation().InstanceProperty;
         // ContentPartRecord POCO accessor
         if (null != partProperty)
         {
             var partPropertyValue = partProperty.GetValue(target, null);
             return(partPropertyValue);
         }
         //ContentField accessor
         var contentPart  = target as ContentPart;
         var contentField = contentPart.Fields.FirstOrDefault(x => x.Name == resourceProperty.ResourceType.Name);
         return(contentField);
     }
     if (target is ContentField)
     {
         var fieldProperty      = resourceProperty.GetAnnotation().InstanceProperty;
         var fieldPropertyvalue = fieldProperty.GetValue(target, null);
         return(fieldPropertyvalue);
     }
     return(null);
 }
Example #7
0
        internal static void AddResourcePropertyFromInstancePropertyInfo(this ResourceType resourceType, PropertyInfo property)
        {
            var resourcePropertyType      = ResourceType.GetPrimitiveResourceType(property.PropertyType);
            var innerEnumerableType       = TypeSystem.GetIEnumerableElementType(property.PropertyType);
            var innerResourcePropertyType = null != innerEnumerableType?ResourceType.GetPrimitiveResourceType(innerEnumerableType) : null;

            resourcePropertyType = null == resourcePropertyType && null != innerResourcePropertyType?ResourceType.GetCollectionResourceType(innerResourcePropertyType) : resourcePropertyType;

            if (null == resourcePropertyType)
            {
                return;
            }

            var resourcePropertyKind = null == innerResourcePropertyType ? ResourcePropertyKind.Primitive : ResourcePropertyKind.Collection;
            var resourceProperty     = new ResourceProperty(
                property.Name,
                resourcePropertyKind,
                resourcePropertyType);

            resourceProperty.CanReflectOnInstanceTypeProperty = false;
            resourceProperty.GetAnnotation().InstanceProperty = property;
            resourceType.AddProperty(resourceProperty);
        }
Example #8
0
        public override IProjection Visit(SingleValuePropertyAccessNode nodeIn)
        {
            var propertyName           = nodeIn.Property.Name;
            var declaringSourceNode    = nodeIn.Source;
            var declaringTypeReference = declaringSourceNode.TypeReference;
            var declaringTypeName      = declaringTypeReference.FullName().Split('.').LastOrDefault();

            ResourceType resourceType;

            if (false == this.metadata.TryResolveResourceType(declaringTypeName, out resourceType))
            {
                return(null);
            }

            ResourceProperty resourceProperty = resourceType.Properties.FirstOrDefault(prop => prop.Name == propertyName);

            if (null == resourceProperty)
            {
                return(null);
            }

            PropertyInfo instancePropertyInfo = resourceProperty.GetAnnotation().InstanceProperty;

            if (null == instancePropertyInfo)
            {
                return(null);
            }

            if (resourceType.InstanceType == typeof(ContentPart))
            {
                var partType           = instancePropertyInfo.ReflectedType;
                var recordPropertyInfo = partType.GetProperties().FirstOrDefault(prop => prop.Name == "Record");
                if (null == recordPropertyInfo)
                {
                    return(null);
                }

                var recordType = recordPropertyInfo.PropertyType;
                if (recordType.IsSubclassOf(typeof(ContentPartVersionRecord)))
                {
                    this.contentItemVersionRecordCriteria.BindCriteriaByAlias(recordType.Name, partType.Name);
                }
                else
                {
                    this.contentItemRecordCriteria.BindCriteriaByAlias(recordType.Name, partType.Name);
                }

                return(Projections.Property(partType.Name + "." + propertyName));
            }

            if (!resourceType.InstanceType.IsSubclassOf(typeof(ContentField)))
            {
                return(null);
            }

            var fieldTable = this.FieldTypeFromCLRType(instancePropertyInfo.PropertyType);

            if (string.IsNullOrEmpty(fieldTable))
            {
                return(null);
            }

            var parentResourceNode = (declaringSourceNode as SingleValuePropertyAccessNode).Source;
            var parentResourceName = parentResourceNode.TypeReference.FullName().Split('.').LastOrDefault();

            this.metadata.TryResolveResourceType(parentResourceName, out resourceType);

            //var resourceSetTypeReference = null != this.filterClause ? this.filterClause.ItemType : this.orderByClause.ItemType;
            var resourceSetTypeName = resourceType.Name; //resourceSetTypeReference.FullName().Split('.').LastOrDefault();
            var aliasJoinName       = resourceSetTypeName + fieldTable;

            var criteria = this.contentItemRecordCriteria
                           .BindCriteriaByPath("FieldIndexPartRecord")
                           .BindCriteriaByAlias(
                fieldTable,
                aliasJoinName,
                NHibernate.SqlCommand.JoinType.LeftOuterJoin);

            if (null != this.filterClause)
            {
                criteria.Add(Restrictions.Eq(
                                 "PropertyName",
                                 resourceSetTypeName + "." + declaringTypeName + "."));
            }

            return(Projections.Property(aliasJoinName + ".Value"));
        }