Click here to Skip to main content
15,885,366 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 825.2K   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.Collections.ObjectModel;
using System.Collections.Generic;
using GalleryServerPro.Business.Interfaces;
using System.Globalization;
using GalleryServerPro.Business.Properties;
using GalleryServerPro.ErrorHandler.CustomExceptions;

namespace GalleryServerPro.Business
{
	/// <summary>
	/// A collection of <see cref="IGalleryObject" /> objects.
	/// </summary>
	public class GalleryObjectCollection : Collection<IGalleryObject>, IGalleryObjectCollection
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="GalleryObjectCollection"/> class.
		/// </summary>
		public GalleryObjectCollection()
			: base(new List<IGalleryObject>())
		{
		}

		/// <summary>
		/// Sort the objects in this collection based on the <see cref="IGalleryObject.Sequence"/> property.
		/// </summary>
		public void Sort()
		{
			// We know galleryObjects is actually a List<IGalleryObject> because we passed it to the constructor.
			List<IGalleryObject> galleryObjects = (List<IGalleryObject>)Items;

			galleryObjects.Sort();
		}

		/// <summary>
		/// Return an unsorted list of items in the collection that match the specified gallery object type. Returns an empty
		/// collection if no matching objects are found.
		/// </summary>
		/// <param name="galleryObjectType">The type of gallery object to return. <see cref="GalleryObjectType.MediaObject"/> specifies
		/// all non-album media objects.</param>
		/// <returns>
		/// Returns an unsorted list of items in the collection that match the specified gallery object type.
		/// </returns>
		public IGalleryObjectCollection FindAll(GalleryObjectType galleryObjectType)
		{
			Type albumType = typeof(Album);
			Type imageType = typeof(Image);
			Type videoType = typeof(Video);
			Type audioType = typeof(Audio);
			Type genericType = typeof(GenericMediaObject);
			Type externalType = typeof(ExternalMediaObject);

			IGalleryObjectCollection filteredGalleryObjects = new GalleryObjectCollection();

			foreach (IGalleryObject galleryObject in (List<IGalleryObject>)Items)
			{
				Type goType = galleryObject.GetType();

				switch (galleryObjectType)
				{
					case GalleryObjectType.MediaObject:
						{
							if (goType != albumType)
								filteredGalleryObjects.Add(galleryObject);
							break;
						}
					case GalleryObjectType.Album:
						{
							if (goType == albumType)
								filteredGalleryObjects.Add(galleryObject);
							break;
						}
					case GalleryObjectType.Image:
						{
							if (goType == imageType)
								filteredGalleryObjects.Add(galleryObject);
							break;
						}
					case GalleryObjectType.Video:
						{
							if (goType == videoType)
								filteredGalleryObjects.Add(galleryObject);
							break;
						}
					case GalleryObjectType.Audio:
						{
							if (goType == audioType)
								filteredGalleryObjects.Add(galleryObject);
							break;
						}
					case GalleryObjectType.Generic:
						{
							if (goType == genericType)
								filteredGalleryObjects.Add(galleryObject);
							break;
						}
					case GalleryObjectType.External:
						{
							if (goType == externalType)
								filteredGalleryObjects.Add(galleryObject);
							break;
						}
					case GalleryObjectType.All:
						{
							filteredGalleryObjects.Add(galleryObject);
							break;
						}
					case GalleryObjectType.None: break;
					case GalleryObjectType.Unknown: break;
					default: throw new BusinessException(String.Format("The method GalleryServerPro.Business.GalleryObjectCollection.FindAll encountered an enumeration it does not recognize. A developer must update this method to handle the {0} enumeration.", galleryObjectType));
				}
			}

			return filteredGalleryObjects;
		}

