diff --git a/Files/DataStandard/Vault.Custom/addinVault/Menus/powerGateEditConfiguration.ps1 b/Files/DataStandard/Vault.Custom/addinVault/Menus/powerGateEditConfiguration.ps1 index 1f0ee23..46b3a97 100644 --- a/Files/DataStandard/Vault.Custom/addinVault/Menus/powerGateEditConfiguration.ps1 +++ b/Files/DataStandard/Vault.Custom/addinVault/Menus/powerGateEditConfiguration.ps1 @@ -4,13 +4,8 @@ $modules = Get-ChildItem -path $commonModulePath -Recurse -Filter *.ps* $modules | ForEach-Object { Import-Module -Name $_.FullName -Global } $cfgPath = "c:\temp\powerGateCfg\powerGateConfiguration.xml" -[xml]$cfg = GetConfigFromVault +[xml]$cfg = Get-PowerGateConfigFromVault New-Item -Path "c:\temp\powerGateCfg" -ItemType Directory -Force -if ($null -eq $cfg) { - Copy-Item "C:\ProgramData\coolOrange\powerGate\powerGateConfigurationTemplate.xml" $cfgPath -Force -} -else { - $cfg.Save($cfgPath) -} +$cfg.Save($cfgPath) Start-Process -FilePath C:\Windows\explorer.exe -ArgumentList "/select, ""$cfgPath""" \ No newline at end of file diff --git a/Files/DataStandard/Vault.Custom/addinVault/Menus/powerGateSaveConfiguration.ps1 b/Files/DataStandard/Vault.Custom/addinVault/Menus/powerGateSaveConfiguration.ps1 index 58e360b..6b9e4d9 100644 --- a/Files/DataStandard/Vault.Custom/addinVault/Menus/powerGateSaveConfiguration.ps1 +++ b/Files/DataStandard/Vault.Custom/addinVault/Menus/powerGateSaveConfiguration.ps1 @@ -6,12 +6,12 @@ $modules | ForEach-Object { Import-Module -Name $_.FullName -Global } $cfgPath = "c:\temp\powerGateCfg\powerGateConfiguration.xml" $testPath = Test-Path $cfgPath if ($testPath -eq $false) { - ShowMessageBox -Message "No config file found. Please download/edit the config file first and then save it back to the Vault server" -Button "OK" -Icon "Warning" | Out-Null + ShowMessageBox -Message "No config file found in '$cfgPath' `n Please download/edit the config file first and then save it back to the Vault server!" -Button "OK" -Icon "Warning" | Out-Null return } [byte[]]$cfg = [System.IO.File]::ReadAllBytes($cfgPath) -SetConfigFromVault -Content $cfg +Set-PowerGateConfigFromVault -Content $cfg ShowMessageBox -Message "Config file saved to Vault server" -Button "OK" -Icon "Information" | Out-Null Remove-Item $cfgPath -Force \ No newline at end of file diff --git a/Files/powerGate/Modules/BomFunctions.psm1 b/Files/powerGate/Modules/BomFunctions.psm1 index 7020ec4..3c027a5 100644 --- a/Files/powerGate/Modules/BomFunctions.psm1 +++ b/Files/powerGate/Modules/BomFunctions.psm1 @@ -13,7 +13,7 @@ $rawMaterialQuantityProperty = "Raw Quantity" function GetErpBomHeader($number) { Log -Begin $erpBomHeader = Get-ERPObject -EntitySet $bomHeaderEntitySet -Keys @{Number = $number } -Expand "BomRows" - $erpBomHeader = CheckResponse -entity $erpBomHeader + $erpBomHeader = Edit-ResponseWithErrorMessage -Entity $erpBomHeader Log -End return $erpBomHeader } @@ -31,7 +31,7 @@ function CreateErpBomHeader($erpBomHeader) { $erpBomHeader.ModifiedDate = [DateTime]::Now $erpBomHeader = Add-ERPObject -EntitySet $bomHeaderEntitySet -Properties $erpBomHeader - $erpBomHeader = CheckResponse -entity $erpBomHeader + $erpBomHeader = Edit-ResponseWithErrorMessage -Entity $erpBomHeader -WriteOperation Log -End return $erpBomHeader } @@ -42,7 +42,7 @@ function UpdateErpBomHeader($erpBomHeader) { $erpBomHeader.ModifiedDate = [DateTime]::Now $erpBomHeader = Update-ERPObject -EntitySet $bomHeaderEntitySet -keys $erpBomHeader._Keys -Properties $erpBomHeader._Properties - $erpBomHeader = CheckResponse -entity $erpBomHeader + $erpBomHeader = Edit-ResponseWithErrorMessage -Entity $erpBomHeader -WriteOperation Log -End return $erpBomHeader } @@ -52,7 +52,7 @@ function UpdateErpBomHeader($erpBomHeader) { function GetErpBomRow($parentNumber, $childNumber, $position) { Log -Begin $erpBomRow = Get-ERPObject -EntitySet $bomRowEntitySet -Keys @{ParentNumber = $parentNumber; ChildNumber = $childNumber; Position = $position } - $erpBomRow = CheckResponse -entity $erpBomRow + $erpBomRow = Edit-ResponseWithErrorMessage -Entity $erpBomRow Log -End return $erpBomRow } @@ -70,7 +70,7 @@ function CreateErpBomRow($erpBomRow) { $erpBomRow.ModifiedDate = [DateTime]::Now $erpBomRow = Add-ERPObject -EntitySet $bomRowEntitySet -Properties $erpBomRow - $erpBomRow = CheckResponse -entity $erpBomRow + $erpBomRow = Edit-ResponseWithErrorMessage -Entity $erpBomRow -WriteOperation Log -End return $erpBomRow } @@ -81,7 +81,7 @@ function UpdateErpBomRow($erpBomRow) { $erpBomRow.ModifiedDate = [DateTime]::Now $erpBomRow = Update-ERPObject -EntitySet $bomRowEntitySet -Keys $erpBomRow._Keys -Properties @{Quantity = $erpBomRow.Quantity } - $erpBomRow = CheckResponse -entity $erpBomRow + $erpBomRow = Edit-ResponseWithErrorMessage -Entity $erpBomRow -WriteOperation Log -End return $erpBomRow } @@ -89,7 +89,7 @@ function UpdateErpBomRow($erpBomRow) { function RemoveErpBomRow($parentNumber, $childNumber, $position) { Log -Begin $erpBomRow = Remove-ERPObject -EntitySet $bomRowEntitySet -Keys @{ParentNumber = $parentNumber; ChildNumber = $childNumber; Position = $position } - $erpBomRow = CheckResponse -entity $erpBomRow + $erpBomRow = Edit-ResponseWithErrorMessage -Entity $erpBomRow -WriteOperation Log -End return $erpBomRow } diff --git a/Files/powerGate/Modules/Communication.psm1 b/Files/powerGate/Modules/Communication.psm1 index 861a54d..22a1539 100644 --- a/Files/powerGate/Modules/Communication.psm1 +++ b/Files/powerGate/Modules/Communication.psm1 @@ -45,13 +45,22 @@ function GetPowerGateError { return $powerGateErrMsg } -function CheckResponse($entity) { +function Edit-ResponseWithErrorMessage { + param( + $Entity, + [Switch]$WriteOperation = $false + ) Log -Begin if ($null -eq $entity) { $entity = $false + if($WriteOperation) { + # In case NO error message from the ERP returned and this means that no request at all was sent out by powerGate, therefore its required to look for a internal error in the powerGate Logs file. + $logFileLocation = "$($env:LocalAppdata)\coolOrange\powerGate\Logs\powerGate.log" + Add-Member -InputObject $entity -Name "_ErrorMessage" -Value "Unexpected error:`n Failed bacause probably some passed values for the create/update operation are not valid, for example 'the input was a text but should be a number'. Therefore check the last error message in the log file, then change your inputs and re-execute the operation: $logFileLocation" -MemberType NoteProperty -Force + } $pGError = GetPowerGateError if ($pGError) { - $message = "The communication with ERP failed!`n '$pGError'" + $message = "Direct error message from the ERP:`n '$pGError'" Add-Member -InputObject $entity -Name "_ErrorMessage" -Value $message -MemberType NoteProperty -Force } } diff --git a/Files/powerGate/Modules/MaterialFunctions.psm1 b/Files/powerGate/Modules/MaterialFunctions.psm1 index 93d5ff7..8a3c28d 100644 --- a/Files/powerGate/Modules/MaterialFunctions.psm1 +++ b/Files/powerGate/Modules/MaterialFunctions.psm1 @@ -20,7 +20,7 @@ function GetErpMaterial($number) { } $number = $number.ToUpper() $erpMaterial = Get-ERPObject -EntitySet $materialEntitySet -Key @{ Number = $number } - $erpMaterial = CheckResponse -entity $erpMaterial + $erpMaterial = Edit-ResponseWithErrorMessage -Entity $erpMaterial Add-Member -InputObject $erpMaterial -Name "IsCreate" -Value $false -MemberType NoteProperty -Force Add-Member -InputObject $erpMaterial -Name "IsUpdate" -Value $true -MemberType NoteProperty -Force @@ -56,7 +56,7 @@ function CreateErpMaterial($erpMaterial) { $erpMaterial = TransformErpMaterial -erpMaterial $erpMaterial $erpMaterial = Add-ErpObject -EntitySet $materialEntitySet -Properties $erpMaterial - $erpMaterial = CheckResponse -entity $erpMaterial + $erpMaterial = Edit-ResponseWithErrorMessage -Entity $erpMaterial -WriteOperation Log -End return $erpMaterial } @@ -68,7 +68,7 @@ function UpdateErpMaterial($erpMaterial) { $erpMaterial = TransformErpMaterial -erpMaterial $erpMaterial $erpMaterial = Update-ERPObject -EntitySet $materialEntitySet -Key $erpMaterial._Keys -Properties $erpMaterial._Properties - $erpMaterial = CheckResponse -entity $erpMaterial + $erpMaterial = Edit-ResponseWithErrorMessage -Entity $erpMaterial -WriteOperation Log -End return $erpMaterial } diff --git a/Files/powerGate/Modules/SearchFunctions.psm1 b/Files/powerGate/Modules/SearchFunctions.psm1 index 2cf37d5..e74348d 100644 --- a/Files/powerGate/Modules/SearchFunctions.psm1 +++ b/Files/powerGate/Modules/SearchFunctions.psm1 @@ -3,7 +3,7 @@ $materialEntityType = "Material" function SearchErpMaterials ($filter, $top = 100) { $erpMaterials = Get-ERPObjects -EntitySet $materialEntitySet -Filter $filter -Top $top - $erpMaterials = CheckResponse -entity $erpMaterials + $erpMaterials = Edit-ResponseWithErrorMessage -Entity $erpMaterials return $erpMaterials } @@ -117,6 +117,7 @@ function ApplyFilter($key, $value) { function ExecuteErpSearch { Log -Begin + $dsWindow.Cursor = "Wait" $dsDiag.Clear() $searchCriteria = $searchWindow.FindName("SearchCriteria").DataContext $CaseSensitive = $searchWindow.FindName("CaseSensitive").IsChecked @@ -124,6 +125,7 @@ function ExecuteErpSearch { $filter = ConvertSearchCriteriaToFilter -SearchCriteria $searchCriteria -CaseSensitive $CaseSensitive $dsDiag.Trace("filter = $filter") $results = SearchErpMaterials -filter $filter -top $topa + $dsWindow.Cursor = "Arrow" if (-not $results -or $false -eq $results) { $searchWindow.FindName("SearchResults").ItemsSource = $null $searchWindow.FindName("RecordsFound").Content = "Results found: 0" diff --git a/Files/powerGate/Modules/SelectionLists.psm1 b/Files/powerGate/Modules/SelectionLists.psm1 index 4c7d163..1d46834 100644 --- a/Files/powerGate/Modules/SelectionLists.psm1 +++ b/Files/powerGate/Modules/SelectionLists.psm1 @@ -1,4 +1,4 @@ -function SetConfigFromVault { +function Set-PowerGateConfigFromVault { param( [byte[]]$Content ) @@ -13,21 +13,31 @@ Log -End } -function GetConfigFromVault { +function Get-PowerGateConfigFromVault { Log -Begin $xmlString = $vault.KnowledgeVaultService.GetVaultOption("powerGateConfig") - if($xmlString -match "^?") { - $xmlString = $xmlString.SubString(1) + + if (-not $xmlString) { + $xmlTemplatePath = "C:\ProgramData\coolOrange\powerGate\powerGateConfigurationTemplate.xml" + [byte[]]$cfg = [System.IO.File]::ReadAllBytes($xmlTemplatePath) + Set-PowerGateConfigFromVault -Content $cfg + $xmlString = $vault.KnowledgeVaultService.GetVaultOption("powerGateConfig") + if (-not $xmlString) { + throw "PowerGateConfiguration is not saved in the Vault options! An administrator must import the XML via the command 'powerGate->Save Configuration' in the Vault Client." + } } - if($xmlString) { - try{ - $xmlObject = New-Object -TypeName System.Xml.XmlDocument - $xmlObject.LoadXml($xmlString) - return $xmlObject - } catch{ - Log -message "Unable to parse XML-String to XML-Object!" - return $null - } + + try{ + $byteOrderMarkUtf8 = [System.Text.Encoding]::UTF8.GetString([System.Text.Encoding]::UTF8.GetPreamble()) + if ($xmlString.StartsWith($byteOrderMarkUtf8)) { + $xmlString = $xmlString.Remove(0, $byteOrderMarkUtf8.Length); + } + + $xmlObject = New-Object -TypeName System.Xml.XmlDocument + $xmlObject.LoadXml($xmlString) + return $xmlObject + } catch { + throw "Unable to parse powerGateConfiguration from the Vault Options to a valid XML-Object! An administrator must import a new XML via the command 'powerGate->Save Configuration' in the Vault Client.`n $($_.Exception.Message)" } Log -End } @@ -37,14 +47,13 @@ function GetSelectionList($section, $withBlank = $false) { $list = @() if (-not $vault) { return $list } - [xml]$cfg = GetConfigFromVault - if ($null -eq $cfg) { - [byte[]]$cfg = [System.IO.File]::ReadAllBytes("C:\ProgramData\coolOrange\powerGate\powerGateConfigurationTemplate.xml") - SetConfigFromVault -Content $cfg - [xml]$cfg = GetConfigFromVault - } + [xml]$cfg = Get-PowerGateConfigFromVault - $entries = Select-Xml -Xml $cfg -XPath "//$section" + try { + $entries = Select-Xml -Xml $cfg -XPath "//$section" + } catch { + throw "Failed to find XML node '$section' in the powerGateConfiguration! An administrator must edit and import the XML via the command 'powerGate->Save Configuration' in the Vault Client!" + } if ($entries) { foreach ($entry in $entries.Node.ChildNodes) { if ($entry.NodeType -eq "Comment") { continue } @@ -78,7 +87,7 @@ function GetBOMStateList($withBlank = $false) { function GetCategoryList($withBlank = $false) { $list = @() $categories = Get-ERPObjects -EntitySet "Categories" - $categories = CheckResponse -entity $categories + $categories = Edit-ResponseWithErrorMessage -entity $categories if (-not $categories -or $false -eq $categories) { return $list } diff --git a/Files/powerGatePlugin/ErpServices/Services/ErpBaseService.cs b/Files/powerGatePlugin/ErpServices/Services/ErpBaseService.cs index 83431c6..f23d5a5 100644 --- a/Files/powerGatePlugin/ErpServices/Services/ErpBaseService.cs +++ b/Files/powerGatePlugin/ErpServices/Services/ErpBaseService.cs @@ -29,7 +29,7 @@ protected IEnumerable GetSearchSettings(IExpression