From 66a97ac07c52afd9b41fc984a6d6179dc5acf28a Mon Sep 17 00:00:00 2001 From: phasefx Date: Wed, 7 Dec 2005 21:57:28 +0000 Subject: [PATCH] changed OpenILS.data. cleaner init, on_error git-svn-id: svn://svn.open-ils.org/ILS/trunk@2273 dcc99617-32d9-48b4-a31d-7c20da2025e4 --- .../chrome/content/evergreen/OpenILS/data.js | 306 +++++++++--------- .../chrome/content/evergreen/main/main.js | 1 + .../chrome/content/evergreen/util/error.js | 2 +- .../xul/staff_client/server/circ/checkout.js | 2 +- .../xul/staff_client/server/patron/display.js | 2 +- 5 files changed, 165 insertions(+), 148 deletions(-) diff --git a/Open-ILS/xul/staff_client/chrome/content/evergreen/OpenILS/data.js b/Open-ILS/xul/staff_client/chrome/content/evergreen/OpenILS/data.js index f61b618ece..066079c783 100644 --- a/Open-ILS/xul/staff_client/chrome/content/evergreen/OpenILS/data.js +++ b/Open-ILS/xul/staff_client/chrome/content/evergreen/OpenILS/data.js @@ -6,161 +6,29 @@ OpenILS.data = function () { JSAN.use('util.error'); this.error = new util.error(); JSAN.use('main.network'); this.network = new main.network(); - var obj = this; - - JSAN.use('util.file'); - JSAN.use('util.functional'); - JSAN.use('util.fm_utils'); - - function gen_fm_retrieval_func(classname,data) { - var app = data[0]; var method = data[1]; var params = data[2]; var cacheable = data[3]; - return function () { - - function convert() { - try { - if (obj.list[classname].constructor.name == 'Array') { - obj.hash[classname] = - util.functional.convert_object_list_to_hash( - obj.list[classname] - ); - } - } catch(E) { - - obj.error.sdump('D_ERROR',E + '\n'); - } - - } - - try { - obj.list[classname] = obj.network.request( app, method, params); - convert(); - // if cacheable, store an offline copy - if (cacheable) { - var file = new util.file( classname ); - file.set_object( obj.list[classname] ); - } - - } catch(E) { - // if cacheable, try offline - if (cacheable) { - try { - var file = new util.file( classname ); - obj.list[classname] = file.get_object(); - convert(); - } catch(E) { - throw(E); - } - } else { - throw(E); // for now - } - } - } - } - - this.chain = []; - - this.chain.push( - function() { - var f = gen_fm_retrieval_func( - 'au', - [ - 'open-ils.search', - 'open-ils.search.actor.user.session', - [ obj.session ], - false - ] - ); - try { - f(); - } catch(E) { - // Probably the one thing we should not cache, so what do we do? - obj.list.au = new au(); - obj.list.au.home_lib( '1' ); - } - } - ); - - obj.error.sdump('D_DEBUG','_fm_objects = ' + js2JSON(this._fm_objects) + '\n'); - - for (var i in this._fm_objects) { - this.chain.push( gen_fm_retrieval_func(i,this._fm_objects[i]) ); - } - - // The previous org_tree call returned a tree, not a list or hash. - this.chain.push( - function () { - obj.org_tree = obj.list.aou; - obj.list.aou = util.fm_utils.flatten_ou_branch( obj.org_tree ); - obj.hash.aou = util.functional.convert_object_list_to_hash( obj.list.aou ); - } - ); - - this.chain.push( - gen_fm_retrieval_func('my_aou', - [ - 'open-ils.actor', - 'open-ils.actor.org_unit.full_path.retrieve', - [ obj.session ], - true - ] - ) - ); - - // Do this after we get the user object - this.chain.push( - - function () { - - gen_fm_retrieval_func( 'my_actsc', - [ - 'open-ils.circ', - 'open-ils.circ.stat_cat.actor.retrieve.all', - [ obj.session, obj.list.au.home_ou() ], - true - ] - )(); - } - ); - return this; -}; +} OpenILS.data.prototype = { 'list' : {}, 'hash' : {}, - 'init' : function (stash) { - - if (stash) { - - try { - const OpenILS=new Components.Constructor("@mozilla.org/openils_data_cache;1", "nsIOpenILS"); - var data_cache=new OpenILS( ); - var dc = data_cache.wrappedJSObject.OpenILS.prototype.data; - for (var i in dc) { - this.error.sdump('D_DATA','Retrieving ' + i + ' : ' + dc[i] + '\n'); - this[i] = dc[i]; - } - } catch(E) { - this.error.sdump('D_ERROR','Error in OpenILS.data.init(true): ' + js2JSON(E) ); - } + 'init' : function (params) { - if (typeof this.on_complete == 'function') { - - this.on_complete(); + try { + if (params && params.via == 'stash') { + this.stash_retrieve(); + } else { + this.network_retrieve(); } + + } catch(E) { + this.error.sdump('D_ERROR','Error in OpenILS.data.init(' + +js2JSON(params)+'): ' + js2JSON(E) ); + } - } else { - if (typeof this.on_complete == 'function') { - - this.chain.push( this.on_complete ); - } - JSAN.use('util.exec'); this.exec = new util.exec(); - this.exec.on_error = function(E) { alert('oops: ' + E ); } - this.exec.chain( this.chain ); - } }, 'stash' : function () { @@ -197,8 +65,156 @@ OpenILS.data.prototype = { 'ccs' : [ 'open-ils.search', 'open-ils.search.config.copy_status.retrieve.all', [], true ], 'aou' : [ 'open-ils.actor', 'open-ils.actor.org_tree.retrieve', [], true ], 'aout' : [ 'open-ils.actor', 'open-ils.actor.org_types.retrieve', [], true ] - } + }, + + 'stash_retrieve' : function() { + const OpenILS=new Components.Constructor("@mozilla.org/openils_data_cache;1", "nsIOpenILS"); + var data_cache=new OpenILS( ); + var dc = data_cache.wrappedJSObject.OpenILS.prototype.data; + for (var i in dc) { + this.error.sdump('D_DATA','Retrieving ' + i + ' : ' + dc[i] + '\n'); + this[i] = dc[i]; + } + if (typeof this.on_complete == 'function') { + + this.on_complete(); + } + }, + + 'network_retrieve' : function() { + var obj = this; + + JSAN.use('util.file'); + JSAN.use('util.functional'); + JSAN.use('util.fm_utils'); + + function gen_fm_retrieval_func(classname,data) { + var app = data[0]; var method = data[1]; var params = data[2]; var cacheable = data[3]; + return function () { + + function convert() { + try { + if (obj.list[classname].constructor.name == 'Array') { + obj.hash[classname] = + util.functional.convert_object_list_to_hash( + obj.list[classname] + ); + } + } catch(E) { + + obj.error.sdump('D_ERROR',E + '\n'); + } + } + + try { + obj.list[classname] = obj.network.request( app, method, params); + convert(); + // if cacheable, store an offline copy + if (cacheable) { + var file = new util.file( classname ); + file.set_object( obj.list[classname] ); + } + + } catch(E) { + // if cacheable, try offline + if (cacheable) { + try { + var file = new util.file( classname ); + obj.list[classname] = file.get_object(); + convert(); + } catch(E) { + throw(E); + } + } else { + throw(E); // for now + } + } + } + } + + this.chain = []; + + this.chain.push( + function() { + var f = gen_fm_retrieval_func( + 'au', + [ + 'open-ils.search', + 'open-ils.search.actor.user.session', + [ obj.session ], + false + ] + ); + try { + f(); + } catch(E) { + // Probably the one thing we should not cache, so what do we do? + obj.list.au = new au(); + obj.list.au.home_lib( '1' ); + } + } + ); + + obj.error.sdump('D_DEBUG','_fm_objects = ' + js2JSON(this._fm_objects) + '\n'); + + for (var i in this._fm_objects) { + this.chain.push( gen_fm_retrieval_func(i,this._fm_objects[i]) ); + } + + // The previous org_tree call returned a tree, not a list or hash. + this.chain.push( + function () { + obj.org_tree = obj.list.aou; + obj.list.aou = util.fm_utils.flatten_ou_branch( obj.org_tree ); + obj.hash.aou = util.functional.convert_object_list_to_hash( obj.list.aou ); + } + ); + + this.chain.push( + gen_fm_retrieval_func('my_aou', + [ + 'open-ils.actor', + 'open-ils.actor.org_unit.full_path.retrieve', + [ obj.session ], + true + ] + ) + ); + + // Do this after we get the user object + this.chain.push( + + function () { + + gen_fm_retrieval_func( 'my_actsc', + [ + 'open-ils.circ', + 'open-ils.circ.stat_cat.actor.retrieve.all', + [ obj.session, obj.list.au.home_ou() ], + true + ] + )(); + } + ); + + if (typeof this.on_complete == 'function') { + + this.chain.push( this.on_complete ); + } + JSAN.use('util.exec'); this.exec = new util.exec(); + this.exec.on_error = function(E) { + + if (typeof obj.on_error == 'function') { + obj.on_error(); + } else { + alert('oops: ' + E ); + } + } + + this.exec.chain( this.chain ); + + } } dump('exiting OpenILS/data.js\n'); diff --git a/Open-ILS/xul/staff_client/chrome/content/evergreen/main/main.js b/Open-ILS/xul/staff_client/chrome/content/evergreen/main/main.js index 9583763515..9f58bd2955 100644 --- a/Open-ILS/xul/staff_client/chrome/content/evergreen/main/main.js +++ b/Open-ILS/xul/staff_client/chrome/content/evergreen/main/main.js @@ -39,6 +39,7 @@ function main_init() { JSAN.use('OpenILS.data'); G.OpenILS.data = new OpenILS.data() + G.OpenILS.data.on_error = G.auth.logoff; G.OpenILS.data.entities = entities; G.OpenILS.data.stash('entities'); diff --git a/Open-ILS/xul/staff_client/chrome/content/evergreen/util/error.js b/Open-ILS/xul/staff_client/chrome/content/evergreen/util/error.js index a7e17d90ba..c343b400d3 100644 --- a/Open-ILS/xul/staff_client/chrome/content/evergreen/util/error.js +++ b/Open-ILS/xul/staff_client/chrome/content/evergreen/util/error.js @@ -197,7 +197,7 @@ util.error.prototype = { 'get_ilsevent' : function(status) { JSAN.use('OpenILS.data'); - this.OpenILS.data = new OpenILS.data(); this.OpenILS.data.init(true); + this.OpenILS.data = new OpenILS.data(); this.OpenILS.data.init({'via':'stash'}); return this.OpenILS.data.entities['ilsevent.'+status]; } } diff --git a/Open-ILS/xul/staff_client/server/circ/checkout.js b/Open-ILS/xul/staff_client/server/circ/checkout.js index fda9135d10..5bbe157d18 100644 --- a/Open-ILS/xul/staff_client/server/circ/checkout.js +++ b/Open-ILS/xul/staff_client/server/circ/checkout.js @@ -7,7 +7,7 @@ circ.checkout = function (params) { JSAN.use('main.network'); this.network = new main.network(); JSAN.use('OpenILS.data'); this.OpenILS = {}; - this.OpenILS.data = new OpenILS.data(); this.OpenILS.data.init(true); + this.OpenILS.data = new OpenILS.data(); this.OpenILS.data.init({'via':'stash'}); } circ.checkout.prototype = { diff --git a/Open-ILS/xul/staff_client/server/patron/display.js b/Open-ILS/xul/staff_client/server/patron/display.js index 693609ca93..fba3a351a4 100644 --- a/Open-ILS/xul/staff_client/server/patron/display.js +++ b/Open-ILS/xul/staff_client/server/patron/display.js @@ -19,7 +19,7 @@ patron.display.prototype = { obj.barcode = params['barcode']; JSAN.use('OpenILS.data'); this.OpenILS = {}; - obj.OpenILS.data = new OpenILS.data(); obj.OpenILS.data.init(true); + obj.OpenILS.data = new OpenILS.data(); obj.OpenILS.data.init({'via':'stash'}); JSAN.use('util.deck'); obj.deck = new util.deck('patron_deck'); -- 2.43.2