From c726b3ba771b84306a5d01f59ce18bdcad01a52f Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Fri, 19 May 2006 11:31:45 +0000 Subject: [PATCH] Added Scholar.Items.erase(id) and Scholar.Item.erase() -- can be used externally with the former or internally with the latter Fixed bug in Scholar.Creators._getHash() --- .../content/scholar/data_access.js | 70 ++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-) diff --git a/chrome/chromeFiles/content/scholar/data_access.js b/chrome/chromeFiles/content/scholar/data_access.js index 4ddb889e5..2f532a2b4 100644 --- a/chrome/chromeFiles/content/scholar/data_access.js +++ b/chrome/chromeFiles/content/scholar/data_access.js @@ -689,6 +689,50 @@ Scholar.Item.prototype.save = function(){ } +/** +* Delete item from database and clear from Scholar.Items internal array +**/ +Scholar.Item.prototype.erase = function(){ + if (!this.getID()){ + return false; + } + + Scholar.debug('Deleting item ' + this.getID()); + + Scholar.DB.beginTransaction(); + + + var parentFolderID = this.getField('parentFolderID'); + var orderIndex = this.getField('orderIndex'); + + var sql = 'DELETE FROM treeStructure WHERE id=' + this.getID() + ";\n"; + + sql += 'UPDATE treeStructure SET orderIndex=orderIndex-1 ' + + 'WHERE parentFolderID=' + parentFolderID + + ' AND orderIndex>' + orderIndex + ";\n\n"; + + sql += 'DELETE FROM itemCreators WHERE itemID=' + this.getID() + ";\n"; + sql += 'DELETE FROM itemKeywords WHERE itemID=' + this.getID() + ";\n"; + sql += 'DELETE FROM itemData WHERE itemID=' + this.getID() + ";\n"; + sql += 'DELETE FROM items WHERE itemID=' + this.getID() + ";\n"; + + Scholar.DB.query(sql); + Scholar.Creators.purge(); + + try { + Scholar.DB.commitTransaction(); + } + catch (e){ + Scholar.DB.rollbackTransaction(); + throw (e); + } + + Scholar.Items.unload(this.getID()); + + // TODO: trigger reloading of treeview +} + + Scholar.Item.prototype.toString = function(){ return this.getTitle(); } @@ -783,6 +827,8 @@ Scholar.Items = new function(){ this.reload = reload; this.reloadAll = reloadAll; this.getNewItemByType = getNewItemByType; + this.erase = erase; + this.unload = unload; /* * Retrieves (and loads, if necessary) an arbitrary number of items @@ -946,6 +992,26 @@ Scholar.Items = new function(){ } + /** + * Delete item from database and clear from internal array + **/ + function erase(id){ + var obj = this.get(id); + obj.erase(); // calls unload() + obj = undefined; + + // TODO: trigger reload of treeview + } + + + /** + * Clear item from internal array (used by Scholar.Item.erase()) + **/ + function unload(id){ + delete _items[id]; + } + + function _load(){ if (!arguments){ return false; @@ -1101,6 +1167,8 @@ Scholar.Creators = new function(){ this.add = add; this.purge = purge; + var self = this; + /* * Returns an array of creator data for a given creatorID */ @@ -1214,7 +1282,7 @@ Scholar.Creators = new function(){ function _getHash(creatorID){ - var creator = this.get(creatorID); + var creator = self.get(creatorID); if (!creator){ return false; }