		/// <summary>
		/// Find the gallery object in the collection that matches the specified <see cref="IGalleryObject.Id">ID</see> and type. If no matching object is found,
		/// null is returned.
		/// </summary>
		/// <param name="galleryObjectId">The <see cref="IGalleryObject.Id">ID</see> that uniquely identifies the album or media object.</param>
		/// <param name="galleryObjectType">The type of gallery object to which the galleryObjectId applies. Valid values
		/// are <see cref="GalleryObjectType.Album"/> and <see cref="GalleryObjectType.MediaObject"/>. An exception is thrown if any other value is specified.</param>
		/// <returns>
		/// Returns an <see cref="IGalleryObject"/>object from the collection that matches the specified
		/// <see cref="IGalleryObject.Id">ID</see> and type, or null if no matching object is found.
		/// </returns>
		/// <remarks>The <see cref="IGalleryObject.Id">ID</see> for albums and media objects are managed separately. As a result, the same
		/// <see cref="IGalleryObject.Id">ID</see> may be used for both an album and media object, and these two objects may end up in the
		/// same collection (for example, if they are both in the same parent album.) Therefore, this method requires that we specify the
		/// type of gallery object.</remarks>
		public IGalleryObject FindById(int galleryObjectId, GalleryObjectType galleryObjectType)
		{
			if ((galleryObjectType != GalleryObjectType.Album) && (galleryObjectType != GalleryObjectType.MediaObject))
			{
				throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, Resources.GalleryObjectCollection_FindById_Ex_Msg, galleryObjectType.ToString()));
			}

			Type albumType = typeof(Album);
			IGalleryObject matchingGalleryObject = null;

			foreach (IGalleryObject galleryObject in (List<IGalleryObject>)Items)
			{
				if (galleryObjectType == GalleryObjectType.Album)
				{
					if ((galleryObject.Id == galleryObjectId) && (galleryObject.GetType() == albumType))
					{
						matchingGalleryObject = galleryObject;
						break;
					}
				}
				else if (galleryObjectType == GalleryObjectType.MediaObject)
				{
					if ((galleryObject.Id == galleryObjectId) && (galleryObject.GetType() != albumType))
					{
						matchingGalleryObject = galleryObject;
						break;
					}
				}
				else
				{
					// We are already validating at the beginning of the method, but let's do it again just to be double sure.
					throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, Resources.GalleryObjectCollection_FindById_Ex_Msg, galleryObjectType.ToString()));
				}
			}

			return matchingGalleryObject;
		}

		/// <summary>
		/// Determines whether the <paramref name="galleryObject"/> is already a member of the collection. An object is considered a member
		/// of the collection if one of the following scenarios is true: (1) They are both of the same type, each ID is 
		/// greater than int.MinValue, and the IDs are equal to each other, or (2) They are new objects that haven't yet
		/// been saved to the data store, the physical path to the original file has been specified, and the paths
		/// are equal to each other.
		/// </summary>
		/// <param name="galleryObject">An <see cref="IGalleryObject"/> to determine whether it is a member of the current collection.</param>
		/// <returns>Returns <c>true</c> if <paramref name="galleryObject"/> is a member of the current collection;
		/// otherwise returns <c>false</c>.</returns>
		public new bool Contains(IGalleryObject galleryObject)
		{
			foreach (IGalleryObject galleryObjectIterator in (List<IGalleryObject>)Items)
			{
				bool existingObjectsAndEqual = ((galleryObjectIterator.Id > int.MinValue) && (galleryObjectIterator.Id.Equals(galleryObject.Id)) && (galleryObjectIterator.GetType() == galleryObject.GetType()));

				bool newObjectsAndFilepathsAreEqual = ((galleryObjectIterator.IsNew) && (galleryObject.IsNew)
																								 && (!String.IsNullOrEmpty(galleryObjectIterator.Original.FileNamePhysicalPath))
																								 && (!String.IsNullOrEmpty(galleryObject.Original.FileNamePhysicalPath))
																								 && (galleryObjectIterator.Original.FileNamePhysicalPath.Equals(galleryObject.Original.FileNamePhysicalPath)));

				if (existingObjectsAndEqual || newObjectsAndFilepathsAreEqual)
				{
					return true;
				}
			}
			return false;
		}

		//public int IndexOf(IGalleryObject galleryObject)
		//{
		//  // We know galleryObjects is actually a List<IGalleryObject> because we passed it to the constructor.
		//  System.Collections.Generic.List<IGalleryObject> galleryObjects = (System.Collections.Generic.List<IGalleryObject>)Items;

		//  return galleryObjects.IndexOf(galleryObject);
		//}

	}
}

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