From 4f115a8e677d0b3b37c5c74277184e5da95bced4 Mon Sep 17 00:00:00 2001 From: adecler Date: Sun, 5 Nov 2023 17:58:14 +0000 Subject: [PATCH 1/2] Add support for stream in the adapter --- Excel_Adapter/AdapterActions/Pull.cs | 2 +- Excel_Adapter/AdapterActions/Push.cs | 98 +++++++++++++++++++++------- Excel_Adapter/CRUD/Read/Read.cs | 11 +++- Excel_Adapter/ExcelAdapter.cs | 21 ++++++ Excel_Adapter/Excel_Adapter.csproj | 12 +--- 5 files changed, 108 insertions(+), 36 deletions(-) diff --git a/Excel_Adapter/AdapterActions/Pull.cs b/Excel_Adapter/AdapterActions/Pull.cs index ee93142..07159ee 100644 --- a/Excel_Adapter/AdapterActions/Pull.cs +++ b/Excel_Adapter/AdapterActions/Pull.cs @@ -42,7 +42,7 @@ public override IEnumerable Pull(IRequest request = null, PullType pullO if (request == null) request = new CellValuesRequest(); - if (!File.Exists(m_FileSettings.GetFullFileName())) + if (m_FileSettings != null && !File.Exists(m_FileSettings.GetFullFileName())) { BH.Engine.Base.Compute.RecordError("No file exists under the location specified in the settings."); return new List(); diff --git a/Excel_Adapter/AdapterActions/Push.cs b/Excel_Adapter/AdapterActions/Push.cs index 8143de6..ec090d5 100644 --- a/Excel_Adapter/AdapterActions/Push.cs +++ b/Excel_Adapter/AdapterActions/Push.cs @@ -43,6 +43,7 @@ public partial class ExcelAdapter : BHoMAdapter public override List Push(IEnumerable objects, string tag = "", PushType pushType = PushType.AdapterDefault, ActionConfig actionConfig = null) { + // Make sure there are objects to push if (objects == null || !objects.Any()) { BH.Engine.Base.Compute.RecordError("No objects were provided for Push action."); @@ -50,6 +51,13 @@ public override List Push(IEnumerable objects, string tag = "", } objects = objects.Where(x => x != null).ToList(); + // Make sure an output stream has been provided if the input is read through a stream + if (m_InputStream != null && m_OutputStream == null) + { + BH.Engine.Base.Compute.RecordError("Please set the Stream for the output to enable the push to work correctly."); + return new List(); + } + // If unset, set the pushType to AdapterSettings' value (base AdapterSettings default is FullCRUD). if (pushType == PushType.AdapterDefault) pushType = PushType.DeleteThenCreate; @@ -74,29 +82,15 @@ public override List Push(IEnumerable objects, string tag = "", } // Check if the workbook exists and create it if not. - string fileName = m_FileSettings.GetFullFileName(); - XLWorkbook workbook; - if (!File.Exists(fileName)) - { - if (pushType == PushType.UpdateOnly) - { - BH.Engine.Base.Compute.RecordError($"There is no workbook to update under {fileName}"); - return new List(); - } - - workbook = new XLWorkbook(); - } + XLWorkbook workbook = null; + if (m_FileSettings != null) + workbook = CreateWorkbookFromFile(m_FileSettings.GetFullFileName(), pushType); + else if (m_InputStream != null) + workbook = CreateWorkbookFromStream(m_InputStream); else { - try - { - workbook = new XLWorkbook(fileName); - } - catch (Exception e) - { - BH.Engine.Base.Compute.RecordError($"The existing workbook could not be accessed due to the following error: {e.Message}"); - return new List(); - } + BH.Engine.Base.Compute.RecordError("File settings or template stream have not been provided."); + return new List(); } // Split the tables into collections to delete, create and update. @@ -148,7 +142,20 @@ public override List Push(IEnumerable objects, string tag = "", try { Update(workbook, config.WorkbookProperties); - workbook.SaveAs(fileName); + + if (m_FileSettings != null) + workbook.SaveAs(m_FileSettings.GetFullFileName()); + else if (m_OutputStream != null) + { + workbook.SaveAs(m_OutputStream); + m_OutputStream.Position = 0; + } + else + { + BH.Engine.Base.Compute.RecordError("Output stream has not been provided. The workbook cannot be saved."); + return new List(); + } + return success ? objects.ToList() : new List(); } catch (Exception e) @@ -162,6 +169,53 @@ public override List Push(IEnumerable objects, string tag = "", /**** Private Methods ****/ /***************************************************/ + private XLWorkbook CreateWorkbookFromFile(string fileName, PushType pushType) + { + XLWorkbook workbook = null; + if (!File.Exists(fileName)) + { + if (pushType == PushType.UpdateOnly) + { + BH.Engine.Base.Compute.RecordError($"There is no workbook to update under {fileName}"); + return null; + } + + workbook = new XLWorkbook(); + } + else + { + try + { + workbook = new XLWorkbook(fileName); + } + catch (Exception e) + { + BH.Engine.Base.Compute.RecordError($"The existing workbook could not be accessed due to the following error: {e.Message}"); + return null; + } + } + + return workbook; + } + + /***************************************************/ + + private XLWorkbook CreateWorkbookFromStream(Stream inputStream) + { + try + { + return new XLWorkbook(inputStream); + } + catch (Exception e) + { + BH.Engine.Base.Compute.RecordError($"The existing workbook could not be accessed due to the following error: {e.Message}"); + return null; + } + } + + + /***************************************************/ + private List ToTableRows(List objects, List properties, List propertiesToIgnore, bool goDeep = false, bool transposeTable = false, bool showPropertyNames = true) { // Get the property dictionary for the object diff --git a/Excel_Adapter/CRUD/Read/Read.cs b/Excel_Adapter/CRUD/Read/Read.cs index 84f4649..0b4638b 100644 --- a/Excel_Adapter/CRUD/Read/Read.cs +++ b/Excel_Adapter/CRUD/Read/Read.cs @@ -48,9 +48,14 @@ protected override IEnumerable Read(IRequest request, ActionConfig XLWorkbook workbook = null; try { - FileStream fileStream = new FileStream(m_FileSettings.GetFullFileName(), FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete); - workbook = new XLWorkbook(fileStream); - fileStream.Close(); + if (m_FileSettings != null) + { + FileStream fileStream = new FileStream(m_FileSettings.GetFullFileName(), FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete); + workbook = new XLWorkbook(fileStream); + fileStream.Close(); + } + else + workbook = new XLWorkbook(m_InputStream); } catch { diff --git a/Excel_Adapter/ExcelAdapter.cs b/Excel_Adapter/ExcelAdapter.cs index 9e169fc..fa216fb 100644 --- a/Excel_Adapter/ExcelAdapter.cs +++ b/Excel_Adapter/ExcelAdapter.cs @@ -63,6 +63,23 @@ public ExcelAdapter(BH.oM.Adapter.FileSettings fileSettings = null) VerifySecurityEvidenceForIsolatedStorage(this.GetType().Assembly); } + /***************************************************/ + + [Description("Adapter to create a new Excel file based on an existing template.")] + [Input("templateStream", "Defines the content of the iput Excel file. This content will not be modifed")] + [Output("outputStream", "Defines the content of the new Excel file. This will be generated on a push and is not required for a pull.")] + public ExcelAdapter(Stream inputStream, Stream outputStream = null) + { + if (inputStream == null) + { + BH.Engine.Base.Compute.RecordError("Please set the Stream for the template to enable the Excel Adapter to work correctly."); + return; + } + + m_InputStream = inputStream; + m_OutputStream = outputStream; + } + /***************************************************/ /**** Override Methods ****/ /***************************************************/ @@ -124,6 +141,10 @@ private void VerifySecurityEvidenceForIsolatedStorage(Assembly assembly) private BH.oM.Adapter.FileSettings m_FileSettings = null; + private Stream m_InputStream = null; + + private Stream m_OutputStream = null; + /***************************************************/ } } diff --git a/Excel_Adapter/Excel_Adapter.csproj b/Excel_Adapter/Excel_Adapter.csproj index a1ed175..d79380c 100644 --- a/Excel_Adapter/Excel_Adapter.csproj +++ b/Excel_Adapter/Excel_Adapter.csproj @@ -23,7 +23,7 @@ - + @@ -83,22 +83,14 @@ - - - - - + - - - - From f1f88acbb118a96076f1a1ceff2ddc3df669bf5a Mon Sep 17 00:00:00 2001 From: Fraser Greenroyd Date: Tue, 7 Nov 2023 10:39:32 +0000 Subject: [PATCH 2/2] Update Excel_Adapter/ExcelAdapter.cs --- Excel_Adapter/ExcelAdapter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Excel_Adapter/ExcelAdapter.cs b/Excel_Adapter/ExcelAdapter.cs index fa216fb..740775b 100644 --- a/Excel_Adapter/ExcelAdapter.cs +++ b/Excel_Adapter/ExcelAdapter.cs @@ -66,7 +66,7 @@ public ExcelAdapter(BH.oM.Adapter.FileSettings fileSettings = null) /***************************************************/ [Description("Adapter to create a new Excel file based on an existing template.")] - [Input("templateStream", "Defines the content of the iput Excel file. This content will not be modifed")] + [Input("inputStream", "Defines the content of the input Excel file. This content will not be modified.")] [Output("outputStream", "Defines the content of the new Excel file. This will be generated on a push and is not required for a pull.")] public ExcelAdapter(Stream inputStream, Stream outputStream = null) {