Skip to content

Commit

Permalink
[BMSPT-300] Handling empty string fields during 2.1 conversion to 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielLepold committed Jul 12, 2024
1 parent e8a367a commit a2d0bba
Show file tree
Hide file tree
Showing 2 changed files with 248 additions and 41 deletions.
142 changes: 101 additions & 41 deletions src/bcf-toolkit/Converter/Bcf21/SchemaConverterToBcf30.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,15 @@ public static Model.Bcf30.Bcf Convert(Model.Bcf21.Bcf from) {
.ToList()));

var project = from.Project;
if (project != null)
builder.SetProject(p => p
.SetProjectId(project.Project.ProjectId)
.SetProjectName(project.Project.Name));

if (project != null) {
builder.SetProject(p => {
p.SetProjectId(project.Project.ProjectId);
if (project.Project.Name != string.Empty) {
p.SetProjectName(project.Project.Name);
}
});
}

return builder.Build();
}
Expand All @@ -38,89 +43,131 @@ private static Model.Bcf30.Markup ConvertMarkup(Model.Bcf21.Markup from) {
var topic = from.Topic;
builder
.AddHeaderFiles(from.Header.Select(ConvertHeaderFile).ToList())
.AddReferenceLinks(topic.ReferenceLink.ToList())
.SetTitle(topic.Title)
.SetPriority(topic.Priority)
.AddReferenceLinks(topic.ReferenceLink.Where(referenceLink => !string.IsNullOrEmpty(referenceLink)).ToList())
.SetIndex(topic.Index)
.AddLabels(topic.Labels.ToList())
.SetTitle(from.Topic.Title)
.AddLabels(topic.Labels.Where(label => !string.IsNullOrEmpty(label)).ToList())
.SetCreationDate(topic.CreationDate)
.SetCreationAuthor(topic.CreationAuthor)
.SetCreationAuthor(from.Topic.CreationAuthor)
.SetModifiedDate(topic.ModifiedDate)
.SetModifiedAuthor(topic.ModifiedAuthor)
.SetDueDate(topic.DueDate)
.SetAssignedTo(topic.AssignedTo)
.SetStage(topic.Stage)
.SetDescription(topic.Description)
.AddRelatedTopics(topic.RelatedTopic.Select(t => t.Guid).ToList())
.AddComments(from.Comment.Select(ConvertComment).ToList())
.AddViewPoints(from.Viewpoints.Select(ConvertViewPoint).ToList())
.SetGuid(from.Topic.Guid)
.SetTopicType(from.Topic.TopicType ??= "ERROR")
.SetTopicStatus(from.Topic.TopicStatus ??= "OPEN")
.SetTopicType(string.IsNullOrEmpty(from.Topic.TopicType) ? "ERROR" : from.Topic.TopicType)
.SetTopicStatus(string.IsNullOrEmpty(from.Topic.TopicStatus) ? "OPEN" : from.Topic.TopicStatus)
.AddDocumentReferences(from.Topic.DocumentReference
.Select(ConvertDocumentReference).ToList());

var bimSnippet = topic.BimSnippet;
if (bimSnippet != null)

if (bimSnippet != null) {
builder
.SetBimSnippet(b => b
.SetReference(bimSnippet.Reference)
.SetReferenceSchema(bimSnippet.ReferenceSchema)
.SetSnippetType(bimSnippet.SnippetType)
.SetIsExternal(bimSnippet.IsExternal));
}

if (from.Topic.Priority != string.Empty)
builder.SetPriority(from.Topic.Priority);

if (from.Topic.ModifiedAuthor != string.Empty)
builder.SetModifiedAuthor(from.Topic.ModifiedAuthor);

if (from.Topic.AssignedTo != string.Empty)
builder.SetAssignedTo(from.Topic.AssignedTo);

if (from.Topic.Stage != string.Empty)
builder.SetStage(from.Topic.Stage);

if (from.Topic.Description != string.Empty)
builder.SetDescription(from.Topic.Description);

return builder.Build();
}

private static Model.Bcf30.File ConvertHeaderFile(Model.Bcf21.HeaderFile from) {
var builder = new FileBuilder();
return builder
.SetFileName(from.Filename)

builder
.SetDate(from.Date)
.SetReference(from.Reference)
.SetIfcProject(from.IfcProject)
.SetIfcSpatialStructureElement(from.IfcSpatialStructureElement)
.SetIsExternal(from.IsExternal)
.Build();
.SetIsExternal(from.IsExternal);

if (from.Filename != string.Empty)
builder.SetFileName(from.Filename);

if (from.Reference != string.Empty)
builder.SetReference(from.Reference);

return builder.Build();
}

