/// <summary>
        /// Updates the all the entries for the given Content with all the details.
        /// </summary>
        /// <param name="contentId">Content ID.</param>
        /// <param name="details">Details provided.</param>
        /// <returns>True if content was updated; otherwise false.</returns>
        private OperationStatus UpdateAllOffensiveContentEntry(long contentId, OffensiveEntry details)
        {
            OperationStatus status = null;

            try
            {
                var offensiveContents = _offensiveContentRepository.GetItems(oc => oc.ContentID == contentId && oc.OffensiveStatusID == (int)OffensiveStatusType.Flagged, null, false);
                if (offensiveContents != null && offensiveContents.Any())
                {
                    foreach (var item in offensiveContents)
                    {
                        item.OffensiveStatusID = (int)details.Status;
                        item.Justification     = details.Justification;

                        item.ReviewerID       = details.ReviewerID;
                        item.ReviewerDatetime = DateTime.UtcNow;

                        _offensiveContentRepository.Update(item);
                    }
                }
            }
            catch (Exception exception)
            {
                status = OperationStatus.CreateFailureStatus(exception);
            }

            // Status will be null if all sub communities and contents have been deleted.
            // If one them is not deleted then the status will have the exception details.
            status = status ?? OperationStatus.CreateSuccessStatus();

            return(status);
        }
        public bool DeleteOffensiveCommunityEntry(string id)
        {
            bool status = false;

            ProfileDetails profileDetails;

            if (ValidateAuthentication(true, out profileDetails))
            {
                IProfileService profileService = DependencyResolver.Current.GetService(typeof(IProfileService)) as IProfileService;
                profileDetails = profileService.GetProfile(profileDetails.PUID);
                if (profileDetails != null)
                {
                    IReportEntityService entityService = DependencyResolver.Current.GetService(typeof(IReportEntityService)) as IReportEntityService;
                    var details = new OffensiveEntry()
                    {
                        EntryID    = long.Parse(id, CultureInfo.InvariantCulture),
                        ReviewerID = profileDetails.ID,
                        Status     = OffensiveStatusType.Reviewed
                    };
                    status = entityService.UpdateOffensiveCommunityEntry(details).Succeeded;
                }
                else
                {
                    throw new WebFaultException <string>(Resources.UserNotRegisteredMessage, HttpStatusCode.Unauthorized);
                }
            }

            return(status);
        }
        /// <summary>
        /// Deletes the specified content from the Earth database.
        /// </summary>
        /// <param name="contentId">Content Id</param>
        /// <param name="profileId">User Identity</param>
        /// <returns>Status of the operation. Success, if succeeded. Failure message and exception details in case of exception.</returns>
        public OperationStatus DeleteContent(long contentId, long profileId)
        {
            var details = new OffensiveEntry()
            {
                EntityID      = contentId,
                ReviewerID    = profileId,
                Status        = OffensiveStatusType.Deleted,
                Justification = "Deleted while deleting the Content."
            };

            return(DeleteContentRecursively(contentId, profileId, false, details));
        }
        public Task <OperationStatus> UpdateAllOffensiveContentEntry(OffensiveEntry details)
        {
            this.CheckNotNull(() => new { details = details });

            OperationStatus status = null;

            try
            {
                if (_userRepository.IsSiteAdmin(details.ReviewerID))
                {
                    var offensiveContents = _offensiveContentRepository.GetItems(oc => oc.ContentID == details.EntityID && oc.OffensiveStatusID == (int)OffensiveStatusType.Flagged, null, false);
                    if (offensiveContents != null && offensiveContents.Any())
                    {
                        foreach (var item in offensiveContents)
                        {
                            item.OffensiveStatusID = (int)details.Status;
                            item.Justification     = details.Justification;

                            item.ReviewerID       = details.ReviewerID;
                            item.ReviewerDatetime = DateTime.UtcNow;

                            _offensiveContentRepository.Update(item);
                        }

                        _offensiveContentRepository.SaveChanges();
                    }
                }
                else
                {
                    status = OperationStatus.CreateFailureStatus(Resources.UserNotSiteAdminError);
                }
            }
            catch (Exception exception)
            {
                status = OperationStatus.CreateFailureStatus(exception);
            }

            // Status will be null if all sub communities and contents have been deleted.
            // If one them is not deleted then the status will have the exception details.
            status = status ?? OperationStatus.CreateSuccessStatus();

            return(Task.FromResult(status));
        }
        public bool DeleteOffensiveContent(string id)
        {
            bool status = false;

            ProfileDetails profileDetails;

            if (ValidateAuthentication(true, out profileDetails))
            {
                IProfileService profileService = DependencyResolver.Current.GetService(typeof(IProfileService)) as IProfileService;
                profileDetails = profileService.GetProfile(profileDetails.PUID);
                if (profileDetails != null)
                {
                    var details = new OffensiveEntry()
                    {
                        EntityID   = long.Parse(id, CultureInfo.InvariantCulture),
                        ReviewerID = profileDetails.ID,
                        Status     = OffensiveStatusType.Offensive
                    };

                    IContentService contentService = DependencyResolver.Current.GetService(typeof(IContentService)) as IContentService;
                    status = contentService.DeleteContent(long.Parse(id, CultureInfo.InvariantCulture), profileDetails.ID, true, details).Succeeded;

                    // Notify the Moderators and Owners about the Deleted Content.
                    INotificationService     notificationService = DependencyResolver.Current.GetService(typeof(INotificationService)) as INotificationService;
                    EntityAdminActionRequest notification        = new EntityAdminActionRequest()
                    {
                        AdminID    = profileDetails.ID,
                        EntityID   = long.Parse(id, CultureInfo.InvariantCulture),
                        EntityType = EntityType.Content,
                        EntityLink = string.Format(CultureInfo.InvariantCulture, "{0}/Content/Index/{1}", HttpContext.Current.Request.UrlReferrer.GetApplicationPath(), id),
                        Action     = AdminActions.Delete
                    };

                    notificationService.NotifyEntityDeleteRequest(notification);
                }
                else
                {
                    throw new WebFaultException <string>(Resources.UserNotRegisteredMessage, HttpStatusCode.Unauthorized);
                }
            }

            return(status);
        }
        /// <summary>
        /// Sets the given access type for the specified Content.
        /// </summary>
        /// <param name="contentId">Content Id</param>
        /// <param name="userId">User Identity</param>
        /// <param name="accessType">Access type of the Content.</param>
        /// <returns>Status of the operation. Success, if succeeded. Failure message and exception details in case of exception.</returns>
        public OperationStatus SetContentAccessType(long contentId, long userId, AccessType accessType)
        {
            OperationStatus status = null;

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

                    // Make sure content exists
                    this.CheckNotNull(() => new { content });

                    content.AccessTypeID = (int)accessType;

                    content.IsOffensive = (accessType == AccessType.Private);

                    var offensiveDetails = new OffensiveEntry()
                    {
                        EntityID   = contentId,
                        ReviewerID = userId,
                        Status     = OffensiveStatusType.Offensive
                    };

                    UpdateAllOffensiveContentEntry(contentId, offensiveDetails);

                    _contentRepository.Update(content);
                    _contentRepository.SaveChanges();

                    // Create Success message if set access type is successful.
                    status = OperationStatus.CreateSuccessStatus();
                }
            }
            catch (Exception exception)
            {
                status = OperationStatus.CreateFailureStatus(exception);
            }

            return(status);
        }
        public OperationStatus UpdateOffensiveContentEntry(OffensiveEntry details)
        {
            this.CheckNotNull(() => new { details = details });

            OperationStatus status = null;

            try
            {
                if (_userRepository.IsSiteAdmin(details.ReviewerID))
                {
                    var offensiveContents = _offensiveContentRepository.GetItem(oc => oc.OffensiveContentID == details.EntryID);
                    if (offensiveContents != null)
                    {
                        offensiveContents.OffensiveStatusID = (int)details.Status;
                        offensiveContents.Justification     = details.Justification;

                        offensiveContents.ReviewerID       = details.ReviewerID;
                        offensiveContents.ReviewerDatetime = DateTime.UtcNow;

                        _offensiveContentRepository.Update(offensiveContents);
                        _offensiveContentRepository.SaveChanges();
                    }
                }
                else
                {
                    status = OperationStatus.CreateFailureStatus(Resources.UserNotSiteAdminError);
                }
            }
            catch (Exception exception)
            {
                status = OperationStatus.CreateFailureStatus(exception);
            }

            // Status will be null if all sub communities and contents have been deleted.
            // If one them is not deleted then the status will have the exception details.
            status = status ?? OperationStatus.CreateSuccessStatus();

            return(status);
        }
        private OperationStatus DeleteContentRecursively(long contentId, long profileId, bool isOffensive, OffensiveEntry offensiveDetails)
        {
            OperationStatus status = null;

            try
            {
                // Get the current content from DB.
                var content = _contentRepository.GetItem((c) => c.ContentID == contentId && c.IsDeleted == false);

                if (content != null)
                {
                    var userRole = GetContentUserRole(content, profileId);

                    if (CanEditDeleteContent(content, profileId, userRole))
                    {
                        content.IsDeleted       = true;
                        content.IsOffensive     = isOffensive;
                        content.DeletedByID     = profileId;
                        content.DeletedDatetime = DateTime.UtcNow;

                        // Update all the offensive entity entries if the content is being deleted.
                        UpdateAllOffensiveContentEntry(content.ContentID, offensiveDetails);

                        foreach (var contentRelation in content.ContentRelation)
                        {
                            contentRelation.Content1.IsDeleted       = true;
                            contentRelation.Content1.IsOffensive     = isOffensive;
                            contentRelation.Content1.DeletedByID     = profileId;
                            contentRelation.Content1.DeletedDatetime = DateTime.UtcNow;
                        }

                        _contentRepository.Update(content);

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

                        status = OperationStatus.CreateSuccessStatus();
                    }
                    else
                    {
                        // In case if user is reader or visitor, he should not be allowed to delete the content.
                        status = OperationStatus.CreateFailureStatus("User does not have permission for deleting the content.");
                    }
                }
                else
                {
                    status = OperationStatus.CreateFailureStatus(string.Format(CultureInfo.CurrentCulture, "Content with ID '{0}' was not found", contentId));
                }
            }
            catch (Exception exception)
            {
                status = OperationStatus.CreateFailureStatus(exception);
            }

            return(status);
        }
 /// <summary>
 /// Deletes the specified content from the Earth database.
 /// </summary>
 /// <param name="contentId">Content Id</param>
 /// <param name="profileId">User Identity</param>
 /// <param name="isOffensive">Whether community is offensive or not.</param>
 /// <param name="offensiveDetails">Offensive Details.</param>
 /// <returns>Status of the operation. Success, if succeeded. Failure message and exception details in case of exception.</returns>
 public OperationStatus DeleteContent(long contentId, long profileId, bool isOffensive, OffensiveEntry offensiveDetails)
 {
     return(DeleteContentRecursively(contentId, profileId, isOffensive, offensiveDetails));
 }