/// <summary> /// Verify there are gallery settings for the current gallery that match every template gallery setting, creating any /// if necessary. /// </summary> /// <returns><c>true</c> if data was changed that necessitates reloading data from the data store, <c>false</c> otherwise.</returns> private bool ConfigureGallerySettingsTable() { var foundTmplGallerySettings = false; var needToClearCache = false; using (var repo = new GallerySettingRepository()) { //repo.Load(); var gallerySettingNamesInGallery = repo.Where(gs => gs.FKGalleryId == GalleryId).Select(gs => gs.SettingName).ToList(); // Loop through each template gallery setting. foreach (var gsTmpl in repo.Where(g => g.Gallery.IsTemplate)) { foundTmplGallerySettings = true; //if (!repo.Local.Any(gs => gs.SettingName == gsTmpl.SettingName && gs.FKGalleryId == GalleryId)) //if (!repo.Where(gs => gs.SettingName == gsTmpl.SettingName && gs.FKGalleryId == GalleryId).Any()) if (!gallerySettingNamesInGallery.Contains(gsTmpl.SettingName)) { // This gallery is missing an entry for a gallery setting. Create one by copying it from the template gallery. repo.Add(new GallerySettingDto() { FKGalleryId = GalleryId, SettingName = gsTmpl.SettingName, SettingValue = gsTmpl.SettingValue }); needToClearCache = true; } } repo.Save(); } if (!foundTmplGallerySettings) { // If there weren't *any* template gallery settings, insert the seed data. Generally this won't be necessary, but it // can help recover from certain conditions, such as when a SQL Server connection is accidentally specified without // the MultipleActiveResultSets keyword (or it was false). In this situation the galleries are inserted but an error // prevents the remaining data from being inserted. Once the user corrects this and tries again, this code can run to // finish inserting the seed data. using (var ctx = new GalleryDb()) { SeedController.InsertSeedData(ctx); } Factory.ValidateGalleries(); } return(needToClearCache); }
/// <summary> /// Retrieves the gallery settings from the data store for all galleries. /// </summary> /// <returns>Returns an <see cref="IGallerySettingsCollection" /> containing the settings for all galleries.</returns> internal static IGallerySettingsCollection RetrieveGallerySettingsFromDataStore() { IGallerySettingsCollection gallerySettings = new GallerySettingsCollection(); IGallerySettings gs = null; int? prevGalleryId = null; Type gsType = typeof(GallerySettings); string boolType = typeof(bool).ToString(); string intType = typeof(int).ToString(); string stringType = typeof(string).ToString(); string stringArrayType = typeof(string[]).ToString(); string floatType = typeof(float).ToString(); string dateTimeType = typeof(DateTime).ToString(); // Loop through each gallery setting and assign to the relevant property. When we encounter a record with a new gallery ID, // automatically create a new GallerySetting instance and start populating that one. When we are done with the loop we will // have created one GallerySetting instance for each gallery and fully populated each one. // SQL: //SELECT // GallerySettingId, FKGalleryId, IsTemplate, SettingName, SettingValue //FROM [gs_GallerySetting] //WHERE FKGalleryId = @GalleryId //ORDER BY FKGalleryId; //foreach (GallerySettingDto gallerySettingDto in Factory.GetDataProvider().GallerySetting_GetGallerySettings()) using (var repo = new GallerySettingRepository()) { foreach (GallerySettingDto gallerySettingDto in repo.All.OrderBy(s => s.FKGalleryId)) { #region Check for new gallery if (!prevGalleryId.HasValue || (gallerySettingDto.FKGalleryId != prevGalleryId)) { // We have encountered settings for a new gallery. Initialize the previous one, then create a new object and add it to our collection. if ((gs != null) && (!gs.IsInitialized)) { gs.Initialize(); } GallerySettingDto dto = gallerySettingDto; gs = new GallerySettings(gallerySettingDto.FKGalleryId, new GalleryRepository().Where(g => g.GalleryId == dto.FKGalleryId).Select(g => g.IsTemplate).First()); gallerySettings.Add(gs); prevGalleryId = gallerySettingDto.FKGalleryId; } #endregion #region Assign property // For each setting in the data store, find the matching property and assign the value to it. string settingName = gallerySettingDto.SettingName.Trim(); PropertyInfo prop = gsType.GetProperty(settingName); if (prop == null) { throw new MissingMemberException(String.Format(CultureInfo.CurrentCulture, "Invalid gallery setting. A gallery setting named '{0}' was found in the data store, but no property by that name exists in the class '{1}'. Check the gallery settings in the data store to ensure they are correct.", settingName, gsType)); } if ((prop.PropertyType.FullName == null)) { continue; } if (prop.PropertyType.FullName.Equals(boolType)) { prop.SetValue(gs, Convert.ToBoolean(gallerySettingDto.SettingValue.Trim(), CultureInfo.InvariantCulture), null); } else if (prop.PropertyType.FullName.Equals(stringType)) { prop.SetValue(gs, Convert.ToString(gallerySettingDto.SettingValue.Trim(), CultureInfo.InvariantCulture), null); } else if (prop.PropertyType.FullName.Equals(intType)) { prop.SetValue(gs, Convert.ToInt32(gallerySettingDto.SettingValue.Trim(), CultureInfo.InvariantCulture), null); } else if (prop.PropertyType.FullName.Equals(floatType)) { prop.SetValue(gs, Convert.ToSingle(gallerySettingDto.SettingValue.Trim(), CultureInfo.InvariantCulture), null); } else if (prop.PropertyType.FullName.Equals(dateTimeType)) { prop.SetValue(gs, HelperFunctions.ToDateTime(gallerySettingDto.SettingValue.Trim(), "O", CultureInfo.InvariantCulture), null); } else if (prop.PropertyType.FullName.Equals(stringArrayType)) { // Parse comma-delimited string to array string[] strings = gallerySettingDto.SettingValue.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); List<String> stringList = new List<string>(strings.Length); // Trim any leading and trailing spaces for (int i = 0; i < strings.Length; i++) { string stringValue = strings[i].Trim(); if (!String.IsNullOrEmpty(stringValue)) { stringList.Add(stringValue); } } prop.SetValue(gs, stringList.ToArray(), null); } else if (prop.PropertyType == typeof(MetadataItemName)) { AssignMetadataItemNameProperty(gs, prop, gallerySettingDto.SettingValue.Trim()); } else if (prop.PropertyType == typeof(MediaObjectTransitionType)) { AssignMediaObjectTransitionTypeProperty(gs, prop, gallerySettingDto.SettingValue.Trim()); } else if (prop.PropertyType == typeof(SlideShowType)) { AssignSlideShowTypeProperty(gs, prop, gallerySettingDto.SettingValue.Trim()); } else if (prop.PropertyType == typeof(ContentAlignment)) { AssignContentAlignmentProperty(gs, prop, gallerySettingDto.SettingValue.Trim()); } else if (prop.PropertyType == typeof(PagerPosition)) { AssignPagerPositionProperty(gs, prop, gallerySettingDto.SettingValue.Trim()); } else if (prop.PropertyType == typeof(IUserAccountCollection)) { AssignUserAccountsProperty(gs, prop, gallerySettingDto.SettingValue.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)); } else if (prop.PropertyType == typeof(IMetadataDefinitionCollection)) { AssignMetadataDisplaySettingsProperty(gs, prop, gallerySettingDto.SettingValue.Trim()); } else if (prop.PropertyType == typeof(IMediaEncoderSettingsCollection)) { AssignMediaEncoderSettingsProperty(gs, prop, gallerySettingDto.SettingValue.Split(new[] { "~~" }, StringSplitOptions.None)); } else { throw new ArgumentOutOfRangeException(String.Format(CultureInfo.CurrentCulture, "GallerySettings.RetrieveGallerySettingsFromDataStore is not designed to process a property of type {0} (encountered in GallerySettings.{1})", prop.PropertyType, prop.Name)); } #endregion } } // The last gallery setting will not be initialized by the previous loop, so when we finish processing the records and // get to this point, do one more initialization. It is expected that gs will never be null or initialized, but we // check anyway just to be safe. if ((gs != null) && (!gs.IsInitialized)) { gs.Initialize(); } return gallerySettings; }
/// <summary> /// Verify there are gallery settings for the current gallery that match every template gallery setting, creating any /// if necessary. /// </summary> private void ConfigureGallerySettingsTable() { var foundTmplGallerySettings = false; using (var repo = new GallerySettingRepository()) { repo.Load(); // Loop through each template gallery setting. foreach (var gsTmpl in repo.Where(g => g.Gallery.IsTemplate)) { foundTmplGallerySettings = true; if (!repo.Local.Any(gs => gs.SettingName == gsTmpl.SettingName && gs.FKGalleryId == GalleryId)) { // This gallery is missing an entry for a gallery setting. Create one by copying it from the template gallery. repo.Add(new GallerySettingDto() { FKGalleryId = GalleryId, SettingName = gsTmpl.SettingName, SettingValue = gsTmpl.SettingValue }); } } repo.Save(); } if (!foundTmplGallerySettings) { // If there weren't *any* template gallery settings, insert the seed data. Generally this won't be necessary, but it // can help recover from certain conditions, such as when a SQL Server connection is accidentally specified without // the MultipleActiveResultSets keyword (or it was false). In this situation the galleries are inserted but an error // prevents the remaining data from being inserted. Once the user corrects this and tries again, this code can run to // finish inserting the seed data. using (var ctx = new GalleryDb()) { SeedController.InsertSeedData(ctx); } } }
/// <summary> /// Persist the current gallery settings to the data store, optionally modifying the default behavior of clearing /// and then reloading the gallery settings from the data store. /// </summary> /// <param name="forceReloadFromDataStore">If set to <c>true</c>, clear the gallery settings stored in memory, which will /// force loading them from the data store. Setting this to <c>false</c> can be useful when updating a simple property that /// does not require a complex recalculation (like, say the <see cref="UsersToNotifyWhenErrorOccurs"/> does). It may also /// be needed when a separate thread is persisting the data and no instance of HttpContext exists, which can cause an /// exception in the DotNetNuke module during the reload process in the web layer.</param> public void Save(bool forceReloadFromDataStore) { ValidateSave(); //Factory.GetDataProvider().GallerySetting_Save(this); using (var repo = new GallerySettingRepository()) { repo.Save(this); } // Clear the settings stored in static variables so they are retrieved from the data store during the next access. IGallerySettingsCollection gallerySettings = Factory.LoadGallerySettings(); if (forceReloadFromDataStore) { lock (gallerySettings) { gallerySettings.Clear(); } } // Invoke the GallerySettingsSaved event. This will be implemented in the web layer, which will finish populating any // properties that can't be done here, such as those of type <see cref="IUserAccountCollection" /> (since they need // access to the Membership provider, which the business layer has no knowledge of). EventHandler<GallerySettingsEventArgs> gallerySaved = GallerySettingsSaved; if (gallerySaved != null) { gallerySaved(null, new GallerySettingsEventArgs(this.GalleryId)); } }