/// <summary>
        /// Retrieves the gallery control settings from the data store for all controls containing galleries.
        /// </summary>
        /// <returns>Returns an <see cref="IGalleryControlSettingsCollection" /> containing the settings for all controls containing galleries.</returns>
        internal static IGalleryControlSettingsCollection RetrieveGalleryControlSettingsFromDataStore()
        {
            IGalleryControlSettingsCollection gallerySettings = new GalleryControlSettingsCollection();
              IGalleryControlSettings gs = null;
              string prevControlId = null;

              Type gsType = typeof(GalleryControlSettings);

              // Loop through each gallery control setting and assign to the relevant property. When we encounter a record with a new control ID,
              // automatically create a new GalleryControlSetting instance and start populating that one. When we are done with the loop we will
              // have created one GalleryControlSetting instance for each control that contains a gallery.

              // SQL:
              // SELECT
              //  GalleryControlSettingId, ControlId, SettingName, SettingValue
              // FROM [gs_GalleryControlSetting]
              // ORDER BY ControlId;
              using (var repo = new GalleryControlSettingRepository())
              {
            foreach (GalleryControlSettingDto gcsDto in repo.GetAll().OrderBy(g => g.ControlId))
            {
              #region Check for new gallery

              string currControlId = gcsDto.ControlId.Trim();

              if (String.IsNullOrEmpty(prevControlId) || (!currControlId.Equals(prevControlId)))
              {
            // We have encountered settings for a new gallery. Create a new object and add it to our collection.
            gs = new GalleryControlSettings(gcsDto.GalleryControlSettingId, currControlId);

            gallerySettings.Add(gs);

            prevControlId = currControlId;
              }

              #endregion

              #region Assign property

              // For each setting in the data store, find the matching property and assign the value to it.
              string settingName = gcsDto.SettingName.Trim();

              PropertyInfo prop = gsType.GetProperty(settingName);

              if (prop == null)
              {
            throw new MissingMemberException(String.Format(CultureInfo.CurrentCulture, "Invalid gallery control setting. A gallery control setting named '{0}' was found in the data store, but no property by that name exists in the class '{1}'. Check the gallery control settings in the data store to ensure they are correct.", settingName, gsType));
              }
              else if (prop.PropertyType == typeof(bool?))
              {
            prop.SetValue(gs, Convert.ToBoolean(gcsDto.SettingValue.Trim(), CultureInfo.InvariantCulture), null);
              }
              else if (prop.PropertyType == typeof(bool))
              {
            prop.SetValue(gs, Convert.ToBoolean(gcsDto.SettingValue.Trim(), CultureInfo.InvariantCulture), null);
              }
              else if (prop.PropertyType == typeof(string))
              {
            prop.SetValue(gs, Convert.ToString(gcsDto.SettingValue.Trim(), CultureInfo.InvariantCulture), null);
              }
              else if (prop.PropertyType == typeof(int))
              {
            prop.SetValue(gs, Convert.ToInt32(gcsDto.SettingValue.Trim(), CultureInfo.InvariantCulture), null);
              }
              else if (prop.PropertyType == typeof(int?))
              {
            prop.SetValue(gs, Convert.ToInt32(gcsDto.SettingValue.Trim(), CultureInfo.InvariantCulture), null);
              }
              else if (prop.PropertyType == typeof(Single))
              {
            prop.SetValue(gs, Convert.ToSingle(gcsDto.SettingValue.Trim(), CultureInfo.InvariantCulture), null);
              }
              else if (prop.PropertyType == typeof(ViewMode))
              {
            AssignViewModeProperty(gs, prop, gcsDto.SettingValue.Trim());
              }
              else if (prop.PropertyType == typeof(SlideShowType))
              {
                        AssignSlideShowTypeProperty(gs, prop, gcsDto.SettingValue.Trim());
              }
              else if (prop.PropertyType == typeof(String[]))
              {
            // Parse comma-delimited string to array
            string[] strings = gcsDto.SettingValue.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

            // Trim any leading and trailing spaces
            for (int i = 0; i < strings.Length; i++)
            {
              strings[i] = strings[i].Trim();
            }

            prop.SetValue(gs, strings, null);
              }
              else
              {
            throw new ArgumentOutOfRangeException(String.Format(CultureInfo.CurrentCulture, "GalleryControlSettings.RetrieveGalleryControlSettingsFromDataStore is not designed to process a property of type {0} (encountered in GalleryControlSettings.{1})", prop.PropertyType, prop.Name));
              }

              #endregion
            }
              }

              return gallerySettings;
        }
        /// <summary>
        /// Retrieves the gallery control settings from the data store for all controls containing galleries.
        /// </summary>
        /// <returns>Returns an <see cref="IGalleryControlSettingsCollection" /> containing the settings for all controls containing galleries.</returns>
        internal static IGalleryControlSettingsCollection RetrieveGalleryControlSettingsFromDataStore()
        {
            IGalleryControlSettingsCollection gallerySettings = new GalleryControlSettingsCollection();
            IGalleryControlSettings           gs = null;
            string prevControlId = null;

            Type gsType = typeof(GalleryControlSettings);

            // Loop through each gallery control setting and assign to the relevant property. When we encounter a record with a new control ID,
            // automatically create a new GalleryControlSetting instance and start populating that one. When we are done with the loop we will
            // have created one GalleryControlSetting instance for each control that contains a gallery.

            // SQL:
            // SELECT
            //  GalleryControlSettingId, ControlId, SettingName, SettingValue
            // FROM [gs_GalleryControlSetting]
            // ORDER BY ControlId;
            using (var repo = new GalleryControlSettingRepository())
            {
                foreach (GalleryControlSettingDto gcsDto in repo.GetAll().OrderBy(g => g.ControlId))
                {
                    #region Check for new gallery

                    string currControlId = gcsDto.ControlId.Trim();

                    if (String.IsNullOrEmpty(prevControlId) || (!currControlId.Equals(prevControlId)))
                    {
                        // We have encountered settings for a new gallery. Create a new object and add it to our collection.
                        gs = new GalleryControlSettings(gcsDto.GalleryControlSettingId, currControlId);

                        gallerySettings.Add(gs);

                        prevControlId = currControlId;
                    }

                    #endregion

                    #region Assign property

                    // For each setting in the data store, find the matching property and assign the value to it.
                    string settingName = gcsDto.SettingName.Trim();

                    PropertyInfo prop = gsType.GetProperty(settingName);

                    if (prop == null)
                    {
                        throw new MissingMemberException(String.Format(CultureInfo.CurrentCulture, "Invalid gallery control setting. A gallery control setting named '{0}' was found in the data store, but no property by that name exists in the class '{1}'. Check the gallery control settings in the data store to ensure they are correct.", settingName, gsType));
                    }
                    else if (prop.PropertyType == typeof(bool?))
                    {
                        prop.SetValue(gs, Convert.ToBoolean(gcsDto.SettingValue.Trim(), CultureInfo.InvariantCulture), null);
                    }
                    else if (prop.PropertyType == typeof(bool))
                    {
                        prop.SetValue(gs, Convert.ToBoolean(gcsDto.SettingValue.Trim(), CultureInfo.InvariantCulture), null);
                    }
                    else if (prop.PropertyType == typeof(string))
                    {
                        prop.SetValue(gs, Convert.ToString(gcsDto.SettingValue.Trim(), CultureInfo.InvariantCulture), null);
                    }
                    else if (prop.PropertyType == typeof(int))
                    {
                        prop.SetValue(gs, Convert.ToInt32(gcsDto.SettingValue.Trim(), CultureInfo.InvariantCulture), null);
                    }
                    else if (prop.PropertyType == typeof(int?))
                    {
                        prop.SetValue(gs, Convert.ToInt32(gcsDto.SettingValue.Trim(), CultureInfo.InvariantCulture), null);
                    }
                    else if (prop.PropertyType == typeof(Single))
                    {
                        prop.SetValue(gs, Convert.ToSingle(gcsDto.SettingValue.Trim(), CultureInfo.InvariantCulture), null);
                    }
                    else if (prop.PropertyType == typeof(ViewMode))
                    {
                        AssignViewModeProperty(gs, prop, gcsDto.SettingValue.Trim());
                    }
                    else if (prop.PropertyType == typeof(SlideShowType))
                    {
                        AssignSlideShowTypeProperty(gs, prop, gcsDto.SettingValue.Trim());
                    }
                    else if (prop.PropertyType == typeof(String[]))
                    {
                        // Parse comma-delimited string to array
                        string[] strings = gcsDto.SettingValue.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                        // Trim any leading and trailing spaces
                        for (int i = 0; i < strings.Length; i++)
                        {
                            strings[i] = strings[i].Trim();
                        }

                        prop.SetValue(gs, strings, null);
                    }
                    else
                    {
                        throw new ArgumentOutOfRangeException(String.Format(CultureInfo.CurrentCulture, "GalleryControlSettings.RetrieveGalleryControlSettingsFromDataStore is not designed to process a property of type {0} (encountered in GalleryControlSettings.{1})", prop.PropertyType, prop.Name));
                    }

                    #endregion
                }
            }

            return(gallerySettings);
        }
