diff --git a/process/DataCaching_lib/process.js b/process/DataCaching_lib/process.js index 6105fbd5e00b826c2d332de2f92859d43f65bb42..7bbcf1d23c54753c4f81c83961a16e72ff3ebc60 100644 --- a/process/DataCaching_lib/process.js +++ b/process/DataCaching_lib/process.js @@ -1,13 +1,15 @@ import("system.vars"); -function CachedData(pIdentifiyingName, pKeepPerLanguage) +function CachedData(pIdentifiyingName, pKeepPerLanguage, pLocaleOverride) { this.identifyingName = pIdentifiyingName; this.keepPerLanguage = pKeepPerLanguage; this.runningOnServer = vars.getString("$sys.isserver") == "true"; - if (this.runningOnServer) + if (pLocaleOverride) + this.locale = pLocaleOverride; + else if (this.runningOnServer) this.locale = vars.get("$sys.serverlocale"); else this.locale = (this.keepPerLanguage ? vars.get("$sys.clientlocale") : "_anyLanguage_"); @@ -15,12 +17,12 @@ function CachedData(pIdentifiyingName, pKeepPerLanguage) CachedData.make = function(pIdentifiyingName, pKeepPerLanguage, pDataCallbackFunction) { - return (new CachedData(pIdentifiyingName, pKeepPerLanguage)).load(pDataCallbackFunction); + return (new CachedData(pIdentifiyingName, pKeepPerLanguage, null)).load(pDataCallbackFunction); } CachedData.prototype.load = function(pDataCallbackFunction) { - //currently it's not possible to cache the data within the serer-context, so inestead the Data-function is called everytime + //currently it's not possible to cache the data within the serer-context, so instead the Data-function is called everytime if (this.runningOnServer) return pDataCallbackFunction.call(this, this.keepPerLanguage, this.locale); else @@ -35,6 +37,8 @@ CachedData.prototype.load = function(pDataCallbackFunction) } data = pDataCallbackFunction.call(this, this.keepPerLanguage, this.locale); + if (data == null) + throw new Error("use unload instead"); vars.set(varname, data); this._register(); return data; @@ -48,17 +52,33 @@ CachedData.prototype.unload = function() this._unregister(); } +CachedData.prototype.getVariableIdentifier = function() +{ + //keep data per user in a global var to assure that translations and grants are correctly applied + var PREFIX = "$global.CachedData."; + var res = PREFIX + this.identifyingName + return res; +}; + CachedData.prototype.getVariableName = function() { - var PREFIX = "$global.CachedData."; - var res = PREFIX + this.identifyingName + "." + this.locale; + var res = this.getVariableIdentifier() + "." + this.locale; return res; +}; + +//functions for registry of variables; reserver for later functionality +CachedData.prototype._register = function() +{ + var reg = CachedData.getRegistry(); + reg.push(this.getVariableName()); + vars.set(CachedData.getRegistryName(), reg); } +//CachedData.prototype._unregister = function(){} CachedData.getRegistryName = function() { return "$global.CachedDataRegistry"; -} +}; CachedData.getRegistry = function() { @@ -66,25 +86,29 @@ CachedData.getRegistry = function() if (vars.exists(registryVarname)) return vars.get(registryVarname); else - return {}; -} - -CachedData.prototype._register = function() -{ - var registry = CachedData.getRegistry(); - if (registry[this.getVariableName()] != undefined) - throw new Error("Already registered. cannot register the same object twice");//TODO: message - else - registry[this.getVariableName()] = {keepPerLanguage: this.keepPerLanguage}; - vars.set(CachedData.getRegistryName(), registry); -}; - -CachedData.prototype._unregister = function() -{ - var registry = CachedData.getRegistry(); - if (registry[this.getVariableName()] == undefined) - throw new Error("Item not found. cannot unregister");//TODO: message - else - delete registry[this.getVariableName()]; - vars.set(CachedData.getRegistryName(), registry); + return []; }; +// +//CachedData.prototype._register = function() +//{ +// var registry = CachedData.getRegistry(); +// +// if (registry[this.getVariableIdentifier()]) +// +// if (registry[this.getVariableName()] != undefined) +// throw new Error("Already registered. cannot register the same object twice");//TODO: message +// else +// registry[this.getVariableName()] = {keepPerLanguage: this.keepPerLanguage}; +// vars.set(CachedData.getRegistryName(), registry); +//}; +// +//CachedData.prototype._unregister = function() +//{ +// var varname = this.getVariableName(); +// var registry = CachedData.getRegistry(); +// if (registry[varname] == undefined) +// throw new Error("Item not found. cannot unregister");//TODO: message +// else +// delete registry[varname]; +// vars.set(CachedData.getRegistryName(), registry); +//};