Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for stream in the adapter #41

Merged
merged 2 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Excel_Adapter/AdapterActions/Pull.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public override IEnumerable<object> 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<IBHoMObject>();
Expand Down
98 changes: 76 additions & 22 deletions Excel_Adapter/AdapterActions/Push.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,21 @@ public partial class ExcelAdapter : BHoMAdapter

public override List<object> Push(IEnumerable<object> 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.");
return new List<object>();
}
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<object>();
}

// If unset, set the pushType to AdapterSettings' value (base AdapterSettings default is FullCRUD).
if (pushType == PushType.AdapterDefault)
pushType = PushType.DeleteThenCreate;
Expand All @@ -74,29 +82,15 @@ public override List<object> Push(IEnumerable<object> 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<object>();
}

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<object>();
}
BH.Engine.Base.Compute.RecordError("File settings or template stream have not been provided.");
return new List<object>();
}

// Split the tables into collections to delete, create and update.
Expand Down Expand Up @@ -148,7 +142,20 @@ public override List<object> Push(IEnumerable<object> 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<object>();
}

return success ? objects.ToList() : new List<object>();
}
catch (Exception e)
Expand All @@ -162,6 +169,53 @@ public override List<object> Push(IEnumerable<object> 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<TableRow> ToTableRows(List<object> objects, List<string> properties, List<string> propertiesToIgnore, bool goDeep = false, bool transposeTable = false, bool showPropertyNames = true)
{
// Get the property dictionary for the object
Expand Down
11 changes: 8 additions & 3 deletions Excel_Adapter/CRUD/Read/Read.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,14 @@
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
{
Expand Down Expand Up @@ -245,23 +250,23 @@
List<string> customProperties = typeof(CustomObject).GetProperties().Select(x => x.Name).ToList();
List<string> keys = rows.First().Content.Select(x => x.ToString()).ToList();

return rows.Skip(1).Select(row =>
{
CustomObject result = new CustomObject();

Dictionary<string, object> item = new Dictionary<string, object>();
for (int i = 0; i < Math.Min((int)keys.Count(), (int)row.Content?.Count()); i ++)
{
if (customProperties.Contains(keys[i]))
result.SetPropertyValue(keys[i], row.Content[i]);
else
item[keys[i]] = row.Content[i];
}

result.CustomData = item;
return result;

}).ToList<IBHoMObject>();

Check warning on line 269 in Excel_Adapter/CRUD/Read/Read.cs

View check run for this annotation

BHoMBot-CI / code-compliance

Excel_Adapter/CRUD/Read/Read.cs#L253-L269

The use of CustomData within the code is discouraged except in circumstances where volatile data is being used. - For more information see https://bhom.xyz/documentation/DevOps/Code%20Compliance%20and%20CI/Compliance%20Checks/IsUsingCustomData
}

/***************************************************/
Expand Down
21 changes: 21 additions & 0 deletions Excel_Adapter/ExcelAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,23 @@
VerifySecurityEvidenceForIsolatedStorage(this.GetType().Assembly);
}

/***************************************************/

[Description("Adapter to create a new Excel file based on an existing template.")]
[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)

Check warning on line 71 in Excel_Adapter/ExcelAdapter.cs

View check run for this annotation

BHoMBot-CI / documentation-compliance

Excel_Adapter/ExcelAdapter.cs#L71

Input parameter requires a matching Input attribute - For more information see https://bhom.xyz/documentation/DevOps/Code%20Compliance%20and%20CI/Compliance%20Checks/IsInputAttributePresent
{
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 ****/
/***************************************************/
Expand Down Expand Up @@ -124,6 +141,10 @@

private BH.oM.Adapter.FileSettings m_FileSettings = null;

private Stream m_InputStream = null;

private Stream m_OutputStream = null;

/***************************************************/
}
}
Expand Down
12 changes: 2 additions & 10 deletions Excel_Adapter/Excel_Adapter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</PropertyGroup>

<Target Name="PostBuild" AfterTargets="PostBuildEvent" Condition="'$(Configuration)'=='Debug' Or '$(Configuration)'=='Release'">
<Exec Command="xcopy &quot;$(TargetPath)&quot; &quot;$(ProgramData)\BHoM\Assemblies&quot; /C /Y&#xD;&#xA;xcopy &quot;$(TargetDir)ClosedXML.dll&quot; &quot;$(ProgramData)\BHoM\Assemblies&quot; /Y&#xD;&#xA;xcopy &quot;$(TargetDir)ExcelNumberFormat.dll&quot; &quot;$(ProgramData)\BHoM\Assemblies&quot; /Y&#xD;&#xA;xcopy &quot;$(TargetDir)DocumentFormat.OpenXml.dll&quot; &quot;$(ProgramData)\BHoM\Assemblies&quot; /Y" />
<Exec Command="xcopy &quot;$(TargetPath)&quot; &quot;$(ProgramData)\BHoM\Assemblies&quot; /C /Y&#xD;&#xA;xcopy &quot;$(TargetDir)ClosedXML.dll&quot; &quot;$(ProgramData)\BHoM\Assemblies&quot; /Y" />
</Target>

<ItemGroup>
Expand Down Expand Up @@ -83,22 +83,14 @@

<ItemGroup>
<PackageReference Include="ClosedXML" Version="0.95.0" />
<PackageReference Include="DocumentFormat.OpenXml" Version="2.7.2" />
<PackageReference Include="ExcelNumberFormat" Version="1.0.10" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="System.IO.Packaging" Version="4.0.0" />
<PackageReference Include="System.Security.AccessControl" Version="6.0.2-mauipre.1.22102.15" />
<PackageReference Include="System.Security.Permissions" Version="8.0.0-preview.2.23128.3" />
<PackageReference Include="System.Security.Permissions" Version="8.0.0-rc.2.23479.6" />
</ItemGroup>

<ItemGroup Condition="'$(Configuration)'=='Debug' Or '$(Configuration)'=='Release'">
<PackageReference Include="System.IO.FileSystem.Primitives" Version="4.0.1" />
</ItemGroup>

<ItemGroup Condition="'$(Configuration)'=='ZeroCodeTool'">
<PackageReference Include="System.IO.FileSystem.Primitives" Version="4.3.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Excel_Engine\Excel_Engine.csproj" />
<ProjectReference Include="..\Excel_oM\Excel_oM.csproj" />
Expand Down
Loading