Mono Support RelatedContent Calles DB even if EntityType is None 

Viewed 13516 time(s), 8 post(s), 1/8/2014 6:23:27 AM - by Dinesh.Sajwan
1/8/2014 6:23:28 AM
1284 Reputation 156 Total posts

Hi
Blog,Groups and Discussions using RelatedContent feature in monox .
RelatedContent finally calls RelatedContentBLL.GetRelatedContent method that
takes sourceContentId and sourceContentEntityType as a parameter. If sourceContentEntityType
is passing None then is this function should called or not ?  Actually for performance I am
putting check sourceContentEntityType!=SnEntityType.None  then only this
function should called .Am I doing right thing or is it going to impact
something?

1
1/8/2014 9:45:04 AM
3016 Reputation 428 Total posts

Hi,

you should set sourceContentEntityType in your parent page/control. That way you will get items related to the type specified. If you do not set contentEntityType  RelatedContentBLL.GetRelatedContent method will be called, but will not be fully executed and you should not see any items.

Regards.

2
1/8/2014 10:45:00 AM
1284 Reputation 156 Total posts

I did not change any functionality related to this user control(RelatedContent ) . I just put a simple check(sourceContentEntityType != SnEntityType.None   then function should called further  otherwise will return from here) in RelatedContentBLL.GetRelatedContent  method .
Is this change will impact anything else ? I should put this check or not ?

3
1/8/2014 10:50:31 AM
3016 Reputation 428 Total posts

It should not affect anything else because its called only from the related content user control. Do you have MonoX source code?

4
1/8/2014 11:35:47 AM
1284 Reputation 156 Total posts

yes I have source code of Monox.
I have made changes in below function only . Changes are highlighted Bold .

