private void DeleteUserAlbum() { if (String.IsNullOrEmpty(this.txtNewUserUserName.Text)) { return; } if (GallerySettings.EnableUserAlbum) { IAlbum album = null; try { IUserGalleryProfile profile = ProfileController.GetProfileForGallery(this.txtNewUserUserName.Text.Trim(), GalleryId); if (profile != null) { album = AlbumController.LoadAlbumInstance(profile.UserAlbumId); } } catch (InvalidAlbumException) { return; } if (album != null) { AlbumController.DeleteAlbum(album); } } }
private static void SaveAlbumIdToProfile(int albumId, string userName, int galleryId) { IUserProfile profile = ProfileController.GetProfile(userName); IUserGalleryProfile pg = profile.GetGalleryProfile(galleryId); pg.UserAlbumId = albumId; ProfileController.SaveProfile(profile); }
/// <summary> /// Gets the gallery profile for the specified <paramref name="galleryId" />. Guaranteed to not return null. /// </summary> /// <param name="galleryId">The gallery ID.</param> /// <returns>A IUserGalleryProfile containing profile information.</returns> public IUserGalleryProfile GetGalleryProfile(int galleryId) { IUserGalleryProfile profile = GalleryProfiles.FindByGalleryId(galleryId); if (profile == null) { profile = CreateDefaultProfile(galleryId); GalleryProfiles.Add(profile); } return(profile); }
/// <summary> /// Compares the current instance with another object of the same type. /// </summary> /// <param name="obj">An object to compare with this instance.</param> /// <returns> /// A 32-bit signed integer that indicates the relative order of the objects being compared. The return value has these meanings: Value Meaning Less than zero This instance is less than <paramref name="obj"/>. Zero This instance is equal to <paramref name="obj"/>. Greater than zero This instance is greater than <paramref name="obj"/>. /// </returns> /// <exception cref="T:System.ArgumentException"> /// <paramref name="obj"/> is not the same type as this instance. </exception> public int CompareTo(object obj) { if (obj == null) { return(1); } else { IUserGalleryProfile other = obj as IUserGalleryProfile; if (other != null) { return(this.GalleryId.CompareTo(other.GalleryId)); } else { return(1); } } }
private void SaveProfile(IUserProfile userProfile) { // Get reference to user's album. We need to do this *before* saving the profile, because if the user disabled their user album, // this method will return null after saving the profile. IAlbum album = UserController.GetUserAlbum(GalleryId); IUserGalleryProfile profile = userProfile.GetGalleryProfile(GalleryId); if (!profile.EnableUserAlbum) { AlbumController.DeleteAlbum(album); } if (!profile.EnableUserAlbum) { profile.UserAlbumId = 0; } ProfileController.SaveProfile(userProfile); }
/// <summary> /// Retrieves the profile for the specified <paramref name="userName" />. Guaranteed to not return null. /// </summary> /// <param name="userName">Name of the user.</param> /// <returns>An instance of <see cref="IUserProfile" />.</returns> public static IUserProfile RetrieveFromDataStore(string userName) { IUserProfile profile = new UserProfile(); profile.UserName = userName; IUserGalleryProfile gs = null; int prevGalleryId = int.MinValue; using (var repo = new ProfileRepository()) { foreach (var profileDto in (repo.Where(p => p.UserName == userName, p => p.Gallery).OrderBy(p => p.FKGalleryId))) { // 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. #region Check for application-wide profile setting if (profileDto.Gallery.IsTemplate) { // Profile items associated with the template gallery are application-wide and map to properties // on the UserProfile object. switch (profileDto.SettingName.Trim()) { case ProfileNameEnableUserAlbum: case ProfileNameUserAlbumId: throw new DataException(String.Format("It is invalid for the profile setting '{0}' to be associated with a template gallery (Gallery ID {1}).", profileDto.SettingName, profileDto.FKGalleryId)); case ProfileNameAlbumProfiles: var albumProfiles = JsonConvert.DeserializeObject <List <AlbumProfile> >(profileDto.SettingValue.Trim()); if (albumProfiles != null) { profile.AlbumProfiles.AddRange(albumProfiles); } break; case ProfileNameMediaObjectProfiles: var moProfiles = JsonConvert.DeserializeObject <List <MediaObjectProfile> >(profileDto.SettingValue.Trim()); if (moProfiles != null) { profile.MediaObjectProfiles.AddRange(moProfiles); } break; } continue; } #endregion #region Check for new gallery int currGalleryId = profileDto.FKGalleryId; 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); 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. switch (profileDto.SettingName.Trim()) { case ProfileNameEnableUserAlbum: gs.EnableUserAlbum = Convert.ToBoolean(profileDto.SettingValue.Trim(), CultureInfo.InvariantCulture); break; case ProfileNameUserAlbumId: gs.UserAlbumId = Convert.ToInt32(profileDto.SettingValue.Trim(), CultureInfo.InvariantCulture); break; case ProfileNameAlbumProfiles: case ProfileNameMediaObjectProfiles: throw new DataException(String.Format("It is invalid for the profile setting '{0}' to be associated with a non-template gallery (Gallery ID {1}).", profileDto.SettingName, profileDto.FKGalleryId)); } #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> /// <exception cref="ArgumentNullException">Thrown when <paramref name="profile" /> is null.</exception> public static void Save(IUserProfile profile) { if (profile == null) { throw new ArgumentNullException("profile"); } using (var repo = new ProfileRepository()) { // AlbumProfiles var pDto = (repo.Where(p => p.UserName == profile.UserName && p.SettingName == ProfileNameAlbumProfiles)).FirstOrDefault(); var templateGalleryId = GetTemplateGalleryId(); if (pDto == null) { pDto = new UserGalleryProfileDto { UserName = profile.UserName, FKGalleryId = templateGalleryId, SettingName = ProfileNameAlbumProfiles, SettingValue = profile.AlbumProfiles.Serialize() }; repo.Add(pDto); } else { pDto.SettingValue = profile.AlbumProfiles.Serialize(); } // Media Object Profiles pDto = (repo.Where(p => p.UserName == profile.UserName && p.SettingName == ProfileNameMediaObjectProfiles)).FirstOrDefault(); if (pDto == null) { pDto = new UserGalleryProfileDto { UserName = profile.UserName, FKGalleryId = templateGalleryId, SettingName = ProfileNameMediaObjectProfiles, SettingValue = profile.MediaObjectProfiles.Serialize() }; repo.Add(pDto); } else { pDto.SettingValue = profile.MediaObjectProfiles.Serialize(); } // User Gallery Profiles foreach (IUserGalleryProfile userGalleryProfile in profile.GalleryProfiles) { IUserGalleryProfile ugp = userGalleryProfile; // EnableUserAlbum pDto = (repo.Where(p => p.UserName == profile.UserName && p.FKGalleryId == ugp.GalleryId && p.SettingName == ProfileNameEnableUserAlbum)).FirstOrDefault(); if (pDto == null) { pDto = new UserGalleryProfileDto { UserName = profile.UserName, FKGalleryId = ugp.GalleryId, SettingName = ProfileNameEnableUserAlbum, SettingValue = ugp.EnableUserAlbum.ToString(CultureInfo.InvariantCulture) }; repo.Add(pDto); } else { pDto.SettingValue = ugp.EnableUserAlbum.ToString(CultureInfo.InvariantCulture); } // UserAlbumId pDto = (repo.Where(p => p.UserName == profile.UserName && p.FKGalleryId == ugp.GalleryId && p.SettingName == ProfileNameUserAlbumId)).FirstOrDefault(); if (pDto == null) { pDto = new UserGalleryProfileDto { UserName = profile.UserName, FKGalleryId = ugp.GalleryId, SettingName = ProfileNameUserAlbumId, SettingValue = ugp.UserAlbumId.ToString(CultureInfo.InvariantCulture) }; repo.Add(pDto); } else { pDto.SettingValue = ugp.UserAlbumId.ToString(CultureInfo.InvariantCulture); } } repo.Save(); } }
/// <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); }