Ejemplo n.º 1
0
        /// <summary>
        /// Updates single zone within a master page.
        /// </summary>
        /// <param name="masterPageZone">The updated master page zone.</param>
        /// <param name="unitOfWork">Unit of work.</param>
        public void UpdateZone(MasterPageZone masterPageZone, 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
            {
                // Perform validation
                _masterPageValidator.ValidateUpdateZone(masterPageZone);

                // Prepare master page zone for update (e.g. set correct sort orders etc)
                PrepareMasterPageZone(masterPageZone);

                // Get zone as it currently stands
                MasterPageZone existingMasterPageZone = ReadZone(masterPageZone.TenantId, masterPageZone.MasterPageId, masterPageZone.MasterPageZoneId, unitOfWork ?? localUnitOfWork);

                // Check if master page zone admin type has changed?
                if (existingMasterPageZone.AdminType != masterPageZone.AdminType)
                {
                    ProcessAdminTypeChange(existingMasterPageZone, masterPageZone, unitOfWork ?? localUnitOfWork);
                }

                // Action performed depends on admin type of master zone
                HashSet <long> removedElements = null;
                if (masterPageZone.AdminType == MasterPageZoneAdminType.Static || masterPageZone.AdminType == MasterPageZoneAdminType.Configurable)
                {
                    // Get elements that may be removed
                    removedElements = GetRemovedElementsForStaticOrConfigurableMasterPageZone(existingMasterPageZone, masterPageZone);
                }
                else if (masterPageZone.AdminType == MasterPageZoneAdminType.Editable)
                {
                    // Get elements that may be removed and master page zone element identifiers that will be removed
                    HashSet <long> removedMasterPageZoneElementIds = GetRemovedMasterPageZoneElementIds(existingMasterPageZone, masterPageZone);
                    removedElements = GetRemovedElementsForEditableMasterPageZone(existingMasterPageZone, masterPageZone, removedMasterPageZoneElementIds, unitOfWork ?? localUnitOfWork);

                    // Finally, remove page zone elements that reference the master page zone element identifiers that will be removed
                    if (removedMasterPageZoneElementIds.Count > 0)
                    {
                        _masterPageRepository.DeletePageZoneElementsByMasterPageZoneElementIds(masterPageZone.TenantId, masterPageZone.MasterPageId, masterPageZone.MasterPageZoneId, removedMasterPageZoneElementIds.ToList(), unitOfWork ?? localUnitOfWork);
                    }
                }

                // Create any new elements
                CreateNewElements(masterPageZone, unitOfWork ?? localUnitOfWork);

                // Do the update
                _masterPageRepository.UpdateZone(masterPageZone, unitOfWork ?? localUnitOfWork);

                // Post update actions
                if (masterPageZone.AdminType == MasterPageZoneAdminType.Editable)
                {
                    // Get all of the page zones associated with the updated master page zone
                    List <PageZone> pageZones = _pageRepository.ListPageZonesByMasterPageZoneId(masterPageZone.TenantId, masterPageZone.MasterPageId, masterPageZone.MasterPageZoneId, unitOfWork ?? localUnitOfWork);

                    // Get identifiers of master page zone elements that are newly created
                    MasterPageZone newMasterPageZone = ReadZone(masterPageZone.TenantId, masterPageZone.MasterPageId, masterPageZone.MasterPageZoneId, unitOfWork ?? localUnitOfWork);
                    Dictionary <long, MasterPageZoneElement> newMasterPageZoneElements = GetNewMasterPageZoneElementIds(existingMasterPageZone, newMasterPageZone);

                    // Construct new page zone elements (including copies of elements specified in new master page zone elements)
                    List <PageZoneElement> pageZoneElements = new List <PageZoneElement>();
                    foreach (KeyValuePair <long, MasterPageZoneElement> kvp in newMasterPageZoneElements)
                    {
                        foreach (PageZone pageZone in pageZones)
                        {
                            long            elementId       = _elementService.Copy(kvp.Value.TenantId, kvp.Value.ElementId, kvp.Value.TenantId, kvp.Value.Element.ElementTypeId, unitOfWork ?? localUnitOfWork);
                            PageZoneElement pageZoneElement = new PageZoneElement
                            {
                                TenantId                = pageZone.TenantId,
                                PageId                  = pageZone.PageId,
                                PageZoneId              = pageZone.PageZoneId,
                                MasterPageId            = kvp.Value.MasterPageId,
                                MasterPageZoneId        = kvp.Value.MasterPageZoneId,
                                MasterPageZoneElementId = kvp.Value.MasterPageZoneElementId,
                                SortOrder               = null,
                                ElementId               = elementId
                            };
                            pageZoneElements.Add(pageZoneElement);
                        }
                    }

                    // Create page zone elements
                    if (pageZoneElements.Count > 0)
                    {
                        _pageRepository.CreatePageZoneElements(masterPageZone.TenantId, pageZoneElements, unitOfWork ?? localUnitOfWork);
                    }
                }

                // Remove elements if they are no longer in-use
                if (removedElements != null)
                {
                    DeleteElementsThatAreNoLongerInUse(masterPageZone.TenantId, removedElements, 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);
            }
        }