Skip to content

Commit

Permalink
[BMSPT-306] removed document info for bcf 2.1, added document data to…
Browse files Browse the repository at this point in the history
… topic reference
  • Loading branch information
BalintBende committed Jul 23, 2024
1 parent d69d60f commit 3b0c456
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 81 deletions.
6 changes: 6 additions & 0 deletions bcf-toolkit.sln.DotSettings.user
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=00488d9b_002Ddfb9_002D47fc_002Da7da_002D60da862eab61/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" Name="All tests from Solution #2" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;
&lt;Solution /&gt;
&lt;/SessionState&gt;</s:String>
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=64259359_002Daf01_002D4100_002Db5a8_002D06c7d51d9f83/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" Name="BuildMaximumInformationBcfFromStreamTest" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;
&lt;TestAncestor&gt;
&lt;TestId&gt;NUnit3x::8113526D-8A68-4E3E-B4DB-CE235875DDD1::net8.0::tests.Builder.Bcf21.BcfBuilderTests.BuildMaximumInformationBcfFromStreamTest&lt;/TestId&gt;
&lt;/TestAncestor&gt;
&lt;/SessionState&gt;</s:String>

<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=7d7fd643_002Dccec_002D4fc7_002D8938_002D22e83b255cb0/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" Name="BuildEmptyBcfFromStream" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;
&lt;TestAncestor&gt;
Expand All @@ -28,6 +33,7 @@
&lt;/SessionState&gt;</s:String>



