function Cache(object, maxSizeEntry) { if(object != undefined && object.maxSize != undefined) { this.maxSize = object.maxSize; this.maxSizeEntry = object.maxSizeEntry; this.valueFor = object.valueFor; this.statsFor = object.statsFor; this.keyArray = object.keyArray; } else { this.maxSize = object == undefined ? 4 : object; this.maxSizeEntry = maxSizeEntry == undefined ? 100000 : maxSizeEntry; this.valueFor = new Object(); this.statsFor = new Object(); this.keyArray = new Array(); } } Cache.prototype.hit = function(key) { this.statsFor[key] = this.statsFor[key] + 1; } Cache.prototype.remove = function(key) { if(this.valueFor[key] == undefined) { // nop } else { delete(this.valueFor[key]); delete(this.statsFor[key]); for(var index = 0; index < this.keyArray.length; index++) { if(this.keyArray[index] == key) { this.keyArray.splice(index, 1); } } } } Cache.prototype.purge = function() { var indexPurge = 0; for(var index = 1; index < this.keyArray.length; index++) { if(this.statsFor[this.keyArray[indexPurge]] > this.statsFor[this.keyArray[index]]) { indexPurge = index; } } this.remove(this.keyArray[indexPurge]); } Cache.prototype.putHelper = function(key, value) { if(this.keyArray.length + 1 <= this.maxSize) { // nop } else { this.purge(); } this.keyArray.push(key); this.valueFor[key] = value; this.statsFor[key] = 0; } Cache.prototype.put = function(key, value) { this.remove(key); if(YAHOO.lang.JSON.stringify(value).length <= this.maxSizeEntry) { this.putHelper(key, value); } } Cache.prototype.getHelper = function(key) { this.hit(key); return this.valueFor[key]; } Cache.prototype.get = function(key) { if(this.valueFor[key] != undefined) { return this.getHelper(key); } else { return undefined; } } var DataSource = { restore : function(input) { if(input.value.length > 0) { DataSource.cache = YAHOO.lang.JSON.parse(input.value); DataSource.cache.searchPrevious = new Cache(DataSource.cache.searchPrevious); DataSource.cache.searchExact = new Cache(DataSource.cache.searchExact); DataSource.cache.searchAll = new Cache(DataSource.cache.searchAll); DataSource.cache.searchAny = new Cache(DataSource.cache.searchAny); } else { DataSource.cache = new Object(); DataSource.cache.type = new Object(); DataSource.cache.container = new Object(); DataSource.cache.tree = new Object(); DataSource.cache.searchPrevious = new Cache(); DataSource.cache.searchExact = new Cache(); DataSource.cache.searchAll = new Cache(); DataSource.cache.searchAny = new Cache(); } }, saveHelper : function(node, state) { for(var index in node.children) { var child = node.children[index]; if(child.expanded) { state[child.data.objectMappingId] = new Object(); DataSource.saveHelper(child, state[child.data.objectMappingId]); } } }, save : function(input, tree) { if(tree != undefined) { DataSource.cache.tree = new Object(); DataSource.saveHelper(tree.getRoot(), DataSource.cache.tree); } input.value = YAHOO.lang.JSON.stringify(DataSource.cache); }, open : function(data, node) { var path = new Array(); path.push(data.objectMappingId); while(node.parent != null) { path.push(node.data.objectMappingId); node = node.parent; } var current = DataSource.cache.tree; for(var index = path.length - 1; index >= 0; index--) { if(current[path[index]] == undefined) { return false; } else { current = current[path[index]]; } } return true; }, loadChildrenForContainer : function(containerId, releaseKey, callback, argument) { if(DataSource.cache.container[containerId] == undefined) { YAHOO.util.Connect.asyncRequest('GET', '/niemtools/mapping/map/mapping/selectObjectByContainer.iepd;jsessionid=3A509C9EEE126C966E6D55CBDC5FA61B?containerId=' + containerId + '&releaseKey=' + releaseKey, { success: function(response) { try { DataSource.cache.container[containerId] = YAHOO.lang.JSON.parse(response.responseText); for(var index = 0; index < DataSource.cache.container[containerId].length; index++) { DataSource.cache.container[containerId][index].children2 = DataSource.cache.container[containerId][index].children delete DataSource.cache.container[containerId][index].children; } callback(DataSource.cache.container[containerId], argument); } catch(e) { } }, failure: function(response) { }, argument: argument }, null); } else { callback(DataSource.cache.container[containerId], argument); } }, loadChildrenForType : function(containerId, typeId, releaseKey, callback, argument) { if(DataSource.cache.type[typeId] == undefined) { YAHOO.util.Connect.asyncRequest('GET', '/niemtools/mapping/map/mapping/selectObjectByType.iepd;jsessionid=3A509C9EEE126C966E6D55CBDC5FA61B?typeId=' + typeId + '&releaseKey=' + releaseKey + '&containerId=' + containerId, { success: function(response) { try { DataSource.cache.type[typeId] = YAHOO.lang.JSON.parse(response.responseText); for(var index = 0; index < DataSource.cache.type[typeId].length; index++) { DataSource.cache.type[typeId][index].children2 = DataSource.cache.type[typeId][index].children delete DataSource.cache.type[typeId][index].children; } callback(DataSource.cache.type[typeId], argument); } catch(e) { } }, failure: function(response) { }, argument: argument }, null); } else { callback(DataSource.cache.type[typeId], argument); } }, loadSearchPreviousForTerm : function(releaseKey, term, id, callback, argument) { if(DataSource.cache.searchPrevious.get(term) == undefined) { YAHOO.util.Connect.asyncRequest('GET', '/niemtools/mapping/map/searchProperty.iepd;jsessionid=3A509C9EEE126C966E6D55CBDC5FA61B?searchType=previous&searchTerm=' + term + '&searchTermId=' + id + '&releaseKey=' + releaseKey, { success: function(response) { try { var responseJSON = YAHOO.lang.JSON.parse(response.responseText); DataSource.cache.searchPrevious.put(term, YAHOO.lang.JSON.parse(response.responseText)); callback(responseJSON, argument); } catch(e) { } }, failure: function(response) { }, argument: argument }); } else { callback(DataSource.cache.searchPrevious.get(term), argument); } }, loadSearchExactForTerm : function(releaseKey, term, id, callback, argument) { if(DataSource.cache.searchExact.get(term) == undefined) { YAHOO.util.Connect.asyncRequest('GET', '/niemtools/mapping/map/searchProperty.iepd;jsessionid=3A509C9EEE126C966E6D55CBDC5FA61B?searchType=exact&searchTerm=' + term + '&searchTermId=' + id + '&releaseKey=' + releaseKey, { success: function(response) { try { var responseJSON = YAHOO.lang.JSON.parse(response.responseText); DataSource.cache.searchExact.put(term, YAHOO.lang.JSON.parse(response.responseText)); callback(responseJSON, argument); } catch(e) { } }, failure: function(response) { }, argument: argument }); } else { callback(DataSource.cache.searchExact.get(term), argument); } }, loadSearchAllForTerm : function(releaseKey, term, id, callback, argument) { if(DataSource.cache.searchAll.get(term) == undefined) { YAHOO.util.Connect.asyncRequest('GET', '/niemtools/mapping/map/searchProperty.iepd;jsessionid=3A509C9EEE126C966E6D55CBDC5FA61B?searchType=all&searchTerm=' + term + '&searchTermId=' + id + '&releaseKey=' + releaseKey, { success: function(response) { try { var responseJSON = YAHOO.lang.JSON.parse(response.responseText); DataSource.cache.searchAll.put(term, YAHOO.lang.JSON.parse(response.responseText)); callback(responseJSON, argument); } catch(e) { } }, failure: function(response) { }, argument: argument }); } else { callback(DataSource.cache.searchAll.get(term), argument); } }, loadSearchAnyForTerm : function(releaseKey, term, id, callback, argument) { if(DataSource.cache.searchAny.get(term) == undefined) { YAHOO.util.Connect.asyncRequest('GET', '/niemtools/mapping/map/searchProperty.iepd;jsessionid=3A509C9EEE126C966E6D55CBDC5FA61B?searchType=any&searchTerm=' + term + '&searchTermId=' + id + '&releaseKey=' + releaseKey, { success: function(response) { try { var responseJSON = YAHOO.lang.JSON.parse(response.responseText); DataSource.cache.searchAny.put(term, YAHOO.lang.JSON.parse(response.responseText)); callback(responseJSON, argument); } catch(e) { } }, failure: function(response) { }, argument: argument }); } else { callback(DataSource.cache.searchAny.get(term), argument); } } };