Ejemplo n.º 1
0
        /// <summary>
        /// Deal with change of master page zone admin type from static to editable or configurable.
        /// </summary>
        /// <param name="existingMasterPageZone">Existing master page zone.</param>
        /// <param name="masterPageZone">Updated master page zone.</param>
        /// <param name="unitOfWork">Unit of work.</param>
        private void ChangeAdminTypeFromStatic(MasterPageZone existingMasterPageZone, MasterPageZone masterPageZone, IUnitOfWork unitOfWork)
        {
            // Create page zones required by editable master page zones, that would not have existed for a static master page zone
            List <PageZone> pageZones = new List <PageZone>();
            List <Page>     pages     = _pageRepository.ListPagesByMasterPage(existingMasterPageZone.TenantId, existingMasterPageZone.MasterPageId, unitOfWork);

            foreach (Page page in pages)
            {
                pageZones.Add(new PageZone
                {
                    TenantId         = existingMasterPageZone.TenantId,
                    PageId           = page.PageId,
                    MasterPageId     = existingMasterPageZone.MasterPageId,
                    MasterPageZoneId = existingMasterPageZone.MasterPageZoneId
                });
            }
            if (pageZones.Count > 0)
            {
                // Create page zones
                _pageRepository.CreatePageZones(existingMasterPageZone.TenantId, pageZones, unitOfWork);

                // Now get back created page zones, so that newly allocated page zone identifiers are populated
                pageZones = _pageRepository.ListPageZonesByMasterPageZoneId(existingMasterPageZone.TenantId, existingMasterPageZone.MasterPageId, existingMasterPageZone.MasterPageZoneId, unitOfWork);
            }

            // Create page zone elements required by editable master page zones, that would not have existed for a static master page zone
            List <PageZoneElement> pageZoneElements = new List <PageZoneElement>();

            foreach (PageZone pageZone in pageZones)
            {
                foreach (MasterPageZoneElement masterPageZoneElement in existingMasterPageZone.MasterPageZoneElements)
                {
                    long            elementId       = _elementService.Copy(existingMasterPageZone.TenantId, masterPageZoneElement.Element.ElementId, existingMasterPageZone.TenantId, masterPageZoneElement.Element.ElementTypeId, unitOfWork);
                    PageZoneElement pageZoneElement = new PageZoneElement
                    {
                        TenantId   = existingMasterPageZone.TenantId,
                        PageId     = pageZone.PageId,
                        PageZoneId = pageZone.PageZoneId,
                        ElementId  = elementId
                    };
                    if (masterPageZone.AdminType == MasterPageZoneAdminType.Editable)
                    {
                        pageZoneElement.MasterPageId            = masterPageZoneElement.MasterPageId;
                        pageZoneElement.MasterPageZoneId        = masterPageZoneElement.MasterPageZoneId;
                        pageZoneElement.MasterPageZoneElementId = masterPageZoneElement.MasterPageZoneElementId;
                    }
                    if (masterPageZone.AdminType == MasterPageZoneAdminType.Configurable)
                    {
                        pageZoneElement.SortOrder = masterPageZoneElement.SortOrder;
                    }
                    pageZoneElements.Add(pageZoneElement);
                }
            }
            if (pageZoneElements.Count > 0)
            {
                _pageRepository.CreatePageZoneElements(existingMasterPageZone.TenantId, pageZoneElements, unitOfWork);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates a new page.
        /// </summary>
        /// <param name="tenantId">The website that created page will belong to.</param>
        /// <param name="parentPageId">Parent page (can be null if page has no parent - i.e. is home page, or if can be determined).</param>
        /// <param name="masterpageId">The master page that newly created page is based on.</param>
        /// <param name="pageInfo">If specified, used to override master page settings.</param>
        /// <param name="unitOfWork">Unit of work.</param>
        /// <returns>Newly allocated page identifier.</returns>
        public long Create(long tenantId, long?parentPageId, long masterPageId, PageInfo pageInfo, IUnitOfWork unitOfWork = null)
        {
            // Multiple actions are performed during page creation, so we need a unit of work to perform rollback if any failures occur
            IUnitOfWork localUnitOfWork = unitOfWork == null?_unitOfWorkFactory.CreateUnitOfWork() : null;

            try
            {
                // Get master page that will determine how new page is created
                MasterPage masterPage = _masterPageRepository.Read(tenantId, masterPageId, unitOfWork ?? localUnitOfWork);

                // Get valid parent pages
                List <Page> parentPages = null;
                if (masterPage.AncestorPageId.HasValue && masterPage.AncestorPageLevel.HasValue)
                {
                    parentPages = ListMasterPageParentPages(masterPage, unitOfWork ?? localUnitOfWork);
                }

                // Validate the page create request (including checking that all or none of the image upload properties are specified)
                _pageValidator.ValidateCreate(tenantId, parentPageId, pageInfo, parentPages);

                // Set parent page identifier?
                if (parentPageId == null && masterPage.AncestorPageId.HasValue && masterPage.AncestorPageLevel.HasValue)
                {
                    parentPageId = parentPages[0].PageId;
                }

                // Construct new page based on master page definition
                DateTime now  = DateTime.UtcNow;
                Page     page = new Page
                {
                    Created                = now,
                    Description            = pageInfo == null ? masterPage.PageDescription : pageInfo.Description,
                    MasterPageId           = masterPageId,
                    Name                   = pageInfo == null ? masterPage.PageName : pageInfo.Name,
                    Occurred               = masterPage.HasOccurred ? DateTime.UtcNow.Date : (DateTime?)null,
                    ParentPageId           = parentPageId,
                    Updated                = now,
                    TenantId               = tenantId,
                    PageZones              = new List <PageZone>(),
                    Tags                   = pageInfo == null ? new List <Tag>() : pageInfo.Tags,
                    ImageTenantId          = pageInfo == null ? null : pageInfo.ImageTenantId,
                    ThumbnailImageUploadId = pageInfo == null ? null : pageInfo.ThumbnailImageUploadId,
                    PreviewImageUploadId   = pageInfo == null ? null : pageInfo.PreviewImageUploadId,
                    ImageUploadId          = pageInfo == null ? null : pageInfo.ImageUploadId
                };

                // Construct page zones
                foreach (MasterPageZone masterPageZone in masterPage.MasterPageZones)
                {
                    if (masterPageZone.AdminType != MasterPageZoneAdminType.Static)
                    {
                        PageZone pageZone = new PageZone
                        {
                            TenantId         = tenantId,
                            MasterPageId     = masterPageId,
                            MasterPageZoneId = masterPageZone.MasterPageZoneId,
                            PageZoneElements = new List <PageZoneElement>()
                        };
                        foreach (MasterPageZoneElement masterPageZoneElement in masterPageZone.MasterPageZoneElements)
                        {
                            long            elementId       = _elementService.Copy(tenantId, masterPageZoneElement.ElementId, tenantId, masterPageZoneElement.Element.ElementTypeId, unitOfWork ?? localUnitOfWork);
                            PageZoneElement pageZoneElement = new PageZoneElement
                            {
                                TenantId      = tenantId,
                                ElementTypeId = masterPageZoneElement.Element.ElementTypeId,
                                ElementId     = elementId,
                                Parent        = pageZone
                            };
                            if (masterPageZone.AdminType == MasterPageZoneAdminType.Configurable)
                            {
                                pageZoneElement.SortOrder = masterPageZoneElement.SortOrder;
                            }
                            if (masterPageZone.AdminType == MasterPageZoneAdminType.Editable)
                            {
                                pageZoneElement.MasterPageId            = masterPageId;
                                pageZoneElement.MasterPageZoneId        = masterPageZone.MasterPageZoneId;
                                pageZoneElement.MasterPageZoneElementId = masterPageZoneElement.MasterPageZoneElementId;
                            }
                            pageZone.PageZoneElements.Add(pageZoneElement);
                        }
                        page.PageZones.Add(pageZone);
                    }
                }

                // Commit page images?
                if (page.ImageUploadId.HasValue)
                {
                    _uploadService.Commit(page.ImageTenantId.Value, page.ThumbnailImageUploadId.Value, GetPageImageStorageHierarchy(), unitOfWork ?? localUnitOfWork);
                    _uploadService.Commit(page.ImageTenantId.Value, page.PreviewImageUploadId.Value, GetPageImageStorageHierarchy(), unitOfWork ?? localUnitOfWork);
                    _uploadService.Commit(page.ImageTenantId.Value, page.ImageUploadId.Value, GetPageImageStorageHierarchy(), unitOfWork ?? localUnitOfWork);
                }

                // Create page and return newly allocated page identifier
                page.PageId = _pageRepository.Create(page, unitOfWork ?? localUnitOfWork);

                // Update page tags?
                if (masterPage.Taggable)
                {
                    _pageRepository.UpdateTags(page, unitOfWork ?? localUnitOfWork);
                }

                // Commit work if local unit of work in place, then return newly allocated page identifier
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Commit();
                }
                return(page.PageId);
            }
            catch (ValidationErrorException)
            {
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Rollback();
                }
                throw;
            }
            catch (Exception ex)
            {
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Rollback();
                }
                throw new ValidationErrorException(new ValidationError(null, ApplicationResource.UnexpectedErrorMessage), ex);
            }
            finally
            {
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Dispose();
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Gets master page from a template page.
        /// </summary>
        /// <param name="tenantId">Website identifier.</param>
        /// <param name="page">The page that is about to be created. Contains hierarchy of pages created so far.</param>
        /// <param name="templatePage">Template page.</param>
        /// <param name="templateElements">Maintains dictionary of template elements and their corresponding master element copies.</param>
        /// <param name="unitOfWork">Unit of work.</param>
        private MasterPage GetMasterPageFromTemplatePage(long tenantId, Page page, TemplatePage templatePage, Dictionary <ElementKeyValue, ElementKeyValue> templateElements, IUnitOfWork unitOfWork)
        {
            // Get ancestor page ID?
            long?ancestorPageId = null;

            if (templatePage.AncestorPageLevel != null)
            {
                PageLevel ancestorPageLevel = templatePage.AncestorPageLevel.Value;
                int       numericPageLevel  = (int)ancestorPageLevel;
                while (numericPageLevel > 0)
                {
                    ancestorPageId = page.ParentPageId;
                    page           = page.ParentPage;
                    numericPageLevel--;
                }
            }

            // Create master page from template page
            MasterPage masterPage = new MasterPage
            {
                Administration           = templatePage.Administration,
                AncestorPageId           = ancestorPageId,
                AncestorPageLevel        = templatePage.AncestorPageLevel,
                Creatable                = templatePage.Creatable,
                Deletable                = templatePage.Deletable,
                HasOccurred              = templatePage.HasOccurred,
                HasImage                 = templatePage.HasImage,
                ThumbnailImageWidth      = templatePage.ThumbnailImageWidth,
                ThumbnailImageHeight     = templatePage.ThumbnailImageHeight,
                ThumbnailImageResizeMode = templatePage.ThumbnailImageResizeMode,
                PreviewImageWidth        = templatePage.PreviewImageWidth,
                PreviewImageHeight       = templatePage.PreviewImageHeight,
                PreviewImageResizeMode   = templatePage.PreviewImageResizeMode,
                ImageMinWidth            = templatePage.ImageMinWidth,
                ImageMinHeight           = templatePage.ImageMinHeight,
                Name            = templatePage.Name,
                PageName        = templatePage.PageName,
                PageDescription = templatePage.PageDescription,
                PageType        = templatePage.PageType,
                Taggable        = templatePage.Taggable,
                TenantId        = tenantId,
                BeginRender     = templatePage.BeginRender,
                EndRender       = templatePage.EndRender,
                MasterPageZones = new List <MasterPageZone>()
            };

            // Create master page zones from template page zones
            foreach (TemplatePageZone templatePageZone in templatePage.TemplatePageZones)
            {
                // Create master page zone
                MasterPageZone masterPageZone = new MasterPageZone
                {
                    Name        = templatePageZone.Name,
                    AdminType   = templatePageZone.AdminType,
                    ContentType = templatePageZone.ContentType,
                    TenantId    = tenantId,
                    BeginRender = templatePageZone.BeginRender,
                    EndRender   = templatePageZone.EndRender,
                    SortOrder   = templatePageZone.SortOrder,
                    MasterPageZoneElementTypes = new List <MasterPageZoneElementType>(),
                    MasterPageZoneElements     = new List <MasterPageZoneElement>()
                };
                masterPage.MasterPageZones.Add(masterPageZone);

                // Create master page zone element types from template page zone element types
                for (int index = 0; index < templatePageZone.TemplatePageZoneElementTypes.Count; index++)
                {
                    TemplatePageZoneElementType templatePageZoneElementType = templatePageZone.TemplatePageZoneElementTypes[index];
                    masterPageZone.MasterPageZoneElementTypes.Add(new MasterPageZoneElementType
                    {
                        TenantId      = tenantId,
                        ElementTypeId = templatePageZoneElementType.ElementTypeId
                    });
                }

                // Create master page zone elements from template page zone elements
                for (int index = 0; index < templatePageZone.TemplatePageZoneElements.Count; index++)
                {
                    // Template elements are copied to create master elements
                    TemplatePageZoneElement templatePageZoneElement = templatePageZone.TemplatePageZoneElements[index];
                    ElementKeyValue         templateElementKeyValue = new ElementKeyValue
                    {
                        ElementTypeId = templatePageZoneElement.ElementTypeId,
                        ElementId     = templatePageZoneElement.ElementId
                    };

                    // Where template elements are re-used, so master element copies should be re-used
                    if (!templateElements.ContainsKey(templateElementKeyValue))
                    {
                        long masterElementId = _elementService.Copy(templatePage.TenantId, templateElementKeyValue.ElementId, tenantId, templateElementKeyValue.ElementTypeId, unitOfWork);
                        templateElements.Add(templateElementKeyValue, new ElementKeyValue {
                            ElementTypeId = templateElementKeyValue.ElementTypeId, ElementId = masterElementId
                        });
                    }

                    // Get master element key value
                    ElementKeyValue masterElementKeyValue = templateElements[templateElementKeyValue];
                    masterPageZone.MasterPageZoneElements.Add(new MasterPageZoneElement
                    {
                        ElementId = masterElementKeyValue.ElementId,
                        Element   = new ElementSettings {
                            TenantId = tenantId, ElementId = masterElementKeyValue.ElementId, ElementTypeId = masterElementKeyValue.ElementTypeId
                        },
                        SortOrder   = index,
                        TenantId    = tenantId,
                        Parent      = masterPageZone,
                        BeginRender = templatePageZoneElement.BeginRender,
                        EndRender   = templatePageZoneElement.EndRender
                    });
                }
            }

            // Return the result
            return(masterPage);
        }