Skip to content

Commit ebefedc

Browse files
committed
* fixes to package saving
1 parent a440f7a commit ebefedc

File tree

2 files changed

+83
-50
lines changed

2 files changed

+83
-50
lines changed

src/AasxCsharpLibrary/AdminShellPackageEnv.cs

+81-49
Original file line numberDiff line numberDiff line change
@@ -334,20 +334,34 @@ private class FindRelTuple
334334
public PackageRelationship Rel { get; set; }
335335
}
336336

337-
private static IEnumerable<FindRelTuple> FindAllRelationships(Package package, string[] relTypes)
337+
private static IEnumerable<FindRelTuple> FindAllRelationships(
338+
Package package, string[] relTypes,
339+
bool? filterForDeprecatedEquals = null)
338340
{
339-
for (int i=0; i<relTypes.Length; i++)
341+
for (int i = 0; i < relTypes.Length; i++)
340342
foreach (var x in package.GetRelationshipsByType(relTypes[i]))
341-
yield return new FindRelTuple() { Deprecated = ( i > 0 ), Rel = x };
343+
{
344+
var res = new FindRelTuple() { Deprecated = (i > 0), Rel = x };
345+
if (filterForDeprecatedEquals.HasValue &&
346+
filterForDeprecatedEquals.Value != res.Deprecated)
347+
continue;
348+
yield return res;
349+
}
342350
}
343351

344352
private static IEnumerable<FindRelTuple> FindAllRelationships(
345353
PackagePart part, string[] relTypes,
346-
bool onlyDeprecated = false)
354+
bool? filterForDeprecatedEquals = null)
347355
{
348-
for (int i = (onlyDeprecated ? 1 : 0); i < relTypes.Length; i++)
356+
for (int i = 0; i < relTypes.Length; i++)
349357
foreach (var x in part.GetRelationshipsByType(relTypes[i]))
350-
yield return new FindRelTuple() { Deprecated = (i > 0), Rel = x };
358+
{
359+
var res = new FindRelTuple() { Deprecated = (i > 0), Rel = x };
360+
if (filterForDeprecatedEquals.HasValue &&
361+
filterForDeprecatedEquals.Value != res.Deprecated)
362+
continue;
363+
yield return res;
364+
}
351365
}
352366

353367
private static AasCore.Aas3_0.Environment LoadXml(string fn)
@@ -772,41 +786,52 @@ public bool SaveAs(string fn, bool writeFreshly = false, SerializationFormat pre
772786
}
773787
_fn = fn;
774788

775-
// get the origin from the package
776-
PackagePart originPart = null;
777-
foreach (var x in FindAllRelationships(package, relTypesOrigin))
789+
// first check, if we have deprecated origin
790+
foreach (var x in FindAllRelationships(package, relTypesOrigin,
791+
filterForDeprecatedEquals: true))
778792
if (x.Rel.SourceUri.ToString() == "/")
779793
{
780-
//originPart = package.GetPart(x.TargetUri);
781-
782794
var absoluteURI = PackUriHelper.ResolvePartUri(x.Rel.SourceUri, x.Rel.TargetUri);
783795
if (package.PartExists(absoluteURI))
784796
{
785-
originPart = package.GetPart(absoluteURI);
797+
var tempPart = package.GetPart(absoluteURI);
786798

787-
// check if it a deprecated URI
788-
if (x.Deprecated)
789-
{
790-
//delete old type, because its not according to spec or something
791-
//then replace with the current type
792-
package.DeleteRelationship(x.Rel.Id);
793-
package.DeletePart(absoluteURI);
794-
//package.CreateRelationship(
795-
// originPart.Uri, TargetMode.Internal,
796-
// relTypesOrigin.FirstOrDefault());
797-
originPart = null;
798-
break;
799-
}
799+
// delete old type, because its not according to spec or something
800+
// then replace with the current type
801+
package.DeleteRelationship(x.Rel.Id);
802+
package.CreateRelationship(
803+
tempPart.Uri, TargetMode.Internal,
804+
relTypesOrigin.FirstOrDefault());
805+
break;
800806
}
801807
}
802808

809+
// now check, if the origin could be found with correct relationships
810+
PackagePart originPart = null;
811+
foreach (var x in FindAllRelationships(package, relTypesOrigin,
812+
filterForDeprecatedEquals: false))
813+
if (x.Rel.SourceUri.ToString() == "/")
814+
{
815+
var absoluteURI = PackUriHelper.ResolvePartUri(x.Rel.SourceUri, x.Rel.TargetUri);
816+
if (package.PartExists(absoluteURI))
817+
{
818+
originPart = package.GetPart(absoluteURI);
819+
}
820+
}
821+
803822
// MIHO, 2024-05-29
804823
// fix the case, that part exists but is not really associated by a
805824
// relationship
806825
var uriOrigin = new Uri("/aasx/aasx-origin", UriKind.RelativeOrAbsolute);
807-
if (package.PartExists(uriOrigin))
826+
if (originPart == null && package.PartExists(uriOrigin))
808827
{
828+
// get the part
809829
originPart = package.GetPart(uriOrigin);
830+
831+
// make the relationship
832+
package.CreateRelationship(
833+
originPart.Uri, TargetMode.Internal,
834+
relTypesOrigin.FirstOrDefault());
810835
}
811836

