From d6e6519437daaa95de8a15a5b3d7e0dee2412126 Mon Sep 17 00:00:00 2001
From: blake <blake@mobiusconsortium.org>
Date: Thu, 9 Nov 2017 15:35:07 +0000
Subject: [PATCH] lp#1724321 Web Client -- existence of record with duplicate
 TCN does not prevent import

This routes the MARC import execution back to the z39.50 code instead of using the generic
MARC editor. This will make use of open-ils.cat.biblio.record.xml.import instead of pcrud.

Test
1. Edit global flag variable "Cat: Use Internal ID for TCN Value" = false
2. Perform a z39.50 search, click a result and click "import"
3. Step two should be successful, now import it again and you should get an error
4. Click the same result but use "Edit then import"
5. From the MARC editor, make no changes and click "Import"
6. It won't complain about duplicate TCNs
7. Apply the patch
8. Perform steps 4 and 5
9. Notice that the editor does not allow you to import the record

Signed-off-by: blake <blake@mobiusconsortium.org>
Signed-off-by: Chris Sharp <csharp@georgialibraries.org>
Signed-off-by: Jason Etheridge <jason@EquinoxInitiative.org>
---
 .../src/templates/staff/cat/z3950/t_marc_edit.tt2  |  2 +-
 .../js/ui/default/staff/cat/services/marcedit.js   |  9 +++++-
 Open-ILS/web/js/ui/default/staff/cat/z3950/app.js  | 32 ++++++++++++++++++----
 3 files changed, 35 insertions(+), 8 deletions(-)

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 8e55ebf054..bcbe41f7e4 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
@@ -7,7 +7,7 @@
   <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}}"
-                         on-save="import_record_callback" fast-add="true"
+                         on-save="import_record_callback" fast-add="true" in-place-mode="in_place_mode"
     />
   </div>
   <div class="modal-footer">
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 d525a90933..bce2ba78da 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
@@ -624,7 +624,14 @@ angular.module('egMarcMod', ['egCoreMod', 'ui.bootstrap'])
             // in-place mode means that the editor is being
             // used just to munge some MARCXML client-side, rather
             // than to (immediately) update the database
-            inPlaceMode : '@',
+            //
+            // In short, we can use inPlaceMode as a way to skip
+            // "normal" bre saving and then process the MARC ourselves
+            // via a callback
+            //
+            // inPlaceMode is r/w to allow our Z39.50 import editor to be
+            // switched back into a normal editor after the initial import
+            inPlaceMode : '=',
             fastAdd : '@',
             flatOnly : '@',
             embedded : '@',
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 09bf70f2aa..83be9cec68 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
@@ -212,20 +212,25 @@ function($scope , $q , $location , $timeout , $window,  egCore , egGridDataProvi
         });
         return groups;
     };
+
     $scope.import = function() {
-        var deferred = $q.defer();
         var items = $scope.gridControls.selectedItems();
+        return $scope._import(items[0]['marcxml']);
+    };
+
+    $scope._import = function(marc_xml) {
+        var deferred = $q.defer();
         egCore.net.request(
             'open-ils.cat',
             'open-ils.cat.biblio.record.xml.import',
             egCore.auth.token(),
-            items[0]['marcxml'],
+            marc_xml,
             null, // FIXME bib source
             null,
             null,
             $scope.selectFieldStripGroups()
         ).then(
-            function() { deferred.resolve() },
+            function(result) { deferred.resolve(result) },
             null, // onerror
             function(result) {
                 var evt = egCore.evt.parse(result);
@@ -265,6 +270,7 @@ function($scope , $q , $location , $timeout , $window,  egCore , egGridDataProvi
     $scope.spawn_editor = function() {
         var items = $scope.gridControls.selectedItems();
         var recId = 0;
+        var _import = $scope._import;
         $uibModal.open({
             templateUrl: './cat/z3950/t_marc_edit',
             backdrop: 'static',
@@ -278,9 +284,23 @@ function($scope , $q , $location , $timeout , $window,  egCore , egGridDataProvi
                 $scope.ok = function(args) { $uibModalInstance.close(args) }
                 $scope.cancel = function () { $uibModalInstance.dismiss() }
                 $scope.save_label = egCore.strings.IMPORT_BUTTON_LABEL;
-                $scope.import_record_callback = function (record_id) {
-                    recId = record_id;
-                    $scope.save_label = egCore.strings.SAVE_BUTTON_LABEL;
+                // Wiring up angular inPlaceMode for editing later
+                $scope.in_place_mode = true;
+                $scope.import_record_callback = function () {
+                    if($scope.in_place_mode) {
+                        // 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) {
+                                if( record_obj.id ) {
+                                    $scope.record_id = record_obj.id();
+                                    $scope.save_label = egCore.strings.SAVE_BUTTON_LABEL;
+                                    // Successful import, no longer want this special z39.50 callback to execute.
+                                    $scope.in_place_mode = undefined;
+                                }
+                            });
+                        });
+                    }
                 };
             }]
         }).result.then(function () {
-- 
2.11.0