/// <summary> /// Restore a specific file version as the current version. /// </summary> /// <param name="versioningFileItem">A versioning file instance containing the version to restore.</param> /// <param name="versionId">Version id of the version to restore.</param> public static void RestoreVersion(IVersioningFile versioningFileItem, string versionId) { Validate.RequiredParameter("versioningFileItem", versioningFileItem); if (String.IsNullOrEmpty(versionId)) { throw new ArgumentException("versionId can not be null or empty", "versionId"); } ThrowIfCheckedOut(versioningFileItem); IList <UnifiedVersion> versions = versioningFileItem.GetVersions(); if (versions.Count == 1) { throw new InvalidVersioningOperationException("Can not restore version when only one version exists."); } UnifiedVersion targetVersion = versions.SingleOrDefault <UnifiedVersion>(v => v.Id.ToString() == versionId); if (targetVersion == null) { throw new VersionNotFoundException(versionId); } var query = from version in versions orderby version.Changed descending select version; UnifiedVersion lastVersion = query.First <UnifiedVersion>(); if (lastVersion.Id.ToString() == targetVersion.Id.ToString()) { throw new InvalidVersioningOperationException("The version to be restored is already the currently active version."); } targetVersion.Restore(); }
/// <summary> /// Ensures that versioning file can be updated. /// </summary> /// <param name="file">The file.</param> private static void ThrowIfCheckedOut(IVersioningFile file) { Validate.RequiredParameter("file", file); if (IsCheckedOutBySomeoneElse(file)) { throw new FileIsCheckedOutException(file.CheckedOutBy); } }
/// <summary> /// Determines whether the currently logged on user can undo a check out of the specified file. /// </summary> /// <param name="file">The file.</param> /// <returns> /// <c>true</c> if the user is allowed to undo check out of the specified file; otherwise, <c>false</c>. /// </returns> /// <remarks>Administrators are always allowed to undo a check out files</remarks> public static bool CanUndoCheckOut(VirtualFileBase file) { Validate.RequiredParameter("file", file); IVersioningFile versioningFile = file as IVersioningFile; if (versioningFile != null) { // Admins are allowed to undo other users check outs return((versioningFile.IsCheckedOut && PrincipalInfo.HasAdminAccess) || IsCheckedOutByCurrentUser(versioningFile)); } return(false); }
/// <summary> /// Determines whether the currently logged on user can check out the specified file. /// </summary> /// <param name="file">The file.</param> /// <returns> /// <c>true</c> if the currently logged on user is permitted to check out the specified file; otherwise, <c>false</c>. /// </returns> public static bool CanCheckOut(VirtualFileBase file) { Validate.RequiredParameter("virtualFileBase", file); if (!CanEdit(file)) { return(false); } IVersioningFile versioningFile = file as IVersioningFile; return(versioningFile == null ? false : !versioningFile.IsCheckedOut); }
/// <summary> /// Ensures that versioning directory can be updated. /// </summary> /// <param name="directory">The directory.</param> private static void ThrowIfCheckedOut(VersioningDirectory directory) { Validate.RequiredParameter("directory", directory); foreach (IVersioningFile file in directory.Files) { ThrowIfCheckedOut(file); } foreach (VersioningDirectory childDirectory in directory.Directories) { ThrowIfCheckedOut(childDirectory); } }
/// <summary> /// Deletes a specific file version. /// </summary> /// <param name="versioningFileItem">File version.</param> /// <param name="versionId">File version Id.</param> public static void DeleteVersion(IVersioningFile versioningFileItem, string versionId) { Validate.RequiredParameter("versioningFileItem", versioningFileItem); if (String.IsNullOrEmpty(versionId)) { throw new ArgumentException("versionId can not be null or empty", "versionId"); } ThrowIfCheckedOut(versioningFileItem); IList <UnifiedVersion> versions = versioningFileItem.GetVersions(); if (versions.Count == 1) { throw new InvalidVersioningOperationException(); } UnifiedVersion targetVersion = versions.SingleOrDefault <UnifiedVersion>(v => v.Id.ToString() == versionId); if (targetVersion == null) { throw new VersionNotFoundException(versionId); } targetVersion.Delete(); }
/// <summary> /// Determines whether can perform operations with versions of the specified file. /// </summary> /// <param name="file">The file.</param> /// <returns> /// <c>true</c> if can perform operations with versions of the specified file; otherwise, <c>false</c>. /// </returns> public static bool CanChangeVersions(IVersioningFile file) { Validate.RequiredParameter("file", file); return(file.GetVersions().Count > 1 && CanEdit(file as VirtualFileBase)); }