private NodeSetFileModel GetProfileModel(ModelNameAndVersion nameVersion, object userId) { NodeSetFileModel myModel = (nameVersion.CCacheId as NodeSetFileModel); if (myModel != null) { return(myModel); } var userToken = userId as UserToken; myModel = _dalNodeSetFile?.Where(s => s.FileName == nameVersion.ModelUri /*&& (TenantID == null || s.AuthorId == null || s.AuthorId == (int)TenantID)*/, userToken, verbose: true)?.Data?.OrderByDescending(s => s.PublicationDate)?.FirstOrDefault(); if (myModel != null) { nameVersion.CCacheId = myModel; } return(myModel); }
public bool GetNodeSet(UANodeSetImportResult results, ModelNameAndVersion nameVersion, object AuthorID) { var authorToken = AuthorID as UserToken; NodeSetFileModel myModel = GetProfileModel(nameVersion, authorToken); if (myModel != null) { // workaround for bug https://github.com/dotnet/runtime/issues/67622 var fileCachepatched = myModel.FileCache.Replace("<Value/>", "<Value xsi:nil='true' />"); using (var nodeSetStream = new System.IO.MemoryStream(Encoding.UTF8.GetBytes(fileCachepatched))) { //nodeSetStream.Write(Encoding.UTF8.GetBytes(myModel.FileCache)); //nodeSetStream.Position = 0; UANodeSet nodeSet = UANodeSet.Read(nodeSetStream); foreach (var ns in nodeSet.Models) { results.AddModelAndDependencies(nodeSet, ns, null, false); foreach (var model in results.Models) { if (model.NameVersion.CCacheId == null) { if (model.NameVersion.ModelUri == nameVersion.ModelUri && model.NameVersion.PublicationDate == nameVersion.PublicationDate) { model.NameVersion.CCacheId = myModel; } else { GetProfileModel(model.NameVersion, authorToken); } } } } } return(true); } return(false); }
public bool AddNodeSet(UANodeSetImportResult results, string nodeSetXml, object authorId) { bool WasNewSet = false; #region Comment Processing var doc = XElement.Load(new StringReader(nodeSetXml)); var comments = doc.DescendantNodes().OfType <XComment>(); foreach (XComment comment in comments) { //inline XML Commments are not showing here...only real XML comments (not file comments with /**/) //Unfortunately all OPC UA License Comments are not using XML Comments but file-comments and therefore cannot be "preserved" } #endregion UANodeSet nodeSet; // workaround for bug https://github.com/dotnet/runtime/issues/67622 var nodeSetXmlPatched = nodeSetXml.Replace("<Value/>", "<Value xsi:nil='true' />"); using (var nodesetBytes = new MemoryStream(Encoding.UTF8.GetBytes(nodeSetXmlPatched))) { nodeSet = UANodeSet.Read(nodesetBytes); } if (nodeSet.Models?.Any() != true) { nodeSet.Models = new ModelTableEntry[] { new ModelTableEntry { ModelUri = nodeSet.NamespaceUris?.FirstOrDefault(), RequiredModel = new ModelTableEntry[] { new ModelTableEntry { ModelUri = "http://opcfoundation.org/UA/" } }, } }; } UANodeSet tOldNodeSet = null; foreach (var ns in nodeSet.Models) { UserToken userToken = authorId as UserToken; var authorToken = userToken; bool isGlobalNodeSet = CESMII.ProfileDesigner.OpcUa.OpcUaImporter._coreNodeSetUris.Contains(ns.ModelUri); if (isGlobalNodeSet) { userToken = UserToken.GetGlobalUser(userToken); // Write as a global node set shared acess user authorToken = null; } NodeSetFileModel myModel = GetProfileModel( new ModelNameAndVersion { ModelUri = ns.ModelUri, ModelVersion = ns.Version, PublicationDate = ns.PublicationDate, }, userToken); if (myModel == null) { myModel = results.Models.FirstOrDefault(m => m.NameVersion.IsNewerOrSame(new ModelNameAndVersion { ModelUri = ns.ModelUri, ModelVersion = ns.Version, PublicationDate = ns.PublicationDate, } ))?.NameVersion?.CCacheId as NodeSetFileModel; } bool CacheNewerVersion = true; if (myModel != null) { CacheNewerVersion = false; // workaround for bug https://github.com/dotnet/runtime/issues/67622 var fileCachepatched = myModel.FileCache.Replace("<Value/>", "<Value xsi:nil='true' />"); using (var nodeSetStream = new MemoryStream(Encoding.UTF8.GetBytes(fileCachepatched))) { if (tOldNodeSet == null) { tOldNodeSet = UANodeSet.Read(nodeSetStream); } var tns = tOldNodeSet.Models.Where(s => s.ModelUri == ns.ModelUri).OrderByDescending(s => s.PublicationDate).FirstOrDefault(); if (tns == null || ns.PublicationDate > tns.PublicationDate) { CacheNewerVersion = true; //Cache the new NodeSet if the old (file) did not contain the model or if the version of the new model is greater } } } int? cacheId = myModel != null ? myModel.ID : 0; bool newInImport = false; if (CacheNewerVersion) //Cache only newer version { if (myModel == null) { myModel = new NodeSetFileModel { ID = cacheId, FileName = ns.ModelUri, Version = ns.Version, PublicationDate = ns.PublicationDate, // TODO clean up the dependency AuthorId = authorToken?.UserId, FileCache = nodeSetXml }; // Defer Upsert until later to make it part of a transaction // _dalNodeSetFile.Upsert(myModel, userToken, false); newInImport = true; } // Defer the updates to the import transaction //var resIns = _dalNodeSetFile.Upsert(nsModel, (AuthorID == null) ? 0 : (int)AuthorID, true).GetAwaiter().GetResult(); //cacheId = resIns.Item1; //newInImport = resIns.Item2; WasNewSet = true; } var tModel = results.AddModelAndDependencies(nodeSet, ns, null, WasNewSet); if (tModel?.NameVersion != null && myModel != null) { tModel.NameVersion.CCacheId = myModel; tModel.NewInThisImport = newInImport; } foreach (var model in results.Models) { if (model.NameVersion.CCacheId == null) { GetProfileModel(model.NameVersion, userToken); } } } return(WasNewSet); }