Exemplo n.º 1
0
        /// <summary>
        /// Update the properties of the resource.
        /// </summary>
        /// <param name="zentityContext">The ZentityContext to which the resource is attached.</param>
        /// <param name="resource">Resource to be update scalar properties.</param>
        /// <param name="dataSection"><typeref name="MetadataSection"/> Metadata which contains property values.</param>
        private static void UpdateResourceProeprties(ZentityContext zentityContext, ScholarlyWork resource,
                                                     MetadataSection dataSection)
        {
            if (null == resource)
            {
                throw new ArgumentNullException("resource");
            }

            if (null == dataSection)
            {
                throw new ArgumentNullException("dataSection");
            }

            resource.DateModified = DateTime.Now;

            PropertyInfo[] properties = resource.GetType().GetProperties()
                                        .Where(property => PropertyMapper.ResourceProperties.ContainsKey(property.Name))
                                        .ToArray();

            // Loop for each property of the resource.
            foreach (PropertyInfo property in properties)
            {
                ReadOnlyCollection <string> values = GetPropertyValues(property.Name, dataSection);

                // If no values are present for current property, do not process.
                if (null == values || 0 >= values.Count)
                {
                    continue;
                }

                // Check if the property is scalar property or Navigation Property.
                object[] navigationAttr = property.GetCustomAttributes(typeof(EdmRelationshipNavigationPropertyAttribute), true);

                if (0 < navigationAttr.Length)
                {
                    UpdateNavigationproperties(zentityContext, resource, property, values);
                }
                else
                {
                    UpdateScalarProperties(resource, property, values);
                }
            }
        }
        /// <summary>
        /// Adds the links and source.
        /// </summary>
        /// <param name="baseUri">The base URI.</param>
        /// <param name="resource">The resource.</param>
        /// <param name="item">The syndication item.</param>
        private static void AddLinksAndSource(Uri baseUri, ScholarlyWork resource, ref SyndicationItem item)
        {
            // NameValue pair for creating Uri from UriTemplate.
            NameValueCollection parameters = new NameValueCollection();

            parameters.Add(AtomPubParameterType.CollectionName.ToString(), resource.GetType().Name);
            parameters.Add(AtomPubParameterType.Id.ToString(), resource.Id.ToString());

            Core.File mediaFile = resource.Files.FirstOrDefault();

            if (null != mediaFile)
            {
                Uri mediaUri = AtomPubHelper.AtomPubTemplates[AtomPubRequestType.EditMedia]
                               .BindByName(baseUri, parameters);

                // Add content linnk and edit-media link
                if (string.IsNullOrEmpty(mediaFile.MimeType))
                {
                    mediaFile.MimeType = AtomPubConstants.DefaultMimeType;
                }

                SyndicationLink resourceMediaLink = new SyndicationLink(mediaUri);
                resourceMediaLink.RelationshipType = AtomPubConstants.EditMedia;
                item.Links.Add(resourceMediaLink);

                item.Content = new UrlSyndicationContent(mediaUri, mediaFile.MimeType);
            }
            else
            {
                ResourceProperty urlContent = GetResourceProperty(resource, AtomPubConstants.ContentUrlProperty);

                if (null != urlContent && !string.IsNullOrEmpty(urlContent.Value))
                {
                    item.Content = GetContent <UrlSyndicationContent>(urlContent.Value, resource.ResourceProperties, AtomPubConstants.DescriptionTypeProperty, false);
                }
                else
                {
                    item.Content = GetContent <SyndicationContent>(resource.Description, resource.ResourceProperties, AtomPubConstants.DescriptionTypeProperty, true);
                }
            }

            // Add resource Edit link
            Uri             resourceUri      = AtomPubHelper.AtomPubTemplates[AtomPubRequestType.EditMember].BindByName(baseUri, parameters);
            SyndicationLink resourceEditLink = new SyndicationLink(resourceUri);

            resourceEditLink.RelationshipType = AtomPubConstants.Edit;
            item.Links.Add(resourceEditLink);

            if (null != resource.Container)
            {
                var relatedItems = resource.Container.ContainedWorks.OfType <ScholarlyWorkContainer>()
                                   .SelectMany(tuple => tuple.ContainedWorks)
                                   .Where(tuple => !(tuple is ScholarlyWorkContainer));

                foreach (ScholarlyWork containedItem in relatedItems)
                {
                    parameters.Set(AtomPubParameterType.CollectionName.ToString(), containedItem.GetType().Name);
                    parameters.Set(AtomPubParameterType.Id.ToString(), containedItem.Id.ToString());
                    resourceUri = AtomPubHelper.AtomPubTemplates[AtomPubRequestType.EditMember].BindByName(baseUri, parameters);
                    SyndicationLink containsEditLink = new SyndicationLink(resourceUri);
                    containsEditLink.RelationshipType = AtomPubConstants.Related;
                    item.Links.Add(containsEditLink);
                }
            }

            AtomEntryDocument entry = new AtomEntryDocument(item);

            IEnumerable <ResourceProperty> links = ZentityAtomPubStoreReader.GetResourceProperties(resource, AtomPubConstants.LinksProperty);

            if (null != links)
            {
                foreach (ResourceProperty link in links)
                {
                    entry.XmlLinks.Add(link.Value);
                }
            }

            ResourceProperty source = GetResourceProperty(resource, AtomPubConstants.SourceProperty);

            if (null != source)
            {
                entry.Source = source.Value;
            }

            using (XmlReader reader = new XmlTextReader(entry.AtomEntry, XmlNodeType.Document, null))
            {
                item = SyndicationItem.Load(reader);
            }
        }