ACQ upload form widget value persistence
authorBill Erickson <berick@esilibrary.com>
Fri, 25 Jan 2013 20:50:48 +0000 (15:50 -0500)
committerBen Shum <bshum@biblio.org>
Thu, 14 Feb 2013 22:43:11 +0000 (17:43 -0500)
Provides a two-layer persistence mechanism for the ACQ MARC file upload
interface(s).  For most of the widgets in the upload form, there is now
a matching org unit setting for configuring values ahead of time.  When
such a value is configured, the value will be used.  When no org unit
setting value is configured for a field, the value selected by staff is
stored locally (at the workstation) and re-used for future loads of the
interface.

Signed-off-by: Bill Erickson <berick@esilibrary.com>
Signed-off-by: Ben Shum <bshum@biblio.org>
Open-ILS/web/js/ui/default/acq/common/vlagent.js
Open-ILS/web/js/ui/default/acq/picklist/upload.js

index c92a135..0d16c86 100644 (file)
@@ -1,5 +1,29 @@
 dojo.require('openils.widget.AutoFieldWidget');
 dojo.require('openils.PermaCrud');
+dojo.require('openils.XUL');
+
+var xulStorage = openils.XUL.localStorage();
+var storekey = 'eg.acq.upload.';
+var osetkey = 'acq.upload.default.';
+var persistOrgSettings;
+
+// map local dijit keys/names to their org setting counterparts
+var setNameMap = {
+    match_set : 'vandelay.match_set',
+    merge_profile : 'vandelay.merge_profile',
+    create_assets : 'vandelay.load_item_for_imported',
+    match_quality_ratio : 'vandelay.quality_ratio',
+    auto_overlay_1match : 'vandelay.merge_on_single',
+    import_no_match : 'vandelay.import_non_matching',
+    fall_through_merge_profile : 'vandelay.low_quality_fall_thru_profile',
+    auto_overlay_exact : 'vandelay.merge_on_exact',
+    auto_overlay_best_match : 'vandelay.merge_on_best'
+}
+
+// per-UI setting to change this?
+// if true, set default widget values from org settings
+// (when defined) regardless of any locally persisted value
+var ouSettingTrumpsPersist = true;
 
 function VLAgent(args) {
     args = args || {};
@@ -21,31 +45,70 @@ function VLAgent(args) {
         {key : 'fall_through_merge_profile', cls : 'vmp'},
         {key : 'existing_queue', cls : 'vbq'}
     ];
-    
+
     this.loaded = false;
 
-    this.init = function() {
+    this.init = function(oncomplete) {
+        var self = this;
+
+        // load org unit persist setting values
+        fieldmapper.standardRequest(
+            ['open-ils.actor','open-ils.actor.ou_setting.ancestor_default.batch'],
+            {   async : true,
+                params : [
+                    new openils.User().user.ws_ou(),
+                    [   osetkey + 'create_po',
+                        osetkey + 'activate_po',
+                        osetkey + 'provider',
+                        osetkey + 'vandelay.match_set',
+                        osetkey + 'vandelay.merge_profile',
+                        osetkey + 'vandelay.import_non_matching',
+                        osetkey + 'vandelay.merge_on_exact',
+                        osetkey + 'vandelay.merge_on_best',
+                        osetkey + 'vandelay.merge_on_single',
+                        osetkey + 'vandelay.quality_ratio',
+                        osetkey + 'vandelay.low_quality_fall_thru_profile',
+                        osetkey + 'vandelay.load_item_for_imported'
+                    ]
+                ],
+                oncomplete : function(r) {
+                    persistOrgSettings = openils.Util.readResponse(r);
+                    self.init2();
+                    if (oncomplete) 
+                        oncomplete();
+                }
+            }
+        );
+    };
+
+    this.init2 = function() {
         var self = this;
 
         dojo.forEach(this.widgets,
             function(widg) {
+                var key = widg.key;
+
                 if (widg.cls) { // selectors
 
                     new openils.widget.AutoFieldWidget({
                         fmClass : widg.cls,
                         selfReference : true,
                         orgLimitPerms : [self.limitPerm || 'CREATE_PURCHASE_ORDER'],
-                        parentNode : dojo.byId('acq_vl:' + widg.key),
+                        parentNode : dojo.byId('acq_vl:' + key),
                         searchFilter : (widg.cls == 'vbq') ? {queue_type : 'acq'} : null,
                         useWriteStore :  (widg.cls == 'vbq')
-                    }).build(function(dijit) { 
-                        widg.dijit = dijit; 
+                    }).build(function(dij) { 
+                        widg.dijit = dij; 
+                        if (!key.match(/queue/))
+                            self.readCachedValue(dij, key);
                         self.attachOnChange(widg);
                     }); 
 
                 } else { // bools
-                    widg.dijit = dijit.byId('acq_vl:' + widg.key);
+                    widg.dijit = dijit.byId('acq_vl:' + key);
                     if (!widg.dijit) return; // some fields optional
+                    if (!key.match(/queue/))
+                        self.readCachedValue(widg.dijit, key);
                     self.attachOnChange(widg);
                 }
             }
@@ -131,11 +194,15 @@ function VLAgent(args) {
     }
 
     this.values = function() {
+        var self = this;
         var values = {};
         dojo.forEach(this.widgets,
             function(widg) {
-                if (widg.dijit)
+                if (widg.dijit) {
                     values[widg.key] = widg.dijit.attr('value');
+                    if (!widg.key.match(/queue/))
+                        self.writeCachedValue(widg.dijit, widg.key);
+                }
             }
         );
         return values;
@@ -189,5 +256,39 @@ function VLAgent(args) {
         }
 
         return false; // not yet complete
-    }
+    };
+
+    this.readCachedValue = function(dij, key) {
+        var val;
+        var setname = osetkey + (setNameMap[key] ? setNameMap[key] : key);
+
+        if (ouSettingTrumpsPersist && persistOrgSettings[setname]) {
+            val = persistOrgSettings[setname].value;
+        } else {
+            val = xulStorage.getItem(storekey + key);
+            if (!val && persistOrgSettings[setname])
+                val = persistOrgSettings[setname].value;
+        }
+
+        if (val) dij.attr('value', val);
+        return val;
+    };
+
+    this.writeCachedValue = function(dij, key) {
+        var setname = osetkey + (setNameMap[key] ? setNameMap[key] : key);
+
+        if (ouSettingTrumpsPersist && persistOrgSettings[setname]) {
+            // don't muck up localStorage if we're using org settings
+            xulStorage.removeItem(storekey + key);
+
+        } else {
+            var val = dij.attr('value');
+
+            if (val === null || val === false || val == '') {
+                xulStorage.removeItem(storekey + key);
+            } else {
+                xulStorage.setItem(storekey + key, val);
+            }
+        }
+    };
 }
index 1eb2933..621e1e5 100644 (file)
@@ -21,6 +21,11 @@ var usingNewPl = false;
 
 function init() {
     dojo.byId('acq-pl-upload-ses').value = openils.User.authtoken;
+    vlAgent = new VLAgent();
+    vlAgent.init(init2);
+}
+
+function init2() {
 
     loadYearSelector();
 
@@ -30,7 +35,10 @@ function init() {
         orgLimitPerms : ['CREATE_PICKLIST', 'CREATE_PURCHASE_ORDER'],
         parentNode : dojo.byId('acq-pl-upload-provider'),
     }).build(
-        function(w) { providerWidget = w }
+        function(w) { 
+            providerWidget = w;
+            vlAgent.readCachedValue(w, 'provider');
+        }
     );
 
     new openils.widget.AutoFieldWidget({
@@ -41,12 +49,13 @@ function init() {
     }).build(
         function(w) { 
             orderAgencyWidget = w 
+            vlAgent.readCachedValue(w, 'ordering_agency');
             dojo.connect(orderAgencyWidget, 'onChange', setDefaultFiscalYear);
         }
     );
 
-    vlAgent = new VLAgent();
-    vlAgent.init();
+    vlAgent.readCachedValue(acqPlUploadCreatePo, 'create_po');
+    vlAgent.readCachedValue(acqPlUploadActivatePo, 'activate_po');
 
     fieldmapper.standardRequest(
         ['open-ils.acq', 'open-ils.acq.picklist.user.retrieve.atomic'],
@@ -80,6 +89,13 @@ function setDefaultFiscalYear(org) {
 }
 
 function acqUploadRecords() {
+
+    // persist widget values
+    vlAgent.writeCachedValue(acqPlUploadCreatePo, 'create_po');
+    vlAgent.writeCachedValue(acqPlUploadActivatePo, 'activate_po');
+    vlAgent.writeCachedValue(providerWidget, 'provider');
+    vlAgent.writeCachedValue(orderAgencyWidget, 'ordering_agency');
+
     openils.Util.show('acq-pl-upload-progress');
     var picklist = acqPlUploadPlSelector.attr('value');
     if(picklist) {