private static Model.Bcf30.DocumentReference ConvertDocumentReference(
Model.Bcf21.TopicDocumentReference from) {
var builder = new DocumentReferenceBuilder();
return builder
.SetDescription(from.Description)

builder
.SetGuid(from.Guid ??= Guid.NewGuid().ToString())
.SetUrl(from.IsExternal ? from.ReferencedDocument : null)
.SetDocumentGuid(from.IsExternal ? null : Guid.NewGuid().ToString()) //TODO: generate guid based on guid and description
.Build();
.SetDocumentGuid(from.IsExternal ? null : Guid.NewGuid().ToString()); //TODO: generate guid based on guid and description

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

return builder.Build();
}

private static Model.Bcf30.Comment ConvertComment(Model.Bcf21.Comment from) {
var builder = new CommentBuilder();
builder
.SetDate(from.Date)
.SetAuthor(from.Author)
.SetCommentProperty(from.CommentProperty)
.SetModifiedDate(from.ModifiedDate)
.SetModifiedAuthor(from.ModifiedAuthor)
.SetGuid(from.Guid);

if (from.Viewpoint != null)
builder.SetViewPointGuid(from.Viewpoint?.Guid);
if (from.ModifiedAuthor != string.Empty) {
builder.SetModifiedAuthor(from.ModifiedAuthor);
}

if (from.Viewpoint != null) {
builder
.SetViewPointGuid(from.Viewpoint?.Guid);

if (from.CommentProperty != string.Empty) {
builder.SetCommentProperty(from.CommentProperty);
}
}
else {
builder.SetCommentProperty(from.CommentProperty);
}

return builder.Build();
}

