forked from AtlasSystems/Aeon-ArchivesSpace-Import
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathASImport.lua
More file actions
640 lines (538 loc) · 23.1 KB
/
Copy pathASImport.lua
File metadata and controls
640 lines (538 loc) · 23.1 KB
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
-- Aeon ArchivesSpace Import
-- This addon will utilize the ArchivesSpace API to look up the collection/item information matching a request in Aeon.
-- Please see README for workflow and configuration explanation.
import("System");
import("System.Xml");
import("System.Net");
import("System.IO");
import("log4net");
require("JsonParser");
require("AtlasHelpers");
local Settings = {};
Settings.RequestMonitorQueue = GetSetting("RequestMonitorQueue");
Settings.SuccessRouteQueue = GetSetting("SuccessRouteQueue");
Settings.ErrorRouteQueue = GetSetting("ErrorRouteQueue");
Settings.ApiBaseURL = GetSetting("ArchivesSpaceApiUrl");
Settings.ArchivesSpaceUsername = GetSetting("ArchivesSpaceUsername");
Settings.ArchivesSpacePassword = GetSetting("ArchivesSpacePassword");
Settings.TopContainerUriField = GetSetting("TopContainerUriField");
Settings.RepoCodeField = GetSetting("RepoCodeField");
Settings.RepoIdMapping = AtlasHelpers.StringSplit(",", GetSetting("RepoIdMapping"));
Settings.BarcodeField = GetSetting("BarcodeField");
Settings.LocationDestinationField = GetSetting("LocationDestinationField");
Settings.ContainerInformationField = GetSetting("ContainerInformationField");
Settings.CollectionIdentifierField = GetSetting("CollectionIdentifierField");
local addonName = "AeonASpaceImport";
local rootLogger = "AtlasSystems.Addons." .. addonName;
local log = LogManager.GetLogger(rootLogger);
local sessionId;
local sessionTimeStamp;
local invalidFieldSettings = false;
function TraverseError(e)
if not e.GetType then
-- Not a .NET type
return nil;
else
if not e.Message then
-- Not a .NET exception
log:Debug(e:ToString());
return nil;
end
end
log:Debug(e.Message);
if e.InnerException then
return TraverseError(e.InnerException);
else
return e.Message;
end
end
function GetAddonVersion()
local success, result = pcall(function()
local configDoc = XmlDocument();
configDoc:Load(AddonInfo.Directory .. "\\config.xml");
local versionNode = configDoc:SelectSingleNode("/Configuration/Version");
if versionNode then
return versionNode.InnerText;
end
end);
if success and result then
return result;
end
if not success then
log:Error("Failed to read addon version from config.xml: " .. TraverseError(result));
end
return "0.0.0";
end
local addonVersion = GetAddonVersion();
function Init()
RegisterSystemEventHandler("SystemTimerElapsed","InitiateASpaceImport");
end
function InitiateASpaceImport()
log:Debug("Initiate ArchivesSpace Import");
local barcode;
if NotNilOrBlank(Settings.RepoCodeField) or NotNilOrBlank(Settings.RepoIdMapping[1]) then
local success, barcodeOrErr = pcall(TagProcessor.ReplaceTags, Settings.BarcodeField);
if not success then
invalidFieldSettings = true;
log:Warn("The BarcodeField setting must be a valid Aeon field in the form of a tag.");
return;
else
barcode = barcodeOrErr;
end
end
if not AreSettingsValid() then
log:Warn("One or more settings are invalid. Addon will not run.");
return;
end
ProcessDataContexts("transactionstatus", Settings.RequestMonitorQueue, "ImportASpaceInfo");
end
function AreSettingsValid()
local function IsTag(value)
if value:find("{%w+:%w+%.%w+}") then
return true;
else
return false;
end
end
local settingsValid = true;
if not NotNilOrBlank(Settings.ApiBaseURL) then
log:Warn("The ArchivesSpaceApiUrl setting cannot be blank.");
settingsValid = false;
end
if not NotNilOrBlank(Settings.ArchivesSpaceUsername) then
log:Warn("The ArchivesSpaceUsername setting cannot be blank.");
settingsValid = false;
end
if not NotNilOrBlank(Settings.ArchivesSpacePassword) then
log:Warn("The ArchivesSpacePassword setting cannot be blank.");
settingsValid = false;
end
if not NotNilOrBlank(Settings.TopContainerUriField) and not NotNilOrBlank(Settings.RepoCodeField) and not NotNilOrBlank(Settings.RepoIdMapping[1]) then
log:Warn("One of the following settings must have a value: TopContainerUriField, RepoCodeField, or RepoIdMapping.");
settingsValid = false;
end
if (NotNilOrBlank(Settings.RepoCodeField) or NotNilOrBlank(Settings.RepoIdMapping[1])) and not NotNilOrBlank(Settings.BarcodeField) then
log:Warn("The BarcodeField setting cannot be blank when using RepoCodeField or RepoIdMapping.");
settingsValid = false;
end
if NotNilOrBlank(Settings.RepoCodeField) and not IsTag(Settings.RepoCodeField) then
log:Warn("The RepoCodeField setting must be a tag.");
settingsValid = false;
end
if NotNilOrBlank(Settings.BarcodeField) and not IsTag(Settings.BarcodeField) then
log:Warn("The BarcodeField must be a tag.");
settingsValid = false;
end
return settingsValid;
end
function ImportASpaceInfo()
-- If a relevant field setting is invalid, we don't want to keep processing transactions.
if invalidFieldSettings then
return;
end
sessionId = GetSessionId();
local transactionNumber = GetFieldValue("Transaction", "TransactionNumber");
if (sessionId == nil or sessionId == "") then
log:Info("Unable to retrieve auth session token from ArchiveSpace API. Skipping this processing interval. The addon will try again on the next interval.")
return;
end
local barcode;
if NotNilOrBlank(Settings.RepoCodeField) or NotNilOrBlank(Settings.RepoIdMapping[1]) then
local success, barcodeOrErr = pcall(TagProcessor.ReplaceTags, Settings.BarcodeField);
if not success then
invalidFieldSettings = true;
log:Warn("The BarcodeField setting must be a valid Aeon field in the form of a tag.");
return;
else
barcode = barcodeOrErr;
end
end
local containerType;
local containerIndicator;
if NotNilOrBlank(Settings.RepoCodeField) or NotNilOrBlank(Settings.RepoIdMapping[1]) then
local success, containerInfoOrErr = pcall(TagProcessor.ReplaceTags, Settings.ContainerInformationField);
if not success then
invalidFieldSettings = true;
log:Warn("The ContainerInformationField setting must be a valid Aeon field in the form of a tag.");
return;
else
containerType = string.match(containerInfoOrErr, "(.-)%s");
containerIndicator = string.match(containerInfoOrErr, "%s(.+)");
end
end
local collectionId;
if NotNilOrBlank(Settings.RepoCodeField) or NotNilOrBlank(Settings.RepoIdMapping[1]) then
local success, collectionIdOrErr = pcall(TagProcessor.ReplaceTags, Settings.CollectionIdentifierField);
if not success then
invalidFieldSettings = true;
log:Warn("The CollectionIdentifierField setting must be a valid Aeon field in the form of a tag.");
return;
else
collectionId = collectionIdOrErr;
end
end
if Settings.TopContainerUriField ~= "" then
local topContainerUriSuccess, topContainerUri = pcall(GetFieldValue, "Transaction.CustomFields", Settings.TopContainerUriField);
if not topContainerUriSuccess then
invalidFieldSettings = true;
log:Warn("The TopContainerUriField setting must be a valid Aeon custom field.");
return;
end
if not NotNilOrBlank(topContainerUri) then
log:Info("Top container URI is missing from " .. Settings.TopContainerUriField .. " for Transaction " .. transactionNumber);
return;
end
local importSuccess, location = pcall(ImportByTopContainerUri, topContainerUri .. "?resolve%5B%5D=container_locations");
if importSuccess and NotNilOrBlank(location) then
SetLocationAndRoute(location, transactionNumber);
else
HandleLocationError(location, transactionNumber, "top container URI", topContainerUri);
end
elseif Settings.RepoCodeField ~= "" then
local repoCode = TagProcessor.ReplaceTags(Settings.RepoCodeField);
if repoCode == "" then
log:Info("repo_code is missing from " .. Settings.RepoCodeField:match("%.(.+)}") .. " for Transaction " .. transactionNumber);
return;
end
local success, repoId = pcall(GetRepoId, repoCode);
if success and NotNilOrBlank(repoId) then
-- if there's a barcode, query on that first.
-- this won't always succeed, so we try it, and if it fails, continue as if it didn't exist
if NotNilOrBlank(barcode) then
log:Info("Running barcode lookup for " .. transactionNumber);
local success, location = pcall(ImportByRepoIdAndBarcode, repoId, barcode);
if success and NotNilOrBlank(location) then
SetLocationAndRoute(location, transactionNumber);
return;
end
end
log:Info("Running collection search for " .. transactionNumber);
if not NotNilOrBlank(containerType) or not NotNilOrBlank(containerIndicator) then
log:Info("Transaction " .. transactionNumber .. " does not have both an item type and indicator");
HandleLocationError(location, transactionNumber, "container info", "N/A");
else
local success, location, barcode = pcall(ImportByRepoIdAndContainerInfo, repoId, collectionId, containerType, AtlasHelpers.Trim(containerIndicator));
if success and NotNilOrBlank(barcode) then
SetBarcode(barcode, transactionNumber);
end
if success and NotNilOrBlank(location) then
log:Info("Found item for " .. transactionNumber);
SetLocationAndRoute(location, transactionNumber);
else
log:Info("Did not find item for " .. transactionNumber);
HandleLocationError(location, transactionNumber, "barcode", barcode);
end
end
else
if not NotNilOrBlank(repoId) then
log:Info("Repo ID not found for Transaction " .. transactionNumber .. " with repo_code " .. repoCode);
else
OnError(repoId);
end
ExecuteCommand("Route", {transactionNumber, Settings.ErrorRouteQueue});
end
elseif NotNilOrBlank(Settings.RepoIdMapping[1]) then
local siteCode = GetFieldValue("Transaction", "Site");
local repoId = nil;
for i = 1, #Settings.RepoIdMapping do
if Settings.RepoIdMapping[i]:lower():find(siteCode:lower()) then
repoId = Settings.RepoIdMapping[i]:match("^(%d+)=");
break;
end
end
if not repoId then
log:Info("Unable to determine repo ID from RepoIdMapping.");
return;
end
if NotNilOrBlank(barcode) then
-- see above re: barcode querying
log:Info("Running barcode lookup for " .. transactionNumber);
local success, location = pcall(ImportByRepoIdAndBarcode, repoId, barcode);
if success and NotNilOrBlank(location) then
SetLocationAndRoute(location, transactionNumber);
return;
end
end
log:Info("Running collection search for " .. transactionNumber);
if not NotNilOrBlank(containerType) or not NotNilOrBlank(containerIndicator) then
log:Info("Transaction " .. transactionNumber .. " does not have both an item type and indicator");
HandleLocationError(location, transactionNumber, "N/A");
else
local success, location, barcode = pcall(ImportByRepoIdAndContainerInfo, repoId, collectionId, containerType, containerIndicator);
if success and NotNilOrBlank(barcode) then
SetBarcode(barcode, transactionNumber);
end
if success and NotNilOrBlank(location) then
log:Info("Found item for " .. transactionNumber);
SetLocationAndRoute(location, transactionNumber);
else
log:Info("Did not find item for " .. transactionNumber);
HandleLocationError(location, transactionNumber, "barcode", barcode);
end
end
else
log:Info("Invalid configuration. TopContainerUri, RepoCodeField, or RepoIdMapping must contain a value.");
return;
end
end
function ImportByTopContainerUri(topContainerUri)
log:Debug("Retrieving ASpace info for top level container " .. topContainerUri);
local response = SendApiRequest(topContainerUri, "GET", nil, sessionId);
log:Debug("API request completed");
local parsedResponse = JsonParser:ParseJSON(response);
local location = GetCurrentContainerLocation(parsedResponse);
if location ~= nil then
log:Debug("Location: " .. location);
else
log:Debug("No location found for container " .. topContainerUri);
end
return location;
end
function ImportByRepoIdAndBarcode(repoId, barcode)
log:Debug("Retrieving ASpace info for barcode " .. barcode);
local apiPath = "/repositories/" .. repoId .. "/top_containers/search?q=" .. barcode .. "&fields[]=json&page=1";
local response = SendApiRequest(apiPath, "GET", nil, sessionId);
log:Debug("API Request completed");
local parsedResponse = JsonParser:ParseJSON(response);
local location = GetCurrentContainerLocation(parsedResponse);
if NotNilOrBlank(location) then
log:Debug("Location: " .. location);
else
log:Debug("No location found for barcode " .. barcode .. " in repo " .. repoId);
end
return location;
end
function ImportByRepoIdAndContainerInfo(repoId, collectionId, containerType, containerIndicator)
-- drop punctuation and spacing, so the query more reliably goes through
local cleanedCollectionId = collectionId:gsub('[%p%c%s]', '');
local apiPath = '/repositories/' .. repoId .. '/search?filter_query[]=type_u_ssort:"' .. containerType .. '"&filter_query[]=indicator_u_icusort:"' .. containerIndicator .. '"&filter_query[]=collection_identifier_u_stext:"' .. cleanedCollectionId .. '"&page=1';
local response = SendApiRequest(apiPath, "GET", nil, sessionId);
log:Debug("API Request completed");
local parsedResponse = JsonParser:ParseJSON(response);
local location = GetCurrentContainerLocationFromSearch(parsedResponse);
if NotNilOrBlank(location) then
log:Debug("Location: " .. location);
else
log:Debug("No location found for container " .. containerType .. " " .. containerIndicator .. " in collection " .. collectionId);
end
local barcode = GetBarcodeFromSearch(parsedResponse);
return location, barcode;
end
function GetRepoId(repoCode)
log:Debug("Retrieving list of ASpace repos");
local apiPath = "/repositories";
local response = SendApiRequest(apiPath, "GET", nil, sessionId);
log:Debug("API Request completed");
local parsedResponse = JsonParser:ParseJSON(response);
local repoId = nil;
for i = 1, #parsedResponse do
if parsedResponse[i].repo_code == repoCode then
repoId = parsedResponse[i].uri:match("%d+$");
break;
end
end
if NotNilOrBlank(repoId) then
log:Debug("Repo ID: " .. repoId);
else
log:Debug("No repo ID found for repo_code " .. repoCode);
end
return repoId;
end
function GetCurrentContainerLocation(parsedResponse)
-- The response from the /repositories/[repoId]/top_containers endpoint has the JSON we need in a "json" field
if parsedResponse["response"] then
log:Debug("Response first JSON field: " .. tostring(parsedResponse["response"]["docs"][1]["json"]));
end
local containerLocations = parsedResponse.container_locations or JsonParser:ParseJSON(parsedResponse["response"]["docs"][1]["json"]).container_locations;
local currentContainerLocationTitle;
-- If there are no locations, don't do anything
if next(containerLocations) == nil then
return nil;
end
for i = 1, #containerLocations do
if containerLocations[i].status == "current" then
currentContainerLocationTitle = containerLocations[i]._resolved.title;
break;
end
end
return currentContainerLocationTitle;
end
-- the /search endpoint returns a different data structure than /top_containers/search
function GetCurrentContainerLocationFromSearch(parsedResponse)
-- The response from the /repositories/[repoId]/search endpoint has the location in:
-- [response]/results/[1]/json/container_locations
log:Debug("Response first JSON field: " .. tostring(parsedResponse["results"][1]["json"]));
local containerLocations = parsedResponse.container_locations or JsonParser:ParseJSON(parsedResponse["results"][1]["json"]).container_locations;
local currentContainerLocationTitle;
-- If there are no locations, don't do anything
if next(containerLocations) == nil then
return nil;
end
for i = 1, #containerLocations do
if containerLocations[i].status == "current" then
currentContainerLocationTitle = containerLocations[i]._resolved.title;
break;
end
end
return currentContainerLocationTitle;
end
-- extension functions to get a barcode from responses and add barcodes to requests
function GetBarcodeFromSearch(parsedResponse)
local barcode = parsedResponse.barcode or JsonParser:ParseJSON(parsedResponse["results"][1]["json"]).barcode;
return barcode;
end
-- TODO: instead of hardcoding this, use the config
function SetBarcode(barcode, transactionNumber)
SetFieldValue("Transaction", "ItemNumber", barcode);
SaveDataSource("Transaction");
end
function SetLocationAndRoute(location, transactionNumber)
SetFieldValue("Transaction" , Settings.LocationDestinationField, location);
SetFieldValue("Transaction" , "SystemID", "ArchivesSpace + Location");
SaveDataSource("Transaction");
ExecuteCommand("Route", {transactionNumber, Settings.SuccessRouteQueue});
end
function GetAuthenticationToken()
local authenticationToken = JsonParser:ParseJSON(SendApiRequest('/users/' .. Settings.ArchivesSpaceUsername .. '/login', 'POST', "password=" .. Settings.ArchivesSpacePassword));
if (authenticationToken == nil or authenticationToken == JsonParser.NIL) then
log:Error("Unable to get valid authentication token.");
return;
end
return authenticationToken;
end
function GetSessionId()
if (sessionId == nil or sessionId == "" or (sessionTimeStamp + 60 * 60) < os.time()) then
log:Debug("Renewing ArchivesSpace authentication token.");
local authentication = GetAuthenticationToken();
sessionId = ExtractProperty(authentication, "session");
if (sessionId == nil or sessionId == JsonParser.NIL) then
log:Error("Unable to get valid session ID token.");
return;
end
sessionTimeStamp = os.time();
end
return sessionId;
end
function SendApiRequest(apiPath, method, parameters, sessionId)
local webClient = WebClient();
webClient.Headers:Clear();
webClient.Headers:Add("User-Agent", addonName .. "/" .. addonVersion);
if (sessionId ~= nil and sessionId ~= "") then
webClient.Headers:Add("X-ArchivesSpace-Session", sessionId);
end
local success, result;
if (method == 'POST') then
success, result = pcall(WebClientPost, webClient, apiPath, method, parameters);
else
success, result = pcall(WebClientGet, webClient, apiPath);
end
webClient:Dispose();
if (success) then
log:Debug("API call successful");
log:Debug("Response: " .. tostring(result));
return result;
else
log:Debug("API call error");
log:Error(GetWebExceptionMessage(result));
return "";
end
end
function HandleLocationError(location, transactionNumber, fieldType, fieldValue);
if not NotNilOrBlank(location) then
log:Info("No location found for Transaction " .. transactionNumber .. " with " .. fieldType .. " " .. fieldValue);
else
OnError(location);
end
SetFieldValue("Transaction" , "SystemID", "ArchivesSpace + Location");
SaveDataSource("Transaction");
ExecuteCommand("Route", {transactionNumber, Settings.ErrorRouteQueue});
end
function ObjectToString(o)
if type(o) == 'table' then
local s = '{ '
for k,v in pairs(o) do
if type(k) ~= 'number' then k = '"'..k..'"' end
s = s .. '['..k..'] = ' .. ObjectToString(v) .. ','
end
return s .. '} '
else
return tostring(o)
end
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
function WebClientPost(webClient, apiPath, method, postParameters)
return webClient:UploadString(PathCombine(Settings.ApiBaseURL, apiPath), method, postParameters);
end
function WebClientGet(webClient, apiPath)
return webClient:DownloadString(PathCombine(Settings.ApiBaseURL, apiPath));
end
function OnError(e)
if e == nil then
log:Error("OnError supplied a nil error");
return;
end
if not e.GetType then
-- Not a .NET type
-- Attempt to log value
pcall(function ()
log:Error(e);
end);
return;
else
if not e.Message then
log:Error(e:ToString());
return;
end
end
local message = TraverseError(e);
if message == nil then
message = "Unspecified Error";
end
log:Error("An error occurred while processing the ArchivesSpace API request:\r\n" .. message);
end
function PathCombine(path1, path2)
local trailingSlashPattern = '/$';
local leadingSlashPattern = '^/';
if (path1 and path2) then
local result = path1:gsub(trailingSlashPattern, '') .. '/' .. path2:gsub(leadingSlashPattern, '');
return result;
else
return "";
end
end
function NotNilOrBlank(value)
if value == nil or value == JsonParser.NIL or value == "" then
return false;
else
return true;
end
end
function GetWebExceptionMessage(exception)
local message = "";
if exception and exception.Message then
message = exception.Message;
if (exception.InnerException) then
message = message .. "\r\n" .. GetWebExceptionMessage(exception.InnerException);
if exception.InnerException.Response and exception.InnerException.Response ~= "Response" then
-- This is necessary to get the response body from exceptions thrown by WebClients.
local streamReader = StreamReader(exception.InnerException.Response:GetResponseStream());
local responseContent = streamReader:ReadToEnd();
log:Debug("Web exception response: \r\n" .. responseContent);
end
end
elseif exception then
message = exception;
end
return message;
end