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 = egCore.env.basePath +
189 'cat/catalog/record/' + items[0].tcn();
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.mark_as_overlay_target = function() {
201 var items = $scope.gridControls.selectedItems();
202 if ($scope.local_overlay_target == items[0].tcn()) {
203 $scope.local_overlay_target = 0;
205 $scope.local_overlay_target = items[0].tcn();
207 egCore.hatch.setLocalItem('eg.cat.marked_overlay_record',$scope.local_overlay_target);
209 $scope.cant_overlay = function() {
210 if (!$scope.local_overlay_target) return true;
211 var items = $scope.gridControls.selectedItems();
212 if (items.length != 1) return true;
214 items[0]['service'] == 'native-evergreen-catalog' &&
215 items[0].tcn() == $scope.local_overlay_target
220 $scope.selectFieldStripGroups = function() {
222 angular.forEach($scope.field_strip_groups, function(grp, idx) {
224 groups.push(grp.id());
230 $scope.import = function() {
231 var items = $scope.gridControls.selectedItems();
232 return $scope._import(items[0]['marcxml']);
235 $scope._import = function(marc_xml,bib_source) {
237 var bibsrc_name = $scope.get_bibsrc_name_from_id(bib_source);
239 var deferred = $q.defer();
242 'open-ils.cat.biblio.record.xml.import',
248 $scope.selectFieldStripGroups()
250 function(result) { deferred.resolve(result) },
253 var evt = egCore.evt.parse(result);
255 if (evt.textcode == 'TCN_EXISTS') {
257 egCore.strings.TCN_EXISTS
260 // we shouldn't get here
261 egAlertDialog.open(egCore.strings.TCN_EXISTS_ERR);
264 egConfirmDialog.open(
265 egCore.strings.IMPORTED_RECORD_FROM_Z3950,
266 egCore.strings.IMPORTED_RECORD_FROM_Z3950_AS_ID,
267 { id : result.id() },
268 egCore.strings.GO_TO_RECORD,
269 egCore.strings.GO_BACK
270 ).result.then(function() {
271 $window.open(egCore.env.basePath + 'cat/catalog/record/' + result.id());
277 return deferred.promise;
279 $scope.need_one_selected = function() {
280 var items = $scope.gridControls.selectedItems();
281 if (items.length == 1) return false;
285 $scope.spawn_editor = function() {
286 var items = $scope.gridControls.selectedItems();
288 var _import = $scope._import;
290 templateUrl: './cat/z3950/t_marc_edit',
294 ['$scope', '$uibModalInstance', function($scope, $uibModalInstance) {
295 $scope.focusMe = true;
296 $scope.record_id = recId;
297 $scope.dirty_flag = false;
299 $scope.args.marc_xml = items[0]['marcxml'];
300 $scope.args.bib_source = null;
301 $scope.ok = function(args) { $uibModalInstance.close(args) }
302 $scope.cancel = function () { $uibModalInstance.dismiss() }
303 $scope.save_label = egCore.strings.IMPORT_BUTTON_LABEL;
304 // Wiring up angular inPlaceMode for editing later
305 $scope.in_place_mode = true;
306 $scope.import_record_callback = function () {
307 if($scope.in_place_mode) {
308 // This timeout is required to allow angular to finish variable assignments
309 // in the marcediter app. Allowing marc_xml to propigate here.
310 $timeout( function() {
311 _import($scope.args.marc_xml, $scope.args.bib_source).then( function(record_obj) {
312 if( record_obj.id ) {
313 $scope.record_id = record_obj.id();
314 $scope.save_label = egCore.strings.SAVE_BUTTON_LABEL;
315 // Successful import, no longer want this special z39.50 callback to execute.
316 $scope.in_place_mode = undefined;
323 }).result.then(function () {
325 $window.location.href = egCore.env.basePath + 'cat/catalog/record/' + recId;
330 $scope.view_marc = function() {
331 var items = $scope.gridControls.selectedItems();
333 templateUrl: './cat/z3950/t_marc_html',
337 ['$scope', '$uibModalInstance', function($scope, $uibModalInstance) {
338 $scope.focusMe = true;
339 $scope.marc_xml = items[0]['marcxml'];
340 $scope.isbn = (items[0].isbn() || '').replace(/ .*/, '');
341 $scope.ok = function(args) { $uibModalInstance.close(args) }
342 $scope.cancel = function () { $uibModalInstance.dismiss() }
344 }).result.then(function (args) {
345 if (!args || !args.name) return;
349 $scope.overlay_record = function() {
350 var items = $scope.gridControls.selectedItems();
351 var overlay_target = $scope.local_overlay_target;
352 var live_overlay_target = egCore.hatch.getLocalItem('eg.cat.marked_overlay_record') || 0;
354 'marc_xml' : items[0]['marcxml'],
359 templateUrl: './cat/z3950/t_overlay',
363 ['$scope', '$uibModalInstance', function($scope, $uibModalInstance) {
365 $scope.immediate_merge = function () {
366 $scope.overlay_target.marc_xml = args.marc_xml;
367 egCore.pcrud.retrieve('bre', $scope.overlay_target.id)
368 .then(function(rec) {
369 $scope.overlay_target.orig_marc_xml = rec.marc();
370 $scope.merge_marc(); // in case a sticky value was already set
374 $scope.merge_marc = function() {
375 if (!$scope.merge_profile) return;
378 'open-ils.cat.merge.marc.per_profile',
380 $scope.merge_profile,
381 [ args.marc_xml, $scope.overlay_target.orig_marc_xml ]
382 ).then(function(merged) {
384 $scope.overlay_target.marc_xml = merged;
385 $scope.overlay_target.merged = true;
390 $scope.editOverlayRecord = function() {
392 templateUrl: './cat/z3950/t_edit_overlay_record',
396 ['$scope', '$uibModalInstance', function($scope, $uibModalInstance) {
397 $scope.focusMe = true;
398 $scope.record_id = 0;
399 $scope.dirty_flag = false;
401 $scope.ok = function() { $uibModalInstance.close($scope.args) }
402 $scope.cancel = function () { $uibModalInstance.dismiss() }
404 }).result.then(function (args) {
406 if (!args || !args.name) return;
410 $scope.focusMe = true;
411 $scope.merge_profile = null;
412 $scope.overlay_target = {
414 live_id : live_overlay_target,
418 $scope.$watch('merge_profile', function(newVal, oldVal) {
419 if (newVal && newVal !== oldVal) {
425 args.overlay_target = $scope.overlay_target;
426 $scope.ok = function(args) { $uibModalInstance.close(args) };
427 $scope.cancel = function () { $uibModalInstance.dismiss() };
429 if (overlay_target != live_overlay_target) {
430 var confirm_title = egCore.strings.OVERLAY_CHANGED_TITLE;
431 var confirm_msg = egCore.strings.OVERLAY_CHANGED;
433 if (live_overlay_target == 0) { // someone unset the target...
434 confirm_title = egCore.strings.OVERLAY_REMOVED_TITLE;
435 confirm_msg = egCore.strings.OVERLAY_REMOVED;
438 egConfirmDialog.open(
441 { id : overlay_target, live_id : live_overlay_target }
443 function () { // proceed -- but check live overlay for unset-ness
444 if (live_overlay_target != 0) {
445 $scope.overlay_target.id = $scope.overlay_target.live_id;
446 overlay_target = live_overlay_target;
448 $scope.immediate_merge();
455 $scope.immediate_merge();
459 }).result.then(function (args) {
460 var bibsrc_name = $scope.get_bibsrc_name_from_id(args.bib_source);
463 'open-ils.cat.biblio.record.marc.replace',
466 (args.overlay_target.merged ? args.overlay_target.marc_xml : args.marc_xml),
469 $scope.selectFieldStripGroups()
472 $scope.local_overlay_target = 0;
473 egCore.hatch.removeLocalItem('eg.cat.marked_overlay_record');
474 console.debug('overlay complete, target removed');
475 $window.open(egCore.env.basePath + 'cat/catalog/record/' + overlay_target);