812837
if (originPart == null)
@@ -825,35 +850,40 @@ public bool SaveAs(string fn, bool writeFreshly = false, SerializationFormat pre
825850
relTypesOrigin.FirstOrDefault());
826851
}
827852

828-
// get the specs from the package
853+
// get the specs from the package, first again: deprecated
854+
foreach (var x in FindAllRelationships(
855+
originPart, relTypesSpec,
856+
filterForDeprecatedEquals: true))
857+
{
858+
var absoluteURI = PackUriHelper.ResolvePartUri(x.Rel.SourceUri, x.Rel.TargetUri);
859+
if (package.PartExists(absoluteURI))
860+
{
861+
var tempPart = package.GetPart(absoluteURI);
862+
863+
//delete old type, because its not according to spec or something
864+
//then replace with the current type
865+
originPart.DeleteRelationship(x.Rel.Id);
866+
originPart.CreateRelationship(
867+
tempPart.Uri, TargetMode.Internal,
868+
relTypesSpec.FirstOrDefault());
869+
break;
870+
}
871+
}
872+
873+
// now check, if the specs could be found with correct relationships
829874
PackagePart specPart = null;
830875
PackageRelationship specRel = null;
831-
foreach (var x in FindAllRelationships(originPart, relTypesSpec))
876+
foreach (var x in FindAllRelationships(originPart, relTypesSpec,
877+
filterForDeprecatedEquals: false))
832878
{
833879
specRel = x.Rel;
834-
//specPart = package.GetPart(x.TargetUri);
835880
var absoluteURI = PackUriHelper.ResolvePartUri(x.Rel.SourceUri, x.Rel.TargetUri);
836881
if (package.PartExists(absoluteURI))
837882
{
838883
specPart = package.GetPart(absoluteURI);
839-
840-
// check if it a deprecated URI
841-
if (x.Deprecated)
842-
{
843-
//delete old type, because its not according to spec or something
844-
//then replace with the current type
845-
package.DeleteRelationship(x.Rel.Id);
846-
package.DeletePart(absoluteURI);
847-
//package.CreateRelationship(
848-
// specPart.Uri, TargetMode.Internal,
849-
// relTypesSpec.FirstOrDefault());
850-
specPart = null;
851-
specRel = null;
852-
break;
853-
}
854884
}
855885
}
856-
886+
857887
// check, if we have to change the spec part
858888
if (specPart != null && specRel != null)
859889
{
@@ -941,11 +971,13 @@ public bool SaveAs(string fn, bool writeFreshly = false, SerializationFormat pre
941971
}
942972
}
943973

944-
//Handling of aas_suppl namespace from v2 to v3
945-
//Need to check/test in detail, with thumbnails as well
946-
if(specPart != null)
974+
// Handling of aas_suppl namespace from v2 to v3
975+
// Need to check/test in detail, with thumbnails as well
976+
if (specPart != null)
947977
{
948-
foreach (var x in FindAllRelationships(specPart, relTypesSuppl, onlyDeprecated: true).ToList())
978+
foreach (var x in FindAllRelationships(
979+
specPart, relTypesSuppl,
980+
filterForDeprecatedEquals: true).ToList())
949981
{
950982
var uri = x.Rel.TargetUri;
951983
PackagePart filePart = null;

src/AasxPackageExplorer/options-debug.MIHO.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
// "AasxToLoad": "C:\\HOMI\\Develop\\Aasx\\repo\\samm-test\\Run2_DNP_plus_CD_SM_step_2.aasx",
3232
// "AasxToLoad": "C:\\HOMI\\Develop\\Aasx\\repo\\Test1.aasx",
3333
// "AasxToLoad": "C:\\HOMI\\Develop\\Aasx\\repo\\samm-test\\SMT_and_SAMM_Showcase_v02.aasx",
34-
"AasxToLoad": "C:\\Users\\homi0002\\Desktop\\SMT_TechData_Work\\IDTA 02003-1-2_SubmodelTemplate_TechnicalData_v1.3_workingFile_MIHO.aasx",
34+
// "AasxToLoad": "C:\\Users\\homi0002\\Desktop\\SMT_TechData_Work\\IDTA 02003-1-2_SubmodelTemplate_TechnicalData_v1.3_workingFile_MIHO.aasx",
35+
"AasxToLoad": "C:\\Users\\homi0002\\Desktop\\tmp\\f.aasx",
3536
// "AasxToLoad": "C:\\Users\\homi0002\\Desktop\\tmp\\SMT_and_SAMM_Showcase_v02_spiel.aasx",
3637
// "AasxToLoad": "C:\\Users\\homi0002\\Desktop\\tmp\\SMT_ProductChangeNotification_Draft_v19_spiel.aasx",
3738
// "AasxToLoad": "C:\\Users\\homi0002\\Desktop\\tmp\\00_FestoDemoBox-Module-2-Kopie.aasx",

0 commit comments

Comments
 (0)