public async Task <IEnumerable <ContentsView> > GetContents(long communityId, long userId)
        {
            // TODO: Need to remove this once we have logic for loading objects from DBSet directly.

            // Get the child contents for the given community/folder first.
            var communityContents            = EarthOnlineDbContext.Set <CommunityContents>();
            IEnumerable <long> subContentIds = communityContents.Where(relation => relation.CommunityID == communityId).Select(relation => relation.ContentID);

            var contentsView = EarthOnlineDbContext.Set <ContentsView>();

            // Get the contents which are public.
            // Get private contents only if:
            //      1. If the user is site administrator.
            //      2. If the user is given explicit permission through roles.
            var result = await contentsView
                         .Where(
                content => subContentIds.Contains(content.ContentID) && (content.AccessType == Resources.Public ||
                                                                         (EarthOnlineDbContext.User
                                                                          .FirstOrDefault(user => user.UserID == userId &&
                                                                                          user.UserTypeID == 1) != null ||
                                                                          EarthOnlineDbContext
                                                                          .UserCommunities
                                                                          .FirstOrDefault(uc => uc.UserID == userId &&
                                                                                          uc.CommunityId == communityId &&
                                                                                          uc.RoleID >= (int)UserRole.Reader) != null)))
                         .OrderByDescending(content => content.LastUpdatedDatetime).ToListAsync();

            return(result);
        }
        /// <summary>
        /// Retrieves the IDs of sub communities of a given community recursively for all children and grand children
        /// </summary>
        /// <param name="communityId">
        /// ID of the community.
        /// </param>
        /// <returns>
        /// Collection of IDS of sub communities.
        /// </returns>
        private IEnumerable <long> GetAllChildrenCommunities(long communityId)
        {
            IEnumerable <long> results = null;
            var communitiesRelation    = EarthOnlineDbContext.Set <CommunityRelation>();

            results = communitiesRelation.Where(relation => relation.ParentCommunityID == communityId).Select(relation => relation.ChildCommunityID);

            // TODO : Optimize multiple calls going to DB
            foreach (var result in results)
            {
                results = results.Concat(GetAllChildrenCommunities(result));
            }

            return(results.ToList());
        }
        /// <summary>
        /// Retrieves the IDs of sub communities of a given community. This only retrieves the immediate children.
        /// </summary>
        /// <param name="communityId">
        /// ID of the community.
        /// </param>
        /// <param name="userId">Id of the user who is accessing</param>
        /// <returns>
        /// Collection of IDS of sub communities.
        /// </returns>
        public IEnumerable <long> GetSubCommunityIDs(long communityId, long userId)
        {
            // TODO: Need to remove this once we have logic for loading objects from DBSet directly.
            IEnumerable <long> subCommunityIds = null;

            // Get the child communities for the given community first.
            var communitiesRelation = EarthOnlineDbContext.Set <CommunityRelation>();

            subCommunityIds = communitiesRelation.Where(relation => relation.ParentCommunityID == communityId).Select(relation => relation.ChildCommunityID);

            // Get the communities which are public. Get private communities only if the owner is current user.
            var result =
                EarthOnlineDbContext.Community.Where(community =>
                                                     subCommunityIds.Contains(community.CommunityID) && !(bool)community.IsDeleted &&
                                                     (community.AccessTypeID == (int)AccessType.Public ||
                                                      (EarthOnlineDbContext.User.FirstOrDefault(user => user.UserID == userId && user.UserTypeID == 1) != null ||
                                                       EarthOnlineDbContext.UserCommunities.FirstOrDefault(uc => uc.UserID == userId && uc.CommunityId == communityId &&
                                                                                                           uc.RoleID >= (int)UserRole.Reader) != null))).OrderByDescending(community => community.ModifiedDatetime).Select(community => community.CommunityID);

            return(result.ToList());
        }