/// <summary>
        /// Updates the relationship for parent and content.
        /// </summary>
        /// <param name="contentDetails">Details of the content.</param>
        /// <param name="content">Content which has to be updated.</param>
        private static void UpdateParent(ContentDetails contentDetails, Content content)
        {
            // Few things to be noted:
            // a) Obviously the count to be 1 always.
            // b) A content can be child of only once parent community or folder
            if (Enumerable.ElementAt(content.CommunityContents, 0).CommunityID != contentDetails.ParentID)
            {
                if (content.CommunityContents.Count > 0)
                {
                    content.CommunityContents.Clear();
                }

                var comCont = new CommunityContents()
                {
                    CommunityID = contentDetails.ParentID,
                    Content     = content
                };

                // Add the relationship
                content.CommunityContents.Add(comCont);
            }
        }
        /// <summary>
        /// Creates the new content in Layerscape with the given details passed in ContentsView instance.
        /// </summary>
        /// <param name="contentDetails">Details of the content</param>
        /// <returns>Id of the content created. Returns -1 is creation is failed.</returns>
        public long CreateContent(ContentDetails contentDetails)
        {
            // Make sure content is not null
            this.CheckNotNull(() => new { contentDetails });

            long contentId = -1;

            var userRole = GetCommunityUserRole(contentDetails.ParentID, contentDetails.CreatedByID);

            if (!CanCreateContent(userRole))
            {
                // TODO: Throw custom permissions exception which will be shown to user in error page.
                throw new HttpException(401, Resources.NoPermissionCreateContentMessage);
            }

            // Upload content data, thumbnail, video and associated file to azure.
            // Since the associated files are already stored in the Temporary container.
            // We need to move the files to actual container.
            if (UploadFilesToAzure(contentDetails))
            {
                if (MoveAssociatedFiles(contentDetails))
                {
                    try
                    {
                        // Create a new instance of content details
                        var content = new Content();

                        // Set values from content details.
                        content.SetValuesFrom(contentDetails);

                        // Set the thumbnail ID.
                        content.ThumbnailID = MoveThumbnail(contentDetails.Thumbnail);

                        // Set Created and modified time.
                        content.CreatedDatetime = content.ModifiedDatetime = DateTime.UtcNow;

                        // Set Created and modified IDs.
                        content.ModifiedByID = contentDetails.CreatedByID;
                        content.CreatedByID  = contentDetails.CreatedByID;

                        // Set Default Values.
                        content.IsDeleted     = false;
                        content.IsSearchable  = true;
                        content.IsOffensive   = false;
                        content.DownloadCount = 0;

                        var parentCommunity = _communityRepository.GetItem(c => c.CommunityID == contentDetails.ParentID);
                        if (parentCommunity != null)
                        {
                            parentCommunity.ModifiedByID     = contentDetails.CreatedByID;
                            parentCommunity.ModifiedDatetime = DateTime.UtcNow;

                            // Updated Parent child relationship
                            var comCont = new CommunityContents()
                            {
                                Community = parentCommunity,
                                Content   = content
                            };

                            // Add the relationship
                            content.CommunityContents.Add(comCont);
                        }

                        // Update video file details in database.
                        CreateVideo(content, contentDetails);

                        // Update associated files details in database.
                        CreateAssociateContents(content, contentDetails);

                        // Update Tags.
                        SetContentTags(contentDetails.Tags, content);

                        //// TODO: Update Permissions Details.
                        //// TODO: Update Consumed size of the user. If the size is more than allowed throw error.

                        _contentRepository.Add(content);

                        // Save changes to the database
                        _contentRepository.SaveChanges();

                        // Get the content ID from database. We need to retrieve it from the database as it is a identity column.
                        contentId = content.ContentID;
                    }
                    catch (Exception)
                    {
                        // Delete Uploaded Content/Video/Thumbnail
                        // Note:- Deleting associated files as will be handled by a worker role
                        //  which will delete temporary file from container.
                        DeleteUploadedFiles(contentDetails);
                        throw;
                    }
                }
                else
                {
                    // Delete Uploaded Content/Video/Thumbnail
                    // Note:- Deleting associated files as will be handled by a worker role
                    //  which will delete temporary file from container.
                    DeleteUploadedFiles(contentDetails);
                }
            }
            return(contentId);
        }
        /// <summary>
        /// Un-deletes the specified content from the Earth database so that it is again accessible in the site.
        /// </summary>
        /// <param name="contentId">Content Id</param>
        /// <param name="userId">User Identity</param>
        /// <returns>Status of the operation. Success, if succeeded. Failure message and exception details in case of exception.</returns>
        public OperationStatus UnDeleteOffensiveContent(long contentId, long userId)
        {
            OperationStatus status = null;

            try
            {
                if (_userRepository.IsSiteAdmin(userId))
                {
                    // Get the current content from DB.
                    var content = _contentRepository.GetItem((c) => c.ContentID == contentId);

                    if (content != null)
                    {
                        content.IsDeleted = false;

                        // We need to mark the community as not offensive as the Admin is marking the content as undeleted.
                        content.IsOffensive = false;

                        // We need to mark the DeletedBy as null as we are Undoing the delete operation.
                        //  Also DeleteBy filed will be used to check if the user has explicitly deleted the content.
                        content.User2 = null;

                        var parentCommunity = Enumerable.ElementAt(content.CommunityContents, 0).Community;

                        if ((parentCommunity.CommunityTypeID == (int)CommunityTypes.Community || parentCommunity.CommunityTypeID == (int)CommunityTypes.Folder) &&
                            parentCommunity.IsDeleted == true)
                        {
                            // Get the "None" (User) community of the user who uploaded the Content.
                            var noneCommunity = _communityRepository.GetItem(
                                c => c.CreatedByID == content.CreatedByID && c.CommunityTypeID == (int)CommunityTypes.User);

                            this.CheckNotNull(() => new { noneCommunity });

                            // Remove the existing relation
                            content.CommunityContents.Remove(Enumerable.ElementAt(content.CommunityContents, 0));

                            // Set the None community as parent community.
                            var noneCommunityContent = new CommunityContents()
                            {
                                CommunityID = noneCommunity.CommunityID,
                                Content     = content
                            };

                            content.CommunityContents.Add(noneCommunityContent);
                        }

                        foreach (var contentRelation in content.ContentRelation)
                        {
                            contentRelation.Content1.IsDeleted = false;
                            contentRelation.Content1.User2     = null;
                        }

                        _contentRepository.Update(content);

                        // Save changes to the database
                        _contentRepository.SaveChanges();

                        status = OperationStatus.CreateSuccessStatus();
                    }
                    else
                    {
                        status = OperationStatus.CreateFailureStatus(string.Format(CultureInfo.CurrentCulture, "Content with ID '{0}' was not found", contentId));
                    }
                }
                else
                {
                    status = OperationStatus.CreateFailureStatus(Resources.UserNotSiteAdminError);
                }
            }
            catch (Exception exception)
            {
                status = OperationStatus.CreateFailureStatus(exception);
            }

            return(status);
        }