コード例 #1
0
 /// <summary>
 /// Creates any new elements that are required.
 /// </summary>
 /// <param name="masterPageZone">The updated master page zone.</param>
 /// <param name="unitOfWork">Unit of work.</param>
 private void CreateNewElements(MasterPageZone masterPageZone, IUnitOfWork unitOfWork)
 {
     foreach (MasterPageZoneElement masterPageZoneElement in masterPageZone.MasterPageZoneElements)
     {
         if (masterPageZoneElement.ElementId == 0)
         {
             IElementSettings element = _elementService.New(masterPageZone.TenantId, masterPageZoneElement.Element.ElementTypeId);
             element.Name = masterPageZoneElement.Element.Name.Trim();
             long elementId = _elementService.Create(element, unitOfWork);
             masterPageZoneElement.ElementId         = elementId;
             masterPageZoneElement.Element.ElementId = elementId;
         }
     }
 }
コード例 #2
0
        /// <summary>
        /// Updates a configurable page zone. Creates new elements and removes old elements. Re-orders existing elements.
        /// </summary>
        /// <param name="tenantId">Website that page belongs to.</param>
        /// <param name="pageId">Page identifier.</param>
        /// <param name="pageZoneId">Page zone identifier.</param>
        /// <param name="pageZoneElements">New page zone contents.</param>
        /// <param name="unitOfWork">Unit of work.</param>
        public void UpdateZone(long tenantId, long pageId, long pageZoneId, List <PageZoneElementInfo> pageZoneElements, IUnitOfWork unitOfWork = null)
        {
            // If we don't have a unit of work in place, create one now so that we can rollback all changes in case of failure
            IUnitOfWork localUnitOfWork = unitOfWork == null?_unitOfWorkFactory.CreateUnitOfWork() : null;

            // Do the page update
            try
            {
                // Get page, master page and page zone that we administering
                Page       page       = Read(tenantId, pageId, unitOfWork ?? localUnitOfWork);
                MasterPage masterPage = _masterPageService.Read(tenantId, page.MasterPageId, unitOfWork ?? localUnitOfWork);
                PageZone   pageZone   = page.PageZones.Where(z => z.PageZoneId == pageZoneId).FirstOrDefault();

                // Perform validation
                _pageValidator.ValidateUpdateZone(masterPage, page, pageZoneId, pageZoneElements);

                // Construct new list of page zone elements
                List <PageZoneElement> newPageZoneElements = new List <PageZoneElement>();
                foreach (PageZoneElementInfo info in pageZoneElements)
                {
                    // Create new page zone element?
                    if (info.PageZoneElementId == 0)
                    {
                        IElementSettings element = _elementService.New(tenantId, info.ElementTypeId);
                        element.Name = info.Name;
                        long elementId = _elementService.Create(element, unitOfWork ?? localUnitOfWork);
                        newPageZoneElements.Add(new PageZoneElement
                        {
                            TenantId          = tenantId,
                            PageId            = pageId,
                            PageZoneId        = pageZoneId,
                            PageZoneElementId = 0,
                            SortOrder         = info.SortOrder,
                            ElementTypeId     = info.ElementTypeId,
                            ElementId         = elementId,
                            Element           = element
                        });
                    }

                    // Update existing page zone element?
                    if (info.PageZoneElementId != 0)
                    {
                        IElementSettings element = _elementService.New(tenantId, info.ElementTypeId);
                        element.Name = info.Name;
                        PageZoneElement pageZoneElement = pageZone.PageZoneElements.Where(e => e.PageZoneElementId == info.PageZoneElementId).First();
                        pageZoneElement.SortOrder = info.SortOrder;
                        pageZoneElement.Element   = element;
                        newPageZoneElements.Add(pageZoneElement);
                    }
                }

                // Do the page zone element update
                _pageRepository.UpdatePageZoneElements(tenantId, pageId, pageZoneId, newPageZoneElements, unitOfWork ?? localUnitOfWork);

                // Get elements that might be removed
                List <ElementKeyValue> elementsToDelete = new List <ElementKeyValue>();
                foreach (PageZoneElement pageZoneElement in pageZone.PageZoneElements)
                {
                    PageZoneElementInfo pageZoneElementInfo = pageZoneElements.Where(e => e.PageZoneElementId == pageZoneElement.PageZoneElementId).FirstOrDefault();
                    if (pageZoneElementInfo == null)
                    {
                        elementsToDelete.Add(new ElementKeyValue {
                            ElementId = pageZoneElement.ElementId, ElementTypeId = pageZoneElement.ElementTypeId
                        });
                    }
                }

                // Delete elements if no longer in use
                foreach (ElementKeyValue elementKeyValue in elementsToDelete)
                {
                    if (!ElementInUse(tenantId, elementKeyValue.ElementId, unitOfWork ?? localUnitOfWork))
                    {
                        _elementService.Delete(tenantId, elementKeyValue.ElementTypeId, elementKeyValue.ElementId, unitOfWork ?? localUnitOfWork);
                    }
                }

                // Commit work if local unit of work in place
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Commit();
                }
            }
            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);
            }
        }