public virtual List<RelatedContentDTO> GetRelatedContent(Guid sourceContentId, SnEntityType sourceContentEntityType, RelatedContentArgs args, int pageIndex, int pageSize, out int recordCount)
{
List<RelatedContentDTO> result = null;
result = cacheManager.Get<List<RelatedContentDTO>>(ParamRelatedContentItems, sourceContentEntityType, sourceContentId, pageIndex, pageSize);
recordCount = cacheManager.Get<int>(ParamRelatedContentItems, sourceContentEntityType, sourceContentId, pageIndex, pageSize, "recordCount");
if (result == null)
{
if (sourceContentEntityType != SnEntityType.None)
{

result = new List<RelatedContentDTO>();
RelationshipRepository relationships = RelationshipRepository.GetInstance();
RelationPredicateBucket filter = new RelationPredicateBucket();
List<string> tags = new List<string>();
//Get source and tags
EntityCollection<SnTagEntity> tagCollection = new EntityCollection<SnTagEntity>();
filter = relationships.ConstructSnEntityTypeRelations(SnEntityType.Tag, filter);
filter = relationships.ConstructSnEntityTypeFilter(sourceContentId, sourceContentEntityType, filter);
relationships.FetchEntityCollection(tagCollection, filter);
tags.AddRange(tagCollection.Select(p => p.Slug).ToList());

if (tags.Count > 0)
{
//Get related content via tags
IEntityCollection2 collection = null;
IPrefetchPath2 prefetch = null;
filter = new RelationPredicateBucket();

switch (sourceContentEntityType)
{
case SnEntityType.File:
filter.Relations.Add(SnFileEntity.Relations.SnRelationshipEntityUsingFileId);
break;
case SnEntityType.Album:
case SnEntityType.Message:
case SnEntityType.Note:
case SnEntityType.BlogPost:
case SnEntityType.DiscussionMessage:
case SnEntityType.Comment:
case SnEntityType.Tag:
case SnEntityType.Rating:
case SnEntityType.DiscussionBoard:
case SnEntityType.DiscussionTopic:
case SnEntityType.Document:
case SnEntityType.NewsItem:
case SnEntityType.ListItem:
case SnEntityType.Blog:
case SnEntityType.Group:
case SnEntityType.User:
case SnEntityType.Campaign:
case SnEntityType.NewsCategory:
case SnEntityType.Newsletter:
case SnEntityType.Page:
case SnEntityType.Poll:
filter = relationships.ConstructSnEntityTypeRelations(sourceContentEntityType, filter);
break;
case SnEntityType.None:
default:
break;
}

filter.Relations.Add(SnRelationshipEntity.Relations.SnTagEntityUsingRelationshipId, JoinHint.Left);
filter.PredicateExpression.Add(new FieldCompareRangePredicate(SnTagFields.Slug, null, tags.ToArray()));

switch (sourceContentEntityType)
{
case SnEntityType.Album:
collection = new EntityCollection<SnAlbumEntity>();
prefetch = AlbumRepository.GetInstance().GetPrefetchPath(true);
break;
case SnEntityType.Message:
collection = new EntityCollection<SnMessageEntity>();
break;
case SnEntityType.Note:
collection = new EntityCollection<SnNoteEntity>();
break;
case SnEntityType.BlogPost:
collection = new EntityCollection<BlogPostEntity>();
prefetch = BlogRepository.GetInstance().GetBlogPostPrefetchPath(false);
prefetch.Add(BlogPostEntity.PrefetchPathBlog);
break;
//case SnEntityType.Custom1:
// break;
//case SnEntityType.Custom2:
// break;
//case SnEntityType.Custom3:
// break;
case SnEntityType.File:
collection = new EntityCollection<SnFileEntity>();
prefetch = FileRepository.GetInstance().GetPrefetchPath(true);
break;
//case SnEntityType.DiscussionMessage:
// break;
//case SnEntityType.Comment:
// break;
//case SnEntityType.Tag:
// break;
//case SnEntityType.Rating:
// break;
case SnEntityType.DiscussionBoard:
collection = new EntityCollection<SnDiscussionBoardEntity>();
prefetch = DiscussionRepository.GetInstance().GetBoardPrefetchPath(true);
break;
case SnEntityType.DiscussionTopic:
collection = new EntityCollection<SnDiscussionTopicEntity>();
prefetch = DiscussionRepository.GetInstance().GetTopicPrefetchPath(true, -1);
break;
case SnEntityType.Document:
collection = new EntityCollection<DocumentEntity>();
prefetch = DocumentRepository.GetInstance().GetDocumentPrefetchPath(true);
break;
case SnEntityType.NewsItem:
collection = new EntityCollection<NewsItemEntity>();
prefetch = NewsRepository.GetInstance().GetNewsItemPrefetchPath(true);
break;
case SnEntityType.ListItem:
collection = new EntityCollection<ListItemEntity>();
break;
case SnEntityType.Blog:
collection = new EntityCollection<BlogEntity>();
prefetch = BlogRepository.GetInstance().GetBlogPrefetchPath(true);
break;
//case SnEntityType.Subscriber:
// break;
case SnEntityType.Group:
collection = new EntityCollection<SnGroupEntity>();
prefetch = GroupRepository.GetInstance().GetGroupPrefetch(true);
break;
case SnEntityType.User:
collection = new EntityCollection<AspnetUsersEntity>();
prefetch = UserRepository.GetInstance().GetUserPrefetchPath();
break;
case SnEntityType.Campaign:
collection = new EntityCollection<CampaignEntity>();
prefetch = AdRepository.GetInstance().GetCampaignPrefetchPath2(true);
break;
case SnEntityType.NewsCategory:
collection = new EntityCollection<NewsCategoryEntity>();
prefetch = new PrefetchPath2(EntityType.NewsCategoryEntity);
break;
case SnEntityType.Newsletter:
collection = new EntityCollection<NewsletterEntity>();
prefetch = NewsletterRepository.GetInstance().GetPrefetchPath(true);
break;
case SnEntityType.Page:
collection = new EntityCollection<PageEntity>();
prefetch = PageRepository.GetInstance().GetPagePrefetchPath();
break;
case SnEntityType.Poll:
collection = new EntityCollection<PollEntity>();
prefetch = PollRepository.GetInstance().GetPrefetchPath(true);
break;
case SnEntityType.None:
default:
return result;
}

relationships.FetchEntityCollection(collection, filter, 0, null, prefetch, pageIndex, pageSize);
recordCount = relationships.GetDbCount(collection, filter);

//TODO: Implement all mappings
foreach (IEntity2 item in collection)
{
AspnetUsersEntity aspnetUser = null;
RelatedContentDTO dtoEntity = null;

switch (sourceContentEntityType)
{
case SnEntityType.Album:
var album = (SnAlbumEntity)item;
dtoEntity = new RelatedContentDTO(album.Id, sourceContentId, sourceContentEntityType);
dtoEntity.DateCreated = album.DateCreated;
dtoEntity.Content = album.Name;
dtoEntity.UserId = album.UserId.GetValueOrDefault();
dtoEntity.ContentUrl = RewrittenUrlBuilder.SocialNetwork.PhotoGallery.GetPhotoListUrl(args.PhotoListUrlPattern, album.Id, args.PhotoListUrl).Url;
aspnetUser = album.AspnetUser;
break;
case SnEntityType.Message:
break;
case SnEntityType.Note:
break;
case SnEntityType.BlogPost:
var blogPost = (BlogPostEntity)item;
dtoEntity = new RelatedContentDTO(blogPost.Id, sourceContentId, sourceContentEntityType);
dtoEntity.DateCreated = blogPost.DateCreated.GetValueOrDefault();
dtoEntity.Content = blogPost.Title;
dtoEntity.UserId = blogPost.UserId;
dtoEntity.ContentUrl = RewrittenUrlBuilder.Blog.GetBlogPostUrl(args.BlogPostUrl, blogPost).Url;
aspnetUser = blogPost.AspnetUser;
break;
case SnEntityType.File:
var file = (SnFileEntity)item;
dtoEntity = new RelatedContentDTO(file.Id, sourceContentId, sourceContentEntityType);
dtoEntity.DateCreated = file.DateCreated;
dtoEntity.Content = String.IsNullOrEmpty(file.Description) ? file.Name : file.Description;
dtoEntity.UserId = file.UserId;
dtoEntity.ContentUrl = RewrittenUrlBuilder.SocialNetwork.Files.GetFileViewUrl(args.ViewFilePageUrl, file.Id, args.CurrentPageId, String.Empty).Url;
aspnetUser = file.AspnetUser;
break;
case SnEntityType.DiscussionBoard:
var entity = (SnDiscussionBoardEntity)item;
dtoEntity = new RelatedContentDTO(entity.Id, sourceContentId, sourceContentEntityType);
dtoEntity.DateCreated = entity.DateCreated;
dtoEntity.Content = entity.Name;
dtoEntity.ContentUrl = DiscussionBase.GenerateBoardUrl(entity);
dtoEntity.UserId = entity.UserId.GetValueOrDefault();
aspnetUser = entity.AspnetUser;
break;
case SnEntityType.DiscussionTopic:
var topic = (SnDiscussionTopicEntity)item;
dtoEntity = new RelatedContentDTO(topic.Id, sourceContentId, sourceContentEntityType);
dtoEntity.DateCreated = topic.DateCreated;
dtoEntity.Content = topic.Title;
dtoEntity.ContentUrl = DiscussionBase.GenerateTopicUrl(topic);
dtoEntity.UserId = topic.UserId.GetValueOrDefault();
aspnetUser = topic.AspnetUser;
break;
case SnEntityType.Document:
var document = (DocumentEntity)item;
dtoEntity = new RelatedContentDTO(document.Id, sourceContentId, sourceContentEntityType);
dtoEntity.DateCreated = document.DateModified;
dtoEntity.Content = document.Title;
dtoEntity.ContentUrl = DocumentRepository.GetInstance().GetDocumentUrl(document);
dtoEntity.UserId = document.UserId.GetValueOrDefault();
aspnetUser = document.AspnetUsers;
break;
case SnEntityType.NewsItem:
var newsItem = (NewsItemEntity)item;
dtoEntity = new RelatedContentDTO(newsItem.Id, sourceContentId, sourceContentEntityType);
dtoEntity.DateCreated = newsItem.PublishStart;
dtoEntity.Content = newsItem.Title;
dtoEntity.ContentUrl = NewsBLL.GetInstance().GetNewsReadMorePageUrl(args.NewsReadMorePageUrl, newsItem.Id);
dtoEntity.UserId = newsItem.UserId;
aspnetUser = newsItem.AspnetUser;
break;
case SnEntityType.ListItem:
break;
case SnEntityType.Blog:
break;
case SnEntityType.Group:
var group = (SnGroupEntity)item;
dtoEntity = new RelatedContentDTO(group.Id, sourceContentId, sourceContentEntityType);
dtoEntity.DateCreated = group.DateCreated;
dtoEntity.Content = group.Name;
dtoEntity.ContentUrl = UrlUtility.RewritePagePath(RewrittenPaths.GroupView.UrlPattern, String.Empty, group);
dtoEntity.UserId = Guid.Empty;
aspnetUser = null;
break;
case SnEntityType.User:
var user = (AspnetUsersEntity)item;
dtoEntity = new RelatedContentDTO(user.UserId, sourceContentId, sourceContentEntityType);
dtoEntity.DateCreated = user.AspnetMembership != null ? user.AspnetMembership.CreateDate : user.LastActivityDate;
dtoEntity.Content = user.UserName;
dtoEntity.ContentUrl = RewrittenUrlBuilder.UserProfile.GetUserProfileUrl(RewrittenPaths.Profile.UrlPattern, user).Url;
dtoEntity.UserId = user.UserId;
aspnetUser = user;
break;
case SnEntityType.Campaign:
var campaign = (CampaignEntity)item;
dtoEntity = new RelatedContentDTO(campaign.Id, sourceContentId, sourceContentEntityType);
dtoEntity.DateCreated = campaign.DateEntered;
dtoEntity.Content = campaign.Name;
dtoEntity.ContentUrl = String.Empty;
dtoEntity.UserId = Guid.Empty;
aspnetUser = null;
break;
case SnEntityType.NewsCategory:
var newsCategory = (NewsCategoryEntity)item;
dtoEntity = new RelatedContentDTO(newsCategory.Id, sourceContentId, sourceContentEntityType);
dtoEntity.DateCreated = newsCategory.DateEntered.GetValueOrDefault();
dtoEntity.Content = LocalizationTextHandler.GetText(newsCategory.NewsCategoryLocalizations, NewsCategoryLocalizationFields.Title, NewsCategoryLocalizationFields.LanguageId);
dtoEntity.ContentUrl = String.Empty;
dtoEntity.UserId = Guid.Empty;
aspnetUser = null;
break;
case SnEntityType.Newsletter:
var newsletter = (NewsletterEntity)item;
dtoEntity = new RelatedContentDTO(newsletter.Id, sourceContentId, sourceContentEntityType);
dtoEntity.DateCreated = newsletter.SentOn.GetValueOrDefault();
dtoEntity.Content = newsletter.TextContent;
dtoEntity.ContentUrl = String.Empty;
dtoEntity.UserId = newsletter.UserId;
aspnetUser = newsletter.AspnetUser;
break;
case SnEntityType.Page:
var page = (PageEntity)item;
dtoEntity = new RelatedContentDTO(page.Id, sourceContentId, sourceContentEntityType);
dtoEntity.DateCreated = page.DateCreated;
dtoEntity.Content = page.Url;
dtoEntity.ContentUrl = LocalizationUtility.RewriteLink(page.Url);
dtoEntity.UserId = page.UserId.GetValueOrDefault();
aspnetUser = page.AspnetUsers;
break;
case SnEntityType.Poll:
var poll = (PollEntity)item;
dtoEntity = new RelatedContentDTO(poll.Id, sourceContentId, sourceContentEntityType);
dtoEntity.DateCreated = poll.DateModified;
dtoEntity.Content = poll.Title;
dtoEntity.ContentUrl = String.Empty;
dtoEntity.UserId = poll.UserId;
aspnetUser = poll.AspnetUser;
break;
case SnEntityType.None:
default:
break;
}

if (aspnetUser != null)
{
dtoEntity.UserName = aspnetUser.UserName;
if (aspnetUser.UserProfile != null)
dtoEntity.Author = String.Format("{0} {1}", aspnetUser.UserProfile.LastName, aspnetUser.UserProfile.FirstName);
dtoEntity.UserProfileUrl = RewrittenUrlBuilder.UserProfile.GetUserProfileUrl(args.UserProfileUrl, aspnetUser).Url;
}
result.Add(dtoEntity);
}
}

RelatedContentDTO sourceItem = result.FirstOrDefault(p => p.Id.Equals(sourceContentId));
if (sourceItem != null)
result.Remove(sourceItem);

cacheManager.Store(result, ParamRelatedContentItems, sourceContentEntityType, sourceContentId, pageIndex, pageSize);
cacheManager.Store(recordCount, ParamRelatedContentItems, sourceContentEntityType, sourceContentId, pageIndex, pageSize, "recordCount");
}
}
return result;
}

5
1/8/2014 12:00:38 PM
3016 Reputation 428 Total posts

Can you please post a file instead of posting all the code here for the future posts? Its easier to view/monitor it. As far as I see your change should not be a problem, this way you prevented some DB calls.

Regards.

6
1/8/2014 12:12:27 PM
1284 Reputation 156 Total posts

Hi
I am attaching the code file of RelatedContentBLL . So as per your suggestion I should go ahead with these changes for better performance?

7
1/8/2014 12:33:33 PM
3016 Reputation 428 Total posts

It seems fine that way as I already mentioned. Just to check, where do you have that control without entityType set?

8
This is a demo site for MonoX. Please visit Mono Software for more info.