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['service'] = result.service;
120 result.records[i].mvr['index'] = resultIndex++;
121 result.records[i].mvr['marcxml'] = result.records[i].marcxml;
122 deferred.notify(result.records[i].mvr);
127 return deferred.promise;
130 $scope.z3950SearchGridProvider = provider;
131 $scope.gridControls = {};
133 $scope.search = function() {
134 $scope.z3950SearchGridProvider.refresh();
136 $scope.clearForm = function() {
137 egZ3950TargetSvc.clearSearchFields();
140 $scope.saveDefaultZ3950Targets = function() {
141 egZ3950TargetSvc.saveDefaultZ3950Targets();
144 var display_form = true;
145 $scope.show_search_form = function() {
148 $scope.toggle_search_form = function() {
149 display_form = !display_form;
152 $scope.raw_search_impossible = function() {
153 return egZ3950TargetSvc.rawSearchImpossible();
156 $scope.get_bibsrc_name_from_id = function(bs_id){
157 // var sel_bib_src = bib_src.id ? bib_src.list.filter(s => s.id() == bib_src.id) : null;
158 // TODO can we use arrow syntax yet???
159 if (!bs_id) return null;
160 var cbs = bib_sources.filter(function(s){ return s.id() == bs_id });
162 return (cbs && cbs[0] ? cbs[0].source() : null);
165 $scope.showRawSearchForm = function() {
167 templateUrl: './cat/z3950/t_raw_search',
171 ['$scope', '$uibModalInstance', function($scope, $uibModalInstance) {
172 egZ3950TargetSvc.setRawSearch('');
173 $scope.focusMe = true;
174 $scope.ok = function(args) { $uibModalInstance.close(args) }
175 $scope.cancel = function () { $uibModalInstance.dismiss() }
177 }).result.then(function (args) {
178 if (!args || !args.raw_search) return;
180 egZ3950TargetSvc.setRawSearch(args.raw_search);
181 $scope.z3950SearchGridProvider.refresh();
185 $scope.showInCatalog = function() {
186 var items = $scope.gridControls.selectedItems();
187 // relying on cant_showInCatalog to protect us
188 var url = '/eg2/staff/catalog/record/' + items[0].tcn();
189 $timeout(function() { $window.open(url, '_blank') });
191 $scope.cant_showInCatalog = function() {
192 var items = $scope.gridControls.selectedItems();
193 if (items.length != 1) return true;
194 if (items[0]['service'] == 'native-evergreen-catalog') return false;
198 $scope.local_overlay_target = egCore.hatch.getLocalItem('eg.cat.marked_overlay_record') || 0;
199 $scope.mark_as_overlay_target = function() {
200 var items = $scope.gridControls.selectedItems();
201 if ($scope.local_overlay_target == items[0].tcn()) {
202 $scope.local_overlay_target = 0;
204 $scope.local_overlay_target = items[0].tcn();
206 egCore.hatch.setLocalItem('eg.cat.marked_overlay_record',$scope.local_overlay_target);
208 $scope.cant_overlay = function() {
209 if (!$scope.local_overlay_target) return true;
210 var items = $scope.gridControls.selectedItems();
211 if (items.length != 1) return true;
213 items[0]['service'] == 'native-evergreen-catalog' &&
214 items[0].tcn() == $scope.local_overlay_target
219 $scope.selectFieldStripGroups = function() {
221 angular.forEach($scope.field_strip_groups, function(grp, idx) {
223 groups.push(grp.id());
229 $scope.import = function() {
230 var items = $scope.gridControls.selectedItems();
231 return $scope._import(items[0]['marcxml']);
234 $scope._import = function(marc_xml,bib_source) {
236 var bibsrc_name = $scope.get_bibsrc_name_from_id(bib_source);
238 var deferred = $q.defer();
241 'open-ils.cat.biblio.record.xml.import',
247 $scope.selectFieldStripGroups()
249 function(result) { deferred.resolve(result) },
252 var evt = egCore.evt.parse(result);
254 if (evt.textcode == 'TCN_EXISTS') {
256 egCore.strings.TCN_EXISTS
259 // we shouldn't get here
260 egAlertDialog.open(egCore.strings.TCN_EXISTS_ERR);
263 egConfirmDialog.open(
264 egCore.strings.IMPORTED_RECORD_FROM_Z3950,
265 egCore.strings.IMPORTED_RECORD_FROM_Z3950_AS_ID,
266 { id : result.id() },
267 egCore.strings.GO_TO_RECORD,
268 egCore.strings.GO_BACK
269 ).result.then(function() {
270 $window.open('/eg2/staff/catalog/record/' + result.id());
276 return deferred.promise;
278 $scope.need_one_selected = function() {
279 var items = $scope.gridControls.selectedItems();
280 if (items.length == 1) return false;
284 $scope.spawn_editor = function() {
285 var items = $scope.gridControls.selectedItems();
287 var _import = $scope._import;
289 templateUrl: './cat/z3950/t_marc_edit',
293 ['$scope', '$uibModalInstance', function($scope, $uibModalInstance) {
294 $scope.focusMe = true;
295 $scope.record_id = recId;
296 $scope.dirty_flag = false;
298 $scope.args.marc_xml = items[0]['marcxml'];
299 $scope.args.bib_source = null;
300 $scope.ok = function(args) { $uibModalInstance.close(args) }
301 $scope.cancel = function () { $uibModalInstance.dismiss() }
302 $scope.save_label = egCore.strings.IMPORT_BUTTON_LABEL;
303 // Wiring up angular inPlaceMode for editing later
304 $scope.in_place_mode = true;
305 $scope.import_record_callback = function () {
306 if($scope.in_place_mode) {
307 // This timeout is required to allow angular to finish variable assignments
308 // in the marcediter app. Allowing marc_xml to propigate here.
309 $timeout( function() {
310 _import($scope.args.marc_xml, $scope.args.bib_source).then( function(record_obj) {
311 if( record_obj.id ) {
312 $scope.record_id = record_obj.id();
313 $scope.save_label = egCore.strings.SAVE_BUTTON_LABEL;
314 // Successful import, no longer want this special z39.50 callback to execute.
315 $scope.in_place_mode = undefined;
322 }).result.then(function () {
324 $window.location.href = '/eg2/staff/catalog/record/' + recId;
329 $scope.view_marc = function() {
330 var items = $scope.gridControls.selectedItems();
332 templateUrl: './cat/z3950/t_marc_html',
336 ['$scope', '$uibModalInstance', function($scope, $uibModalInstance) {
337 $scope.focusMe = true;
338 $scope.marc_xml = items[0]['marcxml'];
339 $scope.isbn = (items[0].isbn() || '').replace(/ .*/, '');
340 $scope.ok = function(args) { $uibModalInstance.close(args) }
341 $scope.cancel = function () { $uibModalInstance.dismiss() }
343 }).result.then(function (args) {
344 if (!args || !args.name) return;
348 $scope.overlay_record = function() {
349 var items = $scope.gridControls.selectedItems();
350 var overlay_target = $scope.local_overlay_target;
351 var live_overlay_target = egCore.hatch.getLocalItem('eg.cat.marked_overlay_record') || 0;
353 'marc_xml' : items[0]['marcxml'],
358 templateUrl: './cat/z3950/t_overlay',
362 ['$scope', '$uibModalInstance', function($scope, $uibModalInstance) {
364 $scope.immediate_merge = function () {
365 $scope.overlay_target.marc_xml = args.marc_xml;
366 egCore.pcrud.retrieve('bre', $scope.overlay_target.id)
367 .then(function(rec) {
368 $scope.overlay_target.orig_marc_xml = rec.marc();
369 $scope.merge_marc(); // in case a sticky value was already set
373 $scope.merge_marc = function() {
374 if (!$scope.merge_profile) return;
377 'open-ils.cat.merge.marc.per_profile',
379 $scope.merge_profile,
380 [ args.marc_xml, $scope.overlay_target.orig_marc_xml ]
381 ).then(function(merged) {
383 $scope.overlay_target.marc_xml = merged;
384 $scope.overlay_target.merged = true;
389 $scope.editOverlayRecord = function() {
391 templateUrl: './cat/z3950/t_edit_overlay_record',
395 ['$scope', '$uibModalInstance', function($scope, $uibModalInstance) {
396 $scope.focusMe = true;
397 $scope.record_id = 0;
398 $scope.dirty_flag = false;
400 $scope.ok = function() { $uibModalInstance.close($scope.args) }
401 $scope.cancel = function () { $uibModalInstance.dismiss() }
403 }).result.then(function (args) {
405 if (!args || !args.name) return;
409 $scope.focusMe = true;
410 $scope.merge_profile = null;
411 $scope.overlay_target = {
413 live_id : live_overlay_target,
417 $scope.$watch('merge_profile', function(newVal, oldVal) {
418 if (newVal && newVal !== oldVal) {
424 args.overlay_target = $scope.overlay_target;
425 $scope.ok = function(args) { $uibModalInstance.close(args) };
426 $scope.cancel = function () { $uibModalInstance.dismiss() };
428 if (overlay_target != live_overlay_target) {
429 var confirm_title = egCore.strings.OVERLAY_CHANGED_TITLE;
430 var confirm_msg = egCore.strings.OVERLAY_CHANGED;
432 if (live_overlay_target == 0) { // someone unset the target...
433 confirm_title = egCore.strings.OVERLAY_REMOVED_TITLE;
434 confirm_msg = egCore.strings.OVERLAY_REMOVED;
437 egConfirmDialog.open(
440 { id : overlay_target, live_id : live_overlay_target }
442 function () { // proceed -- but check live overlay for unset-ness
443 if (live_overlay_target != 0) {
444 $scope.overlay_target.id = $scope.overlay_target.live_id;
445 overlay_target = live_overlay_target;
447 $scope.immediate_merge();
454 $scope.immediate_merge();
458 }).result.then(function (args) {
459 var bibsrc_name = $scope.get_bibsrc_name_from_id(args.bib_source);
462 'open-ils.cat.biblio.record.marc.replace',
465 (args.overlay_target.merged ? args.overlay_target.marc_xml : args.marc_xml),
468 $scope.selectFieldStripGroups()
471 $scope.local_overlay_target = 0;
472 egCore.hatch.removeLocalItem('eg.cat.marked_overlay_record');
473 console.debug('overlay complete, target removed');
474 $window.open('/eg2/staff/catalog/record/' + overlay_target);