Esempio n. 3
0
            /// <summary>
            /// Loads the gallery control settings for the specified <paramref name="controlId"/>. When <paramref name="isWritable"/>
            /// is <c>true</c>, then return a unique instance that is not shared across threads, thus creating a thread-safe object that can
            /// be updated and persisted back to the data store. Calling this method with <paramref name="isWritable"/> set to <c>false</c>
            /// is the same as calling the overload of this method that takes only a control ID. Guaranteed to not return null.
            /// </summary>
            /// <param name="controlId">The value that uniquely identifies the control containing the gallery. Example: "Default.aspx|gsp"</param>
            /// <param name="isWritable">When set to <c>true</c> then return a unique instance that is not shared across threads.</param>
            /// <returns>
            /// Returns a writeable instance of <see cref="IGalleryControlSettings"/>containing  the gallery control settings for the gallery
            /// control specified by <paramref name="controlId"/>.
            /// </returns>
            public static IGalleryControlSettings LoadGalleryControlSetting(string controlId, bool isWritable)
            {
            IGalleryControlSettings galleryControlSettings;

            if (isWritable)
            {
                galleryControlSettings = GalleryControlSettings.RetrieveGalleryControlSettingsFromDataStore().FindByControlId(controlId);
            }
            else
            {
                galleryControlSettings = LoadGalleryControlSettings().FindByControlId(controlId);
            }

            if (galleryControlSettings == null)
            {
                galleryControlSettings = new GalleryControlSettings(int.MinValue, controlId);
            }

            return galleryControlSettings;
            }