<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=b2d5c176_002Dc751_002D42d4_002Da842_002D3ad7ad02438d/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="WorkerTests" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;
&lt;TestAncestor&gt;
&lt;TestId&gt;NUnit3x::8113526D-8A68-4E3E-B4DB-CE235875DDD1::net8.0::Tests.WorkerTests&lt;/TestId&gt;
Expand Down
49 changes: 20 additions & 29 deletions src/bcf-toolkit/Builder/Bcf21/BcfBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public async Task<Bcf> BuildFromStream(Stream source) {
_bcf.Markups =
await BcfExtensions.ParseMarkups<Markup, VisualizationInfo>(source);
_bcf.Project = await BcfExtensions.ParseProject<ProjectExtension>(source);
_bcf.DocumentInfo = ParseDocuments(
source,
SetDocumentData(
source,
_bcf.Markups
.SelectMany(m => m.Topic.DocumentReference)
.ToList());
Expand All @@ -32,7 +32,7 @@ public BcfBuilder SetProject(ProjectExtension? project) {
_bcf.Project = project;
return this;
}

/// <summary>
/// It turns the additional file data that are referenced as internal
/// document in markups into the DocumentInfo.
Expand All @@ -43,40 +43,31 @@ public BcfBuilder SetProject(ProjectExtension? project) {
/// </param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
private static DocumentInfo ParseDocuments(
Stream stream,
private static void SetDocumentData(
Stream stream,
List<TopicDocumentReference> documentReferences) {
if (stream is null || !stream.CanRead)
throw new ArgumentException("Source stream is not readable.");

var objType = typeof(DocumentInfo);
Log.Debug($"\nProcessing {objType.Name}\n");


Log.Debug($"\nProcessing documents\n");

using var archive = new ZipArchive(stream, ZipArchiveMode.Read, true);

var documentInfo = new DocumentInfo {
Documents = new Collection<Document>(documentReferences
.Where(d => !d.IsExternal)
.Select(d => new Document {
FileName = d.ReferencedDocument
})
.ToList()
)
};


// These additional files can be referenced by other files via their
// relative paths. It is recommended to put them in a folder called
// Documents in the root folder of the zip archive.
foreach (var document in documentInfo.Documents) {
var fileName = Path.GetFileName(document.FileName);
var entry = archive.DocumentEntry(fileName);
if(entry is null) continue;
Log.Debug(entry.FullName);
documentInfo.SetDocumentData(entry);
}

documentReferences
.Where(d => !d.IsExternal)
.ToList()
.ForEach(d => {
var fileName = Path.GetFileName(d.ReferencedDocument);
var entry = archive.DocumentEntry(fileName);
if (entry is null) return;
Log.Debug(entry.FullName);
d.SetDocumentData(entry);
});

// Stream must be positioned back to 0 in order to use it again
stream.Position = 0;
return documentInfo;
}
}
7 changes: 3 additions & 4 deletions src/bcf-toolkit/Converter/Bcf21/SchemaConverterToBcf30.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static Model.Bcf30.Bcf Convert(Model.Bcf21.Bcf from) {
.AddMarkups(from.Markups.Select(ConvertMarkup).ToList(), true)
.SetDocument(UpdateDocumentInfo(from.Markups
.SelectMany(m => m.Topic.DocumentReference)
.Where(r => !r.IsExternal)
// .Where(r => !r.IsExternal)
.ToList()));

var project = from.Project;
Expand Down Expand Up @@ -337,14 +337,13 @@ private static Model.Bcf30.Document ConvertDocument(
var builder = new DocumentBuilder();
builder
.SetFileName(docReference.ReferencedDocument)
.SetGuid(docReference.Guid);
.SetGuid(docReference.Guid)
.SetDocumentData(docReference.DocumentData);

if (docReference.Description != string.Empty) {
builder.SetDescription(docReference.Description);
}

return builder.Build();


}
}
4 changes: 1 addition & 3 deletions src/bcf-toolkit/Model/Bcf21/BcfExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
namespace BcfToolkit.Model.Bcf21;

public partial class Bcf {
public DocumentInfo? DocumentInfo { get; set; }
}
public partial class Bcf { }
38 changes: 0 additions & 38 deletions src/bcf-toolkit/Model/Bcf21/Documents.cs

This file was deleted.

26 changes: 24 additions & 2 deletions src/bcf-toolkit/Model/Bcf21/MarkupExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Xml.Serialization;
using BcfToolkit.Model.Interfaces;
using BcfToolkit.Utils;
using Newtonsoft.Json;

namespace BcfToolkit.Model.Bcf21;
Expand Down Expand Up @@ -46,7 +49,9 @@ public void SetViewPoint(
}
}

public partial class Topic : ITopic { }
public partial class Topic : ITopic {

}

public partial class ViewPoint : IViewPoint {
[XmlIgnore] public VisualizationInfo? VisualizationInfo { get; set; }
Expand All @@ -69,7 +74,24 @@ public void SetVisualizationInfo(IVisualizationInfo? visInfo) {

public partial class HeaderFile : IHeaderFile { }
public partial class BimSnippet : IBimSnippet { }
public partial class TopicDocumentReference : IDocReference { }

public partial class TopicDocumentReference : IDocReference {
/// <summary>
/// The document file data as base64 encoded string.
/// </summary>
[XmlIgnore]
[JsonProperty("document_data")]
public FileData DocumentData { get; set; }

public void SetDocumentData(ZipArchiveEntry entry) {
var fileName = Path.GetFileName(this.ReferencedDocument);
var mime = $"data:{MimeTypes.GetMimeType(fileName)};base64";
this.DocumentData = new FileData {
Mime = mime,
Data = entry.Data()
};
}
}

public partial class Comment : IComment {
// This method that controls the access to the `CommentViewpoint` instance.
Expand Down
2 changes: 1 addition & 1 deletion src/bcf-toolkit/Utils/BcfExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public static async Task<TDocumentInfo?>
LoadOptions.None,
CancellationToken.None);
documentInfo = document.BcfObject<TDocumentInfo>();

foreach (var entry in archive.DocumentEntries()) {
Log.Debug(entry.FullName);
documentInfo.SetDocumentData(entry);
Expand Down
4 changes: 2 additions & 2 deletions src/bcf-toolkit/Utils/ZipArchiveExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ public static void CreateEntryFromBytes(
using var entryStream = zipEntry.Open();
entryStream.Write(bytes, 0, bytes.Length);
}

public static List<ZipArchiveEntry> DocumentEntries(this ZipArchive @this) {
return @this
.Entries
.OrderBy(entry => entry.FullName)
.Where(entry => entry.IsDocumentsFolder())
.ToList();
}

public static ZipArchiveEntry? DocumentEntry(this ZipArchive @this, string fileName) {
return @this
.Entries
Expand Down
16 changes: 14 additions & 2 deletions src/tests/Builder/Bcf21/BcfBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using BcfToolkit.Builder.Bcf21;
using BcfToolkit.Converter;
Expand Down Expand Up @@ -145,7 +146,7 @@ public void EmptyFieldsConversationTest() {
Assert.Fail("Error message found: " + res.Exception.Message);

}

[Test]
public async Task BuildMaximumInformationBcfFromStreamTest() {
await using var stream = new FileStream(
Expand All @@ -154,6 +155,17 @@ public async Task BuildMaximumInformationBcfFromStreamTest() {
FileAccess.Read);
var bcf = await _builder.BuildFromStream(stream);
Assert.That(bcf.Markups.Count, Is.EqualTo(2));
Assert.That(bcf.DocumentInfo?.Documents.Count, Is.EqualTo(1));
var markup = bcf
.Markups
.FirstOrDefault(m =>
string.Equals(
m.Topic.Guid,
"7ddc3ef0-0ab7-43f1-918a-45e38b42369c"));
Assert.That(markup?
.Topic
.DocumentReference
.FirstOrDefault(d => !d.IsExternal)?
.DocumentData
.Mime, Is.EqualTo("data:application/xml;base64"));
}
}

0 comments on commit 3b0c456

Please sign in to comment.