Skip to content

Commit

Permalink
Update WexflowEngine.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
aelassas committed Jul 4, 2023
1 parent e9d55ee commit 70d2b9e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 32 deletions.
30 changes: 15 additions & 15 deletions src/net/Wexflow.Core/WexflowEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,8 @@ private void LoadGlobalVariables()
{
var variable = new Variable
{
Key = xvariable.Attribute("name").Value,
Value = xvariable.Attribute("value").Value
Key = (xvariable.Attribute("name") ?? throw new InvalidOperationException("name attribute of global variable not found")).Value,
Value = (xvariable.Attribute("value") ?? throw new InvalidOperationException("value attribute of global variable not found")).Value
};
variables.Add(variable);
}
Expand All @@ -381,7 +381,7 @@ private static string GetWexflowSetting(XDocument xdoc, string name)
{
try
{
var xValue = xdoc.XPathSelectElement($"/Wexflow/Setting[@name='{name}']").Attribute("value");
var xValue = (xdoc.XPathSelectElement($"/Wexflow/Setting[@name='{name}']") ?? throw new InvalidOperationException($"setting {name} not found")).Attribute("value");
return xValue == null ? throw new Exception("Wexflow Setting Value attribute not found.") : xValue.Value;
}
catch (Exception e)
Expand Down Expand Up @@ -464,7 +464,7 @@ public string SaveWorkflow(string userId, UserProfile userProfile, string xml, b
}

var xdoc = XDocument.Parse(xml);
var id = int.Parse(xdoc.XPathSelectElement("/wf:Workflow", xmlNamespaceManager).Attribute("id").Value);
var id = int.Parse(((xdoc.XPathSelectElement("/wf:Workflow", xmlNamespaceManager) ?? throw new InvalidOperationException()).Attribute("id") ?? throw new InvalidOperationException("id attribute of workflow not found")).Value);
var workflow = Workflows.FirstOrDefault(w => w.Id == id);

if (workflow == null) // insert
Expand Down Expand Up @@ -1456,11 +1456,11 @@ public string SaveRecord(string recordId, Record record, List<Db.Version> versio
}
var destPath = Path.Combine(destDir, fileName);
File.Move(version.FilePath, destPath);
var parentDir = Path.GetDirectoryName(version.FilePath);
var parentDir = Path.GetDirectoryName(version.FilePath) ?? throw new InvalidOperationException("parentDir is null");
if (IsDirectoryEmpty(parentDir))
{
Directory.Delete(parentDir);
var recordTempDir = Directory.GetParent(parentDir).FullName;
var recordTempDir = (Directory.GetParent(parentDir) ?? throw new InvalidOperationException("parentDir is null")).FullName;
if (IsDirectoryEmpty(recordTempDir))
{
Directory.Delete(recordTempDir);
Expand Down Expand Up @@ -1500,11 +1500,11 @@ public string SaveRecord(string recordId, Record record, List<Db.Version> versio
File.Delete(version.FilePath);
}

var versionDir = Path.GetDirectoryName(version.FilePath);
var versionDir = Path.GetDirectoryName(version.FilePath) ?? throw new InvalidOperationException("versionDir is null");
if (IsDirectoryEmpty(versionDir))
{
Directory.Delete(versionDir);
var recordDir = Directory.GetParent(versionDir).FullName;
var recordDir = (Directory.GetParent(versionDir) ?? throw new InvalidOperationException("versionDir is null")).FullName;
if (IsDirectoryEmpty(recordDir))
{
Directory.Delete(recordDir);
Expand All @@ -1527,11 +1527,11 @@ public string SaveRecord(string recordId, Record record, List<Db.Version> versio
}
var destPath = Path.Combine(destDir, fileName);
File.Move(version.FilePath, destPath);
var parentDir = Path.GetDirectoryName(version.FilePath);
var parentDir = Path.GetDirectoryName(version.FilePath) ?? throw new InvalidOperationException("parentDir is null");
if (IsDirectoryEmpty(parentDir))
{
Directory.Delete(parentDir);
var recordTempDir = Directory.GetParent(parentDir).FullName;
var recordTempDir = (Directory.GetParent(parentDir) ?? throw new InvalidOperationException("parentDir is null")).FullName;
if (IsDirectoryEmpty(recordTempDir))
{
Directory.Delete(recordTempDir);
Expand Down Expand Up @@ -1570,11 +1570,11 @@ public string SaveRecordFromFile(string filePath, string createdBy)
}
var destPath = Path.Combine(destDir, fileName);
File.Move(filePath, destPath);
var parentDir = Path.GetDirectoryName(destPath);
var parentDir = Path.GetDirectoryName(destPath) ?? throw new InvalidOperationException("parentDir is null");
if (IsDirectoryEmpty(parentDir))
{
Directory.Delete(parentDir);
var recordTempDir = Directory.GetParent(parentDir).FullName;
var recordTempDir = (Directory.GetParent(parentDir) ?? throw new InvalidOperationException("parentDir is null")).FullName;
if (IsDirectoryEmpty(recordTempDir))
{
Directory.Delete(recordTempDir);
Expand Down Expand Up @@ -1611,7 +1611,7 @@ public bool DeleteRecords(string[] recordIds)
Database.DeleteRecords(recordIds);
foreach (var recordId in recordIds)
{
var versions = Database.GetVersions(recordId);
var versions = Database.GetVersions(recordId).ToArray();
var versionIds = versions.Select(v => v.GetDbId()).ToArray();
Database.DeleteVersions(versionIds);
foreach (var version in versions)
Expand All @@ -1620,11 +1620,11 @@ public bool DeleteRecords(string[] recordIds)
{
File.Delete(version.FilePath);

var versionDir = Path.GetDirectoryName(version.FilePath);
var versionDir = Path.GetDirectoryName(version.FilePath) ?? throw new InvalidOperationException("versionDir is null");
if (IsDirectoryEmpty(versionDir))
{
Directory.Delete(versionDir);
var recordDir = Directory.GetParent(versionDir).FullName;
var recordDir = (Directory.GetParent(versionDir) ?? throw new InvalidOperationException("versionDir is null")).FullName;
if (IsDirectoryEmpty(recordDir))
{
Directory.Delete(recordDir);
Expand Down
34 changes: 17 additions & 17 deletions src/netcore/Wexflow.Core/WexflowEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,8 @@ private void LoadGlobalVariables()
{
Variable variable = new()
{
Key = xvariable.Attribute("name").Value,
Value = xvariable.Attribute("value").Value
Key = (xvariable.Attribute("name") ?? throw new InvalidOperationException("name attribute of global variable not found")).Value,
Value = (xvariable.Attribute("value") ?? throw new InvalidOperationException("value attribute of global variable not found")).Value
};
variables.Add(variable);
}
Expand All @@ -376,7 +376,7 @@ private static string GetWexflowSetting(XDocument xdoc, string name)
{
try
{
var xValue = xdoc.XPathSelectElement($"/Wexflow/Setting[@name='{name}']").Attribute("value");
var xValue = (xdoc.XPathSelectElement($"/Wexflow/Setting[@name='{name}']") ?? throw new InvalidOperationException($"setting {name} not found")).Attribute("value");
return xValue == null ? throw new Exception("Wexflow Setting Value attribute not found.") : xValue.Value;
}
catch (Exception e)
Expand Down Expand Up @@ -458,7 +458,7 @@ public string SaveWorkflow(string userId, UserProfile userProfile, string xml, b
}

var xdoc = XDocument.Parse(xml);
var id = int.Parse(xdoc.XPathSelectElement("/wf:Workflow", xmlNamespaceManager).Attribute("id").Value);
var id = int.Parse(((xdoc.XPathSelectElement("/wf:Workflow", xmlNamespaceManager) ?? throw new InvalidOperationException()).Attribute("id") ?? throw new InvalidOperationException("id attribute of workflow not found")).Value);
var workflow = Workflows.FirstOrDefault(w => w.Id == id);

if (workflow == null) // insert
Expand Down Expand Up @@ -1441,19 +1441,19 @@ public string SaveRecord(string recordId, Record record, List<Db.Version> versio
// Move version file from temp folder to Records folder.
if (version.FilePath.Contains(RecordsTempFolder))
{
var fileName = Path.GetFileName(version.FilePath);
var fileName = Path.GetFileName(version.FilePath) ?? throw new InvalidOperationException("fileName is null");
var destDir = Path.Combine(RecordsFolder, DbFolderName, id, versionId);
if (!Directory.Exists(destDir))
{
_ = Directory.CreateDirectory(destDir);
}
var destPath = Path.Combine(destDir, fileName);
File.Move(version.FilePath, destPath);
var parentDir = Path.GetDirectoryName(version.FilePath);
var parentDir = Path.GetDirectoryName(version.FilePath) ?? throw new InvalidOperationException("parentDir is null");
if (IsDirectoryEmpty(parentDir))
{
Directory.Delete(parentDir);
var recordTempDir = Directory.GetParent(parentDir).FullName;
var recordTempDir = (Directory.GetParent(parentDir) ?? throw new InvalidOperationException("parentDir is null")).FullName;
if (IsDirectoryEmpty(recordTempDir))
{
Directory.Delete(recordTempDir);
Expand Down Expand Up @@ -1493,11 +1493,11 @@ public string SaveRecord(string recordId, Record record, List<Db.Version> versio
File.Delete(version.FilePath);
}

var versionDir = Path.GetDirectoryName(version.FilePath);
var versionDir = Path.GetDirectoryName(version.FilePath) ?? throw new InvalidOperationException("versionDir is null");
if (IsDirectoryEmpty(versionDir))
{
Directory.Delete(versionDir);
var recordDir = Directory.GetParent(versionDir).FullName;
var recordDir = (Directory.GetParent(versionDir) ?? throw new InvalidOperationException("versionDir is null")).FullName;
if (IsDirectoryEmpty(recordDir))
{
Directory.Delete(recordDir);
Expand All @@ -1512,19 +1512,19 @@ public string SaveRecord(string recordId, Record record, List<Db.Version> versio
var versionId = Database.InsertVersion(version);

// Move version file from temp folder to Records folder.
var fileName = Path.GetFileName(version.FilePath);
var fileName = Path.GetFileName(version.FilePath) ?? throw new InvalidOperationException("fileName is null");
var destDir = Path.Combine(RecordsFolder, DbFolderName, recordId, versionId);
if (!Directory.Exists(destDir))
{
_ = Directory.CreateDirectory(destDir);
}
var destPath = Path.Combine(destDir, fileName);
File.Move(version.FilePath, destPath);
var parentDir = Path.GetDirectoryName(version.FilePath);
var parentDir = Path.GetDirectoryName(version.FilePath) ?? throw new InvalidOperationException("parentDir is null");
if (IsDirectoryEmpty(parentDir))
{
Directory.Delete(parentDir);
var recordTempDir = Directory.GetParent(parentDir).FullName;
var recordTempDir = (Directory.GetParent(parentDir) ?? throw new InvalidOperationException("parentDir is null")).FullName;
if (IsDirectoryEmpty(recordTempDir))
{
Directory.Delete(recordTempDir);
Expand Down Expand Up @@ -1563,11 +1563,11 @@ public string SaveRecordFromFile(string filePath, string createdBy)
}
var destPath = Path.Combine(destDir, fileName);
File.Move(filePath, destPath);
var parentDir = Path.GetDirectoryName(destPath);
var parentDir = Path.GetDirectoryName(destPath) ?? throw new InvalidOperationException("parentDir is null");
if (IsDirectoryEmpty(parentDir))
{
Directory.Delete(parentDir);
var recordTempDir = Directory.GetParent(parentDir).FullName;
var recordTempDir = (Directory.GetParent(parentDir) ?? throw new InvalidOperationException("parentDir is null")).FullName;
if (IsDirectoryEmpty(recordTempDir))
{
Directory.Delete(recordTempDir);
Expand Down Expand Up @@ -1604,7 +1604,7 @@ public bool DeleteRecords(string[] recordIds)
Database.DeleteRecords(recordIds);
foreach (var recordId in recordIds)
{
var versions = Database.GetVersions(recordId);
var versions = Database.GetVersions(recordId).ToArray();
var versionIds = versions.Select(v => v.GetDbId()).ToArray();
Database.DeleteVersions(versionIds);
foreach (var version in versions)
Expand All @@ -1613,11 +1613,11 @@ public bool DeleteRecords(string[] recordIds)
{
File.Delete(version.FilePath);

var versionDir = Path.GetDirectoryName(version.FilePath);
var versionDir = Path.GetDirectoryName(version.FilePath) ?? throw new InvalidOperationException("versionDir is null");
if (IsDirectoryEmpty(versionDir))
{
Directory.Delete(versionDir);
var recordDir = Directory.GetParent(versionDir).FullName;
var recordDir = (Directory.GetParent(versionDir) ?? throw new InvalidOperationException("versionDir is null")).FullName;
if (IsDirectoryEmpty(recordDir))
{
Directory.Delete(recordDir);
Expand Down

0 comments on commit 70d2b9e

Please sign in to comment.