-
Notifications
You must be signed in to change notification settings - Fork 2
/
persons.ps1
394 lines (315 loc) · 16.2 KB
/
persons.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# Get the source data
function Get-RAETXMLFunctions {
param(
[parameter(Mandatory = $true)]$XMLBasePath,
[parameter(Mandatory = $true)]$FileFilter,
[parameter(Mandatory = $true)][ref]$functions
)
$files = Get-ChildItem -Path $XMLBasePath -Filter $FileFilter | Sort-Object LastWriteTime -Descending
if ($files.Count -eq 0) { return }
# Read content as XML
[xml]$xml = Get-Content $files[0].FullName
# Process all records
foreach ($functie in $xml.GetElementsByTagName("functie")) {
$function = [PSCustomObject]@{}
foreach ($child in $functie.ChildNodes) {
$function | Add-Member -MemberType NoteProperty -Name $child.LocalName -Value $child.'#text' -Force
}
[void]$functions.value.Add($function)
}
}
function Get-RAETXMLDepartments {
param(
[parameter(Mandatory = $true)]$XMLBasePath,
[parameter(Mandatory = $true)]$FileFilter,
[parameter(Mandatory = $true)][ref]$departments
)
$files = Get-ChildItem -Path $XMLBasePath -Filter $FileFilter | Sort-Object LastWriteTime -Descending
if ($files.Count -eq 0) { return }
# Read content as XML
[xml]$xml = Get-Content $files[0].FullName
# Process all records
foreach ($afdeling in $xml.GetElementsByTagName("orgEenheid")) {
$department = [PSCustomObject]@{}
foreach ($child in $afdeling.ChildNodes) {
$department | Add-Member -MemberType NoteProperty -Name $child.LocalName -Value $child.'#text' -Force
}
[void]$departments.value.Add($department)
}
}
function Get-RAETXMLBAFiles {
param(
[parameter(Mandatory = $true)]$XMLBasePath,
[parameter(Mandatory = $true)]$functions,
[parameter(Mandatory = $true)]$departments,
[parameter(Mandatory = $true)][ref]$persons,
[parameter(Mandatory = $true)][ref]$contracts
)
# set date
$now = New-Object "System.DateTime" -ArgumentList (Get-Date).Year, (Get-Date).Month, (Get-Date).Day
# Get function member names
$functionsFirstRecord = $functions.GetEnumerator() | Select-Object -first 1
$functionMemberNames = ($functionsFirstRecord|Get-Member -MemberType NoteProperty).Name
# Group functions on externalId
$functions = $functions | Group-Object functieCode -AsHashTable
# Get department member names
$departmentsFirstRecord = $departments.GetEnumerator() | Select-Object -first 1
$departmentMemberNames = ($departmentsFirstRecord|Get-Member -MemberType NoteProperty).Name
# Group departments on externalId
$departments = $departments | Group-Object orgEenheidID -AsHashTable
# Get location member names
#$locationsFirstRecord = $locations.GetEnumerator() | Select-Object -first 1
#$locationsMemberNames = ($locationsFirstRecord|Get-Member -MemberType NoteProperty).Name
# Group locations on externalId
#$locations = $locations | Group-Object externalId -AsHashTable
# List all files in the selected folder
$files = Get-ChildItem -Path $XMLBasePath -Filter "*.xml"
# Process all files
$count = 1
foreach ($file in $files) {
[xml]$xml = Get-Content $file.FullName
foreach ($werknemer in $xml.GetElementsByTagName("persoon")) {
$person = [PSCustomObject]@{}
foreach ($id in $werknemer.GetElementsByTagName("identificatiePS").ChildNodes) {
$person | Add-Member -MemberType NoteProperty -Name ($id.LocalName + "_identificatiePS") -Value $id.'#text' -Force
}
foreach ($rubriekcode in $werknemer.GetElementsByTagName("rubriekcode")) {
$person | Add-Member -MemberType NoteProperty -Name ($rubriekcode.ParentNode.LocalName + "_" + $rubriekcode.InnerText) -Value $rubriekcode.ParentNode.waarde -Force
}
[void]$persons.value.Add($person)
}
foreach ($dienstverband in $xml.GetElementsByTagName("dienstverband")) {
$contract = [PSCustomObject]@{}
foreach ($id in $dienstverband.GetElementsByTagName("identificatieDV").ChildNodes) {
$contract | Add-Member -MemberType NoteProperty -Name ("dv_" + $id.LocalName + "_identificatieDV") -Value $id.'#text' -Force
}
foreach ($node in $dienstverband | Get-Member -MemberType Property) {
if ($node.name -NE 'inroostering' -And $node.name -NE 'loonVerdeling') {
foreach ($rubriekcode in $dienstverband.($node.Name).GetElementsByTagName("rubriekcode")) {
$waarde = $rubriekcode.ParentNode.waarde
$contract | Add-Member -MemberType NoteProperty -Name ("dv_" + $rubriekcode.ParentNode.LocalName + "_" + $rubriekcode.InnerText) -Value $waarde -Force
# Add the value description as a separate property
if ([string]::IsNullOrEmpty($rubriekcode.ParentNode.omschrijvingWaarde) -eq $false) {
$contract | Add-Member -MemberType NoteProperty -Name ("dv_" + $rubriekcode.ParentNode.LocalName + "_" + $rubriekcode.InnerText + "_desc") -Value $rubriekcode.ParentNode.omschrijvingWaarde -Force
}
}
}
}
if ([string]::IsNullOrEmpty($contract.dv_functiePrimair_P01107) -eq $true) {
foreach ($name in $functionMemberNames) {
$contract | Add-Member -MemberType NoteProperty -Name ("dv_" + "functiePrimair_P01107" + "_" + $name) -Value "" -Force
}
}
else
{
$contractFunction = $functions[$contract.dv_functiePrimair_P01107]
foreach ($name in $functionMemberNames) {
$contract | Add-Member -MemberType NoteProperty -Name ("dv_" + "functiePrimair_P01107" + "_" + $name) -Value $contractFunction."$name" -Force
}
}
<#
$pattern = '[^a-zA-Z]'
if ([string]::IsNullOrEmpty($contract.dv_aanvullendeRubriek_E2078 ) -eq $true) {
foreach ($name in $locationsMemberNames) {
$contract | Add-Member -MemberType NoteProperty -Name ("dv_" + "aanvullendeRubriek_E2078" + "_" + ($name -replace $pattern, '')) -Value "" -Force
}
}
else
{
$contractLocation = $locations[$contract.dv_aanvullendeRubriek_E2078 ]
foreach ($name in $locationsMemberNames) {
$contract | Add-Member -MemberType NoteProperty -Name ("dv_" + "aanvullendeRubriek_E2078" + "_" + ($name -replace $pattern, '')) -Value $contractLocation."$name" -Force
}
}
#>
if ([string]::IsNullOrEmpty($contract.dv_orgEenheid_P01106) -eq $true) {
foreach ($name in $departmentMemberNames) {
$contract | Add-Member -MemberType NoteProperty -Name ("dv_" + "dv_orgEenheid_P01106" + "_" + $name) -Value "" -Force
}
}
else
{
$contractDepartment = $departments[$contract.dv_orgEenheid_P01106 ]
foreach ($name in $departmentMemberNames) {
$contract | Add-Member -MemberType NoteProperty -Name ("dv_" + "dv_orgEenheid_P01106" + "_" + $name) -Value $contractDepartment."$name" -Force
}
}
if($usePositions)
{
# make a contract per inzet
foreach ($inzet in $dienstverband.GetElementsByTagName("inzet")) {
#clone contract object
$position = [PSCustomObject]@{}
foreach ($propery in $contract.psobject.properties) {
$position | Add-Member -MemberType $propery.MemberType -Name $propery.Name -Value $propery.Value
}
foreach ($id in $inzet.GetElementsByTagName("identificatieIZ").ChildNodes) {
$position | Add-Member -MemberType NoteProperty -Name ("iz_" + $id.LocalName + "_identificatieIZ") -Value $id.'#text' -Force
}
# this should be done before the inzet loop & should exclude all nodes in 'inroostering'
foreach ($rubriekcode in $inzet.GetElementsByTagName("rubriekcode")) {
$waarde = $rubriekcode.ParentNode.waarde
$position | Add-Member -MemberType NoteProperty -Name ("iz_" + $rubriekcode.ParentNode.LocalName + "_" + $rubriekcode.InnerText) -Value $waarde -Force
# Add the value description as a separate property
if ([string]::IsNullOrEmpty($rubriekcode.ParentNode.omschrijvingWaarde) -eq $false) {
$position | Add-Member -MemberType NoteProperty -Name ("iz_" + $rubriekcode.ParentNode.LocalName + "_" + $rubriekcode.InnerText + "_desc") -Value $rubriekcode.ParentNode.omschrijvingWaarde -Force
}
}
if ([string]::IsNullOrEmpty($position.iz_operationeleFunctie_P01122) -eq $true) {
foreach ($name in $FunctionMemberNames) {
$contract | Add-Member -MemberType NoteProperty -Name ("iz_" + "operationeleFunctie_P01122" + "_" + $name) -Value "" -Force
}
}
else
{
$contractFunction = $functions[$position.iz_operationeleFunctie_P01122]
foreach ($name in $FunctionMemberNames) {
$position | Add-Member -MemberType NoteProperty -Name ("iz_" + "operationeleFunctie_P01122" + "_" + $name) -Value $contractFunction."$name" -Force
}
}
if ([string]::IsNullOrEmpty($position.iz_operationeleOrgEenheid_P01121 ) -eq $true) {
foreach ($name in $DepartmentMemberNames) {
$contract | Add-Member -MemberType NoteProperty -Name ("iz_" + "operationeleOrgEenheid_P01121" + "_" + $name) -Value "" -Force
}
}
else
{
$contractDepartment = $departments[$position.iz_operationeleOrgEenheid_P01121 ]
foreach ($name in $DepartmentMemberNames) {
$position | Add-Member -MemberType NoteProperty -Name ("iz_" + "operationeleOrgEenheid_P01121" + "_" + $name) -Value $contractDepartment."$name" -Force
}
}
$positionActive = (([DateTime]$position.iz_begindatum_P01125).addDays(0) -le $now -and ([string]::IsNullOrEmpty($position.iz_einddatum_P01126) -or ([DateTime]$position.iz_einddatum_P01126).addDays(0) -gt $now))
$position | Add-Member -MemberType NoteProperty -Name ("iz_" + "is_active") -Value $positionActive -Force
[void]$contracts.value.Add($position)
}
}
else
{
[void]$contracts.value.Add($contract)
}
}
$count += 1
}
}
Write-Verbose -Verbose "Person import started";
# Init variables
$connectionSettings = ConvertFrom-Json $configuration
$xmlPath = $($connectionSettings.xmlPath)
$usePositions = [System.Convert]::ToBoolean($connectionSettings.usePositions)
#$locationCsv = $($connectionSettings.locationCsv)
# Get the source data
$persons = New-Object System.Collections.ArrayList
$contracts = New-Object System.Collections.ArrayList
$functions = New-Object System.Collections.ArrayList
$departments = New-Object System.Collections.ArrayList
#$locations = Import-Csv -Path $locationCsv -Delimiter ";"
Write-Verbose -Verbose "Parsing function file...";
Get-RAETXMLFunctions -XMLBasePath $xmlPath -FileFilter "rst_functie_*.xml" ([ref]$functions)
Write-Verbose -Verbose "Parsing department file...";
Get-RAETXMLDepartments -XMLBasePath $xmlPath -FileFilter "rst_orgeenheid_*.xml" ([ref]$departments)
Write-Verbose -Verbose "Parsing person/contracts files...";
Get-RAETXMLBAFiles -XMLBasePath $xmlPath $functions $departments ([ref]$persons) ([ref]$contracts)
# Group contracts on externalId
$contracts = $contracts | Group-Object dv_persNrDV_identificatieDV -AsHashTable
# Augment the persons
Write-Verbose -Verbose "Augmenting persons...";
$persons | Add-Member -MemberType NoteProperty -Name "Contracts" -Value $null -Force
$persons | Add-Member -MemberType NoteProperty -Name "ExternalId" -Value $null -Force
$persons | Add-Member -MemberType NoteProperty -Name "DisplayName" -Value $null -Force
if($usePositions)
{
$persons | Add-Member -MemberType NoteProperty -Name "AllActiveFunctions" -Value $null -Force
$persons | Add-Member -MemberType NoteProperty -Name "AllActiveDepartments" -Value $null -Force
}
$persons | ForEach-Object {
# Map required fields
$_.ExternalId = $_.persNr_identificatiePS
$_.DisplayName = $_.persNr_identificatiePS
# Add the contracts
$personContracts = $contracts[$_.persNr_identificatiePS]
if ($null -ne $personContracts) {
$_.Contracts = $personContracts
}
if($usePositions)
{
# add all active functions and departments to a new person attribute
$activePersonContracts = $personContracts | Where-Object {$_.iz_is_active -eq $True}
foreach ($activePersonContract in $activePersonContracts) {
if (![string]::IsNullOrEmpty($_.AllActiveFunctions))
{
$_.AllActiveFunctions = $_.AllActiveFunctions + " -- "
}
$_.AllActiveFunctions = $_.AllActiveFunctions + $activePersonContract.iz_operationeleFunctie_P01122_desc
if (![string]::IsNullOrEmpty($_.AllActiveDepartments))
{
$_.AllActiveDepartments = $_.AllActiveDepartments + " -- "
}
$_.AllActiveDepartments = $_.AllActiveDepartments + $activePersonContract.iz_operationeleOrgEenheid_P01121_naamLang
}
if (![string]::IsNullOrEmpty($_.AllActiveFunctions)) { $_.AllActiveFunctions = $_.AllActiveFunctions | Select-Object -Unique }
if (![string]::IsNullOrEmpty($_.AllActiveDepartments)) { $_.AllActiveDepartments = $_.AllActiveDepartments | Select-Object -Unique }
}
}
# Make sure persons are unique
$persons = $persons | Sort-Object ExternalId -Unique
Write-Verbose -Verbose "Person import completed";
Write-Verbose -Verbose "Exporting data to HelloID";
# Output the json
foreach ($Person in $persons) {
# Map DisplayName (as seen in Raw data)
$externalID = "$($person.externalId)".trim()
$firstName = "$($person.roepnaam_P01003)".trim()
$prefix = "$($person.voorvoegsels_P00302)".trim()
$lastName = "$($person.geboortenaam_P00301)".trim()
$partnerPrefix = "$($person.voorvoegsels_P00391)".trim()
$partnerLastname = "$($person.geboortenaam_P00390)".trim()
$nameConvention = $person.k_naamgebruik.Code
switch($nameConvention){
'E' {
# Birthname
$displayName = $firstName
if(-not[String]::IsNullOrEmpty($prefix)){ $displayName = $displayName + " " + $prefix }
$displayName = $displayName + " " + $lastName
$displayName = $displayName + " " + "($externalID)"
}
'B' {
# Partnername - Birthname
$displayName = $firstName
if(-not[String]::IsNullOrEmpty($partnerPrefix)){ $displayName = $displayName + " " + $partnerPrefix }
$displayName = $displayName + " " + $partnerLastname
$displayName = $displayName + " -"
if(-not[String]::IsNullOrEmpty($prefix)){ $displayName = $displayName + " " + $prefix }
$displayName = $displayName + " " + $lastName
$displayName = $displayName + " " + "($externalID)"
}
'P' {
# Partnername
$displayName = $firstName
if(-not[String]::IsNullOrEmpty($partnerPrefix)){ $displayName = $displayName + " " + $partnerPrefix }
$displayName = $displayName + " " + $partnerLastname
$displayName = $displayName + " " + "($externalID)"
}
'C' {
# Birthname - Partnername
$displayName = $firstName
if(-not[String]::IsNullOrEmpty($prefix)){ $displayName = $displayName + " " + $prefix }
$displayName = $displayName + " " + $lastName
$displayName = $displayName + " -"
if(-not[String]::IsNullOrEmpty($partnerPrefix)){ $displayName = $displayName + " " + $partnerPrefix }
$displayName = $displayName + " " + $partnerLastname
$displayName = $displayName + " " + "($externalID)"
}
default {
# Birthname
$displayName = $firstName
if(-not[String]::IsNullOrEmpty($prefix)){ $displayName = $displayName + " " + $prefix }
$displayName = $displayName + " " + $lastName
$displayName = $displayName + " " + "($externalID)"
}
}
$person.DisplayName = $displayName;
$json = $person | ConvertTo-Json -Depth 3
Write-Output $json
}
Write-Verbose -Verbose "Exported data to HelloID";