/// <summary> /// Updates existing master page zone. /// </summary> /// <param name="masterPageZone">Updated master page zone details.</param> /// <param name="unitOfWork">Unit of work.</param> public void UpdateZone(MasterPageZone masterPageZone, IUnitOfWork unitOfWork = null) { IUnitOfWork localUnitOfWork = unitOfWork == null?_unitOfWorkFactory.CreateUnitOfWork() : null; try { IDatabaseManager dbm = _databaseManagerFactory.GetDatabaseManager(unitOfWork ?? localUnitOfWork); dbm.SetSQL(_sqlManager.GetSql("Sql.UpdateMasterPageZone.sql")); dbm.AddParameter("@TenantId", FieldType.BigInt, masterPageZone.TenantId); dbm.AddParameter("@MasterPageId", FieldType.BigInt, masterPageZone.MasterPageId); dbm.AddParameter("@MasterPageZoneId", FieldType.BigInt, masterPageZone.MasterPageZoneId); AddMasterPageZoneParameters(masterPageZone, dbm); dbm.ExecuteNonQuery(); if (localUnitOfWork != null) { localUnitOfWork.Commit(); } } catch { if (localUnitOfWork != null) { localUnitOfWork.Rollback(); } throw; } finally { if (localUnitOfWork != null) { localUnitOfWork.Dispose(); } } }
/// <summary> /// Gets master page zone details. /// </summary> /// <param name="tenantId">Identifies website that master pages belong to.</param> /// <param name="masterPageId">Identifies the master page whose zone is returned.</param> /// <param name="masterPageZoneId">Identifies the master page zone to return.</param> /// <param name="unitOfWork">Unit of work.</param> /// <returns>Master page zone details (or null if master page zone not found).</returns> public MasterPageZone ReadZone(long tenantId, long masterPageId, long masterPageZoneId, IUnitOfWork unitOfWork = null) { IDatabaseManager dbm = _databaseManagerFactory.GetDatabaseManager(unitOfWork); try { dbm.SetSQL(_sqlManager.GetSql("Sql.ReadMasterPageZone.sql")); dbm.AddParameter("@TenantId", FieldType.BigInt, tenantId); dbm.AddParameter("@MasterPageId", FieldType.BigInt, masterPageId); dbm.AddParameter("@MasterPageZoneId", FieldType.BigInt, masterPageZoneId); dbm.ExecuteReader(); if (!dbm.Read()) { return(null); } MasterPageZone masterPageZone = GetMasterPageZoneFromDatabaseManager(dbm); dbm.Read(); while (dbm.Read()) { masterPageZone.MasterPageZoneElementTypes.Add(GetMasterPageZoneElementTypeFromDatabaseManager(dbm)); } while (dbm.Read()) { masterPageZone.MasterPageZoneElements.Add(GetMasterPageZoneElementFromDatabaseManager(dbm)); } return(masterPageZone); } finally { if (unitOfWork == null) { dbm.Dispose(); } } }
/// <summary> /// Gets identifiers of elements that are no longer required following update of editable master page zone. /// </summary> /// <param name="existingMasterPageZone">Existing master page zone details.</param> /// <param name="masterPageZone">New master page zone.</param> /// <param name="removedMasterPageZoneElementIds">Master page zone element identifiers that are no longer required following update of editable master page zone.</param> /// <param name="unitOfWork">Unit of work.</param> /// <returns>Elements that may be redundant following update of master page zone.</returns> private HashSet <long> GetRemovedElementsForEditableMasterPageZone(MasterPageZone existingMasterPageZone, MasterPageZone masterPageZone, HashSet <long> removedMasterPageZoneElementIds, IUnitOfWork unitOfWork) { // The potential list of elements to remove will include any elements referenced directly by master page zone element. // These elements can be retrieved by calling the equivalent method for static master page zones HashSet <long> removedElements = GetRemovedElementsForStaticOrConfigurableMasterPageZone(existingMasterPageZone, masterPageZone); // Finally, we need to get a list of the elements associated with the removed master page zone element identifiers List <long> elementIds = null; if (removedMasterPageZoneElementIds.Count == 0) { elementIds = new List <long>(); } else { elementIds = _masterPageRepository.ListPageElementsByMasterPageZoneElementIds(masterPageZone.TenantId, masterPageZone.MasterPageId, masterPageZone.MasterPageZoneId, removedMasterPageZoneElementIds.ToList(), unitOfWork); } // Add these to the list of potentially redundant elements and we are done foreach (long elementId in elementIds) { if (!removedElements.Contains(elementId)) { removedElements.Add(elementId); } } // Return result return(removedElements); }
/// <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); } }
/// <summary> /// Populates zone element and zone element type collections for table types. /// </summary> /// <param name="masterPageZone">Master page zone.</param> /// <param name="masterPageZoneElementTypeCollection">Master page zone element type collection.</param> /// <param name="masterPageZoneElementCollection">Master page zone element collection.</param> private void PopulateMasterPageZoneCollections(MasterPageZone masterPageZone, MasterPageZoneElementTypeCollection masterPageZoneElementTypeCollection, MasterPageZoneElementCollection masterPageZoneElementCollection) { foreach (MasterPageZoneElementType masterPageZoneElementType in masterPageZone.MasterPageZoneElementTypes) { masterPageZoneElementType.MasterPageZoneSortOrder = masterPageZone.SortOrder; masterPageZoneElementTypeCollection.Add(masterPageZoneElementType); } foreach (MasterPageZoneElement masterPageZoneElement in masterPageZone.MasterPageZoneElements) { masterPageZoneElement.MasterPageZoneSortOrder = masterPageZone.SortOrder; masterPageZoneElementCollection.Add(masterPageZoneElement); } }
/// <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; } } }
/// <summary> /// Adds master page zone SQL parameters. /// </summary> /// <param name="masterPageZone">Master page zone.</param> /// <param name="dbm">The database manager where parameters are added.</param> private void AddMasterPageZoneParameters(MasterPageZone masterPageZone, IDatabaseManager dbm) { MasterPageZoneElementTypeCollection masterPageZoneElementTypeCollection = new MasterPageZoneElementTypeCollection(); MasterPageZoneElementCollection masterPageZoneElementCollection = new MasterPageZoneElementCollection(); PopulateMasterPageZoneCollections(masterPageZone, masterPageZoneElementTypeCollection, masterPageZoneElementCollection); dbm.AddParameter("@Name", FieldType.NVarChar, MasterPageLengths.ZoneNameMaxLength, masterPageZone.Name); dbm.AddParameter("@SortOrder", FieldType.Int, masterPageZone.SortOrder); dbm.AddParameter("@AdminType", FieldType.Int, (int)masterPageZone.AdminType); dbm.AddParameter("@ContentType", FieldType.Int, (int)masterPageZone.ContentType); dbm.AddParameter("@BeginRender", FieldType.NVarChar, -1, masterPageZone.BeginRender ?? (object)DBNull.Value); dbm.AddParameter("@EndRender", FieldType.NVarChar, -1, masterPageZone.EndRender ?? (object)DBNull.Value); dbm.AddTypedParameter("@MasterPageZoneElementTypes", FieldType.Structured, masterPageZoneElementTypeCollection.Count == 0 ? null : masterPageZoneElementTypeCollection, "cms.MasterPageZoneElementTypeTableType"); dbm.AddTypedParameter("@MasterPageZoneElements", FieldType.Structured, masterPageZoneElementCollection.Count == 0 ? null : masterPageZoneElementCollection, "cms.MasterPageZoneElementTableType"); }
/// <summary> /// Gets master page zone from database manager data reader. /// </summary> /// <param name="dbm">Database manager.</param> /// <returns>A populated master page zone object.</returns> private MasterPageZone GetMasterPageZoneFromDatabaseManager(IDatabaseManager dbm) { MasterPageZone masterPageZone = new MasterPageZone { TenantId = (long)dbm.DataReaderValue("TenantId"), MasterPageId = (long)dbm.DataReaderValue("MasterPageId"), MasterPageZoneId = (long)dbm.DataReaderValue("MasterPageZoneId"), Name = (string)dbm.DataReaderValue("Name"), SortOrder = (int)dbm.DataReaderValue("SortOrder"), AdminType = (MasterPageZoneAdminType)(int)dbm.DataReaderValue("AdminType"), ContentType = (MasterPageZoneContentType)(int)dbm.DataReaderValue("ContentType"), BeginRender = dbm.DataReaderValue("BeginRender") == DBNull.Value ? null : (string)dbm.DataReaderValue("BeginRender"), EndRender = dbm.DataReaderValue("EndRender") == DBNull.Value ? null : (string)dbm.DataReaderValue("EndRender"), MasterPageZoneElementTypes = new List <MasterPageZoneElementType>(), MasterPageZoneElements = new List <MasterPageZoneElement>() }; return(masterPageZone); }
/// <summary> /// Performs update of master page zone given submitted form data. /// </summary> /// <param name="form">Form containing updated master page zone data.</param> /// <returns>Result of form post.</returns> private FormResult UpdateMasterPageZone(Form form) { // Get master page details string[] parts = form.Context.Split('|'); long tenantId = _authenticationService.TenantId; long masterPageId = Convert.ToInt64(parts[0]); long masterPageZoneId = Convert.ToInt64(parts[1]); MasterPageZone masterPageZone = JsonConvert.DeserializeObject<MasterPageZone>(form.Data); masterPageZone.TenantId = tenantId; masterPageZone.MasterPageId = masterPageId; masterPageZone.MasterPageZoneId = masterPageZoneId; // Do the update _masterPageService.UpdateZone(masterPageZone); // Return form result with no errors return _formHelperService.GetFormResult(); }
/// <summary> /// Process an admin type change. /// </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 ProcessAdminTypeChange(MasterPageZone existingMasterPageZone, MasterPageZone masterPageZone, IUnitOfWork unitOfWork) { if (existingMasterPageZone.AdminType == MasterPageZoneAdminType.Static) { ChangeAdminTypeFromStatic(existingMasterPageZone, masterPageZone, unitOfWork); } else if (masterPageZone.AdminType == MasterPageZoneAdminType.Static) { ChangeAdminTypeToStatic(existingMasterPageZone, masterPageZone, unitOfWork); } else if (existingMasterPageZone.AdminType == MasterPageZoneAdminType.Editable && masterPageZone.AdminType == MasterPageZoneAdminType.Configurable) { ChangeAdminTypeFromEditableToConfigurable(existingMasterPageZone, masterPageZone, unitOfWork); } else if (existingMasterPageZone.AdminType == MasterPageZoneAdminType.Configurable && masterPageZone.AdminType == MasterPageZoneAdminType.Editable) { ChangeAdminTypeFromConfigurableToEditable(existingMasterPageZone, masterPageZone, unitOfWork); } }
/// <summary> /// Gets master page from database manager result. /// </summary> /// <param name="dbm">Database manager.</param> /// <param name="loadZonesAndElements">Indicates depth of data loaded.</param> /// <returns>Master page (or null if master page not found).</returns> private MasterPage GetMasterPage(IDatabaseManager dbm, bool loadZonesAndElements) { // Get master page if (loadZonesAndElements && !dbm.Read()) { return(null); } MasterPage masterPage = GetMasterPageFromDatabaseManager(dbm); // If not read, we can return result now if (!loadZonesAndElements) { return(masterPage); } // Get master page zones dbm.Read(); Dictionary <long, MasterPageZone> masterPageZonesById = new Dictionary <long, MasterPageZone>(); while (dbm.Read()) { MasterPageZone masterPageZone = GetMasterPageZoneFromDatabaseManager(dbm); masterPage.MasterPageZones.Add(masterPageZone); masterPageZonesById.Add(masterPageZone.MasterPageZoneId, masterPageZone); } // Get the element types that can exist in a master page zone when admin type is configurable while (dbm.Read()) { MasterPageZoneElementType masterPageZoneElementType = GetMasterPageZoneElementTypeFromDatabaseManager(dbm); masterPageZonesById[masterPageZoneElementType.MasterPageZoneId].MasterPageZoneElementTypes.Add(masterPageZoneElementType); } // Get master page zone elements while (dbm.Read()) { MasterPageZoneElement masterPageZoneElement = GetMasterPageZoneElementFromDatabaseManager(dbm); masterPageZonesById[masterPageZoneElement.MasterPageZoneId].MasterPageZoneElements.Add(masterPageZoneElement); } // Return the result return(masterPage); }
/// <summary> /// Validates update master page zone model. Throws validation error exception if validation fails. /// </summary> /// <param name="masterPageZone">Updated master page zone details.</param> /// <param name="keyPrefix">Validation key prefix.</param> public void ValidateUpdateZone(MasterPageZone masterPageZone, string keyPrefix = null) { foreach (MasterPageZoneElement masterPageZoneElement in masterPageZone.MasterPageZoneElements) { if (masterPageZoneElement.ElementId == 0) { // New element details must be specified if element ID zero if (masterPageZoneElement.Element == null || string.IsNullOrWhiteSpace(masterPageZoneElement.Element.Name) || masterPageZoneElement.Element.ElementTypeId == new Guid()) { throw new ValidationErrorException(new ValidationError(null, MasterPageResource.ElementInvalidMessage, keyPrefix)); } // Element name must not contain forward slash if (masterPageZoneElement.Element.Name.Contains("/")) { throw new ValidationErrorException(new ValidationError(null, MasterPageResource.ElementNameInvalidMessage, keyPrefix)); } } } }
/// <summary> /// Deal with change of master page zone admin type from editable or configurable to static. /// </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 ChangeAdminTypeToStatic(MasterPageZone existingMasterPageZone, MasterPageZone masterPageZone, IUnitOfWork unitOfWork) { // Delete page zone elements that were required by editable or configurable master page zones, but no longer required for static master page zone List <long> elementIds = _masterPageRepository.ListPageElementsByMasterPageZone(existingMasterPageZone.TenantId, existingMasterPageZone.MasterPageId, existingMasterPageZone.MasterPageZoneId, unitOfWork); _pageRepository.DeletePageZoneElementsByMasterPageZone(existingMasterPageZone.TenantId, existingMasterPageZone.MasterPageId, existingMasterPageZone.MasterPageZoneId, unitOfWork); HashSet <long> removedElements = new HashSet <long>(); foreach (long elementId in elementIds) { removedElements.Add(elementId); } if (removedElements.Count > 0) { DeleteElementsThatAreNoLongerInUse(existingMasterPageZone.TenantId, removedElements, unitOfWork); } // Delete page zones that were required by editable or configurable master page zones, but no longer required for static master page zone _pageRepository.DeletePageZonesByMasterPageZone(existingMasterPageZone.TenantId, existingMasterPageZone.MasterPageId, existingMasterPageZone.MasterPageZoneId, unitOfWork); }
/// <summary> /// Gets identifiers of master page zone elements that are no longer required following update of editable master page zone. /// </summary> /// <param name="existingMasterPageZone">Existing master page zone details.</param> /// <param name="masterPageZone">New master page zone.</param> /// <returns>Master page zone elements that are redundant following update of master page zone.</returns> private HashSet <long> GetRemovedMasterPageZoneElementIds(MasterPageZone existingMasterPageZone, MasterPageZone masterPageZone) { // Get master page zone element identifiers in master page zone as it currently stands HashSet <long> existingMasterPageZoneElementIds = new HashSet <long>(); foreach (MasterPageZoneElement masterPageZoneElement in existingMasterPageZone.MasterPageZoneElements) { if (!existingMasterPageZoneElementIds.Contains(masterPageZoneElement.MasterPageZoneElementId)) { existingMasterPageZoneElementIds.Add(masterPageZoneElement.MasterPageZoneElementId); } } // Get master page zone element identifiers in updated master page zone HashSet <long> updatedMasterPageZoneElementIds = new HashSet <long>(); foreach (MasterPageZoneElement masterPageZoneElement in masterPageZone.MasterPageZoneElements) { if (!updatedMasterPageZoneElementIds.Contains(masterPageZoneElement.MasterPageZoneElementId)) { updatedMasterPageZoneElementIds.Add(masterPageZoneElement.MasterPageZoneElementId); } } // Get identifiers of master page zone elements that are no longer referenced by master page zone HashSet <long> removedMasterPageZoneElementIds = new HashSet <long>(); foreach (long masterPageZoneElementId in existingMasterPageZoneElementIds) { if (!updatedMasterPageZoneElementIds.Contains(masterPageZoneElementId)) { removedMasterPageZoneElementIds.Add(masterPageZoneElementId); } } // Return result return(removedMasterPageZoneElementIds); }
/// <summary> /// Ensure correct sort orders of zone elements. Ensure element types only specified for zones that are configurable. /// </summary> /// <param name="masterPageZone">Master page zone.</param> private void PrepareMasterPageZone(MasterPageZone masterPageZone) { // Only populate element types if master page zone admin type configurable if (masterPageZone.AdminType != MasterPageZoneAdminType.Configurable) { masterPageZone.MasterPageZoneElementTypes.Clear(); } // Ensure correct zone element sort orders for (int zoneElementIndex = 0; zoneElementIndex < masterPageZone.MasterPageZoneElements.Count; zoneElementIndex++) { MasterPageZoneElement zoneElement = masterPageZone.MasterPageZoneElements[zoneElementIndex]; zoneElement.SortOrder = zoneElementIndex; if (string.IsNullOrWhiteSpace(zoneElement.BeginRender)) { zoneElement.BeginRender = null; } if (string.IsNullOrWhiteSpace(zoneElement.EndRender)) { zoneElement.EndRender = null; } } }
/// <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); } }
/// <summary> /// Get elements for master pages belonging to the specified tenant. /// </summary> /// <param name="tenantId">Tenant identifier.</param> /// <param name="unitOfWork">Unit of work.</param> /// <returns>Collection of elements.</returns> public IEnumerable <MasterPage> ListElementsByMasterPage(long tenantId, IUnitOfWork unitOfWork = null) { IDatabaseManager dbm = _databaseManagerFactory.GetDatabaseManager(unitOfWork); try { List <MasterPage> masterPages = new List <MasterPage>(); dbm.SetSQL(_sqlManager.GetSql("Sql.ListMasterPageElements.sql")); dbm.AddParameter("@TenantId", FieldType.BigInt, tenantId); dbm.ExecuteReader(); MasterPage masterPage = null; MasterPageZone masterPageZone = null; MasterPageZoneElement masterPageZoneElement = null; while (dbm.Read()) { // Get master page details, create new master page each time master page identifier changes long masterPageId = (long)dbm.DataReaderValue("MasterPageId"); if (masterPage == null || masterPage.MasterPageId != masterPageId) { masterPage = new MasterPage { TenantId = tenantId, MasterPageId = masterPageId, Name = (string)dbm.DataReaderValue("MasterPageName"), MasterPageZones = new List <MasterPageZone>() }; masterPages.Add(masterPage); masterPageZone = null; masterPageZoneElement = null; } // Get master page zone details, create new master page zone each time master page zone identifier changes long masterPageZoneId = (long)dbm.DataReaderValue("MasterPageZoneId"); if (masterPageZone == null || masterPageZone.MasterPageZoneId != masterPageZoneId) { masterPageZone = new MasterPageZone { TenantId = tenantId, MasterPageId = masterPageId, MasterPageZoneId = masterPageZoneId, Name = (string)dbm.DataReaderValue("MasterPageZoneName"), MasterPageZoneElements = new List <MasterPageZoneElement>() }; masterPage.MasterPageZones.Add(masterPageZone); masterPageZoneElement = null; } // Get master page zone element details, create new master page zone element each time master page zone element identifier changes long masterPageZoneElementId = (long)dbm.DataReaderValue("MasterPageZoneElementId"); if (masterPageZoneElement == null || masterPageZoneElement.MasterPageZoneElementId != masterPageZoneElementId) { masterPageZoneElement = new MasterPageZoneElement { TenantId = tenantId, MasterPageId = masterPageId, MasterPageZoneId = masterPageZoneId, MasterPageZoneElementId = masterPageZoneElementId }; masterPageZone.MasterPageZoneElements.Add(masterPageZoneElement); } // Get element details masterPageZoneElement.Element = new ElementSettings { TenantId = tenantId, ElementId = (long)dbm.DataReaderValue("ElementId"), ElementTypeId = (Guid)dbm.DataReaderValue("ElementTypeId"), Name = (string)dbm.DataReaderValue("Name") }; masterPageZoneElement.ElementId = masterPageZoneElement.Element.ElementId; } return(masterPages); } finally { if (unitOfWork == null) { dbm.Dispose(); } } }
/// <summary> /// Retrieves form for master page zone update. /// </summary> /// <param name="context">Form context.</param> /// <returns>Form object.</returns> private Form GetMasterPageZoneForm(string context) { // Get tenant identifier long tenantId = _authenticationService.TenantId; // Get identifier of master page zone that is being updated string[] parts = context.Split('|'); long masterPageId = Convert.ToInt64(parts[0]); long masterPageZoneId = Convert.ToInt64(parts[1]); // Get existing master page details MasterPage masterPage = _masterPageService.Read(tenantId, masterPageId); MasterPageZone masterPageZone = masterPage.MasterPageZones.Where(mpz => mpz.MasterPageZoneId == masterPageZoneId).FirstOrDefault(); // Construct form Form form = new Form { Fields = new Dictionary<string, IFormField>(), Id = FormId.ToString(), Context = context, SubmitLabel = MasterPageResource.UpdateZoneButtonLabel }; form.Fields.Add("name", new TextField { Name = "zoneName", Label = MasterPageResource.ZoneNameLabel, Required = true, RequiredErrorMessage = MasterPageResource.ZoneNameRequiredMessage, MaxLength = MasterPageLengths.ZoneNameMaxLength, MaxLengthErrorMessage = string.Format(MasterPageResource.ZoneNameMaxLengthMessage, "name", MasterPageLengths.ZoneNameMaxLength) }); form.Fields.Add("adminType", new SelectListField<string> { Name = "adminType", Label = MasterPageResource.AdminTypeLabel, Items = new List<ListFieldItem<string>> { new ListFieldItem<string> { Name = _dataAnnotationsService.GetEnumDisplayName<MasterPageZoneAdminType>(MasterPageZoneAdminType.Static), Value = ((int)MasterPageZoneAdminType.Static).ToString() }, new ListFieldItem<string> { Name = _dataAnnotationsService.GetEnumDisplayName<MasterPageZoneAdminType>(MasterPageZoneAdminType.Editable), Value = ((int)MasterPageZoneAdminType.Editable).ToString() }, new ListFieldItem<string> { Name = _dataAnnotationsService.GetEnumDisplayName<MasterPageZoneAdminType>(MasterPageZoneAdminType.Configurable), Value = ((int)MasterPageZoneAdminType.Configurable).ToString() } }, Required = true, RequiredErrorMessage = MasterPageResource.AdminTypeRequiredMessage }); form.Fields.Add("contentType", new SelectListField<string> { Name = "contentType", Label = MasterPageResource.ContentTypeLabel, Items = new List<ListFieldItem<string>> { new ListFieldItem<string> { Name = _dataAnnotationsService.GetEnumDisplayName<MasterPageZoneContentType>(MasterPageZoneContentType.Standard), Value = ((int)MasterPageZoneContentType.Standard).ToString() }, new ListFieldItem<string> { Name = _dataAnnotationsService.GetEnumDisplayName<MasterPageZoneContentType>(MasterPageZoneContentType.Main), Value = ((int)MasterPageZoneContentType.Main).ToString() }, new ListFieldItem<string> { Name = _dataAnnotationsService.GetEnumDisplayName<MasterPageZoneContentType>(MasterPageZoneContentType.Comment), Value = ((int)MasterPageZoneContentType.Comment).ToString() } }, Required = true, RequiredErrorMessage = MasterPageResource.ContentTypeRequiredMessage }); form.Fields.Add("beginRender", new MultiLineTextField { Name = "beginRenderZone", Label = MasterPageResource.ZoneBeginRenderLabel, Rows = 4 }); form.Fields.Add("endRender", new MultiLineTextField { Name = "endRenderZone", Label = MasterPageResource.ZoneEndRenderLabel, Rows = 4, Value = masterPageZone.EndRender }); // Create sub forms form.SubForms = new Dictionary<string, Form>(); form.SubForms.Add("zoneElement", GetMasterPageZoneElementForm(context)); form.SubForms.Add("zoneElementNew", GetMasterPageZoneElementNewForm(context)); form.SubForms.Add("zoneElementType", GetMasterPageZoneElementTypeForm(context)); // Set master page zone as form data form.Data = JsonConvert.SerializeObject(masterPageZone); // Return form return form; }
/// <summary> /// Gets identifiers of master page zone elements that are new. /// </summary> /// <param name="existingMasterPageZone">Existing master page zone details.</param> /// <param name="masterPageZone">New master page zone.</param> /// <returns>Master page zone elements that exist in updated master page zone, but not in the old master page zone.</returns> private Dictionary <long, MasterPageZoneElement> GetNewMasterPageZoneElementIds(MasterPageZone existingMasterPageZone, MasterPageZone masterPageZone) { // Get master page zone element identifiers in master page zone as it was previously defined Dictionary <long, MasterPageZoneElement> existingMasterPageZoneElementIds = new Dictionary <long, MasterPageZoneElement>(); foreach (MasterPageZoneElement masterPageZoneElement in existingMasterPageZone.MasterPageZoneElements) { if (!existingMasterPageZoneElementIds.ContainsKey(masterPageZoneElement.MasterPageZoneElementId)) { existingMasterPageZoneElementIds.Add(masterPageZoneElement.MasterPageZoneElementId, masterPageZoneElement); } } // Get master page zone element identifiers in new master page zone Dictionary <long, MasterPageZoneElement> updatedMasterPageZoneElementIds = new Dictionary <long, MasterPageZoneElement>(); foreach (MasterPageZoneElement masterPageZoneElement in masterPageZone.MasterPageZoneElements) { if (!updatedMasterPageZoneElementIds.ContainsKey(masterPageZoneElement.MasterPageZoneElementId)) { updatedMasterPageZoneElementIds.Add(masterPageZoneElement.MasterPageZoneElementId, masterPageZoneElement); } } // Get identifiers of master page zone elements that are new Dictionary <long, MasterPageZoneElement> newMasterPageZoneElementIds = new Dictionary <long, MasterPageZoneElement>(); foreach (KeyValuePair <long, MasterPageZoneElement> kvp in updatedMasterPageZoneElementIds) { if (!existingMasterPageZoneElementIds.ContainsKey(kvp.Key)) { newMasterPageZoneElementIds.Add(kvp.Key, kvp.Value); } } // Return result return(newMasterPageZoneElementIds); }
/// <summary> /// Deal with change of master page zone admin type from editable to 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 ChangeAdminTypeFromEditableToConfigurable(MasterPageZone existingMasterPageZone, MasterPageZone masterPageZone, IUnitOfWork unitOfWork) { _pageRepository.UpdatePageZoneElementsForConfigurableAdminType(existingMasterPageZone.TenantId, existingMasterPageZone.MasterPageId, existingMasterPageZone.MasterPageZoneId, unitOfWork); }
/// <summary> /// Deal with change of master page zone admin type from configurable to editable. /// </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 ChangeAdminTypeFromConfigurableToEditable(MasterPageZone existingMasterPageZone, MasterPageZone masterPageZone, IUnitOfWork unitOfWork) { ChangeAdminTypeToStatic(existingMasterPageZone, masterPageZone, unitOfWork); ChangeAdminTypeFromStatic(existingMasterPageZone, masterPageZone, unitOfWork); }
/// <summary> /// Get elements that are found in the existing master page zone, but not in the updated master page zone. Following update of master page zone, /// these are the elements that may be deleted if they are no longer in-use. /// </summary> /// <param name="existingMasterPageZone">Existing master page zone.</param> /// <param name="masterPageZone">New master page zone.</param> /// <returns>Elements that may be redundant following update of master page zone.</returns> private HashSet <long> GetRemovedElementsForStaticOrConfigurableMasterPageZone(MasterPageZone existingMasterPageZone, MasterPageZone masterPageZone) { // Get identifiers of elements in master page zone as it currently stands HashSet <long> existingElements = new HashSet <long>(); foreach (MasterPageZoneElement masterPageZoneElement in existingMasterPageZone.MasterPageZoneElements) { if (!existingElements.Contains(masterPageZoneElement.ElementId)) { existingElements.Add(masterPageZoneElement.ElementId); } } // Get identifiers of elements in updated master page zone HashSet <long> updatedElements = new HashSet <long>(); foreach (MasterPageZoneElement masterPageZoneElement in masterPageZone.MasterPageZoneElements) { if (masterPageZoneElement.ElementId != 0 && !updatedElements.Contains(masterPageZoneElement.ElementId)) { updatedElements.Add(masterPageZoneElement.ElementId); } } // Get identifiers of elements that are no longer referenced by master page zone HashSet <long> removedElements = new HashSet <long>(); foreach (long elementId in existingElements) { if (!updatedElements.Contains(elementId)) { removedElements.Add(elementId); } } // Return result return(removedElements); }
/// <summary> /// Deletes page zone elements and page zones that are associated with specified master page zone. /// </summary> /// <param name="masterPageZone">Master page zone that is to be removed.</param> /// <param name="unitOfWork">Unit of work.</param> private void DeleteZone(MasterPageZone masterPageZone, IUnitOfWork unitOfWork) { _masterPageRepository.DeletePageZoneElementsByMasterPageZone(masterPageZone.TenantId, masterPageZone.MasterPageId, masterPageZone.MasterPageZoneId, unitOfWork); _masterPageRepository.DeletePageZonesByMasterPageZone(masterPageZone.TenantId, masterPageZone.MasterPageId, masterPageZone.MasterPageZoneId, unitOfWork); }