public EntryRevertedContract RevertToVersion(int archivedAlbumVersionId) { PermissionContext.VerifyPermission(PermissionToken.RestoreRevisions); return(HandleTransaction(session => { var archivedVersion = session.Load <ArchivedAlbumVersion>(archivedAlbumVersionId); var album = archivedVersion.Album; session.AuditLogger.SysLog("reverting " + album + " to version " + archivedVersion.Version); var fullProperties = ArchivedAlbumContract.GetAllProperties(archivedVersion); var warnings = new List <string>(); var diff = new AlbumDiff(); album.Description.Original = fullProperties.Description; album.Description.English = fullProperties.DescriptionEng ?? string.Empty; album.DiscType = fullProperties.DiscType; album.TranslatedName.DefaultLanguage = fullProperties.TranslatedName.DefaultLanguage; // Picture var versionWithPic = archivedVersion.GetLatestVersionWithField(AlbumEditableFields.Cover); if (versionWithPic != null) { album.CoverPictureData = versionWithPic.CoverPicture; album.CoverPictureMime = versionWithPic.CoverPictureMime; if (versionWithPic.CoverPicture != null) { var thumbGenerator = new ImageThumbGenerator(imagePersister); using (var stream = new MemoryStream(versionWithPic.CoverPicture.Bytes)) { var thumb = new EntryThumb(album, versionWithPic.CoverPictureMime); thumbGenerator.GenerateThumbsAndMoveImage(stream, thumb, ImageSizes.Thumb | ImageSizes.SmallThumb | ImageSizes.TinyThumb); } } } else { album.CoverPictureData = null; album.CoverPictureMime = null; } // Assume picture was changed if there's a version between the current version and the restored version where the picture was changed. diff.Cover.Set(!Equals(album.ArchivedVersionsManager.GetLatestVersionWithField(AlbumEditableFields.Cover, album.Version), versionWithPic)); // Original release album.OriginalRelease = (fullProperties.OriginalRelease != null ? new AlbumRelease(fullProperties.OriginalRelease, session.NullSafeLoad <ReleaseEvent>(fullProperties.OriginalRelease.ReleaseEvent)) : null); // Artists DatabaseContextHelper.RestoreObjectRefs <ArtistForAlbum, Artist, ArchivedArtistForAlbumContract>( session.OfType <Artist>(), warnings, album.AllArtists, fullProperties.Artists, (a1, a2) => (a1.Artist != null && a1.Artist.Id == a2.Id) || (a1.Artist == null && a2.Id == 0 && a1.Name == a2.NameHint), (artist, albumRef) => RestoreArtistRef(album, artist, albumRef), albumForArtist => albumForArtist.Delete()); // Songs DatabaseContextHelper.RestoreObjectRefs <SongInAlbum, Song, SongInAlbumRefContract>( session.OfType <Song>(), warnings, album.AllSongs, fullProperties.Songs, (a1, a2) => ((a1.Song != null && a1.Song.Id == a2.Id) || a1.Song == null && a2.Id == 0 && a1.Name == a2.NameHint), (song, songRef) => RestoreTrackRef(album, song, songRef), songInAlbum => songInAlbum.Delete()); // Names if (fullProperties.Names != null) { var nameDiff = album.Names.SyncByContent(fullProperties.Names, album); session.Sync(nameDiff); } // Weblinks if (fullProperties.WebLinks != null) { var webLinkDiff = WebLink.SyncByValue(album.WebLinks, fullProperties.WebLinks, album); session.Sync(webLinkDiff); } // PVs if (fullProperties.PVs != null) { var pvDiff = CollectionHelper.Diff(album.PVs, fullProperties.PVs, (p1, p2) => (p1.PVId == p2.PVId && p1.Service == p2.Service)); foreach (var pv in pvDiff.Added) { session.Save(album.CreatePV(new PVContract(pv))); } foreach (var pv in pvDiff.Removed) { pv.OnDelete(); session.Delete(pv); } } album.UpdateArtistString(); album.UpdateRatingTotals(); Archive(session, album, diff, AlbumArchiveReason.Reverted, string.Format("Reverted to version {0}", archivedVersion.Version)); AuditLog(string.Format("reverted {0} to revision {1}", EntryLinkFactory.CreateEntryLink(album), archivedVersion.Version), session); return new EntryRevertedContract(album, warnings); })); }
/// <summary> /// Reverts an album to an earlier archived version. /// </summary> /// <param name="archivedArtistVersionId">Id of the archived version to be restored.</param> /// <returns>Result of the revert operation, with possible warnings if any. Cannot be null.</returns> /// <remarks>Requires the RestoreRevisions permission.</remarks> public async Task <EntryRevertedContract> RevertToVersion(int archivedArtistVersionId) { PermissionContext.VerifyPermission(PermissionToken.RestoreRevisions); return(await HandleTransactionAsync(async session => { var archivedVersion = await session.LoadAsync <ArchivedArtistVersion>(archivedArtistVersionId); if (archivedVersion.Hidden) { PermissionContext.VerifyPermission(PermissionToken.ViewHiddenRevisions); } var artist = archivedVersion.Artist; session.AuditLogger.SysLog("reverting " + artist + " to version " + archivedVersion.Version); var fullProperties = ArchivedArtistContract.GetAllProperties(archivedVersion); var warnings = new List <string>(); var diff = new ArtistDiff(); artist.ArtistType = fullProperties.ArtistType; artist.Description.Original = fullProperties.Description; artist.Description.English = fullProperties.DescriptionEng ?? string.Empty; artist.TranslatedName.DefaultLanguage = fullProperties.TranslatedName.DefaultLanguage; artist.BaseVoicebank = DatabaseContextHelper.RestoreWeakRootEntityRef(session, warnings, fullProperties.BaseVoicebank); // Picture var versionWithPic = archivedVersion.GetLatestVersionWithField(ArtistEditableFields.Picture); if (versionWithPic != null) { artist.Picture = versionWithPic.Picture; artist.PictureMime = versionWithPic.PictureMime; if (versionWithPic.Picture != null) { var thumbGenerator = new ImageThumbGenerator(imagePersister); using (var stream = new MemoryStream(versionWithPic.Picture.Bytes)) { var thumb = new EntryThumb(artist, versionWithPic.PictureMime, ImagePurpose.Main); thumbGenerator.GenerateThumbsAndMoveImage(stream, thumb, ImageSizes.Thumb | ImageSizes.SmallThumb | ImageSizes.TinyThumb); } } } else { artist.Picture = null; artist.PictureMime = null; } // Assume picture was changed if there's a version between the current version and the restored version where the picture was changed. diff.Picture.Set(!Equals(artist.ArchivedVersionsManager.GetLatestVersionWithField(ArtistEditableFields.Picture, artist.Version), versionWithPic)); // Groups DatabaseContextHelper.RestoreObjectRefs( session, warnings, artist.AllGroups, fullProperties.Groups, (a1, a2) => (a1.Parent.Id == a2.Id), (grp, grpRef) => (!artist.HasGroup(grp) ? artist.AddGroup(grp, grpRef.LinkType) : null), groupForArtist => groupForArtist.Delete()); // Names if (fullProperties.Names != null) { var nameDiff = artist.Names.SyncByContent(fullProperties.Names, artist); await session.SyncAsync(nameDiff); } // Weblinks if (fullProperties.WebLinks != null) { var webLinkDiff = WebLink.SyncByValue(artist.WebLinks, fullProperties.WebLinks, artist); await session.SyncAsync(webLinkDiff); } await ArchiveAsync(session, artist, diff, ArtistArchiveReason.Reverted, string.Format("Reverted to version {0}", archivedVersion.Version)); await AuditLogAsync(string.Format("reverted {0} to revision {1}", entryLinkFactory.CreateEntryLink(artist), archivedVersion.Version), session); return new EntryRevertedContract(artist, warnings); })); }