private static void InflateAlbumFromDto(IAlbum album, AlbumDto albumDto) { if (album == null) throw new ArgumentNullException("album"); if (albumDto == null) throw new ArgumentNullException("albumDto"); // A parent ID = 0 indicates the root album. Use int.MinValue to send to Album constructor. int albumParentId = (albumDto.AlbumParentId == 0 ? int.MinValue : albumDto.AlbumParentId); // Assign parent if it hasn't already been assigned. if ((album.Parent.Id == int.MinValue) && (albumParentId > int.MinValue)) { album.Parent = CreateAlbumInstance(albumParentId, albumDto.FKGalleryId); } album.GalleryId = albumDto.FKGalleryId; album.Title = albumDto.Title; album.DirectoryName = albumDto.DirectoryName; album.Summary = albumDto.Summary; album.Sequence = albumDto.Seq; album.DateStart = HelperFunctions.ToDateTime(albumDto.DateStart); album.DateEnd = HelperFunctions.ToDateTime(albumDto.DateEnd); album.CreatedByUserName = albumDto.CreatedBy.Trim(); album.DateAdded = HelperFunctions.ToDateTime(albumDto.DateAdded); album.LastModifiedByUserName = albumDto.LastModifiedBy.Trim(); album.DateLastModified = HelperFunctions.ToDateTime(albumDto.DateLastModified); album.OwnerUserName = albumDto.OwnedBy.Trim(); album.OwnerRoleName = albumDto.OwnerRoleName.Trim(); album.IsPrivate = albumDto.IsPrivate; // Set the album's thumbnail media object ID. Setting this property sets an internal flag that will cause // the media object info to be retrieved when the Thumbnail property is accessed. That's why we don't // need to set any of the thumbnail properties. // WARNING: No matter what, do not call DisplayObject.CreateInstance() because that creates a new object, // and we might be executing this method from within our Thumbnail display object. Trust me, this // creates hard to find bugs! album.ThumbnailMediaObjectId = albumDto.ThumbnailMediaObjectId; }
/// <summary> /// Gets the album from the data transfer object. Guaranteed to not return null. /// </summary> /// <param name="albumDto">The album data transfer object.</param> /// <returns>Returns an <see cref="IAlbum" />.</returns> /// <exception cref="InvalidAlbumException">Thrown when <paramref name="albumDto" /> is null.</exception> private static IAlbum GetAlbumFromDto(AlbumDto albumDto) { if (albumDto == null) { throw new InvalidAlbumException(); } IAlbum album = null; try { album = new Album(albumDto.AlbumId, albumDto.FKGalleryId, albumDto.AlbumParentId, albumDto.Title, albumDto.DirectoryName, albumDto.Summary, albumDto.ThumbnailMediaObjectId, albumDto.Seq, HelperFunctions.ToDateTime(albumDto.DateStart), HelperFunctions.ToDateTime(albumDto.DateEnd), albumDto.CreatedBy.Trim(), HelperFunctions.ToDateTime(albumDto.DateAdded), albumDto.LastModifiedBy.Trim(), HelperFunctions.ToDateTime(albumDto.DateLastModified), albumDto.OwnedBy.Trim(), albumDto.OwnerRoleName.Trim(), albumDto.IsPrivate); album.IsInflated = true; } catch { if (album != null) album.Dispose(); throw; } return album; }
private static void InsertAlbums(GalleryDb ctx) { // Insert the root album into the first non-template gallery. var galleryId = ctx.Galleries.First(g => !g.IsTemplate).GalleryId; if (!ctx.Albums.Any(a => a.FKAlbumParentId == null && a.FKGalleryId == galleryId)) { var rootAlbum = new AlbumDto { FKGalleryId = galleryId, FKAlbumParentId = null, DirectoryName = String.Empty, ThumbnailMediaObjectId = 0, SortByMetaName = MetadataItemName.DateAdded, SortAscending = true, Seq = 0, DateAdded = DateTime.Now, CreatedBy = "System", LastModifiedBy = "System", DateLastModified = DateTime.Now, OwnedBy = String.Empty, OwnerRoleName = String.Empty, IsPrivate = false }; ctx.Albums.Add(rootAlbum); } SaveChanges(ctx); // NOTE: The title & summary for this album are validated and inserted if necessary in the function InsertMetadata(). // Add the title //ctx.Metadatas.AddOrUpdate(m => new { m.FKAlbumId, m.MetaName }, new MetadataDto // { // FKAlbumId = rootAlbum.AlbumId, // MetaName = MetadataItemName.Title, // Value = "All albums" // }); //// Add the caption //ctx.Metadatas.AddOrUpdate(m => new { m.FKAlbumId, m.MetaName }, new MetadataDto // { // FKAlbumId = rootAlbum.AlbumId, // MetaName = MetadataItemName.Caption, // Value = "Welcome to Gallery Server Pro!" // }); }
/// <summary> /// Verify the current gallery has a root album, creating one if necessary. The root album is returned. /// </summary> /// <returns>An instance of <see cref="AlbumDto" />.</returns> private AlbumDto ConfigureAlbumTable() { using (var repo = new AlbumRepository()) { var rootAlbumDto = repo.Where(a => a.FKGalleryId == GalleryId && a.FKAlbumParentId == null).FirstOrDefault(); if (rootAlbumDto == null) { rootAlbumDto = new AlbumDto { FKGalleryId = GalleryId, FKAlbumParentId = null, DirectoryName = String.Empty, ThumbnailMediaObjectId = 0, SortByMetaName = MetadataItemName.DateAdded, SortAscending = true, Seq = 0, DateAdded = DateTime.Now, CreatedBy = GlobalConstants.SystemUserName, LastModifiedBy = GlobalConstants.SystemUserName, DateLastModified = DateTime.Now, OwnedBy = String.Empty, OwnerRoleName = String.Empty, IsPrivate = false, Metadata = new Collection<MetadataDto> { new MetadataDto {MetaName = MetadataItemName.Caption, Value = Resources.Root_Album_Default_Summary}, new MetadataDto {MetaName = MetadataItemName.Title, Value = Resources.Root_Album_Default_Title} } }; repo.Add(rootAlbumDto); repo.Save(); } return rootAlbumDto; } }
/// <summary> /// Verify there is a UI template/album mapping for the root album in the current gallery, creating them /// if necessary. /// </summary> /// <param name="rootAlbum">The root album.</param> private static void ConfigureUiTemplateAlbumTable(AlbumDto rootAlbum) { using (var repoUiTmpl = new UiTemplateRepository()) { using (var repoUiTmplA = new UiTemplateAlbumRepository(repoUiTmpl.Context)) { // Make sure each template category has at least one template assigned to the root album. // We do this with a union of two queries: // 1. For categories where there is at least one album assignment, determine if at least one of // those assignments is the root album. // 2. Find categories without any albums at all (this is necessary because the SelectMany() in the first // query won't return any categories that don't have related records in the template/album table). var dtos = repoUiTmpl.Where(t => t.FKGalleryId == rootAlbum.FKGalleryId) .SelectMany(t => t.TemplateAlbums, (t, tt) => new { t.TemplateType, tt.FKAlbumId }) .GroupBy(t => t.TemplateType) .Where(t => t.All(ta => ta.FKAlbumId != rootAlbum.AlbumId)) .Select(t => t.Key) .Union(repoUiTmpl.Where(t => t.FKGalleryId == rootAlbum.FKGalleryId).GroupBy(t => t.TemplateType).Where(t => t.All(t2 => !t2.TemplateAlbums.Any())).Select(t => t.Key)) ; foreach (var dto in dtos) { // We have a template type without a root album. Find the default template and assign that one. var dto1 = dto; repoUiTmplA.Add(new UiTemplateAlbumDto() { FKUiTemplateId = repoUiTmpl.Where(t => t.FKGalleryId == rootAlbum.FKGalleryId && t.TemplateType == dto1 && t.Name.Equals("default", StringComparison.OrdinalIgnoreCase)).First().UiTemplateId, FKAlbumId = rootAlbum.AlbumId }); } repoUiTmplA.Save(); } } }
/// <summary> /// Gets the album from the data transfer object. Guaranteed to not return null. /// </summary> /// <param name="albumDto">The album data transfer object.</param> /// <returns>Returns an <see cref="IAlbum" />.</returns> /// <exception cref="InvalidAlbumException">Thrown when <paramref name="albumDto" /> is null.</exception> public static IAlbum GetAlbumFromDto(AlbumDto albumDto) { if (albumDto == null) { throw new InvalidAlbumException(); } IAlbum album = new Album(albumDto.AlbumId, albumDto.FKGalleryId, albumDto.FKAlbumParentId.GetValueOrDefault(0), albumDto.DirectoryName, albumDto.ThumbnailMediaObjectId, albumDto.SortByMetaName, albumDto.SortAscending, albumDto.Seq, HelperFunctions.ToDateTime(albumDto.DateStart), HelperFunctions.ToDateTime(albumDto.DateEnd), albumDto.CreatedBy.Trim(), HelperFunctions.ToDateTime(albumDto.DateAdded), albumDto.LastModifiedBy.Trim(), HelperFunctions.ToDateTime(albumDto.DateLastModified), albumDto.OwnedBy.Trim(), albumDto.OwnerRoleName.Trim(), albumDto.IsPrivate, true, albumDto.Metadata); //ToGalleryObjectMetadataItemCollection(album, albumDto.Metadata); //album.IsInflated = true; // Since we've just loaded this object from the data store, set the corresponding property. album.FullPhysicalPathOnDisk = album.FullPhysicalPath; return album; }