/// <summary> /// Imports a specific XML file according to the import instructions. /// </summary> /// <param name="vault">The vault reference to use for processing the import.</param> /// <param name="importInstruction">The import instructions to execute.</param> /// <param name="fileToImport">The file to import.</param> /// <returns>A list of files which were attached to the object, for deletion.</returns> public List <FileInfo> ImportXmlFile(Vault vault, ImportInstruction importInstruction, FileInfo fileToImport) { // Sanity. if (null == vault) { throw new ArgumentNullException(nameof(vault)); } if (null == importInstruction) { throw new ArgumentNullException(nameof(importInstruction)); } if (null == fileToImport) { throw new ArgumentNullException(nameof(fileToImport)); } // Create a list of attached files (which can then be deleted later). var attachedFilesToDelete = new List <FileInfo>(); // Sanity. if (null == importInstruction) { throw new ArgumentNullException(nameof(importInstruction)); } if (null == fileToImport) { throw new ArgumentNullException(nameof(fileToImport)); } if (false == fileToImport.Exists) { throw new ArgumentException("The file does not exist on disk.", nameof(fileToImport)); } if (null == importInstruction.ObjectSelectors) { return(attachedFilesToDelete); } // Load the file contents into an XDocument. var xDocument = XDocument.Load(fileToImport.FullName); // Iterate over our selectors and find objects to import. foreach (var objectSelector in importInstruction.ObjectSelectors) { // Import the file. attachedFilesToDelete.AddRange(this.ImportXmlFile(vault, xDocument.Root, objectSelector, fileToImport)); } // Return the files to delete. return(attachedFilesToDelete); }
/// <summary> /// Executes an <see cref="ImportInstruction"/>, importing files to the vault as required. /// </summary> /// <param name="importInstruction">The import instruction to execute.</param> public void ImportXmlFile(ImportInstruction importInstruction) { // Sanity. if (null == importInstruction) { throw new ArgumentNullException(nameof(importInstruction)); } if (null == importInstruction.PathsToSearch) { return; } if (string.IsNullOrWhiteSpace(importInstruction.SearchPattern)) { return; } // Iterate over the configured paths to find appropriate files. foreach (var path in importInstruction.PathsToSearch) { // Attempt to find the path. var directoryInfo = new DirectoryInfo(path); if (false == directoryInfo.Exists) { continue; } // Identify the files by the search pattern. foreach (var file in directoryInfo.GetFiles(importInstruction.SearchPattern)) { // Execute the vault extension method (below). try { this.PermanentVault .ExtensionMethodOperations .ExecuteVaultExtensionMethod("ImportXmlFile", file.FullName); } catch { // Allow other files to import. } } } }
/// <summary> /// Handles an exception according to the <see cref="ImportInstruction.ImportExceptionHandlingStrategy"/>. /// </summary> /// <param name="importInstruction">The import instruction being processed.</param> /// <param name="fileBeingProcessed">The file which was being processed when the exception occurred.</param> /// <param name="importException">The exception raised.</param> public void HandleImportException( ImportInstruction importInstruction, FileInfo fileBeingProcessed, Exception importException) { // Sanity. if (null == importInstruction) { throw new ArgumentNullException(nameof(importInstruction)); } if (null == fileBeingProcessed) { throw new ArgumentNullException(nameof(fileBeingProcessed)); } if (null == importException) { throw new ArgumentNullException(nameof(importException)); } #if DEBUG System.Diagnostics.Debugger.Launch(); #endif // Report the error. SysUtils.ReportErrorMessageToEventLog($"Exception importing {fileBeingProcessed.FullName}", importException); // What is the exception handling strategy? try { switch (importInstruction.ImportExceptionHandlingStrategy) { case ImportExceptionHandlingStrategy.Delete: { // Delete the file. fileBeingProcessed.Delete(); break; } case ImportExceptionHandlingStrategy.MoveToSpecificFolder: { // Get a reference to the folder to move the file to. var targetDirectoryInfo = new DirectoryInfo(importInstruction.ExceptionFolderName); // Create it if it does not exist. if (false == targetDirectoryInfo.Exists) { targetDirectoryInfo.Create(); } // Move the file there. fileBeingProcessed.MoveTo( System.IO.Path.Combine(targetDirectoryInfo.FullName, fileBeingProcessed.Name) ); break; } case ImportExceptionHandlingStrategy.MoveToSubFolder: { // Get a reference to the folder to move the file to. var targetDirectoryInfo = new DirectoryInfo(System.IO.Path.Combine( fileBeingProcessed.DirectoryName, importInstruction.ExceptionSubFolderName)); // Create it if it does not exist. if (false == targetDirectoryInfo.Exists) { targetDirectoryInfo.Create(); } // Move the file there. fileBeingProcessed.MoveTo( System.IO.Path.Combine(targetDirectoryInfo.FullName, fileBeingProcessed.Name) ); break; } default: throw new InvalidOperationException( $"Unhandled import exception handling strategy: {importInstruction.ImportExceptionHandlingStrategy}"); } } catch (Exception e) { // Report the exception. SysUtils.ReportErrorMessageToEventLog( "An exception was thrown whilst trying to process the exception handling strategy.", e); } }