From 014b62e9787871e1b96963bf5f4434e5b3f96472 Mon Sep 17 00:00:00 2001
From: Cesar Velez <cesar.velez@equinoxinitiative.org>
Date: Fri, 21 Sep 2018 11:46:59 -0400
Subject: [PATCH] LP#1727345 - fix bibsource when importing or overlaying

This addresses several issues that were causing the
bib source to either not display correctly on the record page
or not save correctly when editing z3950 imports/overlays
Refactors passing around of bib source into eg-marc-edit and it's
child directive egMarcEditBibsource.

Two paths to test:
Path A)
    1. Import a new bib record from z3950, but via "Edit then Import"
    2. Bring up marc editor and choose save w/ bib source.
    3. Verify chosen bib source saved correctly on record page.
Path B)
    1. Mark a record as target for Overlay, go to z3950 import.
    2. Make a search, select item for Overlay.
    3. Choose Edit z3950 record to bring up marceditor, save with a bib source.
    4. Verify chosen bib source saved correctly on record page.

Signed-off by: Cesar Velez <cesar.velez@equinoxinitiative.org>
Signed-off-by: Mike Rylander <mrylander@gmail.com>
Signed-off-by: Jane Sandberg <sandbej@linnbenton.edu>
---
 .../staff/cat/z3950/t_edit_overlay_record.tt2      |  2 +-
 .../src/templates/staff/cat/z3950/t_marc_edit.tt2  |  4 +--
 .../js/ui/default/staff/cat/services/marcedit.js   | 25 ++++++++++++----
 Open-ILS/web/js/ui/default/staff/cat/z3950/app.js  | 34 ++++++++++++++++++----
 4 files changed, 50 insertions(+), 15 deletions(-)

diff --git a/Open-ILS/src/templates/staff/cat/z3950/t_edit_overlay_record.tt2 b/Open-ILS/src/templates/staff/cat/z3950/t_edit_overlay_record.tt2
index 7ebc94e5f3..1281b43d75 100644
--- a/Open-ILS/src/templates/staff/cat/z3950/t_edit_overlay_record.tt2
+++ b/Open-ILS/src/templates/staff/cat/z3950/t_edit_overlay_record.tt2
@@ -5,7 +5,7 @@
     <h4 class="modal-title">[% l('Edit Overlay Record') %]</h4>
   </div>
   <div class="modal-body">
-    <eg-marc-edit-record dirty-flag="dirty_flag" record-id="record_id" marc-xml="args.marc_xml"
+    <eg-marc-edit-record dirty-flag="dirty_flag" record-id="record_id" marc-xml="args.marc_xml" bib-source="args.bib_source"
                          on-save="ok" in-place-mode="true" record-type="bre" save-label="[% l('Save') %]" />
   </div>
   <div class="modal-footer">
diff --git a/Open-ILS/src/templates/staff/cat/z3950/t_marc_edit.tt2 b/Open-ILS/src/templates/staff/cat/z3950/t_marc_edit.tt2
index bcbe41f7e4..6bc7086d64 100644
--- a/Open-ILS/src/templates/staff/cat/z3950/t_marc_edit.tt2
+++ b/Open-ILS/src/templates/staff/cat/z3950/t_marc_edit.tt2
@@ -5,8 +5,8 @@
     <h4 class="modal-title">[% l('Import Record') %]</h4>
   </div>
   <div class="modal-body">
-    <eg-marc-edit-record dirty-flag="dirty_flag" record-id="record_id" marc-xml="marc_xml"
-                         record-type="bre" save-label="{{save_label}}"
+    <eg-marc-edit-record dirty-flag="dirty_flag" record-id="record_id" marc-xml="args.marc_xml"
+                         record-type="bre" save-label="{{save_label}}" bib-source="args.bib_source"
                          on-save="import_record_callback" fast-add="true" in-place-mode="in_place_mode"
     />
   </div>
diff --git a/Open-ILS/web/js/ui/default/staff/cat/services/marcedit.js b/Open-ILS/web/js/ui/default/staff/cat/services/marcedit.js
index a3cca7ad80..d288669533 100644
--- a/Open-ILS/web/js/ui/default/staff/cat/services/marcedit.js
+++ b/Open-ILS/web/js/ui/default/staff/cat/services/marcedit.js
@@ -654,6 +654,7 @@ angular.module('egMarcMod', ['egCoreMod', 'ui.bootstrap'])
             dirtyFlag : '=',
             recordId : '=',
             marcXml : '=',
