/// <summary>
        /// Delete a Photo by the primary key
        /// </summary>
        /// <param name="photo"></param>
        public int Delete(Photo photo, string commandText)
        {
            DbParameter[] parameters =
            {
                DbClient.MakeParameter("@PhotoID",DbType.Int32,4,photo.PhotoID),

            };

            return DbClient.NonQuery(commandText, parameters);
        }
        /// <summary>
        /// Delete a Photo by the primary key
        /// </summary>
        /// <param name="photo">The photo.</param>
        /// <returns></returns>
        public int Delete(Photo photo)
        {
            SqlParameter[] parameters =
            {
                _database.MakeParameter("@PhotoID",SqlDbType.Int,4, photo.PhotoID),

            };

            return _database.NonQuery("Photo_Delete", parameters);
        }
        /// <summary>
        /// Moves a photo to a new gallery
        /// </summary>
        /// <param name="photo"></param>
        /// <param name="commandText"></param>
        public void MovePhotoToNewGallery(Photo photo, string commandText)
        {
            DbParameter[] parameters =
            {
                DbClient.MakeParameter("@PhotoID",DbType.Int32,4,photo.PhotoID),
                DbClient.MakeParameter("@GalleryId",DbType.Int32,4,photo.GalleryID)

            };

            DbClient.NonQuery(commandText, parameters);
        }
        public void Upload(string token, byte[] image, int galleryId, string imageFormat)
        {
            bool isTokenValid = UserLogic.IsValidToken(token);

            //Check for the validity of the token.
            if (!isTokenValid)
            {
                throw new InvalidTokenException();
            }

            //User user = UserLogic.RetrieveUserByToken(token);
            Photo photo = new Photo { GalleryId = galleryId, Title = string.Empty, DateTaken = DateTime.Now, Description = string.Empty };
            GalleryPhoto galleryphoto = new GalleryPhoto { OriginalImage = image };

            new PhotoLogic().Insert(photo, galleryphoto);
        }
        /// <summary>
        /// Inserts Photo into the Photos Table
        /// </summary>
        /// <param name="photo">A new populated photo.</param>
        /// <param name="galleryPhoto"></param>
        /// <param name="adminPhotos">Photos to be used in admin section</param>
        /// <returns>Insert Count</returns>
        public int Insert(Photo photo, GalleryPhoto galleryPhoto, GalleryPhoto adminPhotos, string commandText)
        {
            DbParameter[] parameters =
            {
                    DbClient.MakeParameter("@Title",DbType.String,100,photo.Title),
                    DbClient.MakeParameter("@Description",DbType.String,255,photo.Description),
                    DbClient.MakeParameter("@DateTaken",DbType.DateTime,8,photo.DateTaken),
                    DbClient.MakeParameter("@GalleryID",DbType.Int32,4,photo.GalleryID),
                    DbClient.MakeParameter("@OriginalImage",DbType.Binary, galleryPhoto.OriginalImage.Length, galleryPhoto.OriginalImage),
                    DbClient.MakeParameter("@DisplayImage",DbType.Binary, galleryPhoto.DisplayImage.Length, galleryPhoto.DisplayImage),
                    DbClient.MakeParameter("@ThumbnailImage",DbType.Binary, galleryPhoto.ThumbnailImage.Length, galleryPhoto.ThumbnailImage),
                    DbClient.MakeParameter("@AdminThumbnail",DbType.Binary, adminPhotos.ThumbnailImage.Length, adminPhotos.ThumbnailImage),
                    DbClient.MakeParameter("@AdminFullsizeImage",DbType.Binary, adminPhotos.DisplayImage.Length, adminPhotos.DisplayImage),
                    DbClient.MakeParameter("@Profile",DbType.String, 50, photo.Profile)

            };

            return DbClient.NonQuery(commandText, parameters);
        }
        /// <summary>
        /// Updates the Photo table by the primary key, if the Photo is dirty then an update will occur
        /// </summary>
        /// <param name="photo">a populated photo</param>
        /// <returns>update count</returns>
        public int UpdateMetaData(Photo photo)
        {
            SqlParameter[] parameters =
                {
                    _database.MakeParameter("@PhotoID",SqlDbType.Int,4,photo.PhotoID),
                    _database.MakeParameter("@Title",SqlDbType.NVarChar,100,photo.Title),
                    _database.MakeParameter("@Description",SqlDbType.NVarChar,255,photo.Description),
                    _database.MakeParameter("@DateTaken",SqlDbType.DateTime,8,photo.DateTaken)
                };

            int updateCount = _database.NonQuery("Photo_UpdateMetaData", parameters);

            return updateCount;
        }
        /// <summary>
        /// Gets a random photo from the photo table
        /// </summary>
        /// <returns></returns>
        public Photo RetrieveRandomPhoto()
        {
            Photo photo = new Photo();

               SqlDataReader reader = _database.Reader("Photo_RetreiveRandomPhoto");
            List<Photo> photos = PopulatePhotos(reader);

            if (photos.Count > 0)
            {
                photo = photos[0];
            }

            return photo;
        }
        /// <summary>
        /// Retrieves a random photo from a specified gallery
        /// </summary>
        /// <param name="galleryId">The gallery id.</param>
        /// <returns></returns>
        public Photo RetrieveRandomPhotoByGalleryId(int galleryId)
        {
            Photo photo = new Photo();

            SqlParameter[] parameters =
            {
                _database.MakeParameter("@GalleryId",SqlDbType.Int,4,galleryId)
            };

            SqlDataReader reader = _database.Reader("Photo_RetreiveRandomPhotoByGalleryId", parameters);
            List<Photo> photos = PopulatePhotos(reader);

            if (photos.Count > 0)
            {
                photo = photos[0];
            }

            return photo;
        }
        /// <summary>
        /// Inserts Photo into the Photos Table
        /// </summary>
        /// <param name="photo">A new populated photo.</param>
        /// <param name="galleryPhoto">The gallery photo.</param>
        /// <param name="adminPhotos">Photos to be used in admin section</param>
        /// <returns>Insert Count</returns>
        public int Insert(Photo photo, GalleryPhoto galleryPhoto, GalleryPhoto adminPhotos)
        {
            SqlParameter[] parameters =
            {
                    _database.MakeParameter("@Title",SqlDbType.NVarChar,100,photo.Title),
                    _database.MakeParameter("@GalleryId",SqlDbType.Int,4,photo.GalleryId),
                    _database.MakeParameter("@Description",SqlDbType.NVarChar,255,photo.Description),
                    _database.MakeParameter("@DateTaken",SqlDbType.DateTime,8,photo.DateTaken),
                    _database.MakeParameter("@OriginalImage",SqlDbType.Image, galleryPhoto.OriginalImage.Length, galleryPhoto.OriginalImage),
                    _database.MakeParameter("@DisplayImage",SqlDbType.Image, galleryPhoto.DisplayImage.Length, galleryPhoto.DisplayImage),
                    _database.MakeParameter("@ThumbnailImage",SqlDbType.Image, galleryPhoto.ThumbnailImage.Length, galleryPhoto.ThumbnailImage),
                    _database.MakeParameter("@AdminThumbnail",SqlDbType.Image, adminPhotos.ThumbnailImage.Length, adminPhotos.ThumbnailImage),
                    _database.MakeParameter("@AdminFullsizeImage",SqlDbType.Image, adminPhotos.DisplayImage.Length, adminPhotos.DisplayImage),
                    _database.MakeParameter("@Profile",SqlDbType.NVarChar, 50, photo.Profile)

            };

            try
            {
                _database.NonQuery("Photo_Insert", parameters);
            }
            catch (Exception)
            {

                throw;
            }

            return 1;
        }
        /// <summary>
        /// Moves a photo to a new gallery
        /// </summary>
        /// <param name="photo">The photo.</param>
        public void MovePhotoToNewGallery(Photo photo)
        {
            SqlParameter[] parameters =
            {
                _database.MakeParameter("@PhotoID",SqlDbType.Int,4,photo.PhotoID)
            };

            _database.NonQuery("Photo_MovePhotoToNewGallery", parameters);
        }
 /// <summary>
 /// Delete a Photo by the primary key
 /// </summary>
 /// <param name="photo"></param>
 public void Delete(Photo photo)
 {
     photoDb.Delete(photo, deleteMethod);
 }
 /// <summary>
 /// Inserts Photo into the Photos Table
 /// </summary>
 /// <param name="photo">A new populated photo.</param>
 /// <param name="galleryPhoto"></param>
 /// <param name="adminPhotos">Photos to be used in admin section</param>
 /// <returns>Insert Count</returns>
 public int Insert(Photo photo, GalleryPhoto galleryPhoto, GalleryPhoto adminPhotos)
 {
     return photoDb.Insert(photo, galleryPhoto, adminPhotos, "Photo_Insert");
 }
        /// <summary>
        /// populates a single photo class
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <returns></returns>
        private static Photo GetSingleItem(IDataReader reader)
        {
            Photo photo = new Photo();

            using (reader)
            {
                while (reader.Read())
                {
                    photo.PhotoID = Convert.ToInt32(reader["PhotoID"]);
                    photo.Title = Convert.ToString(reader["Title"]);
                    photo.Description = Convert.ToString(reader["Description"]);
                    photo.DateTaken = SetDateTaken(reader);
                    photo.Profile = EnumParse<PhotoProfile>.Parse(reader["Profile"].ToString());
                }
            }

            return photo;
        }
 /// <summary>
 /// Updates the meta data.
 /// </summary>
 /// <param name="photo">The photo.</param>
 public void UpdateMetaData(Photo photo)
 {
     _resource.UpdateMetaData(photo);
 }
        /// <summary>
        /// Retrieves the first photo from the reader
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <returns></returns>
        internal static Photo Populate(SqlDataReader reader)
        {
            Photo photo = new Photo();

            using (reader)
            {
                while (reader.Read())
                {
                    photo = GetItem(reader);
                }
            }

            return photo;
        }
 /// <summary>
 /// Move a photo from one gallery to another
 /// </summary>
 /// <param name="photo">photo to be move to the new gallery</param>
 public void MovePhotoToNewGallery(Photo photo)
 {
     photoDb.MovePhotoToNewGallery(photo, movePhotoProcedure);
 }
        /// <summary>
        /// Handles the RowDeleting event of the managePhotos control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewDeleteEventArgs"/> instance containing the event data.</param>
        protected void managePhotos_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            List<Photo> photos = new List<Photo>();

            for (int index = 0; managePhotos.Rows.Count > index; index++)
            {
                CheckBox deleteCheckBox = (CheckBox)managePhotos.Rows[index].FindControl("selectedCheckBox");
                Photo photo = new Photo();

                if (deleteCheckBox.Checked)
                {
                    photo.PhotoID = Convert.ToInt32(managePhotos.DataKeys[index].Value);
                    photos.Add(photo);
                }
            }

            new PhotoLogic().Delete(photos);
            message.Text = AdminResources.DeletePhotos;

            BindPhotoGridView();
        }
        /// <summary>
        /// Retrieves the selected rows.
        /// </summary>
        /// <returns></returns>
        private List<Photo> RetrieveSelectedRows()
        {
            List<GridViewRow> selectedRows = RetrieveCheckedRowsFromGridview("selectedRow", managePhotos);
            List<Photo> selectedPhotos = new List<Photo>();

            foreach (GridViewRow row in selectedRows)
            {
                Photo photo = new Photo {PhotoID = Int32.Parse(managePhotos.DataKeys[row.RowIndex].Value.ToString()), GalleryId = Convert.ToInt32(moveGalleriesDropDown.SelectedValue)};

                selectedPhotos.Add(photo);
            }

            return selectedPhotos;
        }
        /// <summary>
        /// Handles the RowUpdating event of the managePhotos control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewUpdateEventArgs"/> instance containing the event data.</param>
        protected void managePhotos_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            GridView grid = (GridView)sender;

            GridViewRow row = grid.Rows[e.RowIndex];
            int photoID = Int32.Parse(grid.DataKeys[e.RowIndex].Value.ToString());

            TextBox title = (TextBox)row.FindControl("titleTextBox");
            TextBox description = (TextBox)row.FindControl("descriptionTextBox");
            TextBox date = (TextBox)row.FindControl("photoDate");

            Photo photo = new Photo {PhotoID = photoID, Title = title.Text, Description = description.Text, DateTaken = DateTime.Parse(date.Text)};

            new PhotoLogic().UpdateMetaData(photo);

            //Reset the edit index.
            managePhotos.EditIndex = -1;

            //Bind data to the GridView control.
            BindPhotoGridView();
        }
        /// <summary>
        /// populates the metadata of a photo
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <returns></returns>
        private static Photo GetItem(IDataRecord reader)
        {
            Photo photo = new Photo
                              {
                                  PhotoID = Convert.ToInt32(reader["PhotoID"]),
                                  Title = Convert.ToString(reader["Title"]),
                                  Description = Convert.ToString(reader["Description"]),
                                  DateTaken = SetDateTaken(reader),
                                  Profile = EnumParse<PhotoProfile>.Parse(reader["Profile"].ToString())
                              };

            return photo;
        }
        /// <summary>
        /// Updates the Photo table by the primary key, if the Photo is dirty then an update will occur
        /// </summary>
        /// <param name="photo">a populated photo</param>
        /// <returns>update count</returns>
        public int UpdateMetaData(Photo photo, string commandText)
        {
            int updateCount = 0;

            if(photo.IsDirty())
            {
                DbParameter[] parameters =
                {
                    DbClient.MakeParameter("@PhotoID",DbType.Int32,4,photo.PhotoID),
                    DbClient.MakeParameter("@Title",DbType.String,100,photo.Title),
                    DbClient.MakeParameter("@Description",DbType.String,255,photo.Description),
                    DbClient.MakeParameter("@DateTaken",DbType.DateTime,8,photo.DateTaken)
                };

                updateCount = DbClient.NonQuery(commandText, parameters);

            }

            return updateCount;
        }
        /// <summary>
        /// Gets a random photo from the photo table
        /// </summary>
        /// <param name="commandText"></param>
        /// <returns></returns>
        public Photo RetrieveRandomPhoto(string commandText)
        {
            DbDataReader reader = DbClient.Reader(commandText);
            List<Photo> photos = PopulatePhotos(reader);
            Photo photo = new Photo();

            if(photos.Count > 0)
            {
                photo = photos[0];
            }

            return photo;
        }
        /// <summary>
        /// Retrieves a random photo from a specified gallery
        /// </summary>
        /// <param name="galleryId"></param>
        /// <param name="commandText"></param>
        /// <returns></returns>
        public Photo RetrieveRandomPhotoByGalleryId(int galleryId, string commandText)
        {
            DbParameter[] parameters =
            {
                DbClient.MakeParameter("@GalleryId",DbType.Int32,4,galleryId)
            };

            DbDataReader reader = DbClient.Reader(commandText, parameters);
            List<Photo> photos = PopulatePhotos(reader);
            Photo photo = new Photo();

            if (photos.Count > 0)
            {
                photo = photos[0];
            }

            return photo;
        }
        /// <summary>
        /// Inserts the specified photo.
        /// </summary>
        /// <param name="photo">The photo.</param>
        /// <param name="galleryPhoto">The gallery photo.</param>
        public void Insert(Photo photo, GalleryPhoto galleryPhoto)
        {
            GalleryPhoto adminPhotos = new GalleryPhoto();

            GallerySettings settings = GallerySettings.Load();
            Bitmap orginalBitmap = ImageConversion.ConvertByteArrayToBitmap(galleryPhoto.OriginalImage);

            //hardcoded administration image dimensions. This will allow the user to change their image sizes and not have it effect my pretty admin layout.
            ImageDimension adminThumbnailDimensions = new ImageDimension { Height = 100, Width = 100 };
            ImageDimension adminFullsizeDimensions = new ImageDimension { Height = 600, Width = 600 };

            adminThumbnailDimensions = FindImagePerspective(orginalBitmap, adminThumbnailDimensions);
            adminFullsizeDimensions = FindImagePerspective(orginalBitmap, adminFullsizeDimensions);

            //Resize the Admin images
            adminPhotos.ThumbnailImage = ImageConversion.Resize(galleryPhoto.OriginalImage, adminThumbnailDimensions);
            adminPhotos.DisplayImage = ImageConversion.Resize(galleryPhoto.OriginalImage, adminFullsizeDimensions);

            //calculate the correct dimensions
            ImageDimension thumbnailDimensions = FindImagePerspective(orginalBitmap, settings.ThumbnailDimensions);
            ImageDimension fullsizeDimensions = FindImagePerspective(orginalBitmap, settings.FullsizeDimensions);

            //Resize the images
            galleryPhoto.ThumbnailImage = ImageConversion.Resize(galleryPhoto.OriginalImage, thumbnailDimensions);
            galleryPhoto.DisplayImage = ImageConversion.Resize(galleryPhoto.OriginalImage, fullsizeDimensions);

            //Set photo profile
            photo.Profile = fullsizeDimensions.PhotoProfile;

            //Insert new images into the Database
            _resource.Insert(photo, galleryPhoto, adminPhotos);
        }
 /// <summary>
 /// Updates the Photo table by the primary key, if the Photo is dirty then an update will occur
 /// </summary>
 /// <param name="photo">a populated photo</param>
 /// <returns>update count</returns>
 public int UpdateMetaData(Photo photo)
 {
     int updateCount = photoDb.UpdateMetaData(photo, "Photo_UpdateMetaData");
     return updateCount;
 }