private static Model.Bcf30.ViewPoint ConvertViewPoint(
Model.Bcf21.ViewPoint from) {
return new Model.Bcf30.ViewPoint {
Viewpoint = from.Viewpoint,
Snapshot = from.Snapshot,
var viewPoint = new Model.Bcf30.ViewPoint {
SnapshotData = from.SnapshotData,
Index = from.Index,
Guid = from.Guid,
VisualizationInfo = ConvertVisualizationInfo(from.VisualizationInfo)
};

if (from.Viewpoint != string.Empty) {
viewPoint.Viewpoint = from.Viewpoint;
}

if (from.Snapshot != string.Empty) {
viewPoint.Snapshot = from.Snapshot;
}

return viewPoint;
}

private static Model.Bcf30.VisualizationInfo? ConvertVisualizationInfo(
Expand Down Expand Up @@ -205,11 +252,18 @@ private static Model.Bcf30.ViewPoint ConvertViewPoint(
private static Model.Bcf30.Component ConvertComponent(
Model.Bcf21.Component from) {
var builder = new ComponentBuilder();
return builder
.SetOriginatingSystem(from.OriginatingSystem)
.SetAuthoringToolId(from.AuthoringToolId)
.SetIfcGuid(from.IfcGuid)
.Build();
builder
.SetIfcGuid(from.IfcGuid);

if (from.OriginatingSystem != string.Empty) {
builder.SetOriginatingSystem(from.OriginatingSystem);
}

if (from.AuthoringToolId != string.Empty) {
builder.SetAuthoringToolId(from.AuthoringToolId);
}

return builder.Build();
}

private static Model.Bcf30.ComponentColoringColor ConvertColor(
Expand Down Expand Up @@ -281,10 +335,16 @@ private static Action<DocumentInfoBuilder> UpdateDocumentInfo(
private static Model.Bcf30.Document ConvertDocument(
Model.Bcf21.TopicDocumentReference docReference) {
var builder = new DocumentBuilder();
return builder
builder
.SetFileName(docReference.ReferencedDocument)
.SetDescription(docReference.Description)
.SetGuid(docReference.Guid)
.Build();
.SetGuid(docReference.Guid);

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

return builder.Build();


}
}
147 changes: 147 additions & 0 deletions src/tests/Builder/BcfBuilder21Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using BcfToolkit.Builder.Bcf21;
using BcfToolkit.Converter;
using BcfToolkit.Model;
using BcfToolkit.Model.Bcf21;
using NUnit.Framework;

namespace Tests.Builder;

public class BcfBuilder21Tests {
private BcfBuilder _builder = null!;


private IConverter _converter = null!;

[SetUp]
public void Setup() {
_builder = new BcfBuilder();
_converter = new BcfToolkit.Converter.Bcf21.Converter();
}

[Test]
public void EmptyFieldsConversationTest() {

var labels = new List<string> { "label1", string.Empty, "label2", };
var referenceLinks = new List<string> { "link1", string.Empty, string.Empty };

// Header
var headerBuilder = new HeaderFileBuilder();
var header =
headerBuilder
.SetDate(DateTime.Now)
.SetReference(string.Empty)
.SetFileName("Filename")
.SetIfcProject("1234567890123456789012")
.SetIfcSpatialStructureElement("1234567890123456789012")
.SetIsExternal(true)
.Build();
var headers = new List<HeaderFile> { header };

// DocumentReference
var topicDocumentReferenceBuilder = new DocumentReferenceBuilder();
var topicDocumentReference =
topicDocumentReferenceBuilder
.SetDescription(string.Empty)
.SetIsExternal(false)
.SetGuid("000b4df2-0187-49a9-8a4a-23992696bafd")
.SetReferencedDocument("ref_document")
.Build();
var topicDocumentReferences = new List<TopicDocumentReference> { topicDocumentReference };

// Comments
var commentBuilder1 = new CommentBuilder();
var commentBuilder2 = new CommentBuilder();
var commentPropMustHaveValue =
commentBuilder1
.SetGuid("999b4df2-0187-49a9-8a4a-23992696bafd")
.SetModifiedAuthor(string.Empty)
.SetDate(DateTime.Today)
.SetModifiedDate(DateTime.Today)
.SetAuthor("author1")
.SetCommentProperty("commProp1")
.Build();
var commentPropCanBeEmpty =
commentBuilder2
.SetGuid("999b4df2-0187-49a9-8a4a-23992696bafd")
.SetModifiedAuthor(string.Empty)
.SetDate(DateTime.Today)
.SetModifiedDate(DateTime.Today)
.SetAuthor("author2")
.SetViewPointGuid("111b4df2-0187-49a9-8a4a-23992696bafd")
.SetCommentProperty(string.Empty)
.Build();
var comments = new List<Comment> { commentPropMustHaveValue, commentPropCanBeEmpty };

// Viewpoint
var visualizationInfoBuilder = new VisualizationInfoBuilder();

var componentBuilder = new ComponentBuilder();
var component =
componentBuilder
.SetIfcGuid("1234567890123456789012")
.SetOriginatingSystem(string.Empty)
.SetAuthoringToolId(string.Empty)
.Build();
var components = new List<Component> { component };

var componentVisibility = new ComponentVisibility();
componentVisibility.DefaultVisibility = false;
componentVisibility.DefaultVisibilityValueSpecified = false;

var visualizationInfo =
visualizationInfoBuilder
.SetGuid("333b4df2-0187-49a9-8a4a-23992696bafd")
.SetVisibility(componentVisibility)
.AddSelections(components)
.Build();

var viewPointBuilder = new ViewPointBuilder();
var viewPoint =
viewPointBuilder
.SetGuid("444b4df2-0187-49a9-8a4a-23992696bafd")
.SetIndex(5)
.SetSnapshot(string.Empty)
.SetSnapshotData("snapshotdata1")
.SetViewPoint(string.Empty)
.SetVisualizationInfo(visualizationInfo)
.Build();

var viewPoints = new List<ViewPoint> { viewPoint };

var bcf = _builder
.AddMarkup(m => m
.AddHeaderFiles(headers)
.SetTitle("required field")
.SetPriority(string.Empty)
.SetGuid("3ffb4df2-0187-49a9-8a4a-23992696bafd")
.SetCreationAuthor("required field")
.SetModifiedAuthor(string.Empty)
.SetAssignedTo(string.Empty)
.SetStage(string.Empty)
.SetTopicType(string.Empty)
.SetTopicStatus(string.Empty)
.SetDescription(string.Empty)
.AddLabels(labels)
.AddReferenceLinks(referenceLinks)
.AddDocumentReferences(topicDocumentReferences)
.AddComments(comments)
.AddViewPoints(viewPoints))
.SetProject(p => p
.SetProjectId("3ZSh2muKX7S8MCESk95seC")
.SetProjectName(string.Empty)
.SetExtensionSchema(string.Empty))
.Build();

var res = _converter.ToBcf(bcf,
BcfVersionEnum.Bcf30);

if (res.Exception != null && res.Exception.Message.Length > 0)
Assert.Fail("Error message found: " + res.Exception.Message);

}
}

0 comments on commit a2d0bba

Please sign in to comment.