Skip to content

Commit

Permalink
fixing namespace for relationships and making it backwards compatible… (
Browse files Browse the repository at this point in the history
#197)

* fixing namespace for relationships and making it backwards compatible (#690)

* added fix for bug regarding two relationship entries

* Support for "aas-suppl" from V2 to V3

---------

Co-authored-by: AlexanderWollbrink <alexander.wollbrink@iosb-ina.fraunhofer.de>
Co-authored-by: juileetikekar <juilee.tikekar@idtwin.org>
  • Loading branch information
3 people authored Apr 17, 2024
1 parent 1ef5fa4 commit 003f5c3
Showing 1 changed file with 116 additions and 13 deletions.
129 changes: 116 additions & 13 deletions src/AasxCsharpLibrary/AdminShellPackageEnv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ private static (AasCore.Aas3_0.Environment, Package) LoadPackageAasx(string fn,
// get the origin from the package
PackagePart originPart = null;
var xs = package.GetRelationshipsByType(
"http://www.admin-shell.io/aasx/relationships/aasx-origin");
"http://admin-shell.io/aasx/relationships/aasx-origin");
foreach (var x in xs)
if (x.SourceUri.ToString() == "/")
{
Expand All @@ -394,11 +394,26 @@ private static (AasCore.Aas3_0.Environment, Package) LoadPackageAasx(string fn,
}

if (originPart == null)
throw (new Exception("Unable to find AASX origin. Aborting!"));
xs = package.GetRelationshipsByType(
"http://www.admin-shell.io/aasx/relationships/aasx-origin");
foreach (var x in xs)
if (x.SourceUri.ToString() == "/")
{
//originPart = package.GetPart(x.TargetUri);
var absoluteURI = PackUriHelper.ResolvePartUri(x.SourceUri, x.TargetUri);
if (package.PartExists(absoluteURI))
{
originPart = package.GetPart(absoluteURI);
}
break;
}
if (originPart == null)
throw (new Exception("Unable to find AASX origin. Aborting!"));

// get the specs from the package
//first try to find it without www, the "updated" ns
PackagePart specPart = null;
xs = originPart.GetRelationshipsByType("http://www.admin-shell.io/aasx/relationships/aas-spec");
xs = originPart.GetRelationshipsByType("http://admin-shell.io/aasx/relationships/aas-spec");
foreach (var x in xs)
{
//specPart = package.GetPart(x.TargetUri);
Expand All @@ -410,9 +425,23 @@ private static (AasCore.Aas3_0.Environment, Package) LoadPackageAasx(string fn,
break;
}

if (specPart == null)
throw (new Exception("Unable to find AASX spec(s). Aborting!"));

//if its still null, then try with the old
//this is to make it backwards compatible
if (specPart == null) {
xs = originPart.GetRelationshipsByType("http://www.admin-shell.io/aasx/relationships/aas-spec");
foreach (var x in xs)
{
//specPart = package.GetPart(x.TargetUri);
var absoluteURI = PackUriHelper.ResolvePartUri(x.SourceUri, x.TargetUri);
if (package.PartExists(absoluteURI))
{
specPart = package.GetPart(absoluteURI);
}
break;
}
if (specPart == null)
throw (new Exception("Unable to find AASX spec(s). Aborting!"));
}
// open spec part to read
try
{
Expand Down Expand Up @@ -752,6 +781,26 @@ public bool SaveAs(string fn, bool writeFreshly = false, SerializationFormat pre
PackagePart originPart = null;
var xs = package.GetRelationshipsByType(
"http://www.admin-shell.io/aasx/relationships/aasx-origin");
foreach (var x in xs)
if (x.SourceUri.ToString() == "/")
{
//originPart = package.GetPart(x.TargetUri);

var absoluteURI = PackUriHelper.ResolvePartUri(x.SourceUri, x.TargetUri);
if (package.PartExists(absoluteURI))
{
originPart = package.GetPart(absoluteURI);
}
//delete old type, because its not according to spec or something
//then replace with the current type
package.DeleteRelationship(x.Id);
package.CreateRelationship(
originPart.Uri, TargetMode.Internal,
"http://admin-shell.io/aasx/relationships/aasx-origin");
originPart = null;
break;
}
xs = package.GetRelationshipsByType("http://admin-shell.io/aasx/relationships/aasx-origin");
foreach (var x in xs)
if (x.SourceUri.ToString() == "/")
{
Expand All @@ -776,7 +825,7 @@ public bool SaveAs(string fn, bool writeFreshly = false, SerializationFormat pre
}
package.CreateRelationship(
originPart.Uri, TargetMode.Internal,
"http://www.admin-shell.io/aasx/relationships/aasx-origin");
"http://admin-shell.io/aasx/relationships/aasx-origin");
}

// get the specs from the package
Expand All @@ -792,8 +841,30 @@ public bool SaveAs(string fn, bool writeFreshly = false, SerializationFormat pre
{
specPart = package.GetPart(absoluteURI);
}
//delete old type, because its not according to spec or something
//then replace with the current type
originPart.DeleteRelationship(x.Id);
originPart.CreateRelationship(
specPart.Uri, TargetMode.Internal,
"http://admin-shell.io/aasx/relationships/aas-spec");
specPart = null;
specRel = null;
break;
}
xs = originPart.GetRelationshipsByType("http://admin-shell.io/aasx/relationships/aas-spec");
foreach (var x in xs)
{
specRel = x;
//specPart = package.GetPart(x.TargetUri);
var absoluteURI = PackUriHelper.ResolvePartUri(x.SourceUri, x.TargetUri);
if (package.PartExists(absoluteURI))
{
specPart = package.GetPart(absoluteURI);
}

break;
}


// check, if we have to change the spec part
if (specPart != null && specRel != null)
Expand Down Expand Up @@ -836,7 +907,7 @@ public bool SaveAs(string fn, bool writeFreshly = false, SerializationFormat pre
System.Net.Mime.MediaTypeNames.Text.Xml, CompressionOption.Maximum);
originPart.CreateRelationship(
specPart.Uri, TargetMode.Internal,
"http://www.admin-shell.io/aasx/relationships/aas-spec");
"http://admin-shell.io/aasx/relationships/aas-spec");
}

// now, specPart shall be != null!
Expand Down Expand Up @@ -875,6 +946,34 @@ public bool SaveAs(string fn, bool writeFreshly = false, SerializationFormat pre
}
}

//Handling of aas_suppl namespace from v2 to v3
//Need to check/test in detail, with thumbnails as well
if(specPart != null)
{

xs = specPart.GetRelationshipsByType("http://www.admin-shell.io/aasx/relationships/aas-suppl");
if(xs != null)
{
foreach(var x in xs.ToList())
{
var uri = x.TargetUri;
PackagePart filePart = null;
var absoluteURI = PackUriHelper.ResolvePartUri(x.SourceUri, x.TargetUri);
if (package.PartExists(absoluteURI))
{
filePart = package.GetPart(absoluteURI);
}
//delete old type, because its not according to spec or something
//then replace with the current type
specPart.DeleteRelationship(x.Id);
specPart.CreateRelationship(
filePart.Uri, TargetMode.Internal,
"http://admin-shell.io/aasx/relationships/aas-suppl");
}
}
}


// there might be pending files to be deleted (first delete, then add,
// in case of identical files in both categories)
foreach (var psfDel in _pendingFilesToDelete)
Expand All @@ -883,7 +982,10 @@ public bool SaveAs(string fn, bool writeFreshly = false, SerializationFormat pre
var found = false;

// normal files
xs = specPart.GetRelationshipsByType("http://www.admin-shell.io/aasx/relationships/aas-suppl");
xs = specPart.GetRelationshipsByType("http://admin-shell.io/aasx/relationships/aas-suppl");
//if its not found, use the "old" namespace, this is to make it backwards compatible
if(xs == null) xs = specPart.GetRelationshipsByType("http://www.admin-shell.io/aasx/relationships/aas-suppl");

foreach (var x in xs)
if (x.TargetUri == psfDel.Uri)
{
Expand Down Expand Up @@ -933,8 +1035,8 @@ public bool SaveAs(string fn, bool writeFreshly = false, SerializationFormat pre
PackagePart filePart = null;
if (psfAdd.SpecialHandling == AdminShellPackageSupplementaryFile.SpecialHandlingType.None)
{
xs = specPart.GetRelationshipsByType(
"http://www.admin-shell.io/aasx/relationships/aas-suppl");
xs = specPart.GetRelationshipsByType("http://admin-shell.io/aasx/relationships/aas-suppl");
if(xs == null) xs = specPart.GetRelationshipsByType("http://www.admin-shell.io/aasx/relationships/aas-suppl");
foreach (var x in xs)
if (x.TargetUri == psfAdd.Uri)
{
Expand Down Expand Up @@ -984,7 +1086,7 @@ public bool SaveAs(string fn, bool writeFreshly = false, SerializationFormat pre
AdminShellPackageSupplementaryFile.SpecialHandlingType.None)
specPart.CreateRelationship(
filePart.Uri, TargetMode.Internal,
"http://www.admin-shell.io/aasx/relationships/aas-suppl");
"http://admin-shell.io/aasx/relationships/aas-suppl");
if (psfAdd.SpecialHandling ==
AdminShellPackageSupplementaryFile.SpecialHandlingType.EmbedAsThumbnail)
package.CreateRelationship(
Expand Down Expand Up @@ -1434,7 +1536,7 @@ public ListOfAasSupplementaryFile GetListOfSupplementaryFiles()
// get the origin from the package
PackagePart originPart = null;
xs = _openPackage.GetRelationshipsByType(
"http://www.admin-shell.io/aasx/relationships/aasx-origin");
"http://admin-shell.io/aasx/relationships/aasx-origin");
foreach (var x in xs)
if (x.SourceUri.ToString() == "/")
{
Expand Down Expand Up @@ -1467,6 +1569,7 @@ public ListOfAasSupplementaryFile GetListOfSupplementaryFiles()
{
// get the supplementaries from the package, derived from spec
xs = specPart.GetRelationshipsByType("http://www.admin-shell.io/aasx/relationships/aas-suppl");
if(xs == null) xs = specPart.GetRelationshipsByType("http://www.admin-shell.io/aasx/relationships/aas-suppl");
foreach (var x in xs)
{
result.Add(
Expand Down

0 comments on commit 003f5c3

Please sign in to comment.