commit 84d8c17d6e8b246f6064c32612580aae6041a491
parent cb56b9607d7af8649c37f234b3cb430d9a2a6e2a
Author: Aurimas Vinckevicius <aurimas.dev@gmail.com>
Date: Fri, 14 Nov 2014 04:42:42 -0600
Move erase to DataObject and make it modular
Diffstat:
2 files changed, 116 insertions(+), 82 deletions(-)
diff --git a/chrome/content/zotero/xpcom/data/dataObject.js b/chrome/content/zotero/xpcom/data/dataObject.js
@@ -576,6 +576,50 @@ Zotero.DataObject.prototype._initSave = Zotero.Promise.coroutine(function* (env)
});
/**
+ * Delete object from database
+ */
+Zotero.DataObject.prototype.erase = Zotero.Promise.coroutine(function* () {
+ var env = {};
+
+ var proceed = yield this._eraseInit(env);
+ if (!proceed) return false;
+
+ Zotero.debug('Deleting ' + this.objectType + ' ' + this.id);
+
+ yield Zotero.DB.executeTransaction(function* () {
+ yield this._eraseData(env);
+ yield this._erasePreCommit(env);
+ }.bind(this))
+ .catch(e => {
+ return this._eraseRecoverFromFailure(env);
+ });
+
+ return this._erasePostCommit(env);
+});
+
+Zotero.DataObject.prototype._eraseInit = function(env) {
+ if (!this.id) return Zotero.Promise.resolve(false);
+
+ return Zotero.Promise.resolve(true);
+};
+
+Zotero.DataObject.prototype._eraseData = function(env) {
+ throw new Error("Zotero.DataObject.prototype._eraseData is an abstract method");
+};
+
+Zotero.DataObject.prototype._erasePreCommit = function(env) {
+ return Zotero.Promise.resolve();
+};
+
+Zotero.DataObject.prototype._erasePostCommit = function(env) {
+ return Zotero.Promise.resolve();
+};
+
+Zotero.DataObject.prototype._eraseRecoverFromFailure = function(env) {
+ throw new Error("Zotero.DataObject.prototype._eraseRecoverFromFailure is an abstract method");
+};
+
+/**
* Generates data object key
* @return {String} key
*/
diff --git a/chrome/content/zotero/xpcom/data/item.js b/chrome/content/zotero/xpcom/data/item.js
@@ -3780,99 +3780,89 @@ Zotero.Item.prototype.copy = Zotero.Promise.coroutine(function* () {
});;
-/**
- * Delete item from database and clear from Zotero.Items internal array
- *
- * Items.erase() should be used for multiple items
- */
-Zotero.Item.prototype.erase = Zotero.Promise.coroutine(function* () {
- if (!this.id) {
- return false;
- }
-
- Zotero.debug('Deleting item ' + this.id);
+Zotero.Item.prototype._eraseInit = Zotero.Promise.coroutine(function* (env) {
+ var proceed = yield Zotero.Item._super.prototype._eraseInit.apply(this, arguments);
+ if (!proceed) return false;
- var changedItems = [];
- var changedItemsNotifierData = {};
- var deletedItemNotifierData = {};
+ env.deletedItemNotifierData = {};
+ env.deletedItemNotifierData[this.id] = { old: this.toJSON() };
- yield Zotero.DB.executeTransaction(function* () {
- deletedItemNotifierData[this.id] = { old: this.toJSON() };
-
- // Remove item from parent collections
- var parentCollectionIDs = this.collections;
- if (parentCollectionIDs) {
- for (var i=0; i<parentCollectionIDs.length; i++) {
- let parentCollection = yield this.ContainerObjectsClass.getAsync(parentCollectionIDs[i]);
- yield parentCollection.removeItem(this.id);
- }
+ return true;
+});
+
+Zotero.Item.prototype._eraseData = Zotero.Promise.coroutine(function* (env) {
+ // Remove item from parent collections
+ var parentCollectionIDs = this.collections;
+ if (parentCollectionIDs) {
+ for (var i=0; i<parentCollectionIDs.length; i++) {
+ let parentCollection = yield Zotero.Collections.getAsync(parentCollectionIDs[i]);
+ yield parentCollection.removeItem(this.id);
}
-
- var parentItem = this.parentKey;
- parentItem = parentItem ? this.ObjectsClass.getByLibraryAndKey(this.libraryID, parentItem) : null;
-
- // // Delete associated attachment files
- if (this.isAttachment()) {
- let linkMode = this.getAttachmentLinkMode();
- // If link only, nothing to delete
- if (linkMode != Zotero.Attachments.LINK_MODE_LINKED_URL) {
- try {
- let file = Zotero.Attachments.getStorageDirectory(this);
- yield OS.File.removeDir(file.path, {
- ignoreAbsent: true,
- ignorePermissions: true
- });
- }
- catch (e) {
- Zotero.debug(e, 2);
- Components.utils.reportError(e);
- }
+ }
+
+ var parentItem = this.parentKey;
+ parentItem = parentItem ? this.ObjectsClass.getByLibraryAndKey(this.libraryID, parentItem) : null;
+
+ // // Delete associated attachment files
+ if (this.isAttachment()) {
+ let linkMode = this.getAttachmentLinkMode();
+ // If link only, nothing to delete
+ if (linkMode != Zotero.Attachments.LINK_MODE_LINKED_URL) {
+ try {
+ let file = Zotero.Attachments.getStorageDirectory(this);
+ yield OS.File.removeDir(file.path, {
+ ignoreAbsent: true,
+ ignorePermissions: true
+ });
}
- }
- // Regular item
- else {
- let sql = "SELECT itemID FROM itemNotes WHERE parentItemID=?1 UNION "
- + "SELECT itemID FROM itemAttachments WHERE parentItemID=?1";
- let toDelete = yield Zotero.DB.columnQueryAsync(sql, [this.id]);
- for (let i=0; i<toDelete.length; i++) {
- let obj = yield this.ObjectsClass.getAsync(toDelete[i]);
- yield obj.erase();
+ catch (e) {
+ Zotero.debug(e, 2);
+ Components.utils.reportError(e);
}
}
-
- // Flag related items for notification
- // TEMP: Do something with relations
- /*var relateds = this._getRelatedItems(true);
- for each(var id in relateds) {
- let relatedItem = this.ObjectsClass.get(id);
- }*/
-
- // Clear fulltext cache
- if (this.isAttachment()) {
- yield Zotero.Fulltext.clearItemWords(this.id);
- //Zotero.Fulltext.clearItemContent(this.id);
- }
-
- // Remove relations (except for merge tracker)
- var uri = Zotero.URI.getItemURI(this);
- yield Zotero.Relations.eraseByURI(uri, [Zotero.Relations.deletedItemPredicate]);
-
- yield Zotero.DB.queryAsync('DELETE FROM items WHERE itemID=?', this.id);
-
- if (parentItem) {
- yield parentItem.reload(['primaryData', 'childItems'], true);
- parentItem.clearBestAttachmentState();
+ }
+ // Regular item
+ else {
+ let sql = "SELECT itemID FROM itemNotes WHERE parentItemID=?1 UNION "
+ + "SELECT itemID FROM itemAttachments WHERE parentItemID=?1";
+ let toDelete = yield Zotero.DB.columnQueryAsync(sql, [this.id]);
+ for (let i=0; i<toDelete.length; i++) {
+ let obj = yield this.ObjectsClass.getAsync(toDelete[i]);
+ yield obj.erase();
}
- }.bind(this));
+ }
- this.ObjectsClass.unload(this.id);
+ // Flag related items for notification
+ // TEMP: Do something with relations
+ /*var relateds = this._getRelatedItems(true);
+ for each(var id in relateds) {
+ let relatedItem = Zotero.Items.get(id);
+ }*/
- // Send notification of changed items
- if (changedItems.length) {
- Zotero.Notifier.trigger('modify', 'item', changedItems, changedItemsNotifierData);
+ // Clear fulltext cache
+ if (this.isAttachment()) {
+ yield Zotero.Fulltext.clearItemWords(this.id);
+ //Zotero.Fulltext.clearItemContent(this.id);
}
- Zotero.Notifier.trigger('delete', 'item', this.id, deletedItemNotifierData);
+ // Remove relations (except for merge tracker)
+ var uri = Zotero.URI.getItemURI(this);
+ yield Zotero.Relations.eraseByURI(uri, [Zotero.Relations.deletedItemPredicate]);
+
+ env.parentItem = parentItem;
+});
+
+Zotero.Item.prototype._erasePreCommit = Zotero.Promise.coroutine(function* (env) {
+ yield Zotero.DB.queryAsync('DELETE FROM items WHERE itemID=?', this.id);
+
+ if (env.parentItem) {
+ yield env.parentItem.reload(['primaryData', 'childItems'], true);
+ env.parentItem.clearBestAttachmentState();
+ }
+
+ this.ObjectsClass.unload(this.id);
+
+ Zotero.Notifier.trigger('delete', 'item', this.id, env.deletedItemNotifierData);
Zotero.Prefs.set('purge.items', true);
Zotero.Prefs.set('purge.creators', true);