Click here to Skip to main content
15,881,666 members
Articles / Web Development / HTML

Gallery Server Pro - An ASP.NET Gallery for Sharing Photos, Video, Audio and Other Media

Rate me:
Please Sign up or sign in to vote.
4.86/5 (131 votes)
18 Oct 2013GPL331 min read 824.3K   539  
Gallery Server Pro is a complete, stable ASP.NET gallery for sharing photos, video, audio and other media. This article presents the overall architecture and major features.
using System;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;
using GalleryServerPro.Business;
using GalleryServerPro.Business.Interfaces;

namespace GalleryServerPro.Data.SqlServer
{
	/// <summary>
	/// Contains functionality for persisting / retrieving user profiles to / from the SQL Server data store.
	/// </summary>
	internal static class ProfileData
	{
		#region Private Fields

		private const string ProfileNameShowMediaObjectMetadata = "ShowMediaObjectMetadata";
		private const string ProfileNameEnableUserAlbum = "EnableUserAlbum";
		private const string ProfileNameUserAlbumId = "UserAlbumId";

		#endregion

		#region Internal Static Methods

		/// <summary>
		/// Gets the profile for the specified user. Guaranteed to not return null.
		/// </summary>
		/// <param name="userName">The user name that uniquely identifies the user.</param>
		/// <param name="factory">An instance of <see cref="IFactory" />. It is used to instantiate the necessary object(s).</param>
		/// <returns>Returns an <see cref="IUserProfile" /> object containing the profile for the user.</returns>
		internal static IUserProfile GetUserProfile(string userName, IFactory factory)
		{
			IUserProfile profile = factory.CreateUserProfile();
			profile.UserName = userName;

			IUserGalleryProfile gs = null;
			int prevGalleryId = int.MinValue;

			using (IDataReader dr = GetDataReaderProfile(userName))
			{
				// Loop through each user profile setting and assign to the relevant property. When we encounter a record with a new gallery ID, 
				// automatically create a new UserGalleryProfile instance and start populating that one. When we are done with the loop we will
				// have created one UserGalleryProfile instance for each gallery the user has a profile for.

				// SQL:
				//SELECT
				//	ProfileId, UserId, FKGalleryId, SettingName, SettingValue
				//FROM [gs_UserGalleryProfile]
				//WHERE UserName=@UserName
				//ORDER BY UserId, FKGalleryId;
				while (dr.Read())
				{
					#region Check for new gallery

					int currGalleryId = Convert.ToInt32(dr["FKGalleryId"], CultureInfo.InvariantCulture);

					if ((gs == null) || (!currGalleryId.Equals(prevGalleryId)))
					{
						// We have encountered settings for a new user gallery profile. Create a new object and add it to our collection.
						gs = profile.GalleryProfiles.CreateNewUserGalleryProfile(currGalleryId);
						gs.UserName = userName;

						profile.GalleryProfiles.Add(gs);

						prevGalleryId = currGalleryId;
					}

					#endregion

					#region Assign property

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

					switch (settingName)
					{
						case ProfileNameShowMediaObjectMetadata:
							gs.ShowMediaObjectMetadata = Convert.ToBoolean(dr["SettingValue"].ToString().Trim(), CultureInfo.InvariantCulture);
							break;

						case ProfileNameEnableUserAlbum:
							gs.EnableUserAlbum = Convert.ToBoolean(dr["SettingValue"].ToString().Trim(), CultureInfo.InvariantCulture);
							break;

						case ProfileNameUserAlbumId:
							gs.UserAlbumId = Convert.ToInt32(dr["SettingValue"].ToString().Trim(), CultureInfo.InvariantCulture);
							break;
					}

					#endregion
				}
			}

			return profile;
		}

		/// <summary>
		/// Persist the specified <paramref name="profile" /> to the data store.
		/// </summary>
		/// <param name="profile">The profile to persist to the data store.</param>
		internal static void Save(IUserProfile profile)
		{
			using (SqlConnection cn = SqlDataProvider.GetDbConnection())
			{
				using (SqlCommand cmd = GetCommandUserGalleryProfileSave(cn))
				{
					cmd.Parameters["@UserName"].Value = profile.UserName;

					cn.Open();

					foreach (IUserGalleryProfile userGalleryProfile in profile.GalleryProfiles)
					{
						cmd.Parameters["@GalleryId"].Value = userGalleryProfile.GalleryId;

						cmd.Parameters["@SettingName"].Value = ProfileNameShowMediaObjectMetadata;
						cmd.Parameters["@SettingValue"].Value = userGalleryProfile.ShowMediaObjectMetadata;
						cmd.ExecuteNonQuery();

						cmd.Parameters["@SettingName"].Value = ProfileNameEnableUserAlbum;
						cmd.Parameters["@SettingValue"].Value = userGalleryProfile.EnableUserAlbum;
						cmd.ExecuteNonQuery();

						cmd.Parameters["@SettingName"].Value = ProfileNameUserAlbumId;
						cmd.Parameters["@SettingValue"].Value = userGalleryProfile.UserAlbumId;
						cmd.ExecuteNonQuery();
					}
				}
			}
		}

