public BlogPost(Entity entity, ApplicationPath applicationPath, IBlogAuthor author, BlogCommentPolicy commentPolicy, int commentCount, IEnumerable <IBlogPostTag> tags, IRatingInfo ratingInfo = null) { if (entity == null) { throw new ArgumentNullException("entity"); } if (entity.LogicalName != "adx_blogpost") { throw new ArgumentException(string.Format("Value must have logical name {0}.", entity.LogicalName), "entity"); } if (applicationPath == null) { throw new ArgumentNullException("applicationPath"); } if (commentCount < 0) { throw new ArgumentException("Value can't be negative.", "commentCount"); } if (tags == null) { throw new ArgumentNullException("tags"); } Entity = entity; ApplicationPath = applicationPath; Author = author ?? new NullBlogAuthor(); CommentPolicy = commentPolicy; CommentCount = commentCount; HasExcerpt = !string.IsNullOrWhiteSpace(entity.GetAttributeValue <string>("adx_summary")); Id = entity.Id; IsPublished = entity.GetAttributeValue <bool?>("adx_published").GetValueOrDefault(false); LastUpdatedTime = entity.GetAttributeValue <DateTime?>("modifiedon").GetValueOrDefault(DateTime.UtcNow); PublishDate = entity.GetAttributeValue <DateTime?>("adx_date").GetValueOrDefault(DateTime.UtcNow); Summary = new HtmlString(entity.GetAttributeValue <string>("adx_summary")); Tags = tags.ToArray(); Title = entity.GetAttributeValue <string>("adx_name"); RatingInfo = ratingInfo; var copy = entity.GetAttributeValue <string>("adx_copy"); Content = new HtmlString(HasExcerpt ? "{0}\n{1}".FormatWith(Summary, copy) : copy); }
private void WriteCommentPolicy(Storage postStorage, BlogCommentPolicy commentPolicy) { switch (commentPolicy) { case BlogCommentPolicy.Unspecified: WriteString(postStorage, POST_COMMENT_POLICY, COMMENT_POLICY_UNSPECIFIED); break; case BlogCommentPolicy.None: WriteString(postStorage, POST_COMMENT_POLICY, COMMENT_POLICY_NONE); break; case BlogCommentPolicy.Closed: WriteString(postStorage, POST_COMMENT_POLICY, COMMENT_POLICY_CLOSED); break; case BlogCommentPolicy.Open: WriteString(postStorage, POST_COMMENT_POLICY, COMMENT_POLICY_OPEN); break; default: Debug.Fail("Unexpected BlogCommentPolicy"); WriteString(postStorage, POST_COMMENT_POLICY, COMMENT_POLICY_UNSPECIFIED); break; } }
public static IDictionary <Guid, Tuple <string, string, BlogCommentPolicy, string[], IRatingInfo> > FetchBlogPostExtendedData(this OrganizationServiceContext serviceContext, IEnumerable <Guid> postIds, BlogCommentPolicy defaultCommentPolicy, Guid websiteId) { if (!postIds.Any()) { return(new Dictionary <Guid, Tuple <string, string, BlogCommentPolicy, string[], IRatingInfo> >()); } var ids = postIds.ToArray(); var fetchXml = XDocument.Parse(@" <fetch mapping=""logical""> <entity name=""adx_blogpost""> <attribute name=""adx_commentpolicy"" /> <filter type=""and""> </filter> <link-entity name=""adx_blog"" from=""adx_blogid"" to=""adx_blogid"" alias=""blog""> <attribute name=""adx_commentpolicy"" /> </link-entity> <link-entity link-type=""outer"" name=""contact"" from=""contactid"" to=""adx_authorid"" alias=""author""> <attribute name=""contactid"" /> <attribute name=""fullname"" /> <attribute name=""firstname"" /> <attribute name=""lastname"" /> <attribute name=""emailaddress1"" /> </link-entity> <link-entity link-type=""outer"" name=""adx_blogpost_tag"" from=""adx_blogpostid"" to=""adx_blogpostid""> <link-entity link-type=""outer"" name=""adx_tag"" from=""adx_tagid"" to=""adx_tagid"" alias=""tag""> <attribute name=""adx_name"" /> <filter type=""and""> <condition attribute=""adx_websiteid"" operator=""eq"" /> </filter> </link-entity> </link-entity> </entity> </fetch>" ); var postFilter = fetchXml.XPathSelectElement("//entity[@name='adx_blogpost']/filter"); if (postFilter == null) { throw new InvalidOperationException(string.Format("Unable to select {0} element.", "adx_blogpostid filter condition")); } postFilter.AddFetchXmlFilterInCondition("adx_blogpostid", ids.Select(id => id.ToString())); var websiteConditions = fetchXml.XPathSelectElements("//filter/condition[@attribute='adx_websiteid']"); foreach (var websiteCondition in websiteConditions) { websiteCondition.SetAttributeValue("value", websiteId.ToString()); } var fetch = Fetch.Parse(fetchXml.ToString()); var contactLink = fetch.Entity.Links.FirstOrDefault(l => l.Name.Equals("contact")); if (contactLink != null) { contactLink.IsUnique = true; } var response = (serviceContext as IOrganizationService).RetrieveMultiple(fetch); var aggregateFilter = fetchXml.Descendants("filter").First(); aggregateFilter.AddFetchXmlFilterInCondition("adx_blogpostid", ids.Select(id => id.ToString())); EntityCollection aggregateResponse = null; if (FeatureCheckHelper.IsFeatureEnabled(FeatureNames.Feedback)) { XDocument aggregateFetchXml = XDocument.Parse(@" <fetch mapping=""logical"" aggregate=""true""> <entity name=""feedback""> <attribute name=""regardingobjectid"" alias=""ratingcount"" aggregate=""countcolumn""/> <attribute name=""rating"" alias=""ratingsum"" aggregate=""sum"" /> <attribute name=""rating"" alias=""value"" groupby=""true"" /> <filter type=""and""> <condition attribute=""statecode"" operator=""eq"" value=""0"" /> <condition attribute=""rating"" operator=""not-null"" /> </filter> <link-entity name=""adx_blogpost"" from=""adx_blogpostid"" to=""regardingobjectid""> <attribute name=""adx_blogpostid"" alias=""postid"" groupby=""true"" /> <filter type=""and"" /> </link-entity> </entity> </fetch>" ); aggregateResponse = (serviceContext as IOrganizationService).RetrieveMultiple(Fetch.Parse(aggregateFetchXml.ToString())); } return(ids.ToDictionary(id => id, id => { var entities = response.Entities.Where(e => e.Id == id).ToArray(); if (!entities.Any()) { return new Tuple <string, string, BlogCommentPolicy, string[], IRatingInfo>(null, null, defaultCommentPolicy, new string[] {}, null); } var entity = entities.First(); var authorName = Localization.LocalizeFullName(entity.GetAttributeAliasedValue <string>("author.firstname"), entity.GetAttributeAliasedValue <string>("author.lastname")); var authorEmail = entity.GetAttributeAliasedValue <string>("author.emailaddress1"); object postCommentPolicyAttributeValue; var postCommentPolicy = entity.Attributes.TryGetValue("adx_commentpolicy", out postCommentPolicyAttributeValue) && (postCommentPolicyAttributeValue is OptionSetValue) ? (BlogPostCommentPolicy)Enum.ToObject(typeof(BlogPostCommentPolicy), ((OptionSetValue)postCommentPolicyAttributeValue).Value) : BlogPostCommentPolicy.Inherit; var blogCommentPolicyOption = entity.GetAttributeAliasedValue <OptionSetValue>("blog.adx_commentpolicy"); var blogCommentPolicy = blogCommentPolicyOption == null ? defaultCommentPolicy : (BlogCommentPolicy)Enum.ToObject(typeof(BlogCommentPolicy), blogCommentPolicyOption.Value); var commentPolicy = postCommentPolicy == BlogPostCommentPolicy.Inherit ? blogCommentPolicy : (BlogCommentPolicy)Enum.ToObject(typeof(BlogCommentPolicy), (int)postCommentPolicy); var tags = entities .Select(e => e.GetAttributeAliasedValue <string>("tag.adx_name")) .Where(tagName => !string.IsNullOrWhiteSpace(tagName)) .ToArray(); int ratingCount = 0; int ratingSum = 0; double averageRating = 0; int yesCount = 0; int noCount = 0; if (aggregateResponse != null) { var aggregateResults = aggregateResponse.Entities .Where(e => e.GetAttributeAliasedValue <Guid?>("postid") == id); var aggregateYesResult = aggregateResponse.Entities .Where(e => e.GetAttributeAliasedValue <Guid?>("postid") == id) .FirstOrDefault(e => e.GetAttributeAliasedValue <int?>("value") == 1); var aggregateNoResult = aggregateResponse.Entities .Where(e => e.GetAttributeAliasedValue <Guid?>("postid") == id) .FirstOrDefault(e => e.GetAttributeAliasedValue <int?>("value") == 0); yesCount = (aggregateYesResult != null) ? aggregateYesResult.GetAttributeAliasedValue <int?>("ratingcount") ?? 0 : 0; noCount = (aggregateNoResult != null) ? aggregateNoResult.GetAttributeAliasedValue <int?>("ratingcount") ?? 0 : 0; foreach (var aggregateResult in aggregateResults) { ratingCount = ratingCount + aggregateResult.GetAttributeAliasedValue <int?>("ratingcount") ?? 0; ratingSum = ratingSum + aggregateResult.GetAttributeAliasedValue <int?>("ratingsum") ?? 0; } if (ratingCount == 0) { averageRating = 0; } else { averageRating = ratingSum / (double)ratingCount; } } var ratingInfo = new RatingInfo(yesCount, noCount, averageRating, ratingCount, ratingSum); return new Tuple <string, string, BlogCommentPolicy, string[], IRatingInfo>(authorName, authorEmail, commentPolicy, tags, ratingInfo); })); }