-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMain.lua
682 lines (567 loc) · 23.9 KB
/
Main.lua
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
settings = {} --settings is a global to allow other lua files access
settings["TabName"] = GetSetting("TabName")
settings["APIUrl"] = GetSetting("APIUrl")
settings["Password"] = GetSetting("APIPassword")
settings["Username"] = GetSetting("APIUsername")
settings["RepoCode"] = GetSetting("RepoCode")
-- Logging needs to precede all but settings to enable supporting libraries to log
globalInterfaceMngr = GetInterfaceManager()
settings["AddonName"] = globalInterfaceMngr.environment.Info.Name
settings["AddonVersion"] = globalInterfaceMngr.environment.Info.Version
settings["LogLabel"] = settings.AddonName .. " v" .. settings.AddonVersion
LogDebug("Launching ASpace Basic Plugin")
LogDebug("Loading Assemblies")
LogDebug("Loading System Data Assembly")
luanet.load_assembly("System")
luanet.load_assembly("System.Data")
luanet.load_assembly("System.Net")
LogDebug("Loading .NET Types")
Types = {}
Types["System.Data.DataTable"] = luanet.import_type("System.Data.DataTable")
Types["System.Data.DataColumn"] = luanet.import_type("System.Data.DataColumn")
Types["System.Net.WebClient"] = luanet.import_type("System.Net.WebClient")
Types["System.Collections.Specialized.NameValueCollection"] = luanet.import_type("System.Collections.Specialized.NameValueCollection")
Types["System.Text.Encoding"] = luanet.import_type("System.Text.Encoding")
Buttons = {}
Ribbons = {}
require("Helpers")
require("EventHandlers")
require("Grids")
require("API")
require "Atlas-Addons-Lua-ParseJson.JsonParser"
local form = nil
interfaceMngr = nil
function Init()
interfaceMngr = GetInterfaceManager()
form = interfaceMngr:CreateForm(settings.TabName, settings.TabName)
settings['APIUrl'] = removeTrailingSlash(settings['APIUrl'])
settings['sessionID'] = GetSessionId()
if settings['sessionID'] == nil then
interfaceMngr:ShowMessage('The session ID for interaction with the API could not be retrieved. Please check the username and password in the container lookup addon settings.', 'Network error')
return
end
settings["repoTable"] = getListOfRepo()
settings["numberOfRepos"] = tableLength(settings["repoTable"])
if settings["numberOfRepos"] == 0 then
interfaceMngr:ShowMessage('You do not currently have access to the ArchivesSpace API. Please check that the network you are on is allowed to access the ArchivesSpace API.', 'API access error')
return
end
Ribbons["CN"] = form:CreateRibbonPage("Container Search")
if (AddonInfo.CurrentForm == "FormRequest") then
Buttons["CNS-ICaC"] = Ribbons["CN"]:CreateButton("Import Container and Citation", GetClientImage("impt_32x32"), "importContainerAndCitation", "Container Search")
Buttons["CNS-IC"] = Ribbons["CN"]:CreateButton("Import Container", GetClientImage("impt_32x32"), "importContainer", "Container Search")
Buttons["CNS-PS"] = Ribbons["CN"]:CreateButton("Perform Search", GetClientImage("srch_32x32"), "containersSearch", "Container Search")
asItemsGrid = form:CreateGrid("MySqlGrid", "ArchivesSpace Results")
asItemsGrid.GridControl.MainView.OptionsView.ShowGroupPanel = false
numberSearchResult = form:CreateTextEdit("NumberSearchResult", "Number of item(s) found:")
numberSearchResult.ReadOnly = true
numberSearchResult.Value = 0
end
callNumber = GetFieldValue("Transaction", "CallNumber")
if callNumber == '' or callNumber == nil then
-- precedence of the call number for the search over the title
title = GetFieldValue("Transaction", "ItemTitle")
end
--Build the "Request" TextEdit Box
searchTerm = form:CreateTextEdit("Search", "Call Number")
searchTerm.Value = callNumber
searchTerm.Editor.KeyDown:Add(CallNumberSubmitCheck)
--Add a spot for collection title
collectionTitle = form:CreateTextEdit("CollectionTitle", "Collection Title")
if title ~= nil then
collectionTitle.Value = title
end
collectionTitle.Editor.KeyDown:Add(TitleSubmitCheck)
eadidTerm = form:CreateTextEdit('eadid', "EADID")
eadidTerm.Editor.KeyDown:Add(EADIDSubmitCheck)
barcodeTerm = form:CreateTextEdit("barcode", "Barcode")
barcodeTerm.Editor.KeyDown:Add(BarcodeSubmitCheck)
-- This specifies the layout of the different component of the addon (the grid, the ribbons etc..) the default placement being rather poor.
form:LoadLayout("layout.xml")
form:Show()
local result = {}
local searchTypeStr = 'call number'
if callNumber == nil or callNumber == '' then
if title ~= nil and title ~= '' then
result = getTopContainersByTitle(title)
searchTypeStr = 'title'
else
searchTypeStr = 'resource'
end
else
result = getTopContainersByCallNumber(callNumber)
end
local tab = convertResultsIntoDataTable(result)
GetBoxes(tab, searchTypeStr)
end
function convertResultsIntoDataTable(repoJsonTable)
local resultDataTable = Types["System.Data.DataTable"]()
for repoCode, jsonRes in pairs(repoJsonTable) do
local currDataTable = jsonArrayToDataTable(jsonRes, repoCode)
resultDataTable:Merge(currDataTable)
end
return resultDataTable
end
function containersSearch()
function isFieldFilled(field)
return field.Value ~= '' and field.Value ~= nil
end
titleIsFilled = isFieldFilled(collectionTitle)
eadIsFilled = isFieldFilled(eadidTerm)
callIsFilled = isFieldFilled(searchTerm)
barcodeIsFilled = isFieldFilled(barcodeTerm)
function countIfTrue(cond)
if cond then return 1 else return 0 end
end
local nField = countIfTrue(titleIsFilled) + countIfTrue(eadIsFilled) + countIfTrue(callIsFilled) + countIfTrue(barcodeIsFilled)
if nField ~= 1 then
interfaceMngr:ShowMessage('Only one search field should be filled for containers search', 'Cannot retrieve search results')
return
end
local gridControl = asItemsGrid.GridControl
-- this is a way to have some 'loading' info displayed to the user
noSearchResult(gridControl, 'Fetching search results, please wait')
if titleIsFilled then
performSearch(collectionTitle, 'title')
elseif eadIsFilled then
performSearch(eadidTerm, 'ead_id')
elseif callIsFilled then
performSearch(searchTerm, 'call number')
else
performSearch(barcodeTerm, 'barcode')
end
end
function getFullResourceById(repoID, resourceId)
local searchResourceReq = 'repositories/' .. repoID .. '/resources/' .. resourceId
return getElementBySearchQuery(searchResourceReq)
end
function getResourceByCallNumber(callNumb, repoId)
local searchResourceReq = 'repositories/' .. repoId .. '/search?page=1&q=four_part_id:("' .. callNumb .. '")&type[]=resource'
local res = getElementBySearchQuery(searchResourceReq)
total_hits = ExtractProperty(res, "total_hits")
if total_hits == 0 then
interfaceMngr:ShowMessage('The resource corresponding to the call number "' .. callNumb .. '" could not be found on the database.','Error')
return nil-- no point in trying to do anything else in such case
elseif total_hits > 1 then
LogDebug('Call number search returned ' .. total_hits .. ' results when only 1 should have been returned. The first result will be used')
end
results = ExtractProperty(res, "results")
return results[1]
end
function getTopContainersByCallNumber(callNumb)
return getTopContainersBySearchQuery('q=collection_identifier_u_stext:("'..callNumb..'")')
end
function getTopContainersByTitle(title)
return getTopContainersBySearchQuery('q=collection_display_string_u_sstr:("'..title..'")')
end
function getTopContainersByBarcode(barcode)
return getTopContainersBySearchQuery('q=barcode_u_sstr:("'..barcode..'")')
end
function getTopContainersByEADID(eadid)
-- ead_id is always lowercase in the db. ':lower()' makes the search case insensitive on the user side.
local repoCode = string.match(eadid, '[a-zA-Z][a-zA-Z][a-zA-Z]')
if repoCode == nil or repoCode == '' then
-- need to return an empty table as the return argument will get passed to convertResultsIntoDataTable
-- which expect to have a table as argument.
return {}
else
-- need to be sure the three first letters of the eadid corresponds to the
-- repository code stored in the setting's repoTable (where they are all uppercased)
repoCode = repoCode:upper()
end
local repoId = settings["repoTable"][repoCode]
if repoId == nil or repoId == '' then
return {}
end
local resourceId = getResourceIdByEADID(eadid:lower(), repoId)
if resourceId == nil then
return {}
end
return getTopContainersByResourceId(resourceId, repoCode)
end
function getTopContainersByResourceId(resourceId, repoCode)
local resultTable = {}
local repoId = split(resourceId, '/')[2]
local searchTopContReq = 'repositories/' .. repoId .. '/top_containers/search?q=collection_uri_u_sstr:("'..resourceId..'")'
local res = getElementBySearchQuery(searchTopContReq)
getResultAndPopulateTableOfJson(searchTopContReq, resultTable, repoCode)
return resultTable
end
function getTopContainersBySearchQuery(searchQuery)
local resultTable = {}
for code, id in pairs(settings['repoTable']) do
resultTable[code] = nil
local searchResourceReq = 'repositories/' .. id .. '/top_containers/search?' .. searchQuery
getResultAndPopulateTableOfJson(searchResourceReq, resultTable, code)
end
return resultTable
end
function getResultAndPopulateTableOfJson(searchResourceQuery, jsonTable, repoCode)
local res = getElementBySearchQuery(searchResourceQuery)
local response = ExtractProperty(res, "response")
if response ~= '' then
local numFound = ExtractProperty(response, "numFound")
if numFound ~= '' and numFound > 0 then
local docs = ExtractProperty(response, "docs")
jsonTable[repoCode] = docs
end
end
end
function getResourceIdByEADID(eadid, repoId)
local searchResourceReq = 'repositories/' .. repoId .. '/search?page=1&q=ead_id:("' .. eadid .. '")&type[]=resource'
local res = getElementBySearchQuery(searchResourceReq)
-- to reformat
local results = ExtractProperty(res, "results")
if results == '' then
return nil
end
local numFound = ExtractProperty(res, "total_hits")
if numFound == '' or numFound < 1 then
return nil
end
return ExtractProperty(results[1], "id")
end
function getElementBySearchQuery(searchQuery)
local res = ArchivesSpaceGetRequest(settings['sessionID'], searchQuery)
--LogDebug(res)
local errorCode = res['httpErrorCode']
if errorCode == 412 then
-- the http error code of 412 means the session id has expired
settings['sessionID'] = GetSessionId()
return getElementBySearchQuery(searchQuery)
end
if res == nil then
interfaceMngr:ShowMessage('could not retrieve search query result', 'error')
return nil -- no point in trying to do anything else in such case
end
return res
end
function getListOfRepo()
local resTable = {}
local searchResourceReq = 'repositories'
local res = getElementBySearchQuery(searchResourceReq)
for i=1, #res do
local currRepo = res[i]
local repoCode = ExtractProperty(currRepo, 'repo_code')
local repoUri = split(ExtractProperty(currRepo, 'uri'), '/')
local repoId = repoUri[#repoUri]
-- In HL at least, no resources have 0 as an ID
local resource = getFullResourceById(repoId, 0)
httpErrorCode = resource['httpErrorCode']
-- a 404 means the user could access the repo but the resource was not found. A 403 would be returned if the resource
if httpErrorCode == 404 then
resTable[repoCode] = repoId
end
end
return resTable
end
function setItemNode(itemRow, aeonField, data)
local success, err = pcall(function()
itemRow:set_Item(aeonField, data)
end)
if success then
if data ~= nil then
LogDebug('Setting '..aeonField..' to '..data)
else
LogDebug('Setting '..aeonField..' to a nil value')
end
else
if data ~= nil then
LogDebug('Error setting '..aeonField..' to '..data)
else
LogDebug('Error setting '..aeonField)
end
LogDebug("Error: ".. err.code)
end
return itemRow
end
function jsonArrayToDataTable(json_arr, repoCode)
local asItemTable = Types["System.Data.DataTable"]()
if json_arr == nil then
return asItemTable
end
local allRecords = {}
for i = 1, #json_arr do
local obj = json_arr[i]
local callNumbers = ExtractProperty(obj, 'collection_identifier_stored_u_sstr')
local titles = ExtractProperty(obj, 'collection_display_string_u_sstr')
local docIds = ExtractProperty(obj, 'collection_uri_u_sstr')
for i=1,#callNumbers do
local currCN = callNumbers[i]
local currTitle = titles[i]
local currDocIds = docIds[i]
-- in the barcode case, one search result will be linked to one or more resources.
allRecords[#allRecords + 1] = extractTopContainersInformation(obj, currCN, currTitle, currDocIds, repoCode)
-- a[#a+1] is an efficient way to append an element at the end of an array-like
end
end
-- "This order function receives two arguments and must return true if the first argument should come first in the sorted array."
function sortingByCallNumberContainerIndicator(tab1, tab2)
-- this assumes that the title of the json object is in the following format: "<callNumber>, <ContainterType> <ContainerNumber> [...]"
local cn1, cont1, indic1 = tab1['callNumber'], tab1['hidden_container'], tab1['hidden_indicator']
local cn2, cont2, indic2 = tab2['callNumber'], tab2['hidden_container'], tab2['hidden_indicator']
if cn1 ~= cn2 then
return cn1 < cn2
elseif cont1 ~= cont2 then
return cont1 < cont2
else
-- some indicators might still be string
if type(indic1) == type(indic2) then
return indic1 < indic2
else
return false
end
end
end
table.sort(allRecords, sortingByCallNumberContainerIndicator)
asItemTable.Columns:Add("collectionTitle")
asItemTable.Columns:Add("callNumber")
asItemTable.Columns:Add("enumeration")
asItemTable.Columns:Add("item_barcode")
asItemTable.Columns:Add("location")
asItemTable.Columns:Add("restrictions")
asItemTable.Columns:Add("item_id")
asItemTable.Columns:Add("series")
asItemTable.Columns:Add("profile")
asItemTable.Columns:Add("repo_code")
asItemTable.Columns:Add("doc_path")
for _, value in ipairs(allRecords) do
local row = asItemTable:NewRow()
setItemNode(row,'collectionTitle', value['collectionTitle'])
setItemNode(row,'callNumber', value['callNumber'])
setItemNode(row,'enumeration', value['enumeration'])
setItemNode(row,'item_barcode', value['item_barcode'])
setItemNode(row,'location', value['location'])
setItemNode(row,'restrictions', value['restrictions'])
setItemNode(row,'item_id', value['item_id'])
setItemNode(row,'series', value['series'])
setItemNode(row,'profile', value['profile'])
setItemNode(row,'repo_code', value['repoCode'])
setItemNode(row, 'doc_path', value['docPath']) -- hidden value
asItemTable.Rows:Add(row)
end
return asItemTable
end
function extractTopContainersInformation(obj, callNumber, title, docId, repoCode)
local row = {}
row['callNumber'] = truncateIfNotNil(callNumber)
row['collectionTitle'] = truncateIfNotNil(title)
local jsonString = JsonParser:ParseJSON(ExtractProperty(obj, 'json'))
local indicator = ExtractProperty(jsonString, 'indicator')
local containers = ExtractProperty(obj, 'type_enum_s')
local container = nil
if containers ~= nil then
container = containers[1]
end
local typeEnum = nil
if indicator ~= nil and container ~= nil then
-- concatenating the indicator with the container type.
typeEnum = container .. ' ' .. indicator
if isnumeric(indicator) then
indicator = tonumber(indicator)
else
indicator = 0
end
else
if not container then
-- failsafe so the sorting works later.
container = ''
end
typeEnum = container
-- sort nil is not stored inside the hidden indicator column
indicator = 0
end
row['hidden_indicator'] = truncateIfNotNil(indicator)
row['hidden_container'] = truncateIfNotNil(container)
row['enumeration'] = truncateIfNotNil(typeEnum)
row['item_barcode'] = ExtractProperty(obj, 'barcode_u_sstr')[1]
-- apparently some locations can be empty!
row['location'] = truncateIfNotNil(ExtractProperty(obj, 'location_display_string_u_sstr')[1])
-- fetching this information from the 'restricted' field of the json embedded data
local restricted = 'N'
if ExtractProperty(jsonString, 'restricted') then
restricted = 'Y'
end
row['restrictions'] = restricted
-- all the ids are prepended with the database path.
-- format of a top container path: /repositories/[repoID]/top_containers/[TopContainerID]
local tcId = split(ExtractProperty(obj, 'id'), '/')
row['item_id'] = tcId[#tcId]
--useful to make a callback when making the import later (hidden value on the grid)
row['docPath'] = docId
local seriesStr = ''
local seriesArray = ExtractProperty(jsonString, 'series')
if #seriesArray > 0 then
seriesStr = ExtractProperty(seriesArray[1], 'display_string')
for i = 2,#seriesArray do
local displayString = ExtractProperty(seriesArray[i], 'display_string')
if displayString ~= '' and displayString ~= nil then
seriesStr = seriesStr .. ' ' .. displayString
end
end
end
row['series'] = truncateIfNotNil(seriesStr)
local profile = ExtractProperty(obj, 'container_profile_display_string_u_sstr')[1]
row['profile'] = truncateIfNotNil(profile)
row['repoCode'] = repoCode
return row
end
function truncateIfNotNil(value)
-- If string are longer than 255 char, Aeon will not import them in the grid.
if type(value) == 'string' then
if value ~= nil and value ~= '' then
return value:sub(0,255)
else
return ''
end
else
return value
end
end
function GetBoxes(tab, itemQuery)
-- itemQuery specify which term was used for the search (call number or title), usefule for outputting the was "not found" message.
LogDebug("Retrieving boxes.")
clearTable(asItemsGrid) --Clear item grid to avoid mixed series/items
numberSearchResult.Value = tab.Rows.Count -- for the user to see the number of search results
local itemGridControl = asItemsGrid.GridControl
if (tab.Rows.Count ~= 0) then
itemGridControl:BeginUpdate()
--asItemsGrid.PrimaryTable = tab
itemGridControl.DataSource=tab
itemGridControl:EndUpdate()
fillItemTable(itemGridControl)
asItemsGrid.GridControl:Focus()
else
noSearchResult(itemGridControl, 'No top containers were found for the current '..itemQuery)
LogDebug("No results returned from webservice on box search")
end
end
function importContainer() DoItemImport(false) end
function importContainerAndCitation() DoItemImport(true) end
function DoItemImport(withCitation) --note no ID since even for the event handler the selected row is the one which has been double clicked
LogDebug("Performing Import")
LogDebug("Retrieving import row.")
local itemRow = asItemsGrid.GridControl.MainView:GetFocusedRow()
if ((itemRow == nil)) then
LogDebug("No rows selected - aborting")
return
end
local collectionTitle = itemRow:get_Item("collectionTitle")
local callNumber = itemRow:get_Item("callNumber")
local itemVolume = itemRow:get_Item("enumeration")
local barcode = itemRow:get_Item("item_barcode")
local location = itemRow:get_Item("location")
local itemInfo1 = itemRow:get_Item("restrictions")
local series = itemRow:get_Item("series")
local repoCode = itemRow:get_Item("repo_code")
local recordId = itemRow:get_Item("item_id")
-- Update the item object with the new values.
function setFieldValueIfNotNil(formName, fieldName, value)
if value ~= '' and value ~= nil then
-- this way the empty field won't be highlighted in the import
SetFieldValue(formName, fieldName, value)
end
end
LogDebug("Updating the item object.")
setFieldValueIfNotNil("Transaction", "ItemVolume", itemVolume)
setFieldValueIfNotNil("Transaction", "ItemNumber", barcode)
setFieldValueIfNotNil("Transaction", "ItemInfo5", location)
if withCitation then
setFieldValueIfNotNil("Transaction", "CallNumber", callNumber)
setFieldValueIfNotNil("Transaction", "ItemTitle", collectionTitle)
setFieldValueIfNotNil("Transaction", "Location", repoCode)
local documentPath = itemRow:get_Item("doc_path")
-- format of a document path: /repositories/[repoID]/[resources|accessions]/[documentID]
local documentType = split(documentPath, '/')
if documentType[3] == 'resources' then
setFieldValueIfNotNil("Transaction", "ItemCitation", series)
local res = getResourceByCallNumber(callNumber, settings["repoTable"][repoCode])
local creators = ExtractProperty(res, 'creators')
local creator = nil
if creators ~= nil then
creator = creators[1]
end
setFieldValueIfNotNil("Transaction", "ItemAuthor", creator)
local resourceURL = ExtractProperty(res, 'id')
local resourceElems = split(resourceURL, '/')
local resourceId = resourceElems[#resourceElems]
local repoId = settings["repoTable"][repoCode]
local resourceObj = getFullResourceById(repoId, resourceId)
local notes = ExtractProperty(resourceObj, 'notes')
local a_id = extractNoteContent(notes, 'label', 'Alma ID', 'subnotes')
if a_id == nil then
a_id = extractNoteContent(notes, 'label', 'Aleph ID', 'subnotes')
end
if a_id ~= nil then
a_id = ExtractProperty(a_id[1], 'content')
end
setFieldValueIfNotNil('Transaction', 'ReferenceNumber', a_id)
-- the content of a physical location is an array.
local physicLocation = extractNoteContent(notes, 'type', 'physloc', 'content')
if physicLocation ~= nil then
physicLocation = physicLocation[1]
end
setFieldValueIfNotNil('Transaction', 'SubLocation', physicLocation)
local accessRestrictNotes = extractNoteContent(notes, 'type', 'accessrestrict', 'subnotes')
if accessRestrictNotes and #accessRestrictNotes > 0 then
local subnoteContent = ExtractProperty(accessRestrictNotes[1], 'content')
if subnoteContent then
-- Aeon Transaction fields
local truncated = subnoteContent:sub(0, 255)
setFieldValueIfNotNil('Transaction', 'ItemInfo2', truncated)
end
end
elseif documentType[3] == 'accessions' then
-- there is only two types of document: resources and accessions
-- if the else part is reached, that means the current document is an accession.
local creatorApiPath = getAccessionCreatorAgentById(documentType[#documentType], settings["repoTable"][repoCode])
if creatorApiPath ~= nil and creatorApiPath ~= '' then
local agentJson = getElementBySearchQuery(creatorApiPath)
if agentJson ~= nil and agentJson ~= '' then
local creatorNames = ExtractProperty(agentJson, 'names')
if creatorNames ~= nil and creatorNames ~= '' and #creatorNames > 0 then
setFieldValueIfNotNil("Transaction", "ItemAuthor", ExtractProperty(creatorNames[1], 'sort_name'))
end
end
end
end
end
LogDebug("Switching to the detail tab.")
ExecuteCommand("SwitchTab", {"Detail"})
end
function getAccessionCreatorAgentById(accessId, repoId)
local searchQuery = '/repositories/'..repoId..'/accessions/'..accessId
local accessJson = getElementBySearchQuery(searchQuery)
if accessJson ~= nil then
local linked_agents = ExtractProperty(accessJson, 'linked_agents')
if linked_agents ~= nil then
for _, v in pairs(linked_agents) do
if ExtractProperty(v, 'role') == 'creator' then
return ExtractProperty(v, 'ref')
end
end
end
end
return nil
end
function extractNoteContent(notesArray, jsonField, fieldValue, toExtract)
for i = 1, #notesArray do
local currNote = notesArray[i]
if currNote[jsonField] == fieldValue then
return currNote[toExtract]
end
end
return nil
end
function ExtractProperty(object, property)
if object then
return EmptyStringIfNil(object[property])
end
end
function EmptyStringIfNil(value)
if (value == nil or value == JsonParser.NIL) then
return ""
else
return value
end
end