/// <summary>
        /// Puts the specified request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns><c>PutPageResponse</c> with created or updated item id.</returns>
        public PutPagePropertiesResponse Put(PutPagePropertiesRequest request)
        {
            if (request.Data.IsMasterPage)
            {
                accessControlService.DemandAccess(securityService.GetCurrentPrincipal(), RootModuleConstants.UserRoles.Administration);
            }
            else
            {
                accessControlService.DemandAccess(securityService.GetCurrentPrincipal(), RootModuleConstants.UserRoles.EditContent);
            }

            PageProperties pageProperties = null;

            var isNew = !request.Id.HasValue || request.Id.Value.HasDefaultValue();

            if (!isNew)
            {
                pageProperties =
                    repository.AsQueryable<PageProperties>(e => e.Id == request.Id.GetValueOrDefault())
                        .FetchMany(p => p.Options)
                        .Fetch(p => p.Layout)
                        .ThenFetchMany(l => l.LayoutOptions)
                        .FetchMany(p => p.MasterPages)
                        .FetchMany(f => f.AccessRules)
                        .ToList()
                        .FirstOrDefault();

                isNew = pageProperties == null;
            }
            UpdatingPagePropertiesModel beforeChange = null;
            if (isNew)
            {
                pageProperties = new PageProperties
                                     {
                                         Id = request.Id.GetValueOrDefault(),
                                         Status = PageStatus.Unpublished,
                                         AccessRules = new List<AccessRule>()
                                     };
            }
            else if (request.Data.Version > 0)
            {
                pageProperties.Version = request.Data.Version;
            }

            if (!isNew)
            {
                beforeChange = new UpdatingPagePropertiesModel(pageProperties);
            }

            if (!isNew && pageProperties.IsMasterPage != request.Data.IsMasterPage)
            {
                const string message = "IsMasterPage cannot be changed for updating page. It can be modified only when creating a page.";
                var logMessage = string.Format("{0} PageId: {1}", message, request.Id);
                throw new ValidationException(() => message, logMessage);
            }

            // Load master pages for updating page's master path and page's children master path
            IList<Guid> newMasterIds;
            IList<Guid> oldMasterIds;
            IList<Guid> childrenPageIds;
            IList<MasterPage> existingChildrenMasterPages;
            masterPageService.PrepareForUpdateChildrenMasterPages(pageProperties, request.Data.MasterPageId, out newMasterIds, out oldMasterIds, out childrenPageIds, out existingChildrenMasterPages);

            unitOfWork.BeginTransaction();

            if (!string.IsNullOrEmpty(request.Data.PageUrl) || string.IsNullOrEmpty(pageProperties.PageUrl))
            {
                var pageUrl = request.Data.PageUrl;
                if (string.IsNullOrEmpty(pageUrl) && !string.IsNullOrWhiteSpace(request.Data.Title))
                {
                    pageUrl = pageService.CreatePagePermalink(request.Data.Title, null, null, request.Data.LanguageId, request.Data.Categories);
                }
                else
                {
                    pageUrl = urlService.FixUrl(pageUrl);
                    pageService.ValidatePageUrl(pageUrl, request.Id);
                }

                pageProperties.PageUrl = pageUrl;
                pageProperties.PageUrlHash = pageUrl.UrlHash();
            }

            pageProperties.Title = request.Data.Title;
            pageProperties.Description = request.Data.Description;

            var newStatus = request.Data.IsMasterPage || request.Data.IsPublished ? PageStatus.Published : PageStatus.Unpublished;
            if (!request.Data.IsMasterPage && pageProperties.Status != newStatus)
            {
                accessControlService.DemandAccess(securityService.GetCurrentPrincipal(), RootModuleConstants.UserRoles.PublishContent);
            }

            pageProperties.Status = newStatus;
            pageProperties.PublishedOn = request.Data.IsPublished && !request.Data.PublishedOn.HasValue ? DateTime.Now : request.Data.PublishedOn;

            masterPageService.SetMasterOrLayout(pageProperties, request.Data.MasterPageId, request.Data.LayoutId);

            categoryService.CombineEntityCategories<PageProperties, PageCategory>(pageProperties, request.Data.Categories);

            pageProperties.IsArchived = request.Data.IsArchived;
            pageProperties.IsMasterPage = request.Data.IsMasterPage;
            pageProperties.LanguageGroupIdentifier = request.Data.LanguageGroupIdentifier;
            pageProperties.ForceAccessProtocol = (Core.DataContracts.Enums.ForceProtocolType)(int)request.Data.ForceAccessProtocol;
            pageProperties.Language = request.Data.LanguageId.HasValue && !request.Data.LanguageId.Value.HasDefaultValue()
                                    ? repository.AsProxy<Language>(request.Data.LanguageId.Value)
                                    : null;

            pageProperties.Image = request.Data.MainImageId.HasValue
                                    ? repository.AsProxy<MediaImage>(request.Data.MainImageId.Value)
                                    : null;
            pageProperties.FeaturedImage = request.Data.FeaturedImageId.HasValue
                                    ? repository.AsProxy<MediaImage>(request.Data.FeaturedImageId.Value)
                                    : null;
            pageProperties.SecondaryImage = request.Data.SecondaryImageId.HasValue
                                    ? repository.AsProxy<MediaImage>(request.Data.SecondaryImageId.Value)
                                    : null;

            pageProperties.CustomCss = request.Data.CustomCss;
            pageProperties.CustomJS = request.Data.CustomJavaScript;
            pageProperties.UseCanonicalUrl = request.Data.UseCanonicalUrl;
            pageProperties.UseNoFollow = request.Data.UseNoFollow;
            pageProperties.UseNoIndex = request.Data.UseNoIndex;

            if (request.Data.MetaData != null)
            {
                pageProperties.MetaTitle = request.Data.MetaData.MetaTitle;
                pageProperties.MetaDescription = request.Data.MetaData.MetaDescription;
                pageProperties.MetaKeywords = request.Data.MetaData.MetaKeywords;
            }

            IList<Tag> newTags = null;
            if (request.Data.Tags != null)
            {
                tagService.SavePageTags(pageProperties, request.Data.Tags, out newTags);
            }

            if (request.Data.AccessRules != null)
            {
                pageProperties.AccessRules.RemoveDuplicateEntities();
                var accessRules =
                    request.Data.AccessRules.Select(
                        r => (IAccessRule)new AccessRule { AccessLevel = (Core.Security.AccessLevel)(int)r.AccessLevel, Identity = r.Identity, IsForRole = r.IsForRole })
                        .ToList();
                accessControlService.UpdateAccessControl(pageProperties, accessRules);
            }

            if (request.Data.PageOptions != null)
            {
                var options = request.Data.PageOptions.ToServiceModel();

                var pageOptions = pageProperties.Options != null ? pageProperties.Options.Distinct() : null;
                pageProperties.Options = optionService.SaveOptionValues(options, pageOptions, () => new PageOption { Page = pageProperties });
            }

            if (!isNew)
            {
                // Notify about page properties changing.
                var cancelEventArgs = Events.PageEvents.Instance.OnPagePropertiesChanging(beforeChange, new UpdatingPagePropertiesModel(pageProperties));
                if (cancelEventArgs.Cancel)
                {
                    throw new CmsApiValidationException(
                        cancelEventArgs.CancellationErrorMessages != null && cancelEventArgs.CancellationErrorMessages.Count > 0
                            ? string.Join(",", cancelEventArgs.CancellationErrorMessages)
                            : "Page properties saving was canceled.");
                }
            }

            repository.Save(pageProperties);

            //
            // If creating new page, page id is unknown when children pages are loaded, so Guid may be empty
            // Updating id to saved page's Id manually
            //
            if (isNew && childrenPageIds != null && childrenPageIds.Count == 1 && childrenPageIds[0].HasDefaultValue())
            {
                childrenPageIds[0] = pageProperties.Id;
            }

            masterPageService.UpdateChildrenMasterPages(existingChildrenMasterPages, oldMasterIds, newMasterIds, childrenPageIds);

            unitOfWork.Commit();

            // Fire events.
            Events.RootEvents.Instance.OnTagCreated(newTags);
            if (isNew)
            {
                Events.PageEvents.Instance.OnPageCreated(pageProperties);
            }
            else
            {
                Events.PageEvents.Instance.OnPagePropertiesChanged(pageProperties);
            }

            return new PutPagePropertiesResponse { Data = pageProperties.Id };
        }
        /// <summary>
        /// Puts the specified request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns><c>PutPageResponse</c> with created or updated item id.</returns>
        public PutPagePropertiesResponse Put(PutPagePropertiesRequest request)
        {
            if (request.Data.IsMasterPage)
            {
                accessControlService.DemandAccess(securityService.GetCurrentPrincipal(), RootModuleConstants.UserRoles.Administration);
            }
            else
            {
                accessControlService.DemandAccess(securityService.GetCurrentPrincipal(), RootModuleConstants.UserRoles.EditContent);
            }

            PageProperties pageProperties = null;

            var isNew = !request.Id.HasValue || request.Id.Value.HasDefaultValue();

            if (!isNew)
            {
                pageProperties =
                    repository.AsQueryable <PageProperties>(e => e.Id == request.Id.GetValueOrDefault())
                    .FetchMany(p => p.Options)
                    .Fetch(p => p.Layout)
                    .ThenFetchMany(l => l.LayoutOptions)
                    .FetchMany(p => p.MasterPages)
                    .FetchMany(f => f.AccessRules)
                    .ToList()
                    .FirstOrDefault();

                isNew = pageProperties == null;
            }
            UpdatingPagePropertiesModel beforeChange = null;

            if (isNew)
            {
                pageProperties = new PageProperties
                {
                    Id          = request.Id.GetValueOrDefault(),
                    Status      = PageStatus.Unpublished,
                    AccessRules = new List <AccessRule>()
                };
            }
            else if (request.Data.Version > 0)
            {
                pageProperties.Version = request.Data.Version;
            }

            if (!isNew)
            {
                beforeChange = new UpdatingPagePropertiesModel(pageProperties);
            }

            if (!isNew && pageProperties.IsMasterPage != request.Data.IsMasterPage)
            {
                const string message    = "IsMasterPage cannot be changed for updating page. It can be modified only when creating a page.";
                var          logMessage = string.Format("{0} PageId: {1}", message, request.Id);
                throw new ValidationException(() => message, logMessage);
            }

            // Load master pages for updating page's master path and page's children master path
            IList <Guid>       newMasterIds;
            IList <Guid>       oldMasterIds;
            IList <Guid>       childrenPageIds;
            IList <MasterPage> existingChildrenMasterPages;

            masterPageService.PrepareForUpdateChildrenMasterPages(pageProperties, request.Data.MasterPageId, out newMasterIds, out oldMasterIds, out childrenPageIds, out existingChildrenMasterPages);

            unitOfWork.BeginTransaction();

            if (!string.IsNullOrEmpty(request.Data.PageUrl) || string.IsNullOrEmpty(pageProperties.PageUrl))
            {
                var pageUrl = request.Data.PageUrl;
                if (string.IsNullOrEmpty(pageUrl) && !string.IsNullOrWhiteSpace(request.Data.Title))
                {
                    pageUrl = pageService.CreatePagePermalink(request.Data.Title, null, null, request.Data.LanguageId, request.Data.CategoryId);
                }
                else
                {
                    pageUrl = urlService.FixUrl(pageUrl);
                    pageService.ValidatePageUrl(pageUrl, request.Id);
                }

                pageProperties.PageUrl     = pageUrl;
                pageProperties.PageUrlHash = pageUrl.UrlHash();
            }

            pageProperties.Title       = request.Data.Title;
            pageProperties.Description = request.Data.Description;

            var newStatus = request.Data.IsMasterPage || request.Data.IsPublished ? PageStatus.Published : PageStatus.Unpublished;

            if (!request.Data.IsMasterPage && pageProperties.Status != newStatus)
            {
                accessControlService.DemandAccess(securityService.GetCurrentPrincipal(), RootModuleConstants.UserRoles.PublishContent);
            }

            pageProperties.Status      = newStatus;
            pageProperties.PublishedOn = request.Data.IsPublished && !request.Data.PublishedOn.HasValue ? DateTime.Now : request.Data.PublishedOn;

            masterPageService.SetMasterOrLayout(pageProperties, request.Data.MasterPageId, request.Data.LayoutId);

            pageProperties.Category = request.Data.CategoryId.HasValue
                                    ? repository.AsProxy <Category>(request.Data.CategoryId.Value)
                                    : null;
            pageProperties.IsArchived              = request.Data.IsArchived;
            pageProperties.IsMasterPage            = request.Data.IsMasterPage;
            pageProperties.LanguageGroupIdentifier = request.Data.LanguageGroupIdentifier;
            pageProperties.Language = request.Data.LanguageId.HasValue && !request.Data.LanguageId.Value.HasDefaultValue()
                                    ? repository.AsProxy <Language>(request.Data.LanguageId.Value)
                                    : null;

            pageProperties.Image = request.Data.MainImageId.HasValue
                                    ? repository.AsProxy <MediaImage>(request.Data.MainImageId.Value)
                                    : null;
            pageProperties.FeaturedImage = request.Data.FeaturedImageId.HasValue
                                    ? repository.AsProxy <MediaImage>(request.Data.FeaturedImageId.Value)
                                    : null;
            pageProperties.SecondaryImage = request.Data.SecondaryImageId.HasValue
                                    ? repository.AsProxy <MediaImage>(request.Data.SecondaryImageId.Value)
                                    : null;

            pageProperties.CustomCss       = request.Data.CustomCss;
            pageProperties.CustomJS        = request.Data.CustomJavaScript;
            pageProperties.UseCanonicalUrl = request.Data.UseCanonicalUrl;
            pageProperties.UseNoFollow     = request.Data.UseNoFollow;
            pageProperties.UseNoIndex      = request.Data.UseNoIndex;

            if (request.Data.MetaData != null)
            {
                pageProperties.MetaTitle       = request.Data.MetaData.MetaTitle;
                pageProperties.MetaDescription = request.Data.MetaData.MetaDescription;
                pageProperties.MetaKeywords    = request.Data.MetaData.MetaKeywords;
            }

            IList <Tag> newTags = null;

            if (request.Data.Tags != null)
            {
                tagService.SavePageTags(pageProperties, request.Data.Tags, out newTags);
            }

            if (request.Data.AccessRules != null)
            {
                pageProperties.AccessRules.RemoveDuplicateEntities();
                var accessRules =
                    request.Data.AccessRules.Select(
                        r => (IAccessRule) new AccessRule {
                    AccessLevel = (Core.Security.AccessLevel)(int) r.AccessLevel, Identity = r.Identity, IsForRole = r.IsForRole
                })
                    .ToList();
                accessControlService.UpdateAccessControl(pageProperties, accessRules);
            }

            if (request.Data.PageOptions != null)
            {
                var options = request.Data.PageOptions.ToServiceModel();

                var pageOptions = pageProperties.Options != null?pageProperties.Options.Distinct() : null;

                pageProperties.Options = optionService.SaveOptionValues(options, pageOptions, () => new PageOption {
                    Page = pageProperties
                });
            }

            if (!isNew)
            {
                // Notify about page properties changing.
                var cancelEventArgs = Events.PageEvents.Instance.OnPagePropertiesChanging(beforeChange, new UpdatingPagePropertiesModel(pageProperties));
                if (cancelEventArgs.Cancel)
                {
                    throw new CmsApiValidationException(
                              cancelEventArgs.CancellationErrorMessages != null && cancelEventArgs.CancellationErrorMessages.Count > 0
                            ? string.Join(",", cancelEventArgs.CancellationErrorMessages)
                            : "Page properties saving was canceled.");
                }
            }

            repository.Save(pageProperties);

            //
            // If creating new page, page id is unknown when children pages are loaded, so Guid may be empty
            // Updating id to saved page's Id manually
            //
            if (isNew && childrenPageIds != null && childrenPageIds.Count == 1 && childrenPageIds[0].HasDefaultValue())
            {
                childrenPageIds[0] = pageProperties.Id;
            }

            masterPageService.UpdateChildrenMasterPages(existingChildrenMasterPages, oldMasterIds, newMasterIds, childrenPageIds);

            unitOfWork.Commit();

            // Fire events.
            Events.RootEvents.Instance.OnTagCreated(newTags);
            if (isNew)
            {
                Events.PageEvents.Instance.OnPageCreated(pageProperties);
            }
            else
            {
                Events.PageEvents.Instance.OnPagePropertiesChanged(pageProperties);
            }

            return(new PutPagePropertiesResponse {
                Data = pageProperties.Id
            });
        }