+            bibSource : '=?',
             onSave : '=',
             // in-place mode means that the editor is being
             // used just to munge some MARCXML client-side, rather
@@ -719,8 +720,11 @@ angular.module('egMarcMod', ['egCoreMod', 'ui.bootstrap'])
                     if (newVal != oldVal) egCore.hatch.setItem('cat.marcedit.flateditor', newVal);
                 });
 
+                // necessary to prevent ng-model scope hiding ugliness in egMarcEditBibSource:
+                $scope.bib_source = {
+                    id : $scope.bibSource ? $scope.bibSource : null
+                };
                 $scope.brandNewRecord = false;
-                $scope.bib_source = null;
                 $scope.record_type = $scope.recordType || 'bre';
                 $scope.max_undo = $scope.maxUndo || 100;
                 $scope.record_undo_stack = [];
@@ -1189,8 +1193,8 @@ angular.module('egMarcMod', ['egCoreMod', 'ui.bootstrap'])
                         $scope.dirtyFlag = false;
                         $scope.flat_text_marc = $scope.record.toBreaker();
 
-                        if ($scope.record_type == 'bre') {
-                            $scope.bib_source = $scope.Record().source();
+                        if ($scope.record_type == 'bre' && !$scope.brandNewRecord) {
+                            $scope.bib_source.id = $scope.bibSource = rec.source(); //$scope.Record().source();
                         }
 
                     }).then(function(){
@@ -1368,10 +1372,17 @@ angular.module('egMarcMod', ['egCoreMod', 'ui.bootstrap'])
                 };
 
                 $scope.saveRecord = function () {
+                    
                     if ($scope.inPlaceMode) {
                         $scope.marcXml = $scope.record.toXmlString();
+                        
+                        if ($scope.record_type == 'bre'){
+                            $scope.bibSource = $scope.bib_source.id;
+                        }
+
                         return $timeout(processOnSaveCallbacks);
                     }
+
                     $scope.mangle_005();
                     $scope.Record().editor(egCore.auth.user().id());
                     $scope.Record().edit_date('now');
@@ -1519,7 +1530,7 @@ angular.module('egMarcMod', ['egCoreMod', 'ui.bootstrap'])
         restrict: 'E',
         replace: true,
         template: '<span class="nullable">'+
-                    '<select class="form-control" ng-model="bib_source" ng-options="s.id() as s.source() for s in bib_sources | orderBy: \'source()\'">'+
+                    '<select class="form-control" ng-model="bib_source.id" ng-options="s.id() as s.source() for s in bib_sources | orderBy: \'source()\'">'+
                       '<option value="">Select a Source</option>'+
                     '</select>'+
                   '</span>',
@@ -1527,9 +1538,11 @@ angular.module('egMarcMod', ['egCoreMod', 'ui.bootstrap'])
             function ($scope , egCore) {
 
                 egCore.pcrud.retrieveAll('cbs', {}, {atomic : true})
-                    .then(function(list) { $scope.bib_sources = list; });
+                    .then(function(list) {
+                        $scope.bib_sources = list;
+                    });
 
-                $scope.$watch('bib_source',
+                $scope.$watch('bib_source.id',
                     function(newVal, oldVal) {
                         if (newVal !== oldVal) {
                             $scope.bre.source(newVal);
diff --git a/Open-ILS/web/js/ui/default/staff/cat/z3950/app.js b/Open-ILS/web/js/ui/default/staff/cat/z3950/app.js
index b524797630..94d68040e7 100644
--- a/Open-ILS/web/js/ui/default/staff/cat/z3950/app.js
+++ b/Open-ILS/web/js/ui/default/staff/cat/z3950/app.js
@@ -68,6 +68,10 @@ function($scope , $q , $location , $timeout , $window,  egCore , egGridDataProvi
 
     $scope.total_hits = 0;
 
+    var bib_sources = null;
+    egCore.pcrud.retrieveAll('cbs', {}, {atomic : true})
+        .then(function(l) { bib_sources = l; });
+
     var provider = egGridDataProvider.instance({});
 
     provider.get = function(offset, count) {
@@ -148,6 +152,16 @@ function($scope , $q , $location , $timeout , $window,  egCore , egGridDataProvi
     $scope.raw_search_impossible = function() {
         return egZ3950TargetSvc.rawSearchImpossible();
     }
+
+    $scope.get_bibsrc_name_from_id = function(bs_id){
+        // var sel_bib_src = bib_src.id ? bib_src.list.filter(s => s.id() == bib_src.id) : null;
+        // TODO can we use arrow syntax yet???
+        if (!bs_id) return null;
+        var cbs = bib_sources.filter(function(s){ return s.id() == bs_id });
+
+        return (cbs && cbs[0] ? cbs[0].source() : null);
+    };
+
     $scope.showRawSearchForm = function() {
         $uibModal.open({
             templateUrl: './cat/z3950/t_raw_search',
@@ -218,14 +232,17 @@ function($scope , $q , $location , $timeout , $window,  egCore , egGridDataProvi
         return $scope._import(items[0]['marcxml']);
     };
 
-    $scope._import = function(marc_xml) {
+    $scope._import = function(marc_xml,bib_source) {
+
+        var bibsrc_name = $scope.get_bibsrc_name_from_id(bib_source);
+
         var deferred = $q.defer();
         egCore.net.request(
             'open-ils.cat',
             'open-ils.cat.biblio.record.xml.import',
             egCore.auth.token(),
             marc_xml,
-            null, // FIXME bib source
+            bibsrc_name,
             null,
             null,
             $scope.selectFieldStripGroups()
@@ -278,7 +295,9 @@ function($scope , $q , $location , $timeout , $window,  egCore , egGridDataProvi
                 $scope.focusMe = true;
                 $scope.record_id = recId;
                 $scope.dirty_flag = false;
-                $scope.marc_xml = items[0]['marcxml'];
+                $scope.args = {};
+                $scope.args.marc_xml = items[0]['marcxml'];
+                $scope.args.bib_source = null;
                 $scope.ok = function(args) { $uibModalInstance.close(args) }
                 $scope.cancel = function () { $uibModalInstance.dismiss() }
                 $scope.save_label = egCore.strings.IMPORT_BUTTON_LABEL;
@@ -289,7 +308,7 @@ function($scope , $q , $location , $timeout , $window,  egCore , egGridDataProvi
                         // This timeout is required to allow angular to finish variable assignments
                         // in the marcediter app. Allowing marc_xml to propigate here.
                         $timeout( function() {
-                            _import($scope.marc_xml).then( function(record_obj) {
+                            _import($scope.args.marc_xml, $scope.args.bib_source).then( function(record_obj) {
                                 if( record_obj.id ) {
                                     $scope.record_id = record_obj.id();
                                     $scope.save_label = egCore.strings.SAVE_BUTTON_LABEL;
@@ -332,8 +351,10 @@ function($scope , $q , $location , $timeout , $window,  egCore , egGridDataProvi
         var overlay_target = $scope.local_overlay_target;
         var live_overlay_target = egCore.hatch.getLocalItem('eg.cat.marked_overlay_record') || 0;
         var args = {
-            'marc_xml' : items[0]['marcxml']
+            'marc_xml' : items[0]['marcxml'],
+            'bib_source' : null
         };
+
         $uibModal.open({
             templateUrl: './cat/z3950/t_overlay',
             backdrop: 'static',
@@ -436,13 +457,14 @@ function($scope , $q , $location , $timeout , $window,  egCore , egGridDataProvi
 
             }]
         }).result.then(function (args) {
+            var bibsrc_name = $scope.get_bibsrc_name_from_id(args.bib_source);
             egCore.net.request(
                 'open-ils.cat',
                 'open-ils.cat.biblio.record.marc.replace',
                 egCore.auth.token(),
                 overlay_target,
                 (args.overlay_target.merged ? args.overlay_target.marc_xml : args.marc_xml),
-                null, // FIXME bib source
+                bibsrc_name,
                 null,
                 $scope.selectFieldStripGroups()
             ).then(
-- 
2.11.0