		/// <summary>
		/// Permanently delete the profile records for the specified <paramref name="userName" />.
		/// </summary>
		/// <param name="userName">The user name that uniquely identifies the user.</param>
		internal static void DeleteProfileForUser(string userName)
		{
			using (SqlConnection cn = SqlDataProvider.GetDbConnection())
			{
				using (SqlCommand cmd = GetCommandUserGalleryProfileDeleteForUser(userName, cn))
				{
					cn.Open();
					cmd.ExecuteNonQuery();
				}
			}
		}

		/// <summary>
		/// Permanently delete the profile records associated with the specified <paramref name="galleryId" />.
		/// </summary>
		/// <param name="galleryId">The gallery ID.</param>
		internal static void DeleteProfilesForGallery(int galleryId)
		{
			using (SqlConnection cn = SqlDataProvider.GetDbConnection())
			{
				using (SqlCommand cmd = GetCommandUserGalleryProfileDeleteForGallery(galleryId, cn))
				{
					cn.Open();
					cmd.ExecuteNonQuery();
				}
			}
		}

		#endregion

		#region Private Methods

		private static IDataReader GetDataReaderProfile(string userName)
		{
			SqlCommand cmd = new SqlCommand(Util.GetSqlName("gs_UserGalleryProfileSelect"), SqlDataProvider.GetDbConnection());

			cmd.Parameters.Add(new SqlParameter("@UserName", SqlDbType.NVarChar, DataConstants.UserNameLength));

			cmd.Parameters["@UserName"].Value = userName;

			cmd.CommandType = CommandType.StoredProcedure;

			cmd.Connection.Open();

			return cmd.ExecuteReader(CommandBehavior.CloseConnection);
		}

		private static SqlCommand GetCommandUserGalleryProfileSave(SqlConnection cn)
		{
			SqlCommand cmd = new SqlCommand(Util.GetSqlName("gs_UserGalleryProfileSave"), cn);
			cmd.CommandType = CommandType.StoredProcedure;

			cmd.Parameters.Add(new SqlParameter("@UserName", SqlDbType.NVarChar, DataConstants.UserNameLength));
			cmd.Parameters.Add(new SqlParameter("@GalleryId", SqlDbType.Int));
			cmd.Parameters.Add(new SqlParameter("@SettingName", SqlDbType.NVarChar, DataConstants.SettingNameLength));
			cmd.Parameters.Add(new SqlParameter("@SettingValue", SqlDbType.NVarChar, DataConstants.SettingValueLength));

			return cmd;
		}

		private static SqlCommand GetCommandUserGalleryProfileDeleteForUser(string userName, SqlConnection cn)
		{
			SqlCommand cmd = new SqlCommand(Util.GetSqlName("gs_UserGalleryProfileDeleteForUser"), cn);
			cmd.CommandType = CommandType.StoredProcedure;

			cmd.Parameters.Add(new SqlParameter("@UserName", SqlDbType.NVarChar, DataConstants.UserNameLength));
			cmd.Parameters["@UserName"].Value = userName;

			return cmd;
		}

		private static SqlCommand GetCommandUserGalleryProfileDeleteForGallery(int galleryId, SqlConnection cn)
		{
			SqlCommand cmd = new SqlCommand(Util.GetSqlName("gs_UserGalleryProfileDeleteForGallery"), cn);
			cmd.CommandType = CommandType.StoredProcedure;

			cmd.Parameters.Add(new SqlParameter("@GalleryId", SqlDbType.Int));
			cmd.Parameters["@GalleryId"].Value = galleryId;

			return cmd;
		}

		#endregion
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Software Developer (Senior) Tech Info Systems
United States United States
I have nearly 20 years of industry experience in software development, architecture, and Microsoft Office products. My company Tech Info Systems provides custom software development services for corporations, governments, and other organizations. Tech Info Systems is a registered member of the Microsoft Partner Program and I am a Microsoft Certified Professional Developer (MCPD).

I am the creator and lead developer of Gallery Server Pro, a free, open source ASP.NET gallery for sharing photos, video, audio, documents, and other files over the web. It has been developed over several years and has involved thousands of hours. The end result is a robust, configurable, and professional grade gallery that can be integrated into your web site, whether you are a large corporation, small business, professional photographer, or a local church.

Comments and Discussions