2 * Z39.50 search and import
5 angular.module('egCatZ3950Search',
6 ['ngRoute', 'ui.bootstrap', 'ngOrderObjectBy', 'egCoreMod', 'egUiMod', 'egGridMod', 'egZ3950Mod', 'egMarcMod'])
8 .config(function($routeProvider, $locationProvider, $compileProvider) {
9 $locationProvider.html5Mode(true);
10 $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|mailto|blob):/); // grid export
12 var resolver = {delay : function(egStartup) {return egStartup.go()}};
14 // search page shows the list view by default
15 $routeProvider.when('/cat/z3950/search', {
16 templateUrl: './cat/z3950/t_list',
17 controller: 'Z3950SearchCtrl',
21 // default page / bucket view
22 $routeProvider.otherwise({redirectTo : '/cat/z3950/search'});
26 * List view - grid stuff
28 .controller('Z3950SearchCtrl',
29 ['$scope','$q','$location','$timeout','$window','egCore','egGridDataProvider','egZ3950TargetSvc','$uibModal',
30 'egConfirmDialog','egAlertDialog',
31 function($scope , $q , $location , $timeout , $window, egCore , egGridDataProvider, egZ3950TargetSvc, $uibModal,
32 egConfirmDialog, egAlertDialog) {
34 // get list of targets
35 egZ3950TargetSvc.loadTargets();
36 egZ3950TargetSvc.loadActiveSearchFields();
38 $scope.field_strip_groups = [];
39 egCore.startup.go().then(function() {
40 // and list of field strip groups; need to ensure
41 // that enough of the startup has happened so that
42 // we have the user WS
43 egCore.pcrud.search('vibtg',
51 transform : 'actor.org_unit_ancestors',
57 id : egCore.auth.user().ws_ou()
62 { order_by : { vibtq : ['label'] } }
63 ).then(null, null, function(strip_group) {
64 strip_group.selected = false;
65 $scope.field_strip_groups.push(strip_group);
69 $scope.total_hits = 0;
71 var bib_sources = null;
72 egCore.pcrud.retrieveAll('cbs', {}, {atomic : true})
73 .then(function(l) { bib_sources = l; });
75 var provider = egGridDataProvider.instance({});
77 provider.get = function(offset, count) {
78 var deferred = $q.defer();
80 var query = egZ3950TargetSvc.currentQuery();
81 if (!query.raw_search && Object.keys(query.search).length == 0) {
85 var method = query.raw_search ?
86 'open-ils.search.z3950.search_service' :
87 'open-ils.search.z3950.search_class';
89 if (query.raw_search) {
90 query.query = query.raw_search;
91 delete query['search'];
92 delete query['raw_search'];
93 query.service = query.service[0];
94 query.username = query.username[0];
95 query.password = query.password[0];
98 query['limit'] = count;
99 query['offset'] = offset;
101 var resultIndex = offset;
102 $scope.total_hits = 0;
103 $scope.searchInProgress = true;
110 function() { $scope.searchInProgress = false; deferred.resolve() },
113 // FIXME when the search offset is > 0, the
114 // total hits count can be wrong if one of the
115 // Z39.50 targets has fewer than $offset hits; in that
116 // case, result.count is not supplied.
117 $scope.total_hits += (result.count || 0);
118 for (var i in result.records) {
119 result.records[i].mvr['bibid'] = result.records[i].bibid;
120 result.records[i].mvr['service'] = result.service;
121 result.records[i].mvr['index'] = resultIndex++;
122 result.records[i].mvr['marcxml'] = result.records[i].marcxml;
123 deferred.notify(result.records[i].mvr);
128 return deferred.promise;
131 $scope.z3950SearchGridProvider = provider;
132 $scope.gridControls = {};
134 $scope.search = function() {
135 $scope.z3950SearchGridProvider.refresh();
137 $scope.clearForm = function() {
138 egZ3950TargetSvc.clearSearchFields();
141 $scope.saveDefaultZ3950Targets = function() {
142 egZ3950TargetSvc.saveDefaultZ3950Targets();
145 var display_form = true;
146 $scope.show_search_form = function() {
149 $scope.toggle_search_form = function() {
150 display_form = !display_form;
153 $scope.raw_search_impossible = function() {
154 return egZ3950TargetSvc.rawSearchImpossible();
157 $scope.get_bibsrc_name_from_id = function(bs_id){
158 // var sel_bib_src = bib_src.id ? bib_src.list.filter(s => s.id() == bib_src.id) : null;
159 // TODO can we use arrow syntax yet???
160 if (!bs_id) return null;
161 var cbs = bib_sources.filter(function(s){ return s.id() == bs_id });
163 return (cbs && cbs[0] ? cbs[0].source() : null);
166 $scope.showRawSearchForm = function() {
168 templateUrl: './cat/z3950/t_raw_search',
172 ['$scope', '$uibModalInstance', function($scope, $uibModalInstance) {
173 egZ3950TargetSvc.setRawSearch('');
174 $scope.focusMe = true;
175 $scope.ok = function(args) { $uibModalInstance.close(args) }
176 $scope.cancel = function () { $uibModalInstance.dismiss() }
178 }).result.then(function (args) {
179 if (!args || !args.raw_search) return;
181 egZ3950TargetSvc.setRawSearch(args.raw_search);
182 $scope.z3950SearchGridProvider.refresh();
186 $scope.showInCatalog = function() {
187 var items = $scope.gridControls.selectedItems();
188 // relying on cant_showInCatalog to protect us
189 var url = '/eg2/staff/catalog/record/' + items[0]['bibid'];
190 $timeout(function() { $window.open(url, '_blank') });
192 $scope.cant_showInCatalog = function() {
193 var items = $scope.gridControls.selectedItems();
194 if (items.length != 1) return true;
195 if (items[0]['service'] == 'native-evergreen-catalog') return false;
199 $scope.local_overlay_target = egCore.hatch.getLocalItem('eg.cat.marked_overlay_record') || 0;
200 $scope.local_overlay_target_tcn = egCore.hatch.getLocalItem('eg.cat.marked_overlay_tcn') || 0;
201 if($scope.local_overlay_target) {
202 var currTarget = $scope.local_overlay_target;
205 $scope.mark_as_overlay_target = function() {
206 var items = $scope.gridControls.selectedItems();
207 if ($scope.local_overlay_target == items[0]['bibid']) {
208 $scope.local_overlay_target = 0;
209 $scope.local_overlay_target_tcn = 0;
211 $scope.local_overlay_target = items[0]['bibid'];
212 var currTarget = items[0] ['bibid'];
215 egCore.hatch.setLocalItem('eg.cat.marked_overlay_record',$scope.local_overlay_target);
216 egCore.hatch.setLocalItem('eg.cat.marked_overlay_tcn',$scope.local_overlay_target_tcn);
219 function get_tcn(currTarget) {
220 egCore.pcrud.retrieve('bre', currTarget, {
221 select: {bre: ['tcn_value']}
222 }).then(function(rec) {
223 $scope.local_overlay_target_tcn = rec.tcn_value();
228 $scope.cant_overlay = function() {
229 if (!$scope.local_overlay_target) return true;
230 var items = $scope.gridControls.selectedItems();
231 if (items.length != 1) return true;
233 items[0]['service'] == 'native-evergreen-catalog' &&
234 items[0]['bibid'] == $scope.local_overlay_target
239 $scope.selectFieldStripGroups = function() {
241 angular.forEach($scope.field_strip_groups, function(grp, idx) {
243 groups.push(grp.id());
249 $scope.import = function() {
250 var items = $scope.gridControls.selectedItems();
251 return $scope._import(items[0]['marcxml']);
254 $scope._import = function(marc_xml,bib_source) {
256 var bibsrc_name = $scope.get_bibsrc_name_from_id(bib_source);
258 var deferred = $q.defer();
261 'open-ils.cat.biblio.record.xml.import',
267 $scope.selectFieldStripGroups()
269 function(result) { deferred.resolve(result) },
272 var evt = egCore.evt.parse(result);
274 if (evt.textcode == 'TCN_EXISTS') {
276 egCore.strings.TCN_EXISTS
279 // we shouldn't get here
280 egAlertDialog.open(egCore.strings.TCN_EXISTS_ERR);
283 egConfirmDialog.open(
284 egCore.strings.IMPORTED_RECORD_FROM_Z3950,
285 egCore.strings.IMPORTED_RECORD_FROM_Z3950_AS_ID,
286 { id : result.id() },
287 egCore.strings.GO_TO_RECORD,
288 egCore.strings.GO_BACK
289 ).result.then(function() {
290 $window.open('/eg2/staff/catalog/record/' + result.id());
296 return deferred.promise;
298 $scope.need_one_selected = function() {
299 var items = $scope.gridControls.selectedItems();
300 if (items.length == 1) return false;
304 $scope.spawn_editor = function() {
305 var items = $scope.gridControls.selectedItems();
307 var _import = $scope._import;
309 templateUrl: './cat/z3950/t_marc_edit',
313 ['$scope', '$uibModalInstance', function($scope, $uibModalInstance) {
314 $scope.focusMe = true;
315 $scope.record_id = recId;
316 $scope.dirty_flag = false;
318 $scope.args.marc_xml = items[0]['marcxml'];
319 $scope.args.bib_source = null;
320 $scope.ok = function(args) { $uibModalInstance.close(args) }
321 $scope.cancel = function () { $uibModalInstance.dismiss() }
322 $scope.save_label = egCore.strings.IMPORT_BUTTON_LABEL;
323 // Wiring up angular inPlaceMode for editing later
324 $scope.in_place_mode = true;
325 $scope.import_record_callback = function () {
326 if($scope.in_place_mode) {
327 // This timeout is required to allow angular to finish variable assignments
328 // in the marcediter app. Allowing marc_xml to propigate here.
329 $timeout( function() {
330 _import($scope.args.marc_xml, $scope.args.bib_source).then( function(record_obj) {
331 if( record_obj.id ) {
332 $scope.record_id = record_obj.id();
333 $scope.save_label = egCore.strings.SAVE_BUTTON_LABEL;
334 // Successful import, no longer want this special z39.50 callback to execute.
335 $scope.in_place_mode = undefined;
342 }).result.then(function () {
344 $window.location.href = '/eg2/staff/catalog/record/' + recId;
349 $scope.view_marc = function() {
350 var items = $scope.gridControls.selectedItems();
352 templateUrl: './cat/z3950/t_marc_html',
356 ['$scope', '$uibModalInstance', function($scope, $uibModalInstance) {
357 $scope.focusMe = true;
358 $scope.marc_xml = items[0]['marcxml'];
359 $scope.isbn = (items[0].isbn() || '').replace(/ .*/, '');
360 $scope.ok = function(args) { $uibModalInstance.close(args) }
361 $scope.cancel = function () { $uibModalInstance.dismiss() }
363 }).result.then(function (args) {
364 if (!args || !args.name) return;
368 $scope.overlay_record = function() {
369 var items = $scope.gridControls.selectedItems();
370 var overlay_target = $scope.local_overlay_target;
371 var overlay_target_tcn = $scope.local_overlay_target_tcn;
372 var live_overlay_target = egCore.hatch.getLocalItem('eg.cat.marked_overlay_record') || 0;
373 var live_overlay_target_tcn = egCore.hatch.getLocalItem('eg.cat.marked_overlay_tcn') || 0;
375 'marc_xml' : items[0]['marcxml'],
380 templateUrl: './cat/z3950/t_overlay',
384 ['$scope', '$uibModalInstance', function($scope, $uibModalInstance) {
386 $scope.immediate_merge = function () {
387 $scope.overlay_target.marc_xml = args.marc_xml;
388 egCore.pcrud.retrieve('bre', $scope.overlay_target.id)
389 .then(function(rec) {
390 $scope.overlay_target.orig_marc_xml = rec.marc();
391 $scope.merge_marc(); // in case a sticky value was already set
392 $scope.overlay_target_tcn = overlay_target_tcn;
396 $scope.merge_marc = function() {
397 if (!$scope.merge_profile) return;
400 'open-ils.cat.merge.marc.per_profile',
402 $scope.merge_profile,
403 [ args.marc_xml, $scope.overlay_target.orig_marc_xml ]
404 ).then(function(merged) {
406 $scope.overlay_target.marc_xml = merged;
407 $scope.overlay_target.merged = true;
412 $scope.editOverlayRecord = function() {
414 templateUrl: './cat/z3950/t_edit_overlay_record',
418 ['$scope', '$uibModalInstance', function($scope, $uibModalInstance) {
419 $scope.focusMe = true;
420 $scope.record_id = 0;
421 $scope.dirty_flag = false;
423 $scope.ok = function() { $uibModalInstance.close($scope.args) }
424 $scope.cancel = function () { $uibModalInstance.dismiss() }
426 }).result.then(function (args) {
428 if (!args || !args.name) return;
432 $scope.focusMe = true;
433 $scope.merge_profile = null;
434 $scope.overlay_target = {
436 live_id : live_overlay_target,
440 $scope.$watch('merge_profile', function(newVal, oldVal) {
441 if (newVal && newVal !== oldVal) {
447 args.overlay_target = $scope.overlay_target;
448 $scope.ok = function(args) { $uibModalInstance.close(args) };
449 $scope.cancel = function () { $uibModalInstance.dismiss() };
451 if (overlay_target != live_overlay_target) {
452 var confirm_title = egCore.strings.OVERLAY_CHANGED_TITLE;
453 var confirm_msg = egCore.strings.OVERLAY_CHANGED;
455 if (live_overlay_target == 0) { // someone unset the target...
456 confirm_title = egCore.strings.OVERLAY_REMOVED_TITLE;
457 confirm_msg = egCore.strings.OVERLAY_REMOVED;
460 egConfirmDialog.open(
463 { id : overlay_target, live_id : live_overlay_target,
464 tcn : overlay_target_tcn, live_tcn: live_overlay_target_tcn}
466 function () { // proceed -- but check live overlay for unset-ness
467 if (live_overlay_target != 0) {
468 $scope.overlay_target.id = $scope.overlay_target.live_id;
469 overlay_target = live_overlay_target;
471 $scope.immediate_merge();
478 $scope.immediate_merge();
482 }).result.then(function (args) {
483 var bibsrc_name = $scope.get_bibsrc_name_from_id(args.bib_source);
486 'open-ils.cat.biblio.record.marc.replace',
489 (args.overlay_target.merged ? args.overlay_target.marc_xml : args.marc_xml),
492 $scope.selectFieldStripGroups()
495 $scope.local_overlay_target = 0;
496 $scope.local_overlay_target_tcn = 0;
497 egCore.hatch.removeLocalItem('eg.cat.marked_overlay_record');
498 egCore.hatch.removeLocalItem('eg.cat.marked_overlay_tcn');
499 console.debug('overlay complete, target removed');
500 $window.open('/eg2/staff/catalog/record/' + overlay_target);