forked from hudmol/YaleAeonSpace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathYaleAeonAspace.lua
371 lines (291 loc) · 10.2 KB
/
YaleAeonAspace.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
function GetAuthenticationToken(username, password)
Log("Username = " .. username);
local apiPath = 'users/' .. username .. '/login'
Log('apiPath = ' .. apiPath, LOG_INFO);
local authenticationToken = JsonParser:ParseJSON(SendApiRequest(apiPath, 'POST', "password=".. UrlEncode(password)));
if (authenticationToken == nil or authenticationToken == JsonParser.NIL or authenticationToken == '') then
ReportError("Unable to get valid authentication token.");
return;
end
return authenticationToken
end
function GetSession(webclient, username, password)
local authentication = GetAuthenticationToken(username, password);
local sessionId = ExtractProperty(authentication, "session");
if (sessionId == nil or sessionId == JsonParser.NIL or sessionId == '') then
ReportError("Unable to get valid session ID token.");
return;
end
return sessionId;
end
function SendApiRequest(apiPath, method, parameters, authToken)
Log('[SendApiRequest] ' .. method);
Log('apiPath: ' .. apiPath);
local webClient = Ctx.WebClient()
webClient.Headers:Clear();
if (authToken ~= nil and authToken ~= "") then
webClient.Headers:Add("X-ArchivesSpace-Session", authToken);
end
local success, result;
success, result = pcall(WebClientPost, webClient, apiPath, method, parameters);
if (success) then
Log("API call successful");
Log("Response: " .. result);
return result;
else
Log("API call error");
OnError(result);
return "";
end
end
function WebClientPost(webClient, apiPath, method, postParameters)
return webClient:UploadString(PathCombine(Ctx.BaseUrl, apiPath), method, postParameters);
end
function Logout(webclient, sessionId)
webclient.QueryString = Ctx.NameValueCollection()
webclient.Headers:Add("X-ArchivesSpace-Session", sessionId)
local logoutPath = PathCombine(Ctx.BaseUrl, "users/logout")
pcall(webclient.UploadValues,
webclient,
logoutPath,
"POST",
Ctx.NameValueCollection())
end
function PerformSearch(query)
Log("PerformSearch", LOG_INFO)
local webclient = Ctx.WebClient()
-- This is the Atlas way of setting encoding from a type, saved as a reference in case the HM way below seems to stop working
-- webclient.Encoding = Types["System.Text.Encoding"].UTF8;
webclient.Encoding = Ctx.Encoding.UTF8;
local sessionId = GetSession(webclient, Ctx.Username, DecodeBase64(Ctx.Password))
Log("ASpace sessionId = " .. sessionId)
webclient.Headers:Add("X-ArchivesSpace-Session", sessionId)
webclient.QueryString = Ctx.NameValueCollection()
webclient.QueryString:Add("q", query)
Log("ASpace query = " .. query)
local hmPluginPath = PathCombine(Ctx.BaseUrl, "plugins/yale_as_requests/search")
local success, result = pcall(webclient.DownloadString,
webclient,
hmPluginPath)
pcall(Logout, webclient, sessionId)
if (success) then
local response = JsonParser:ParseJSON(result)
return response
else
Log("API call error")
OnError(result)
end
end
function ShowMessageInGrid(grid, message)
grid.GridControl:BeginUpdate()
grid.GridControl.MainView.Columns:Clear()
local msg = grid.GridControl.MainView.Columns:Add()
msg.Caption = "Message"
msg.FieldName = "Message"
msg.Name = "Message"
msg.Width = 10240
msg.Visible = true
msg.VisibleIndex = 0
msg.OptionsColumn.ReadOnly = true
local data = Ctx.DataTable()
data.Columns:Add("Message")
local row = data:NewRow();
row:set_Item("Message", message)
data.Rows:Add(row)
grid.GridControl.DataSource = data
grid.GridControl:EndUpdate()
grid.GridControl:Refresh()
end
function ConfigureForm()
local form = Ctx.InterfaceManager:CreateForm(Ctx.TabName, "ArchivesSpace Search")
local ribbon = form:CreateRibbonPage("ArchivesSpace Search")
local searchInput = form:CreateTextEdit("ArchivesSpaceSearch", "Search ArchivesSpace")
local grid = form:CreateGrid("ArchivesSpaceGrid", "ArchivesSpace Results")
local seenFields = {}
function HandleSearchInput(sender, args)
if tostring(args.KeyCode) == "Return: 13" then
ShowMessageInGrid(grid, "Searching...")
local results = PerformSearch(searchInput.Value)
ShowSearchResults(form, results, grid)
end
end
function HandleImport()
local selection = grid.GridControl.MainView:GetFocusedRow()
if selection == nil then
Ctx.InterfaceManager:ShowMessage("No record selected", "Import Failed")
return
end
-- Clear any previous fields
for k in pairs(seenFields) do
local success, _ = pcall(SetFieldValue, "Transaction", k, "")
end
seenFields = {}
local success, requestJson = pcall(selection.get_Item, selection, "request_json")
if not success then
Ctx.InterfaceManager:ShowMessage("No record selected", "Import Failed")
return
end
local mapped = JsonParser:ParseJSON(requestJson)
for k in pairs(mapped) do
local v = tostring(mapped[k]):sub(0, 255)
-- Use config nodes (indirectly) to map ArchivesSpace fields onto Aeon fields
-- Allows ArchivesSpace fields to be semantic and decoupled from Aeon fields
-- Implemented when rolling back from Aeon Custom Fields to core fields
if mapToAeon[k] then
k = mapToAeon[k];
end
local success, _ = pcall(SetFieldValue, "Transaction", k, v)
if success then
seenFields[k] = true
else
Log("Field not present in Transaction form: " .. k)
local success, _ = pcall(SetFieldValue, "Transaction.CustomFields", k, v)
if success then
seenFields[k] = true
else
Log("Custom or other field (still) not present in Transaction form: " .. k)
end
end
end
ExecuteCommand("SwitchTab", {"Detail"})
end
searchInput.Editor.KeyDown:Add(HandleSearchInput)
ribbon:CreateButton("Import Selected", GetClientImage("impt_32x32"), "HandleImport", "")
form:Show()
end
function ShowSearchResults(form, results, grid)
local tableData = Ctx.DataTable()
grid.GridControl.MainView.Columns:Clear()
for colIdx, columnDef in ipairs(results["columns"]) do
local col = grid.GridControl.MainView.Columns:Add()
col.Caption = columnDef["title"]
col.FieldName = columnDef["request_field"]
col.Name = columnDef["title"]
col.Width = columnDef["width"]
col.Visible = true
col.VisibleIndex = colIdx
col.OptionsColumn.ReadOnly = true
tableData.Columns:Add(columnDef["request_field"])
end
local hiddenRequest = grid.GridControl.MainView.Columns:Add()
hiddenRequest.Caption = "request_json"
hiddenRequest.FieldName = "request_json"
hiddenRequest.Name = "request_json"
hiddenRequest.Width = 0
hiddenRequest.Visible = false
hiddenRequest.VisibleIndex = -1
hiddenRequest.OptionsColumn.ReadOnly = true
tableData.Columns:Add("request_json")
for _, request in ipairs(results["requests"]) do
local row = tableData:NewRow();
for field in pairs(request) do
local success, _ = pcall(row.set_Item, row, field, request[field])
if not success then
Log("Failed to set field: " .. field)
end
end
tableData.Rows:Add(row)
end
if (tableData.Rows.Count == 0) then
ShowMessageInGrid(grid, "No matching records were found")
else
grid.GridControl.DataSource = tableData
end
end
--[[
ERROR HANDLING HELPERS
]]--
-- Internal intentional improved error handler
function ReportError(message)
if (message == nil) then
message = "Unspecific error";
end
Log("An error occurred: " .. message);
Ctx.InterfaceManager:ShowMessage("An error occurred:\r\n" .. message, Ctx.TabName);
end;
-- Primary Lua error handler
function OnError(e)
Log("[OnError]", LOG_DEBUG);
if e == nil then
Log("OnError supplied a nil error");
return;
end
if not e.GetType then
-- Not a .NET type
-- Attempt to log value
pcall(function ()
Log("Just the e: " .. e);
end);
return;
else
if not e.Message then
Log(e:ToString());
return;
end
end
local message = TraverseError(e);
if message == nil then
message = "Unspecified Error";
end
if message == "The operation has timed out" then
message = "The search time exceeded the allowed amount.\r\nThis most often happens when the search is too broad.\r\nPlease try again with a narrower search.";
end
ReportError(message);
end
-- Recursively logs exception messages and returns the innermost message to caller
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(e:ToString());
return nil;
end
end
Log(e.Message);
if e.InnerException then
return TraverseError(e.InnerException);
else
return e.Message;
end
end
--[[
GENERAL HELPERS
]]--
-- Makes a string appropriate for use in a URL
function UrlEncode(str)
if (str) then
str = string.gsub (str, "\n", "\r\n");
str = string.gsub (str, "([^%w ])",
function (c) return string.format ("%%%02X", string.byte(c)) end);
str = string.gsub (str, " ", "+");
end
return str;
end
-- Safely gets a property from an object
function ExtractProperty(object, property)
if object then
return EmptyStringIfNil(object[property]);
end
end
-- Ensures use of empty string rather than `nil` when you want
function EmptyStringIfNil(value)
if (value == nil or value == JsonParser.NIL) then
return "";
else
return value;
end
end
-- Combines two parts of a path, ensuring they're separated by a / character
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