/// <summary> /// Export a byte stream stored on a record in the Version table to a file /// </summary> /// <param name="dbContext"></param> /// <param name="exportParentFolder"></param> /// <param name="demand"></param> /// <param name="attachment"></param> private static void ExportFile(IServerDBContext dbContext, string exportParentFolder, ObjectDBE demand, FolderContentsDBE attachment) { string safeObjectName = string.Join("_", demand.ObjectName.Split(System.IO.Path.GetInvalidFileNameChars())); string folderName = System.IO.Path.Combine(exportParentFolder, safeObjectName); // 1. Optionally Create Demand Folder if (!System.IO.Directory.Exists(folderName)) { System.IO.Directory.CreateDirectory(folderName); } // 2. Query version table var version = dbContext.Versions.Where(o => o.ObjectID == attachment.ObjectID).FirstOrDefault(); if (version == null) { throw new ApplicationException($"Version record: [{attachment.ObjectID}] does not exist!"); } // 3. get the file contents if (version.VersionImage == null) { return; } byte[] fileContents = version.VersionImage; // 4. build the target file path name string filePathName = System.IO.Path.Combine(folderName, $"{version.ObjectName}{version.FileExtension}"); // 5. Optionally delete the file if it exists from a previous export run if (System.IO.File.Exists(filePathName)) { System.IO.File.Delete(filePathName); } // 6. Export the file System.IO.File.WriteAllBytes(filePathName, fileContents); Console.WriteLine($"==> Exported: {filePathName}"); }
/// <summary> /// Query IServer DB and export the denands and their contents /// </summary> private static void ExportDemands(bool isCheckMode) { // create the Entity Framework (EF) DB Context var optionsBuilder = new DbContextOptionsBuilder <IServerDBContext>(); using (var dbContext = new IServerDBContext(optionsBuilder.Options)) { // turn off all change tracking dbContext.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking; // ============================================================= // 1. query the objects table and get a list of the demand folders // ============================================================= var demands = dbContext.Objects.Where(o => o.ObjectTypeID == OBJECT_TYPE_ID_FOR_DEMANDS).ToList(); // sanity check on what we found if (!isCheckMode) { Console.WriteLine($"Total Demands: {demands.Count}"); } // ============================================================= // 2. loop thru each demand folder and find its contents // ============================================================= int demandCtr = 1; int checkedOutFilesCount = 0; foreach (var demand in demands) { if (!isCheckMode) { Console.WriteLine($"Demand: [{demandCtr}] {demand.ObjectName}"); } // exec the sproc to get the contents of the demand folder /* * USE [iServer_TOGAF] * GO * * DECLARE @return_value int * * EXEC @return_value = [copypaste].[uspFolderContentsSelect] * @FolderId = 'AD835305-0062-46AF-B391-5239FD05EFB0', * @ProfileID = 54 * * SELECT 'Return Value' = @return_value * * GO */ List <FolderContentsDBE> attachments = null; dbContext.LoadStoredProc("copypaste.uspFolderContentsSelect") .AddParam("FolderId", demand.ObjectID.ToString()) .AddParam("ProfileID", ISERVER_USER_ID) .ReturnValue(out IOutParam <int> retParam) .Exec(r => attachments = r.ToList <FolderContentsDBE>()); // ============================================================= // 3. loop thru the folder contents // ============================================================= foreach (var attachment in attachments) { if (!isCheckMode) { Console.WriteLine($" Attachment: {attachment.ObjectID} [{attachment.ObjectName}{attachment.FileExtension}] Is Checked Out: {attachment.IsCheckedOut}"); ExportFile(dbContext, EXPORT_FOLDER_PATHNAME, demand, attachment); } else if (attachment.IsCheckedOut.HasValue && attachment.IsCheckedOut.Value) { var user = dbContext.Users.Where(u => u.UserID == attachment.CheckedOutUserId).FirstOrDefault(); if (user == null) { throw new ApplicationException($"UserId [{attachment.CheckedOutUserId}] cannot be found!"); } Console.WriteLine($"{attachment.ObjectName}{attachment.FileExtension} is Checked Out by: [{user.Username}]"); checkedOutFilesCount++; } } if (!isCheckMode) { Console.WriteLine($"==> Total Attachments: {attachments.Count}"); Console.WriteLine(); } demandCtr++; } } }